source: src/AST/TypeSubstitution.cpp @ bab42de

Last change on this file since bab42de was bccd70a, checked in by Andrew Beach <ajbeach@…>, 13 months ago

Removed internal code from TypeSubstitution? header. It caused a chain of include problems, which have been corrected.

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[c671112]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// TypeSubstitution.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[dafe9e1]11// Last Modified By : Andrew Beach
[bccd70a]12// Last Modified On : Thr May 25 11:24:00 2023
13// Update Count     : 6
[c671112]14//
15
16#include "TypeSubstitution.hpp"
17
[bccd70a]18#include "Type.hpp"   // for TypeInstType, Type, StructInstType, UnionInstType
19#include "Pass.hpp"   // for Pass, PureVisitor, WithGuards, WithVisitorRef
[c15085d]20
[bccd70a]21namespace ast {
[c15085d]22
[c671112]23TypeSubstitution::TypeSubstitution() {
24}
25
26TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) : Node() {
27        initialize( other, *this );
28}
29
[dafe9e1]30TypeSubstitution::~TypeSubstitution() {}
[c671112]31
32TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
33        if ( this == &other ) return *this;
34        initialize( other, *this );
35        return *this;
36}
37
38void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
[a8b87d3]39        dest.typeMap.clear();
[c671112]40        dest.add( src );
41}
42
43void TypeSubstitution::add( const TypeSubstitution &other ) {
[a8b87d3]44        for ( TypeMap::const_iterator i = other.typeMap.begin(); i != other.typeMap.end(); ++i ) {
45                typeMap[ i->first ] = i->second;
[c671112]46        } // for
47}
48
[3e5dd913]49void TypeSubstitution::add( const TypeInstType * formalType, const Type *actualType ) {
[a8b87d3]50        typeMap[ *formalType ] = actualType;
[c671112]51}
52
[93c10de]53void TypeSubstitution::add( const TypeEnvKey & key, const Type * actualType) {
[a8b87d3]54        typeMap[ key ] = actualType;
[172d9342]55}
56
[3e5dd913]57void TypeSubstitution::remove( const TypeInstType * formalType ) {
[a8b87d3]58        TypeMap::iterator i = typeMap.find( *formalType );
59        if ( i != typeMap.end() ) {
60                typeMap.erase( *formalType );
[c671112]61        } // if
62}
63
[a8b87d3]64const Type *TypeSubstitution::lookup(
[93c10de]65                const TypeEnvKey & formalType ) const {
[a8b87d3]66        TypeMap::const_iterator i = typeMap.find( formalType );
[c671112]67
68        // break on not in substitution set
[a8b87d3]69        if ( i == typeMap.end() ) return 0;
[c671112]70
71        // attempt to transitively follow TypeInstType links.
72        while ( const TypeInstType *actualType = i->second.as<TypeInstType>()) {
73                // break cycles in the transitive follow
[a8b87d3]74                if ( formalType == *actualType ) break;
[c671112]75
76                // Look for the type this maps to, returning previous mapping if none-such
[a8b87d3]77                i = typeMap.find( *actualType );
78                if ( i == typeMap.end() ) return actualType;
[c671112]79        }
80
81        // return type from substitution set
82        return i->second;
83}
84
[a8b87d3]85const Type *TypeSubstitution::lookup( const TypeInstType * formalType ) const {
[93c10de]86        return lookup( ast::TypeEnvKey( *formalType ) );
[a8b87d3]87}
88
[c671112]89bool TypeSubstitution::empty() const {
[a8b87d3]90        return typeMap.empty();
[c671112]91}
92
93namespace {
94        struct EnvTrimmer {
[55b6476]95                const TypeSubstitution * env;
[c671112]96                TypeSubstitution * newEnv;
97                EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
[9e23b446]98                void previsit( const FunctionType * ftype ) {
[c671112]99                        // transfer known bindings for seen type variables
[3e5dd913]100                        for (auto & formal : ftype->forall) {
101                                if ( const Type * t = env->lookup( formal ) ) {
102                                        newEnv->add( formal, t );
103                                }
[c671112]104                        }
105                }
106        };
107} // namespace
108
109/// reduce environment to just the parts that are referenced in a given expression
110TypeSubstitution * TypeSubstitution::newFromExpr( const Expr * expr, const TypeSubstitution * env ) {
111        if ( env ) {
112                TypeSubstitution * newEnv = new TypeSubstitution();
113                Pass<EnvTrimmer> trimmer( env, newEnv );
114                expr->accept( trimmer );
115                return newEnv;
116        }
117        return nullptr;
118}
119
[bccd70a]120// definitition must happen after PassVisitor is included so that WithGuards can be used
121struct TypeSubstitution::Substituter : public WithGuards, public WithVisitorRef<Substituter>, public PureVisitor {
122        //static size_t traceId;
123
124        Substituter( const TypeSubstitution & sub, bool freeOnly ) : sub( sub ), freeOnly( freeOnly ) {}
125
126        const Type * postvisit( const TypeInstType * aggregateUseType );
127
128        /// Records type variable bindings from forall-statements
129        void previsit( const FunctionType * type );
130        /// Records type variable bindings from forall-statements and instantiations of generic types
131        // void handleAggregateType( const BaseInstType * type );
132
133        // void previsit( const StructInstType * aggregateUseType );
134        // void previsit( const UnionInstType * aggregateUseType );
135
136        const TypeSubstitution & sub;
137        int subCount = 0;
138        bool freeOnly;
139        typedef std::unordered_set< TypeEnvKey > BoundVarsType;
140        BoundVarsType boundVars;
141};
142
143// size_t TypeSubstitution::Substituter::traceId = Stats::Heap::new_stacktrace_id("TypeSubstitution");
144
[c671112]145void TypeSubstitution::normalize() {
[55b6476]146        Pass<Substituter> sub( *this, true );
[c671112]147        do {
[7ff3e522]148                sub.core.subCount = 0;
149                sub.core.freeOnly = true;
[a8b87d3]150                for ( TypeMap::iterator i = typeMap.begin(); i != typeMap.end(); ++i ) {
[55b6476]151                        i->second = i->second->accept( sub );
[c671112]152                }
[7ff3e522]153        } while ( sub.core.subCount );
[c671112]154}
155
[bccd70a]156TypeSubstitution::ApplyResult<Node> TypeSubstitution::applyBase(
157                const Node * input, bool isFree ) const {
158        assert( input );
159        Pass<Substituter> sub( *this, isFree );
160        const Node * output = input->accept( sub );
161        return { output, sub.core.subCount };
162}
163
[55b6476]164const Type * TypeSubstitution::Substituter::postvisit( const TypeInstType *inst ) {
[3e5dd913]165        BoundVarsType::const_iterator bound = boundVars.find( *inst );
[c671112]166        if ( bound != boundVars.end() ) return inst;
167
[a8b87d3]168        TypeMap::const_iterator i = sub.typeMap.find( *inst );
169        if ( i == sub.typeMap.end() ) {
[c671112]170                return inst;
171        } else {
172                // cut off infinite loop for the case where a type is bound to itself.
173                // Note: this does not prevent cycles in the general case, so it may be necessary to do something more sophisticated here.
174                // TODO: investigate preventing type variables from being bound to themselves in the first place.
[55b6476]175                if ( const TypeInstType * replacement = i->second.as<TypeInstType>() ) {
[3e5dd913]176                        if ( *inst == *replacement ) {
[c671112]177                                return inst;
178                        }
179                }
180                // std::cerr << "found " << inst->name << ", replacing with " << i->second << std::endl;
181                subCount++;
[55b6476]182                ptr<Type> newType = i->second; // force clone if needed
183                add_qualifiers( newType, inst->qualifiers );
[2890212]184                // Note: need to recursively apply substitution to the new type because normalize does not
[55b6476]185                // substitute bound vars, but bound vars must be substituted when not in freeOnly mode.
186                newType = newType->accept( *visitor );
187                return newType.release();
[c671112]188        } // if
189}
190
[361bf01]191void TypeSubstitution::Substituter::previsit( const FunctionType * ptype ) {
[c671112]192        GuardValue( boundVars );
193        // bind type variables from forall-qualifiers
194        if ( freeOnly ) {
[3e5dd913]195                for ( auto & tyvar : ptype->forall ) {
196                                boundVars.insert( *tyvar );
[c671112]197                } // for
198        } // if
199}
200
[3e5dd913]201/*
[98e8b3b]202void TypeSubstitution::Substituter::handleAggregateType( const BaseInstType * type ) {
[c671112]203        GuardValue( boundVars );
204        // bind type variables from forall-qualifiers
205        if ( freeOnly ) {
206                // bind type variables from generic type instantiations
[55b6476]207                if ( auto decl = type->aggr() ) {
[417117e]208                        if ( ! type->params.empty() ) {
209                                for ( const TypeDecl * tyvar : decl->params ) {
[3e5dd913]210                                        boundVars.insert( *tyvar );
[55b6476]211                                } // for
212                        } // if
213                }
[c671112]214        } // if
215}
216
[55b6476]217void TypeSubstitution::Substituter::previsit( const StructInstType * aggregateUseType ) {
[c671112]218        handleAggregateType( aggregateUseType );
219}
220
[55b6476]221void TypeSubstitution::Substituter::previsit( const UnionInstType *aggregateUseType ) {
[c671112]222        handleAggregateType( aggregateUseType );
223}
[3e5dd913]224*/
[c671112]225
226} // namespace ast
227
228// Local Variables: //
229// tab-width: 4 //
230// mode: c++ //
231// compile-command: "make install" //
232// End: //
Note: See TracBrowser for help on using the repository browser.