source: src/AST/TypeSubstitution.cpp @ 361bf01

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 361bf01 was 361bf01, checked in by Fangren Yu <f37yu@…>, 3 years ago

remove ParameterizedType? and put content into FunctionType?

  • Property mode set to 100644
File size: 6.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
12// Last Modified On : Mon Jun  3 13:26:00 2017
13// Update Count     : 5
[c671112]14//
15
16#include "Type.hpp"   // for TypeInstType, Type, StructInstType, UnionInstType
17#include "TypeSubstitution.hpp"
18
19namespace ast {
20
[c15085d]21
[0d070ca]22// size_t TypeSubstitution::Substituter::traceId = Stats::Heap::new_stacktrace_id("TypeSubstitution");
[c15085d]23
[c671112]24TypeSubstitution::TypeSubstitution() {
25}
26
27TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) : Node() {
28        initialize( other, *this );
29}
30
[dafe9e1]31TypeSubstitution::~TypeSubstitution() {}
[c671112]32
33TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
34        if ( this == &other ) return *this;
35        initialize( other, *this );
36        return *this;
37}
38
39void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
40        dest.typeEnv.clear();
41        dest.varEnv.clear();
42        dest.add( src );
43}
44
45void TypeSubstitution::add( const TypeSubstitution &other ) {
46        for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) {
47                typeEnv[ i->first ] = i->second;
48        } // for
49        for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) {
50                varEnv[ i->first ] = i->second;
51        } // for
52}
53
54void TypeSubstitution::add( std::string formalType, const Type *actualType ) {
55        typeEnv[ formalType ] = actualType;
56}
57
[172d9342]58void TypeSubstitution::addVar( std::string formalExpr, const Expr *actualExpr ) {
59        varEnv[ formalExpr ] = actualExpr;
60}
61
[c671112]62void TypeSubstitution::remove( std::string formalType ) {
63        TypeEnvType::iterator i = typeEnv.find( formalType );
64        if ( i != typeEnv.end() ) {
65                typeEnv.erase( formalType );
66        } // if
67}
68
69const Type *TypeSubstitution::lookup( std::string formalType ) const {
70        TypeEnvType::const_iterator i = typeEnv.find( formalType );
71
72        // break on not in substitution set
73        if ( i == typeEnv.end() ) return 0;
74
75        // attempt to transitively follow TypeInstType links.
76        while ( const TypeInstType *actualType = i->second.as<TypeInstType>()) {
77                const std::string& typeName = actualType->name;
78
79                // break cycles in the transitive follow
80                if ( formalType == typeName ) break;
81
82                // Look for the type this maps to, returning previous mapping if none-such
83                i = typeEnv.find( typeName );
84                if ( i == typeEnv.end() ) return actualType;
85        }
86
87        // return type from substitution set
88        return i->second;
89}
90
91bool TypeSubstitution::empty() const {
92        return typeEnv.empty() && varEnv.empty();
93}
94
95namespace {
96        struct EnvTrimmer {
[55b6476]97                const TypeSubstitution * env;
[c671112]98                TypeSubstitution * newEnv;
99                EnvTrimmer( const TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
100                void previsit( TypeDecl * tyDecl ) {
101                        // transfer known bindings for seen type variables
102                        if ( const Type * t = env->lookup( tyDecl->name ) ) {
103                                newEnv->add( tyDecl->name, t );
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
120void TypeSubstitution::normalize() {
[55b6476]121        Pass<Substituter> sub( *this, true );
[c671112]122        do {
[7ff3e522]123                sub.core.subCount = 0;
124                sub.core.freeOnly = true;
[c671112]125                for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
[55b6476]126                        i->second = i->second->accept( sub );
[c671112]127                }
[7ff3e522]128        } while ( sub.core.subCount );
[c671112]129}
130
[55b6476]131const Type * TypeSubstitution::Substituter::postvisit( const TypeInstType *inst ) {
[c671112]132        BoundVarsType::const_iterator bound = boundVars.find( inst->name );
133        if ( bound != boundVars.end() ) return inst;
134
135        TypeEnvType::const_iterator i = sub.typeEnv.find( inst->name );
136        if ( i == sub.typeEnv.end() ) {
137                return inst;
138        } else {
139                // cut off infinite loop for the case where a type is bound to itself.
140                // Note: this does not prevent cycles in the general case, so it may be necessary to do something more sophisticated here.
141                // TODO: investigate preventing type variables from being bound to themselves in the first place.
[55b6476]142                if ( const TypeInstType * replacement = i->second.as<TypeInstType>() ) {
[c671112]143                        if ( inst->name == replacement->name ) {
144                                return inst;
145                        }
146                }
147                // std::cerr << "found " << inst->name << ", replacing with " << i->second << std::endl;
148                subCount++;
[55b6476]149                ptr<Type> newType = i->second; // force clone if needed
150                add_qualifiers( newType, inst->qualifiers );
[2890212]151                // Note: need to recursively apply substitution to the new type because normalize does not
[55b6476]152                // substitute bound vars, but bound vars must be substituted when not in freeOnly mode.
153                newType = newType->accept( *visitor );
154                return newType.release();
[c671112]155        } // if
156}
157
[55b6476]158const Expr * TypeSubstitution::Substituter::postvisit( const NameExpr * nameExpr ) {
[c671112]159        VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name );
160        if ( i == sub.varEnv.end() ) {
161                return nameExpr;
162        } else {
163                subCount++;
[55b6476]164                return i->second;
[c671112]165        } // if
166}
167
[361bf01]168void TypeSubstitution::Substituter::previsit( const FunctionType * ptype ) {
[c671112]169        GuardValue( boundVars );
170        // bind type variables from forall-qualifiers
171        if ( freeOnly ) {
[55b6476]172                for ( const TypeDecl * tyvar : ptype->forall ) {
173                                boundVars.insert( tyvar->name );
[c671112]174                } // for
175        } // if
176}
177
[98e8b3b]178void TypeSubstitution::Substituter::handleAggregateType( const BaseInstType * type ) {
[c671112]179        GuardValue( boundVars );
180        // bind type variables from forall-qualifiers
181        if ( freeOnly ) {
182                // bind type variables from generic type instantiations
[55b6476]183                if ( auto decl = type->aggr() ) {
[417117e]184                        if ( ! type->params.empty() ) {
185                                for ( const TypeDecl * tyvar : decl->params ) {
[55b6476]186                                        boundVars.insert( tyvar->name );
187                                } // for
188                        } // if
189                }
[c671112]190        } // if
191}
192
[55b6476]193void TypeSubstitution::Substituter::previsit( const StructInstType * aggregateUseType ) {
[c671112]194        handleAggregateType( aggregateUseType );
195}
196
[55b6476]197void TypeSubstitution::Substituter::previsit( const UnionInstType *aggregateUseType ) {
[c671112]198        handleAggregateType( aggregateUseType );
199}
200
201} // namespace ast
202
203// Local Variables: //
204// tab-width: 4 //
205// mode: c++ //
206// compile-command: "make install" //
207// End: //
Note: See TracBrowser for help on using the repository browser.