[51b7345] | 1 | #include "typeops.h" |
---|
| 2 | #include "SynTree/Type.h" |
---|
| 3 | #include "TypeEnvironment.h" |
---|
| 4 | #include "SymTab/Indexer.h" |
---|
| 5 | |
---|
| 6 | namespace ResolvExpr { |
---|
[d9a0e76] | 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); |
---|
[51b7345] | 23 | |
---|
[d9a0e76] | 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 | } |
---|
[51b7345] | 33 | |
---|
[d9a0e76] | 34 | AdjustExprType::AdjustExprType( const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
| 35 | : env( env ), indexer( indexer ) { |
---|
| 36 | } |
---|
[51b7345] | 37 | |
---|
[d9a0e76] | 38 | Type *AdjustExprType::mutate(VoidType *voidType) { |
---|
| 39 | return voidType; |
---|
| 40 | } |
---|
[51b7345] | 41 | |
---|
[d9a0e76] | 42 | Type *AdjustExprType::mutate(BasicType *basicType) { |
---|
| 43 | return basicType; |
---|
| 44 | } |
---|
[51b7345] | 45 | |
---|
[d9a0e76] | 46 | Type *AdjustExprType::mutate(PointerType *pointerType) { |
---|
| 47 | return pointerType; |
---|
| 48 | } |
---|
[51b7345] | 49 | |
---|
[d9a0e76] | 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 | } |
---|
[51b7345] | 55 | |
---|
[d9a0e76] | 56 | Type *AdjustExprType::mutate(FunctionType *functionType) { |
---|
| 57 | PointerType *pointerType = new PointerType( Type::Qualifiers(), functionType ); |
---|
| 58 | return pointerType; |
---|
| 59 | } |
---|
[51b7345] | 60 | |
---|
[d9a0e76] | 61 | Type *AdjustExprType::mutate(StructInstType *aggregateUseType) { |
---|
| 62 | return aggregateUseType; |
---|
| 63 | } |
---|
[51b7345] | 64 | |
---|
[d9a0e76] | 65 | Type *AdjustExprType::mutate(UnionInstType *aggregateUseType) { |
---|
| 66 | return aggregateUseType; |
---|
[51b7345] | 67 | } |
---|
[d9a0e76] | 68 | |
---|
| 69 | Type *AdjustExprType::mutate(EnumInstType *aggregateUseType) { |
---|
| 70 | return aggregateUseType; |
---|
[51b7345] | 71 | } |
---|
| 72 | |
---|
[d9a0e76] | 73 | Type *AdjustExprType::mutate(ContextInstType *aggregateUseType) { |
---|
| 74 | return aggregateUseType; |
---|
| 75 | } |
---|
[51b7345] | 76 | |
---|
[d9a0e76] | 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 | } |
---|
[51b7345] | 94 | |
---|
[d9a0e76] | 95 | Type *AdjustExprType::mutate(TupleType *tupleType) { |
---|
| 96 | return tupleType; |
---|
| 97 | } |
---|
[51b7345] | 98 | } // namespace ResolvExpr |
---|