| 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 | //
 | 
|---|
| 7 | // AdjustExprType.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Sat May 16 23:41:42 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Wed Mar  2 17:34:53 2016
 | 
|---|
| 13 | // Update Count     : 4
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "Common/PassVisitor.h"
 | 
|---|
| 17 | #include "SymTab/Indexer.h"       // for Indexer
 | 
|---|
| 18 | #include "SynTree/Declaration.h"  // for TypeDecl, TypeDecl::Kind::Ftype
 | 
|---|
| 19 | #include "SynTree/Mutator.h"      // for Mutator
 | 
|---|
| 20 | #include "SynTree/Type.h"         // for PointerType, TypeInstType, Type
 | 
|---|
| 21 | #include "TypeEnvironment.h"      // for EqvClass, TypeEnvironment
 | 
|---|
| 22 | 
 | 
|---|
| 23 | namespace ResolvExpr {
 | 
|---|
| 24 |         class AdjustExprType : public WithShortCircuiting {
 | 
|---|
| 25 |           public:
 | 
|---|
| 26 |                 AdjustExprType( const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 27 |                 void premutate( VoidType * ) { visit_children = false; }
 | 
|---|
| 28 |                 void premutate( BasicType * ) { visit_children = false; }
 | 
|---|
| 29 |                 void premutate( PointerType * ) { visit_children = false; }
 | 
|---|
| 30 |                 void premutate( ArrayType * ) { visit_children = false; }
 | 
|---|
| 31 |                 void premutate( FunctionType * ) { visit_children = false; }
 | 
|---|
| 32 |                 void premutate( StructInstType * ) { visit_children = false; }
 | 
|---|
| 33 |                 void premutate( UnionInstType * ) { visit_children = false; }
 | 
|---|
| 34 |                 void premutate( EnumInstType * ) { visit_children = false; }
 | 
|---|
| 35 |                 void premutate( TraitInstType * ) { visit_children = false; }
 | 
|---|
| 36 |                 void premutate( TypeInstType * ) { visit_children = false; }
 | 
|---|
| 37 |                 void premutate( TupleType * ) { visit_children = false; }
 | 
|---|
| 38 |                 void premutate( VarArgsType * ) { visit_children = false; }
 | 
|---|
| 39 |                 void premutate( ZeroType * ) { visit_children = false; }
 | 
|---|
| 40 |                 void premutate( OneType * ) { visit_children = false; }
 | 
|---|
| 41 | 
 | 
|---|
| 42 |                 Type * postmutate( ArrayType *arrayType );
 | 
|---|
| 43 |                 Type * postmutate( FunctionType *functionType );
 | 
|---|
| 44 |                 Type * postmutate( TypeInstType *aggregateUseType );
 | 
|---|
| 45 | 
 | 
|---|
| 46 |           private:
 | 
|---|
| 47 |                 const TypeEnvironment &env;
 | 
|---|
| 48 |                 const SymTab::Indexer &indexer;
 | 
|---|
| 49 |         };
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         void adjustExprType( Type *&type, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
 | 
|---|
| 52 |                 PassVisitor<AdjustExprType> adjuster( env, indexer );
 | 
|---|
| 53 |                 Type *newType = type->acceptMutator( adjuster );
 | 
|---|
| 54 |                 type = newType;
 | 
|---|
| 55 |         }
 | 
|---|
| 56 | 
 | 
|---|
| 57 |         AdjustExprType::AdjustExprType( const TypeEnvironment &env, const SymTab::Indexer &indexer )
 | 
|---|
| 58 |                 : env( env ), indexer( indexer ) {
 | 
|---|
| 59 |         }
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         Type * AdjustExprType::postmutate( ArrayType * arrayType ) {
 | 
|---|
| 62 |                 PointerType *pointerType = new PointerType( arrayType->get_qualifiers(), arrayType->base );
 | 
|---|
| 63 |                 arrayType->base = nullptr;
 | 
|---|
| 64 |                 delete arrayType;
 | 
|---|
| 65 |                 return pointerType;
 | 
|---|
| 66 |         }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |         Type * AdjustExprType::postmutate( FunctionType * functionType ) {
 | 
|---|
| 69 |                 return new PointerType( Type::Qualifiers(), functionType );
 | 
|---|
| 70 |         }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         Type * AdjustExprType::postmutate( TypeInstType * typeInst ) {
 | 
|---|
| 73 |                 EqvClass eqvClass;
 | 
|---|
| 74 |                 if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
 | 
|---|
| 75 |                         if ( eqvClass.data.kind == TypeDecl::Ftype ) {
 | 
|---|
| 76 |                                 PointerType *pointerType = new PointerType( Type::Qualifiers(), typeInst );
 | 
|---|
| 77 |                                 return pointerType;
 | 
|---|
| 78 |                         }
 | 
|---|
| 79 |                 } else if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) {
 | 
|---|
| 80 |                         if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) {
 | 
|---|
| 81 |                                 if ( tyDecl->get_kind() == TypeDecl::Ftype ) {
 | 
|---|
| 82 |                                         PointerType *pointerType = new PointerType( Type::Qualifiers(), typeInst );
 | 
|---|
| 83 |                                         return pointerType;
 | 
|---|
| 84 |                                 } // if
 | 
|---|
| 85 |                         } // if
 | 
|---|
| 86 |                 } // if
 | 
|---|
| 87 |                 return typeInst;
 | 
|---|
| 88 |         }
 | 
|---|
| 89 | } // namespace ResolvExpr
 | 
|---|
| 90 | 
 | 
|---|
| 91 | // Local Variables: //
 | 
|---|
| 92 | // tab-width: 4 //
 | 
|---|
| 93 | // mode: c++ //
 | 
|---|
| 94 | // compile-command: "make install" //
 | 
|---|
| 95 | // End: //
 | 
|---|