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 "GenPoly.h" // for TyVarMap, isPolyType, isDynType
|
---|
21 | #include "SynTree/Mutator.h" // for Mutator
|
---|
22 | #include "SynTree/Type.h" // for Type (ptr only), PointerType (ptr only)
|
---|
23 |
|
---|
24 | class AlignofExpr;
|
---|
25 | class Expression;
|
---|
26 | class SizeofExpr;
|
---|
27 |
|
---|
28 | namespace GenPoly {
|
---|
29 | class ScrubTyVars : public Mutator {
|
---|
30 | /// Whether to scrub all type variables from the provided map, dynamic type variables from the provided map, or all type variables
|
---|
31 | enum ScrubMode { FromMap, DynamicFromMap, All };
|
---|
32 |
|
---|
33 | ScrubTyVars() : tyVars(nullptr), mode( All ) {}
|
---|
34 |
|
---|
35 | ScrubTyVars( const TyVarMap &tyVars, ScrubMode mode = FromMap ): tyVars( &tyVars ), mode( mode ) {}
|
---|
36 |
|
---|
37 | public:
|
---|
38 | /// For all polymorphic types with type variables in `tyVars`, replaces generic types, dtypes, and ftypes with the appropriate void type,
|
---|
39 | /// and sizeof/alignof expressions with the proper variable
|
---|
40 | template< typename SynTreeClass >
|
---|
41 | static SynTreeClass *scrub( SynTreeClass *target, const TyVarMap &tyVars );
|
---|
42 |
|
---|
43 | /// For all dynamic-layout types with type variables in `tyVars`, replaces generic types, dtypes, and ftypes with the appropriate void type,
|
---|
44 | /// and sizeof/alignof expressions with the proper variable
|
---|
45 | template< typename SynTreeClass >
|
---|
46 | static SynTreeClass *scrubDynamic( SynTreeClass *target, const TyVarMap &tyVars );
|
---|
47 |
|
---|
48 | /// For all polymorphic types, replaces generic types, dtypes, and ftypes with the appropriate void type,
|
---|
49 | /// and sizeof/alignof expressions with the proper variable
|
---|
50 | template< typename SynTreeClass >
|
---|
51 | static SynTreeClass *scrubAll( SynTreeClass *target );
|
---|
52 |
|
---|
53 | virtual Type* mutate( TypeInstType *typeInst );
|
---|
54 | virtual Type* mutate( StructInstType *structInst );
|
---|
55 | virtual Type* mutate( UnionInstType *unionInst );
|
---|
56 | virtual Expression* mutate( SizeofExpr *szeof );
|
---|
57 | virtual Expression* mutate( AlignofExpr *algnof );
|
---|
58 | virtual Type* mutate( PointerType *pointer );
|
---|
59 |
|
---|
60 | private:
|
---|
61 | /// Returns the type if it should be scrubbed, NULL otherwise.
|
---|
62 | Type* shouldScrub( Type *ty ) {
|
---|
63 | switch ( mode ) {
|
---|
64 | case FromMap: return isPolyType( ty, *tyVars );
|
---|
65 | case DynamicFromMap: return isDynType( ty, *tyVars );
|
---|
66 | case All: return isPolyType( ty );
|
---|
67 | }
|
---|
68 | assert(false); return nullptr; // unreachable
|
---|
69 | // return dynamicOnly ? isDynType( ty, tyVars ) : isPolyType( ty, tyVars );
|
---|
70 | }
|
---|
71 |
|
---|
72 | /// Mutates (possibly generic) aggregate types appropriately
|
---|
73 | Type* mutateAggregateType( Type *ty );
|
---|
74 |
|
---|
75 | const TyVarMap *tyVars; ///< Type variables to scrub
|
---|
76 | ScrubMode mode; ///< which type variables to scrub? [FromMap]
|
---|
77 | };
|
---|
78 |
|
---|
79 | template< typename SynTreeClass >
|
---|
80 | SynTreeClass * ScrubTyVars::scrub( SynTreeClass *target, const TyVarMap &tyVars ) {
|
---|
81 | ScrubTyVars scrubber( tyVars );
|
---|
82 | return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
|
---|
83 | }
|
---|
84 |
|
---|
85 | template< typename SynTreeClass >
|
---|
86 | SynTreeClass * ScrubTyVars::scrubDynamic( SynTreeClass *target, const TyVarMap &tyVars ) {
|
---|
87 | ScrubTyVars scrubber( tyVars, ScrubTyVars::DynamicFromMap );
|
---|
88 | return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
|
---|
89 | }
|
---|
90 |
|
---|
91 | template< typename SynTreeClass >
|
---|
92 | SynTreeClass * ScrubTyVars::scrubAll( SynTreeClass *target ) {
|
---|
93 | ScrubTyVars scrubber;
|
---|
94 | return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
|
---|
95 | }
|
---|
96 |
|
---|
97 | } // namespace GenPoly
|
---|
98 |
|
---|
99 | // Local Variables: //
|
---|
100 | // tab-width: 4 //
|
---|
101 | // mode: c++ //
|
---|
102 | // compile-command: "make install" //
|
---|
103 | // End: //
|
---|