1 | #include "typeops.h" |
---|
2 | #include "SynTree/Type.h" |
---|
3 | #include "TypeEnvironment.h" |
---|
4 | #include "SymTab/Indexer.h" |
---|
5 | |
---|
6 | namespace ResolvExpr { |
---|
7 | class AdjustExprType : public Mutator { |
---|
8 | typedef Mutator Parent; |
---|
9 | public: |
---|
10 | AdjustExprType( const TypeEnvironment &env, const SymTab::Indexer &indexer ); |
---|
11 | private: |
---|
12 | virtual Type* mutate(VoidType *voidType); |
---|
13 | virtual Type* mutate(BasicType *basicType); |
---|
14 | virtual Type* mutate(PointerType *pointerType); |
---|
15 | virtual Type* mutate(ArrayType *arrayType); |
---|
16 | virtual Type* mutate(FunctionType *functionType); |
---|
17 | virtual Type* mutate(StructInstType *aggregateUseType); |
---|
18 | virtual Type* mutate(UnionInstType *aggregateUseType); |
---|
19 | virtual Type* mutate(EnumInstType *aggregateUseType); |
---|
20 | virtual Type* mutate(ContextInstType *aggregateUseType); |
---|
21 | virtual Type* mutate(TypeInstType *aggregateUseType); |
---|
22 | virtual Type* mutate(TupleType *tupleType); |
---|
23 | |
---|
24 | const TypeEnvironment &env; |
---|
25 | const SymTab::Indexer &indexer; |
---|
26 | }; |
---|
27 | |
---|
28 | void adjustExprType( Type *&type, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
29 | AdjustExprType adjuster( env, indexer ); |
---|
30 | Type *newType = type->acceptMutator( adjuster ); |
---|
31 | type = newType; |
---|
32 | } |
---|
33 | |
---|
34 | AdjustExprType::AdjustExprType( const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
35 | : env( env ), indexer( indexer ) { |
---|
36 | } |
---|
37 | |
---|
38 | Type *AdjustExprType::mutate(VoidType *voidType) { |
---|
39 | return voidType; |
---|
40 | } |
---|
41 | |
---|
42 | Type *AdjustExprType::mutate(BasicType *basicType) { |
---|
43 | return basicType; |
---|
44 | } |
---|
45 | |
---|
46 | Type *AdjustExprType::mutate(PointerType *pointerType) { |
---|
47 | return pointerType; |
---|
48 | } |
---|
49 | |
---|
50 | Type *AdjustExprType::mutate(ArrayType *arrayType) { |
---|
51 | PointerType *pointerType = new PointerType( arrayType->get_qualifiers(), arrayType->get_base()->clone() ); |
---|
52 | delete arrayType; |
---|
53 | return pointerType; |
---|
54 | } |
---|
55 | |
---|
56 | Type *AdjustExprType::mutate(FunctionType *functionType) { |
---|
57 | PointerType *pointerType = new PointerType( Type::Qualifiers(), functionType ); |
---|
58 | return pointerType; |
---|
59 | } |
---|
60 | |
---|
61 | Type *AdjustExprType::mutate(StructInstType *aggregateUseType) { |
---|
62 | return aggregateUseType; |
---|
63 | } |
---|
64 | |
---|
65 | Type *AdjustExprType::mutate(UnionInstType *aggregateUseType) { |
---|
66 | return aggregateUseType; |
---|
67 | } |
---|
68 | |
---|
69 | Type *AdjustExprType::mutate(EnumInstType *aggregateUseType) { |
---|
70 | return aggregateUseType; |
---|
71 | } |
---|
72 | |
---|
73 | Type *AdjustExprType::mutate(ContextInstType *aggregateUseType) { |
---|
74 | return aggregateUseType; |
---|
75 | } |
---|
76 | |
---|
77 | Type *AdjustExprType::mutate(TypeInstType *typeInst) { |
---|
78 | EqvClass eqvClass; |
---|
79 | if ( env.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
80 | if ( eqvClass.kind == TypeDecl::Ftype ) { |
---|
81 | PointerType *pointerType = new PointerType( Type::Qualifiers(), typeInst ); |
---|
82 | return pointerType; |
---|
83 | } |
---|
84 | } else if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) { |
---|
85 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) { |
---|
86 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) { |
---|
87 | PointerType *pointerType = new PointerType( Type::Qualifiers(), typeInst ); |
---|
88 | return pointerType; |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | return typeInst; |
---|
93 | } |
---|
94 | |
---|
95 | Type *AdjustExprType::mutate(TupleType *tupleType) { |
---|
96 | return tupleType; |
---|
97 | } |
---|
98 | } // namespace ResolvExpr |
---|