| 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 | // ScrubTyVars.h --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Sat Jul 22 09:21:47 2017
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #pragma once
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include <cassert>            // for assert
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Common/PassVisitor.h"
 | 
|---|
| 21 | #include "GenPoly.h"          // for TyVarMap, isPolyType, isDynType
 | 
|---|
| 22 | #include "SynTree/Mutator.h"  // for Mutator
 | 
|---|
| 23 | #include "SynTree/Type.h"     // for Type (ptr only), PointerType (ptr only)
 | 
|---|
| 24 | 
 | 
|---|
| 25 | class AlignofExpr;
 | 
|---|
| 26 | class Expression;
 | 
|---|
| 27 | class SizeofExpr;
 | 
|---|
| 28 | 
 | 
|---|
| 29 | namespace GenPoly {
 | 
|---|
| 30 |         struct ScrubTyVars : public WithVisitorRef<ScrubTyVars>, public WithShortCircuiting, public WithGuards {
 | 
|---|
| 31 |                 /// Whether to scrub all type variables from the provided map, dynamic type variables from the provided map, or all type variables
 | 
|---|
| 32 |                 enum ScrubMode { FromMap, DynamicFromMap, All };
 | 
|---|
| 33 | 
 | 
|---|
| 34 |                 ScrubTyVars() : tyVars(nullptr), mode( All ) {}
 | 
|---|
| 35 | 
 | 
|---|
| 36 |                 ScrubTyVars( const TyVarMap &tyVars, ScrubMode mode = FromMap ): tyVars( &tyVars ), mode( mode ) {}
 | 
|---|
| 37 | 
 | 
|---|
| 38 |         public:
 | 
|---|
| 39 |                 /// For all polymorphic types with type variables in `tyVars`, replaces generic types, dtypes, and ftypes with the appropriate void type,
 | 
|---|
| 40 |                 /// and sizeof/alignof expressions with the proper variable
 | 
|---|
| 41 |                 template< typename SynTreeClass >
 | 
|---|
| 42 |                 static SynTreeClass *scrub( SynTreeClass *target, const TyVarMap &tyVars );
 | 
|---|
| 43 | 
 | 
|---|
| 44 |                 /// For all dynamic-layout types with type variables in `tyVars`, replaces generic types, dtypes, and ftypes with the appropriate void type,
 | 
|---|
| 45 |                 /// and sizeof/alignof expressions with the proper variable
 | 
|---|
| 46 |                 template< typename SynTreeClass >
 | 
|---|
| 47 |                 static SynTreeClass *scrubDynamic( SynTreeClass *target, const TyVarMap &tyVars );
 | 
|---|
| 48 | 
 | 
|---|
| 49 |                 /// For all polymorphic types, replaces generic types, dtypes, and ftypes with the appropriate void type,
 | 
|---|
| 50 |                 /// and sizeof/alignof expressions with the proper variable
 | 
|---|
| 51 |                 template< typename SynTreeClass >
 | 
|---|
| 52 |                 static SynTreeClass *scrubAll( SynTreeClass *target );
 | 
|---|
| 53 | 
 | 
|---|
| 54 |                 /// determine if children should be visited based on whether base type should be scrubbed.
 | 
|---|
| 55 |                 void primeBaseScrub( Type * );
 | 
|---|
| 56 | 
 | 
|---|
| 57 |                 void premutate( TypeInstType * ) { visit_children = false; }
 | 
|---|
| 58 |                 void premutate( StructInstType * ) { visit_children = false; }
 | 
|---|
| 59 |                 void premutate( UnionInstType * ) { visit_children = false; }
 | 
|---|
| 60 |                 void premutate( SizeofExpr * szeof ) { primeBaseScrub( szeof->type ); }
 | 
|---|
| 61 |                 void premutate( AlignofExpr * algnof ) { primeBaseScrub( algnof->type ); }
 | 
|---|
| 62 |                 void premutate( PointerType * pointer ) { primeBaseScrub( pointer->base ); }
 | 
|---|
| 63 | 
 | 
|---|
| 64 |                 Type * postmutate( TypeInstType * typeInst );
 | 
|---|
| 65 |                 Type * postmutate( StructInstType * structInst );
 | 
|---|
| 66 |                 Type * postmutate( UnionInstType * unionInst );
 | 
|---|
| 67 |                 Expression * postmutate( SizeofExpr * szeof );
 | 
|---|
| 68 |                 Expression * postmutate( AlignofExpr * algnof );
 | 
|---|
| 69 |                 Type * postmutate( PointerType * pointer );
 | 
|---|
| 70 | 
 | 
|---|
| 71 |           private:
 | 
|---|
| 72 |                 /// Returns the type if it should be scrubbed, NULL otherwise.
 | 
|---|
| 73 |                 Type* shouldScrub( Type *ty ) {
 | 
|---|
| 74 |                         switch ( mode ) {
 | 
|---|
| 75 |                         case FromMap: return isPolyType( ty, *tyVars );
 | 
|---|
| 76 |                         case DynamicFromMap: return isDynType( ty, *tyVars );
 | 
|---|
| 77 |                         case All: return isPolyType( ty );
 | 
|---|
| 78 |                         }
 | 
|---|
| 79 |                         assert(false); return nullptr; // unreachable
 | 
|---|
| 80 |                         // return dynamicOnly ? isDynType( ty, tyVars ) : isPolyType( ty, tyVars );
 | 
|---|
| 81 |                 }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |                 /// Mutates (possibly generic) aggregate types appropriately
 | 
|---|
| 84 |                 Type* mutateAggregateType( Type *ty );
 | 
|---|
| 85 | 
 | 
|---|
| 86 |                 const TyVarMap *tyVars;  ///< Type variables to scrub
 | 
|---|
| 87 |                 ScrubMode mode;          ///< which type variables to scrub? [FromMap]
 | 
|---|
| 88 | 
 | 
|---|
| 89 |                 Type * dynType = nullptr; ///< result of shouldScrub
 | 
|---|
| 90 |         };
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         template< typename SynTreeClass >
 | 
|---|
| 93 |         SynTreeClass * ScrubTyVars::scrub( SynTreeClass *target, const TyVarMap &tyVars ) {
 | 
|---|
| 94 |                 PassVisitor<ScrubTyVars> scrubber( tyVars );
 | 
|---|
| 95 |                 return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
 | 
|---|
| 96 |         }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |         template< typename SynTreeClass >
 | 
|---|
| 99 |         SynTreeClass * ScrubTyVars::scrubDynamic( SynTreeClass *target, const TyVarMap &tyVars ) {
 | 
|---|
| 100 |                 PassVisitor<ScrubTyVars> scrubber( tyVars, ScrubTyVars::DynamicFromMap );
 | 
|---|
| 101 |                 return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
 | 
|---|
| 102 |         }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         template< typename SynTreeClass >
 | 
|---|
| 105 |         SynTreeClass * ScrubTyVars::scrubAll( SynTreeClass *target ) {
 | 
|---|
| 106 |                 PassVisitor<ScrubTyVars> scrubber;
 | 
|---|
| 107 |                 return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
 | 
|---|
| 108 |         }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | } // namespace GenPoly
 | 
|---|
| 111 | 
 | 
|---|
| 112 | // Local Variables: //
 | 
|---|
| 113 | // tab-width: 4 //
 | 
|---|
| 114 | // mode: c++ //
 | 
|---|
| 115 | // compile-command: "make install" //
 | 
|---|
| 116 | // End: //
 | 
|---|