source: src/AST/TypeSubstitution.cpp@ 86b8d16

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 86b8d16 was 3e5dd913, checked in by Fangren Yu <f37yu@…>, 5 years ago

reimplement function type and eliminate deep copy

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