[a32b204] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[d57e349] | 7 | // AdjustExprType_old.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sat May 16 23:41:42 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[07de76b] | 12 | // Last Modified On : Wed Dec 11 21:43:56 2019 |
---|
| 13 | // Update Count : 6 |
---|
[834d4fc] | 14 | // |
---|
[a32b204] | 15 | |
---|
[d57e349] | 16 | #include "AST/Node.hpp" |
---|
| 17 | #include "AST/Pass.hpp" |
---|
| 18 | #include "AST/SymbolTable.hpp" |
---|
| 19 | #include "AST/Type.hpp" |
---|
| 20 | #include "AST/TypeEnvironment.hpp" |
---|
[93fe7141] | 21 | #include "Common/PassVisitor.h" |
---|
[ea6332d] | 22 | #include "SymTab/Indexer.h" // for Indexer |
---|
| 23 | #include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Kind::Ftype |
---|
| 24 | #include "SynTree/Mutator.h" // for Mutator |
---|
| 25 | #include "SynTree/Type.h" // for PointerType, TypeInstType, Type |
---|
| 26 | #include "TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
[51b7345] | 27 | |
---|
| 28 | namespace ResolvExpr { |
---|
[d57e349] | 29 | |
---|
| 30 | namespace { |
---|
| 31 | class AdjustExprType_old final : public WithShortCircuiting { |
---|
| 32 | public: |
---|
| 33 | AdjustExprType_old( const TypeEnvironment & env, const SymTab::Indexer & indexer ); |
---|
[93fe7141] | 34 | void premutate( VoidType * ) { visit_children = false; } |
---|
| 35 | void premutate( BasicType * ) { visit_children = false; } |
---|
| 36 | void premutate( PointerType * ) { visit_children = false; } |
---|
[954ef5b] | 37 | void premutate( ArrayType * ) { visit_children = false; } |
---|
[93fe7141] | 38 | void premutate( FunctionType * ) { visit_children = false; } |
---|
| 39 | void premutate( StructInstType * ) { visit_children = false; } |
---|
| 40 | void premutate( UnionInstType * ) { visit_children = false; } |
---|
| 41 | void premutate( EnumInstType * ) { visit_children = false; } |
---|
| 42 | void premutate( TraitInstType * ) { visit_children = false; } |
---|
| 43 | void premutate( TypeInstType * ) { visit_children = false; } |
---|
| 44 | void premutate( TupleType * ) { visit_children = false; } |
---|
| 45 | void premutate( VarArgsType * ) { visit_children = false; } |
---|
| 46 | void premutate( ZeroType * ) { visit_children = false; } |
---|
| 47 | void premutate( OneType * ) { visit_children = false; } |
---|
| 48 | |
---|
[ef5b828] | 49 | Type * postmutate( ArrayType * arrayType ); |
---|
| 50 | Type * postmutate( FunctionType * functionType ); |
---|
| 51 | Type * postmutate( TypeInstType * aggregateUseType ); |
---|
[a32b204] | 52 | |
---|
[d57e349] | 53 | private: |
---|
[c20b0fea] | 54 | const TypeEnvironment & env; |
---|
| 55 | const SymTab::Indexer & indexer; |
---|
[a32b204] | 56 | }; |
---|
| 57 | |
---|
[d57e349] | 58 | AdjustExprType_old::AdjustExprType_old( const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
[a32b204] | 59 | : env( env ), indexer( indexer ) { |
---|
| 60 | } |
---|
| 61 | |
---|
[d57e349] | 62 | Type * AdjustExprType_old::postmutate( ArrayType * arrayType ) { |
---|
[ef5b828] | 63 | PointerType * pointerType = new PointerType{ arrayType->get_qualifiers(), arrayType->base }; |
---|
[93fe7141] | 64 | arrayType->base = nullptr; |
---|
[a32b204] | 65 | delete arrayType; |
---|
| 66 | return pointerType; |
---|
| 67 | } |
---|
| 68 | |
---|
[d57e349] | 69 | Type * AdjustExprType_old::postmutate( FunctionType * functionType ) { |
---|
[00ac42e] | 70 | return new PointerType{ Type::Qualifiers(), functionType }; |
---|
[a32b204] | 71 | } |
---|
| 72 | |
---|
[d57e349] | 73 | Type * AdjustExprType_old::postmutate( TypeInstType * typeInst ) { |
---|
[ef5b828] | 74 | if ( const EqvClass * eqvClass = env.lookup( typeInst->get_name() ) ) { |
---|
[00ac42e] | 75 | if ( eqvClass->data.kind == TypeDecl::Ftype ) { |
---|
| 76 | return new PointerType{ Type::Qualifiers(), typeInst }; |
---|
[a32b204] | 77 | } |
---|
[ef5b828] | 78 | } else if ( const NamedTypeDecl * ntDecl = indexer.lookupType( typeInst->get_name() ) ) { |
---|
| 79 | if ( const TypeDecl * tyDecl = dynamic_cast< const TypeDecl * >( ntDecl ) ) { |
---|
[a32b204] | 80 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) { |
---|
[00ac42e] | 81 | return new PointerType{ Type::Qualifiers(), typeInst }; |
---|
[a32b204] | 82 | } // if |
---|
| 83 | } // if |
---|
| 84 | } // if |
---|
| 85 | return typeInst; |
---|
| 86 | } |
---|
[d57e349] | 87 | } // anonymous namespace |
---|
| 88 | |
---|
| 89 | void adjustExprType( Type *&type, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
| 90 | PassVisitor<AdjustExprType_old> adjuster( env, indexer ); |
---|
[ef5b828] | 91 | Type * newType = type->acceptMutator( adjuster ); |
---|
[d57e349] | 92 | type = newType; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void adjustExprType( Type *& type ) { |
---|
| 96 | TypeEnvironment env; |
---|
| 97 | SymTab::Indexer indexer; |
---|
| 98 | adjustExprType( type, env, indexer ); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | namespace { |
---|
| 102 | struct AdjustExprType_new final : public ast::WithShortCircuiting { |
---|
| 103 | const ast::TypeEnvironment & tenv; |
---|
| 104 | const ast::SymbolTable & symtab; |
---|
| 105 | |
---|
| 106 | AdjustExprType_new( const ast::TypeEnvironment & e, const ast::SymbolTable & syms ) |
---|
| 107 | : tenv( e ), symtab( syms ) {} |
---|
| 108 | |
---|
| 109 | void premutate( const ast::VoidType * ) { visit_children = false; } |
---|
| 110 | void premutate( const ast::BasicType * ) { visit_children = false; } |
---|
| 111 | void premutate( const ast::PointerType * ) { visit_children = false; } |
---|
| 112 | void premutate( const ast::ArrayType * ) { visit_children = false; } |
---|
| 113 | void premutate( const ast::FunctionType * ) { visit_children = false; } |
---|
| 114 | void premutate( const ast::StructInstType * ) { visit_children = false; } |
---|
| 115 | void premutate( const ast::UnionInstType * ) { visit_children = false; } |
---|
| 116 | void premutate( const ast::EnumInstType * ) { visit_children = false; } |
---|
| 117 | void premutate( const ast::TraitInstType * ) { visit_children = false; } |
---|
| 118 | void premutate( const ast::TypeInstType * ) { visit_children = false; } |
---|
| 119 | void premutate( const ast::TupleType * ) { visit_children = false; } |
---|
| 120 | void premutate( const ast::VarArgsType * ) { visit_children = false; } |
---|
| 121 | void premutate( const ast::ZeroType * ) { visit_children = false; } |
---|
| 122 | void premutate( const ast::OneType * ) { visit_children = false; } |
---|
| 123 | |
---|
| 124 | const ast::Type * postmutate( const ast::ArrayType * at ) { |
---|
| 125 | return new ast::PointerType{ at->base, at->qualifiers }; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | const ast::Type * postmutate( const ast::FunctionType * ft ) { |
---|
| 129 | return new ast::PointerType{ ft }; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | const ast::Type * postmutate( const ast::TypeInstType * inst ) { |
---|
| 133 | // replace known function-type-variables with pointer-to-function |
---|
| 134 | if ( const ast::EqvClass * eqvClass = tenv.lookup( inst->name ) ) { |
---|
[07de76b] | 135 | if ( eqvClass->data.kind == ast::TypeDecl::Ftype ) { |
---|
[d57e349] | 136 | return new ast::PointerType{ inst }; |
---|
| 137 | } |
---|
| 138 | } else if ( const ast::NamedTypeDecl * ntDecl = symtab.lookupType( inst->name ) ) { |
---|
| 139 | if ( auto tyDecl = dynamic_cast< const ast::TypeDecl * >( ntDecl ) ) { |
---|
[07de76b] | 140 | if ( tyDecl->kind == ast::TypeDecl::Ftype ) { |
---|
[d57e349] | 141 | return new ast::PointerType{ inst }; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | return inst; |
---|
| 146 | } |
---|
| 147 | }; |
---|
| 148 | } // anonymous namespace |
---|
| 149 | |
---|
[ef5b828] | 150 | const ast::Type * adjustExprType( |
---|
| 151 | const ast::Type * type, const ast::TypeEnvironment & env, const ast::SymbolTable & symtab |
---|
[d57e349] | 152 | ) { |
---|
| 153 | ast::Pass<AdjustExprType_new> adjuster{ env, symtab }; |
---|
| 154 | return type->accept( adjuster ); |
---|
| 155 | } |
---|
| 156 | |
---|
[51b7345] | 157 | } // namespace ResolvExpr |
---|
[a32b204] | 158 | |
---|
| 159 | // Local Variables: // |
---|
| 160 | // tab-width: 4 // |
---|
| 161 | // mode: c++ // |
---|
| 162 | // compile-command: "make install" // |
---|
| 163 | // End: // |
---|