Gyoji Compiler
Loading...
Searching...
No Matches
types.hpp
1/* Copyright 2025 Jonathan S. Arney
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * https://github.com/jarney/gyoji/blob/master/LICENSE
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#pragma once
16
17#include <gyoji-misc/pointers.hpp>
18#include <gyoji-context.hpp>
19#include <string>
20#include <map>
21#include <vector>
22
41namespace Gyoji::mir {
42 class Type;
43 class Types;
44 class Argument;
45
54 class Types {
55 public:
64 Types();
71 ~Types();
72
79 Type * get_type(std::string type) const;
80
88 const Type * get_pointer_to(const Type *_type, const Gyoji::context::SourceReference & _src_ref);
89
97 const Type * get_reference_to(const Type *_type, const Gyoji::context::SourceReference & _src_ref);
98
107 const Type * get_array_of(const Type *_type, size_t _length, const Gyoji::context::SourceReference & _src_ref);
108
118
123 void dump(FILE *out) const;
124
130 private:
132 };
133
141 class Argument {
142 public:
148 Argument(
149 const Type *_argument_type,
150 const Gyoji::context::SourceReference & _source_ref
151 );
158 Argument(const Argument & _other);
165 ~Argument();
169 const Type* get_type() const;
175 private:
176 const Type *argument_type;
177 const Gyoji::context::SourceReference *source_ref;
178 };
179
192 public:
200 std::string _member_name,
201 size_t _index,
202 const Type *_member_type,
203 const Gyoji::context::SourceReference & _source_ref
204 );
211 TypeMember(const TypeMember & other);
218 ~TypeMember();
222 const std::string & get_name() const;
223
240 size_t get_index() const;
241
245 const Type *get_type() const;
251 private:
252 std::string member_name;
253 size_t index;
254 const Type *member_type;
255 const Gyoji::context::SourceReference *source_ref;
256 };
257
270 public:
272 std::string _method_name,
273 const Gyoji::context::SourceReference & _source_ref,
274 const Type *_class_type,
275 const Type *_return_type,
276 const std::vector<Argument> _arguments
277 );
278 ~TypeMethod();
279
280 const std::string & get_name() const;
281 const Gyoji::context::SourceReference & get_source_ref() const;
282 const Type *get_class_type() const;
283 const Type *get_return_type() const;
284 const std::vector<Argument> & get_arguments() const;
285 private:
286 std::string method_name;
287 const Gyoji::context::SourceReference & source_ref;
288 const Type *class_type;
289 const Type *return_type;
290 std::vector<Argument> arguments;
291 };
292
313 class Type {
314 public:
500
514 Type(
515 std::string _name,
516 std::string _simple_name,
517 TypeType _type,
518 bool _complete,
519 const Gyoji::context::SourceReference & _source_ref
520 );
521
522 Type(
523 std::string _name,
524 TypeType _type,
525 bool _complete,
526 const Gyoji::context::SourceReference & _source_ref
527 );
533 Type(
534 std::string _name,
535 std::string _simple_name,
536 const Gyoji::context::SourceReference & _source_ref,
537 const Type & _other
538 );
539
546 ~Type();
547
551 const std::string & get_name() const;
552
559 const std::string & get_simple_name() const;
560
567 bool is_complete() const;
568
573 bool is_primitive() const;
578 bool is_pointer() const;
583 bool is_reference() const;
590 bool is_numeric() const;
596 bool is_integer() const;
601 bool is_unsigned() const;
606 bool is_signed() const;
611 bool is_float() const;
616 bool is_bool() const;
621 bool is_void() const;
626 bool is_enum() const;
632 bool is_composite() const;
633
638 bool is_anonymous() const;
639
644 bool is_function_pointer() const;
645
650 bool is_array() const;
651
658 size_t get_primitive_size() const;
659
663 TypeType get_type() const;
664
675 const std::vector<TypeMember> & get_members() const;
676
682
689 const TypeMember *member_get(const std::string & member_name) const;
690
691 const TypeMethod *method_get(const std::string & member_name) const;
692
693 const TypeMethod *method_get_destructor() const;
694
700 const Type * get_pointer_target() const;
701
708 size_t get_array_length() const;
709
715 const Type * get_return_type() const;
716
723
728 bool is_unsafe() const;
729
730 const Type * get_class_type() const;
731 const Type * get_function_pointer_type() const;
732
739 const Gyoji::context::SourceReference & _source_ref
740 );
741
745 void complete_pointer_definition(const Type *_type, const Gyoji::context::SourceReference & _source_ref);
746
750 void complete_array_definition(const Type *_type, size_t _array_size, const Gyoji::context::SourceReference & _source_ref);
751
752 void complete_enum_definition(const Type *_type, const Gyoji::context::SourceReference & _source_ref);
753
760 const Type *_return_type,
761 const std::vector<Argument> & _argument_types,
762 bool _is_unsafe,
763 const Gyoji::context::SourceReference & _source_ref
764 );
765
770 void dump(FILE *out) const;
771
780
788
789 private:
790 std::string name;
791 std::string simple_name;
792 TypeType type;
793 bool complete;
794 bool m_is_unsafe;
795
796 const Gyoji::context::SourceReference *declared_source_ref;
797 const Gyoji::context::SourceReference *defined_source_ref;
798
799 // Used only for pointer and reference types.
800 const Type *pointer_or_ref;
801 size_t array_length;
802
803 // Used only for function pointer types.
804 const Type *return_type;
805 std::vector<Argument> argument_types;
806
807 // Used only for class/composite types.
810
811 // Used only for class/composite types.
813
814
815 // Used only for class method call types.
816 const Type *class_type;
817 const Type *function_pointer_type;
818 };
819};
References a location in the source-file.
Definition source-reference.hpp:30
Function argument.
Definition types.hpp:141
const Gyoji::context::SourceReference & get_source_ref() const
Definition type.cpp:470
~Argument()
Move along, nothing to see here.
Definition type.cpp:463
const Type * get_type() const
Definition type.cpp:466
This represents a typed member variable of a class.
Definition types.hpp:191
~TypeMember()
Move along, nothing to see here.
Definition type-member.cpp:40
const Gyoji::context::SourceReference & get_source_ref() const
Definition type-member.cpp:50
size_t get_index() const
Definition type-member.cpp:53
const Type * get_type() const
Definition type-member.cpp:47
const std::string & get_name() const
Definition type-member.cpp:44
Method of a class.
Definition types.hpp:269
This represents a type as declared in a translation unit.
Definition types.hpp:313
bool is_float() const
Definition type.cpp:179
bool is_complete() const
Definition type.cpp:92
bool is_signed() const
Definition type.cpp:159
bool is_void() const
Definition type.cpp:194
void complete_pointer_definition(const Type *_type, const Gyoji::context::SourceReference &_source_ref)
Definition type.cpp:327
const std::string & get_simple_name() const
Definition type.cpp:253
const std::vector< TypeMember > & get_members() const
Definition type.cpp:257
void complete_composite_definition(std::vector< TypeMember > _members, std::map< std::string, TypeMethod > _methods, const Gyoji::context::SourceReference &_source_ref)
Definition type.cpp:344
const Type * get_return_type() const
Definition type.cpp:300
bool is_composite() const
Definition type.cpp:204
void dump(FILE *out) const
Definition type.cpp:382
bool is_anonymous() const
Definition type.cpp:207
bool is_unsigned() const
Definition type.cpp:169
bool is_bool() const
Definition type.cpp:187
bool is_numeric() const
Definition type.cpp:126
bool is_array() const
Definition type.cpp:215
const TypeMember * member_get(const std::string &member_name) const
Definition type.cpp:265
bool is_pointer() const
Definition type.cpp:118
const std::map< std::string, TypeMethod > & get_methods() const
Definition type.cpp:261
const Gyoji::context::SourceReference & get_defined_source_ref() const
Definition type.cpp:378
~Type()
Move along, nothing to see here.
Definition type.cpp:88
size_t get_primitive_size() const
Definition type.cpp:219
bool is_unsafe() const
Definition type.cpp:310
bool is_primitive() const
Definition type.cpp:96
const Type * get_pointer_target() const
Definition type.cpp:292
bool is_reference() const
Definition type.cpp:122
const std::vector< Argument > & get_argument_types() const
Definition type.cpp:304
size_t get_array_length() const
Definition type.cpp:296
bool is_integer() const
Definition type.cpp:144
TypeType get_type() const
Definition type.cpp:246
const Gyoji::context::SourceReference & get_declared_source_ref() const
Definition type.cpp:374
const std::string & get_name() const
Definition type.cpp:249
bool is_function_pointer() const
Definition type.cpp:211
void complete_array_definition(const Type *_type, size_t _array_size, const Gyoji::context::SourceReference &_source_ref)
Definition type.cpp:335
bool is_enum() const
Definition type.cpp:200
void complete_function_pointer_definition(const Type *_return_type, const std::vector< Argument > &_argument_types, bool _is_unsafe, const Gyoji::context::SourceReference &_source_ref)
Definition type.cpp:359
TypeType
Type of type.
Definition types.hpp:335
@ TYPE_PRIMITIVE_u16
Primitive 16-bit unsigned integer.
Definition types.hpp:351
@ TYPE_ANONYMOUS_STRUCTURE
Definition types.hpp:468
@ TYPE_ENUM
Definition types.hpp:493
@ TYPE_ARRAY
Definition types.hpp:498
@ TYPE_PRIMITIVE_void
Primitive 'void' type.
Definition types.hpp:448
@ TYPE_REFERENCE
Definition types.hpp:487
@ TYPE_PRIMITIVE_f32
Primitive 32 bit floating-point number.
Definition types.hpp:412
@ TYPE_COMPOSITE
Definition types.hpp:457
@ TYPE_PRIMITIVE_i8
Primitive 8-bit signed integer.
Definition types.hpp:377
@ TYPE_PRIMITIVE_f64
Primitive 64 bit floating-point number.
Definition types.hpp:419
@ TYPE_PRIMITIVE_i16
Primitive 16-bit signed integer.
Definition types.hpp:386
@ TYPE_PRIMITIVE_i32
Primitive 32-bit signed integer.
Definition types.hpp:395
@ TYPE_PRIMITIVE_u8
Primitive 8-bit unsigned integer.
Definition types.hpp:343
@ TYPE_POINTER
Definition types.hpp:475
@ TYPE_FUNCTION_POINTER
Definition types.hpp:480
@ TYPE_PRIMITIVE_bool
Primitive boolean value holding 'true' or 'false'.
Definition types.hpp:432
@ TYPE_PRIMITIVE_i64
Primitive 64-bit signed integer.
Definition types.hpp:404
@ TYPE_PRIMITIVE_u64
Primitive 64-bit unsigned integer.
Definition types.hpp:367
@ TYPE_PRIMITIVE_u32
Primitive 32-bit unsigned integer.
Definition types.hpp:359
Set of types extracted from a translation unit.
Definition types.hpp:54
const Type * get_reference_to(const Type *_type, const Gyoji::context::SourceReference &_src_ref)
Definition types.cpp:80
const std::map< std::string, Gyoji::owned< Type > > & get_types() const
Definition types.cpp:122
Type * get_type(std::string type) const
Definition types.cpp:56
void define_type(Gyoji::owned< Type > type)
Definition types.cpp:115
const Type * get_pointer_to(const Type *_type, const Gyoji::context::SourceReference &_src_ref)
Definition types.cpp:65
const Type * get_array_of(const Type *_type, size_t _length, const Gyoji::context::SourceReference &_src_ref)
Definition types.cpp:96
void dump(FILE *out) const
Definition types.cpp:127
Types()
Creates a new types table with only the primitive types defined.
Definition types.cpp:29
~Types()
Move along, nothing to see here.
Definition types.cpp:52
Middle intermediate representation (MIR) of a translation unit.
Definition gyoji-mir.hpp:51