Type ManagerΒΆ
This class stores information about all types existing in a program. A type stores information required to perform type checking.
Note that this includes not only primitive types, but programmer-defined types such as array of 10 integers or function that receives a boolean and an array of 5 char and returns an integer.
Thus, class TypesMgr
class stores all seen types, and offers methods to find out information about them
when we are type checking or generating code. For instance:
is type X an array?
is type X the same than type Y?
is type X compatible with type Y?
which type have the elements of type X? (when X is an array type, obviously)
which type returns type X? (when X is a function type, obviously)
Here is the whole definition of TypesMgr
. Check the comments to find out what each method does.
1////////////////////////////////////////////////////////////////
2// Class TypesMgr: creates and stores types, and give access
3// to type information.
4// When a type is created the TypesMgr returns its TypeId.
5// The TypeId is the public way to work with types.
6// This TypeMgr works with five kinds of primitive types:
7// integer, float, boolean, character and void. Also it
8// recognizes two compound types: functions and fixed-size
9// arrays. Finally there exist a special type 'error'.
10
11class TypesMgr {
12
13public:
14
15 // The TypeId is an index in a vector
16 typedef std::size_t TypeId;
17
18 // Constructor
19 TypesMgr ();
20
21 // Methods to create a Type and return its TypeId
22 // - Primitive and error types
23 TypeId createErrorTy ();
24 TypeId createIntegerTy ();
25 TypeId createFloatTy ();
26 TypeId createBooleanTy ();
27 TypeId createCharacterTy ();
28 TypeId createVoidTy ();
29 // - Compound types
30 TypeId createFunctionTy (const std::vector<TypeId> & paramsTypes,
31 TypeId returnType);
32 TypeId createArrayTy (unsigned int size,
33 TypeId elemType);
34
35 // Accessors to work with primitive and error types
36 bool isErrorTy (TypeId tid) const;
37 bool isIntegerTy (TypeId tid) const;
38 bool isFloatTy (TypeId tid) const;
39 bool isBooleanTy (TypeId tid) const;
40 bool isCharacterTy (TypeId tid) const;
41 bool isVoidTy (TypeId tid) const;
42 bool isNumericTy (TypeId tid) const;
43 bool isPrimitiveTy (TypeId tid) const;
44 bool isPrimitiveNonVoidTy (TypeId tid) const;
45 bool isCompoundTy (TypeId tid) const;
46
47 // Accessors to work with function types
48 bool isFunctionTy (TypeId tid) const;
49 const std::vector<TypeId> & getFuncParamsTypes (TypeId tid) const;
50 TypeId getFuncReturnType (TypeId tid) const;
51 std::size_t getNumOfParameters (TypeId tid) const;
52 TypeId getParameterType (TypeId tid,
53 unsigned int i) const;
54 bool isVoidFunction (TypeId tid) const;
55
56 // Accessors to work with array types
57 bool isArrayTy (TypeId tid) const;
58 unsigned int getArraySize (TypeId tid) const;
59 TypeId getArrayElemType (TypeId tid) const;
60
61 // Methods to check different compatibilities of types
62 // - structurally equal?
63 bool equalTypes (TypeId tid1, TypeId tid2) const;
64 // - comparable with the relational operator op?
65 bool comparableTypes (TypeId tid1, TypeId tid2,
66 const std::string & op) const;
67 // - tidFrom values can be copied to tidTo?
68 bool copyableTypes (TypeId tidTo, TypeId tidFrom) const;
69
70 // Method to compute the size of a type (primitive type size = 1)
71 std::size_t getSizeOfType (TypeId tid) const;
72
73 // Methods to convert to string and print types.
74 std::string to_string (TypeId tidm) const;
75 void dump (TypeId tid,
76 std::ostream & os = std::cout) const;
77 // will return type name for basic types, element type name for arrays, 'none' for functions.
78 // useful for GenCode add_var and add_param
79 std::string to_string_basic (TypeId tidm) const;
80
81}; // class TypesMgr