source: src/AST/TypeSubstitution.cpp@ 90ce35aa

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 90ce35aa was 2890212, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Startup.cfa now compiles with new ast

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