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