source: src/GenPoly/ScrubTyVars.h @ 6c6e36c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6c6e36c was b8a4f47, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Convert ScrubTyVars? to PassVisitor?

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[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//
[ea6332d]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
[ea6332d]18#include <cassert>            // for assert
[78dd0da]19
[b8a4f47]20#include "Common/PassVisitor.h"
[08fc48f]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)
[51b7345]24
[08fc48f]25class AlignofExpr;
26class Expression;
27class SizeofExpr;
[51b7345]28
29namespace GenPoly {
[b8a4f47]30        struct ScrubTyVars : public WithVisitorRef<ScrubTyVars>, public WithShortCircuiting, public WithGuards {
[5a3ac84]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 };
[f8b961b]33
[5a3ac84]34                ScrubTyVars() : tyVars(nullptr), mode( All ) {}
35
36                ScrubTyVars( const TyVarMap &tyVars, ScrubMode mode = FromMap ): tyVars( &tyVars ), mode( mode ) {}
37
38        public:
[ebe9b3a]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
[01aeade]41                template< typename SynTreeClass >
42                static SynTreeClass *scrub( SynTreeClass *target, const TyVarMap &tyVars );
[b18b0b5]43
[3bb195cb]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
[5a3ac84]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
[b8a4f47]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 );
[b18b0b5]70
[01aeade]71          private:
[3bb195cb]72                /// Returns the type if it should be scrubbed, NULL otherwise.
73                Type* shouldScrub( Type *ty ) {
[5a3ac84]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 );
[3bb195cb]81                }
[ea6332d]82
[b18b0b5]83                /// Mutates (possibly generic) aggregate types appropriately
84                Type* mutateAggregateType( Type *ty );
[ea6332d]85
[5a3ac84]86                const TyVarMap *tyVars;  ///< Type variables to scrub
87                ScrubMode mode;          ///< which type variables to scrub? [FromMap]
[b8a4f47]88
89                Type * dynType = nullptr; ///< result of shouldScrub
[01aeade]90        };
91
[bdd516a]92        template< typename SynTreeClass >
[01aeade]93        SynTreeClass * ScrubTyVars::scrub( SynTreeClass *target, const TyVarMap &tyVars ) {
[b8a4f47]94                PassVisitor<ScrubTyVars> scrubber( tyVars );
[01aeade]95                return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
96        }
97
[3bb195cb]98        template< typename SynTreeClass >
99        SynTreeClass * ScrubTyVars::scrubDynamic( SynTreeClass *target, const TyVarMap &tyVars ) {
[b8a4f47]100                PassVisitor<ScrubTyVars> scrubber( tyVars, ScrubTyVars::DynamicFromMap );
[5a3ac84]101                return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
102        }
103
104        template< typename SynTreeClass >
105        SynTreeClass * ScrubTyVars::scrubAll( SynTreeClass *target ) {
[b8a4f47]106                PassVisitor<ScrubTyVars> scrubber;
[3bb195cb]107                return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
108        }
109
[51b7345]110} // namespace GenPoly
111
[51587aa]112// Local Variables: //
113// tab-width: 4 //
114// mode: c++ //
115// compile-command: "make install" //
116// End: //
Note: See TracBrowser for help on using the repository browser.