source: src/GenPoly/ScrubTyVars.h @ c8837e5

ADTast-experimental
Last change on this file since c8837e5 was c8837e5, checked in by Andrew Beach <ajbeach@…>, 21 months ago

Rewrite in GenPoly? to avoid mixing new AST and TyVarMap? (which internally has old AST code). Some nearby functions got writen out even though they are not used, and hence not tested.

  • Property mode set to 100644
File size: 6.0 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
[3606fe4]11// Last Modified By : Andrew Beach
[c8837e5]12// Last Modified On : Fri Oct  7 15:51:00 2022
13// Update Count     : 4
[51587aa]14//
[01aeade]15
[6b0b624]16#pragma once
[51b7345]17
[ea6332d]18#include <cassert>            // for assert
[78dd0da]19
[3606fe4]20#include "AST/Fwd.hpp"        // for Node
[b8a4f47]21#include "Common/PassVisitor.h"
[08fc48f]22#include "GenPoly.h"          // for TyVarMap, isPolyType, isDynType
23#include "SynTree/Mutator.h"  // for Mutator
24#include "SynTree/Type.h"     // for Type (ptr only), PointerType (ptr only)
[51b7345]25
[08fc48f]26class AlignofExpr;
27class Expression;
28class SizeofExpr;
[51b7345]29
30namespace GenPoly {
[b8a4f47]31        struct ScrubTyVars : public WithVisitorRef<ScrubTyVars>, public WithShortCircuiting, public WithGuards {
[5a3ac84]32                /// Whether to scrub all type variables from the provided map, dynamic type variables from the provided map, or all type variables
33                enum ScrubMode { FromMap, DynamicFromMap, All };
[f8b961b]34
[5a3ac84]35                ScrubTyVars() : tyVars(nullptr), mode( All ) {}
36
37                ScrubTyVars( const TyVarMap &tyVars, ScrubMode mode = FromMap ): tyVars( &tyVars ), mode( mode ) {}
38
39        public:
[ebe9b3a]40                /// For all polymorphic 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
[01aeade]42                template< typename SynTreeClass >
43                static SynTreeClass *scrub( SynTreeClass *target, const TyVarMap &tyVars );
[b18b0b5]44
[3bb195cb]45                /// For all dynamic-layout types with type variables in `tyVars`, 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 *scrubDynamic( SynTreeClass *target, const TyVarMap &tyVars );
49
[5a3ac84]50                /// For all polymorphic types, replaces generic types, dtypes, and ftypes with the appropriate void type,
51                /// and sizeof/alignof expressions with the proper variable
52                template< typename SynTreeClass >
53                static SynTreeClass *scrubAll( SynTreeClass *target );
54
[b8a4f47]55                /// determine if children should be visited based on whether base type should be scrubbed.
56                void primeBaseScrub( Type * );
57
58                void premutate( TypeInstType * ) { visit_children = false; }
59                void premutate( StructInstType * ) { visit_children = false; }
60                void premutate( UnionInstType * ) { visit_children = false; }
61                void premutate( SizeofExpr * szeof ) { primeBaseScrub( szeof->type ); }
62                void premutate( AlignofExpr * algnof ) { primeBaseScrub( algnof->type ); }
63                void premutate( PointerType * pointer ) { primeBaseScrub( pointer->base ); }
64
65                Type * postmutate( TypeInstType * typeInst );
66                Type * postmutate( StructInstType * structInst );
67                Type * postmutate( UnionInstType * unionInst );
68                Expression * postmutate( SizeofExpr * szeof );
69                Expression * postmutate( AlignofExpr * algnof );
70                Type * postmutate( PointerType * pointer );
[b18b0b5]71
[01aeade]72          private:
[3bb195cb]73                /// Returns the type if it should be scrubbed, NULL otherwise.
74                Type* shouldScrub( Type *ty ) {
[5a3ac84]75                        switch ( mode ) {
76                        case FromMap: return isPolyType( ty, *tyVars );
77                        case DynamicFromMap: return isDynType( ty, *tyVars );
78                        case All: return isPolyType( ty );
79                        }
80                        assert(false); return nullptr; // unreachable
81                        // return dynamicOnly ? isDynType( ty, tyVars ) : isPolyType( ty, tyVars );
[3bb195cb]82                }
[ea6332d]83
[b18b0b5]84                /// Mutates (possibly generic) aggregate types appropriately
85                Type* mutateAggregateType( Type *ty );
[ea6332d]86
[5a3ac84]87                const TyVarMap *tyVars;  ///< Type variables to scrub
88                ScrubMode mode;          ///< which type variables to scrub? [FromMap]
[b8a4f47]89
90                Type * dynType = nullptr; ///< result of shouldScrub
[01aeade]91        };
92
[bdd516a]93        template< typename SynTreeClass >
[01aeade]94        SynTreeClass * ScrubTyVars::scrub( SynTreeClass *target, const TyVarMap &tyVars ) {
[b8a4f47]95                PassVisitor<ScrubTyVars> scrubber( tyVars );
[01aeade]96                return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
97        }
98
[3bb195cb]99        template< typename SynTreeClass >
100        SynTreeClass * ScrubTyVars::scrubDynamic( SynTreeClass *target, const TyVarMap &tyVars ) {
[b8a4f47]101                PassVisitor<ScrubTyVars> scrubber( tyVars, ScrubTyVars::DynamicFromMap );
[5a3ac84]102                return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
103        }
104
105        template< typename SynTreeClass >
106        SynTreeClass * ScrubTyVars::scrubAll( SynTreeClass *target ) {
[b8a4f47]107                PassVisitor<ScrubTyVars> scrubber;
[3bb195cb]108                return static_cast< SynTreeClass * >( target->acceptMutator( scrubber ) );
109        }
110
[c8837e5]111/// For all polymorphic types with type variables in `typeVars`,
112/// replaces generic types, dtypes, and ftypes with the appropriate void type,
113/// and sizeof/alignof expressions with the proper variable.
114template<typename node_t>
115node_t const * scrubTypeVars(
116                node_t const * target, const TypeVarMap & typeVars ) {
117        return strict_dynamic_cast<node_t const *>(
118                        scrubTypeVars<ast::Node>( target ) );
119}
120
121/// For all dynamic-layout types with type variables in `typeVars`,
122/// replaces generic types, dtypes, and ftypes with the appropriate void type,
123/// and sizeof/alignof expressions with the proper variable.
124template<typename node_t>
125ast::Node const * scrubTypeVarsDynamic(
126                node_t const * target, const TypeVarMap & typeVars ) {
127        return strict_dynamic_cast<node_t const *>(
128                        scrubTypeVarsDynamic<ast::Node>( target, typeVars ) );
129}
130
[3606fe4]131/// For all polymorphic types, replaces generic types, with the appropriate
132/// void type, and sizeof/alignof expressions with the proper variable.
133template<typename node_t>
134node_t const * scrubAllTypeVars( node_t const * target ) {
[c8837e5]135        return strict_dynamic_cast<node_t const *>(
136                        scrubAllTypeVars<ast::Node>( target ) );
[3606fe4]137}
138
[c8837e5]139// We specialize for Node as a base case.
140template<>
141ast::Node const * scrubTypeVars<ast::Node>(
142                const ast::Node * target, const TypeVarMap & typeVars );
143
144template<>
145ast::Node const * scrubTypeVarsDynamic<ast::Node>(
146                ast::Node const * target, const TypeVarMap & typeVars );
147
[3606fe4]148template<>
149ast::Node const * scrubAllTypeVars<ast::Node>( const ast::Node * target );
150
[51b7345]151} // namespace GenPoly
152
[51587aa]153// Local Variables: //
154// tab-width: 4 //
155// mode: c++ //
156// compile-command: "make install" //
157// End: //
Note: See TracBrowser for help on using the repository browser.