source: src/SynTree/TypeSubstitution.cc @ bfc7811

new-envwith_gc
Last change on this file since bfc7811 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 6 years ago

First pass at delete removal

  • Property mode set to 100644
File size: 6.4 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 : Peter A. Buhr
12// Last Modified On : Thu Mar 16 15:54:35 2017
13// Update Count     : 4
14//
15
16#include <ostream>  // for ostream, basic_ostream, operator<<, endl
17
18#include "Type.h"   // for TypeInstType, Type, StructInstType, UnionInstType
19#include "TypeSubstitution.h"
20
21TypeSubstitution::TypeSubstitution() {
22}
23
24TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) {
25        initialize( other, *this );
26}
27
28TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
29        if ( this == &other ) return *this;
30        initialize( other, *this );
31        return *this;
32}
33
34void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
35        dest.typeEnv.clear();
36        dest.varEnv.clear();
37        dest.add( src );
38}
39
40void TypeSubstitution::add( const TypeSubstitution &other ) {
41        for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) {
42                typeEnv[ i->first ] = i->second->clone();
43        } // for
44        for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) {
45                varEnv[ i->first ] = i->second->clone();
46        } // for
47}
48
49void TypeSubstitution::add( std::string formalType, Type *actualType ) {
50        typeEnv[ formalType ] = actualType->clone();
51}
52
53void TypeSubstitution::remove( std::string formalType ) {
54        typeEnv.erase( formalType );
55}
56
57Type *TypeSubstitution::lookup( std::string formalType ) const {
58        TypeEnvType::const_iterator i = typeEnv.find( formalType );
59
60        // break on not in substitution set
61        if ( i == typeEnv.end() ) return 0;
62
63        // attempt to transitively follow TypeInstType links.
64        while ( TypeInstType *actualType = dynamic_cast< TypeInstType* >( i->second ) ) {
65                const std::string& typeName = actualType->get_name();
66
67                // break cycles in the transitive follow
68                if ( formalType == typeName ) break;
69
70                // Look for the type this maps to, returning previous mapping if none-such
71                i = typeEnv.find( typeName );
72                if ( i == typeEnv.end() ) return actualType;
73        }
74
75        // return type from substitution set
76        return i->second;
77
78#if 0
79        if ( i == typeEnv.end() ) {
80                return 0;
81        } else {
82                return i->second;
83        } // if
84#endif
85}
86
87bool TypeSubstitution::empty() const {
88        return typeEnv.empty() && varEnv.empty();
89}
90
91namespace {
92        struct EnvTrimmer {
93                TypeSubstitution * env, * newEnv;
94                EnvTrimmer( TypeSubstitution * env, TypeSubstitution * newEnv ) : env( env ), newEnv( newEnv ){}
95                void previsit( TypeDecl * tyDecl ) {
96                        // transfer known bindings for seen type variables
97                        if ( Type * t = env->lookup( tyDecl->name ) ) {
98                                newEnv->add( tyDecl->name, t );
99                        }
100                }
101        };
102} // namespace
103
104/// reduce environment to just the parts that are referenced in a given expression
105TypeSubstitution * TypeSubstitution::newFromExpr( Expression * expr, TypeSubstitution * env ) {
106        if ( env ) {
107                TypeSubstitution * newEnv = new TypeSubstitution();
108                PassVisitor<EnvTrimmer> trimmer( env, newEnv );
109                expr->accept( trimmer );
110                return newEnv;
111        }
112        return nullptr;
113}
114
115void TypeSubstitution::normalize() {
116        PassVisitor<Substituter> sub( *this, true );
117        do {
118                sub.pass.subCount = 0;
119                sub.pass.freeOnly = true;
120                for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
121                        i->second = i->second->acceptMutator( sub );
122                }
123        } while ( sub.pass.subCount );
124}
125
126Type * TypeSubstitution::Substituter::postmutate( TypeInstType *inst ) {
127        BoundVarsType::const_iterator bound = boundVars.find( inst->name );
128        if ( bound != boundVars.end() ) return inst;
129
130        TypeEnvType::const_iterator i = sub.typeEnv.find( inst->get_name() );
131        if ( i == sub.typeEnv.end() ) {
132                return inst;
133        } else {
134///         std::cerr << "found " << inst->get_name() << ", replacing with ";
135///         i->second->print( std::cerr );
136///         std::cerr << std::endl;
137                subCount++;
138                Type * newtype = i->second->clone();
139                newtype->get_qualifiers() |= inst->get_qualifiers();
140                return newtype;
141        } // if
142}
143
144Expression * TypeSubstitution::Substituter::postmutate( NameExpr * nameExpr ) {
145        VarEnvType::const_iterator i = sub.varEnv.find( nameExpr->name );
146        if ( i == sub.varEnv.end() ) {
147                return nameExpr;
148        } else {
149                subCount++;
150                return i->second->clone();
151        } // if
152}
153
154void TypeSubstitution::Substituter::premutate( Type * type ) {
155        GuardValue( boundVars );
156        // bind type variables from forall-qualifiers
157        if ( freeOnly ) {
158                for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
159                        boundVars.insert( (*tyvar)->name );
160                } // for
161        } // if
162}
163
164template< typename TypeClass >
165void TypeSubstitution::Substituter::handleAggregateType( TypeClass * type ) {
166        GuardValue( boundVars );
167        // bind type variables from forall-qualifiers
168        if ( freeOnly ) {
169                for ( Type::ForallList::const_iterator tyvar = type->forall.begin(); tyvar != type->forall.end(); ++tyvar ) {
170                        boundVars.insert( (*tyvar)->name );
171                } // for
172                // bind type variables from generic type instantiations
173                std::list< TypeDecl* > *baseParameters = type->get_baseParameters();
174                if ( baseParameters && ! type->parameters.empty() ) {
175                        for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) {
176                                boundVars.insert( (*tyvar)->name );
177                        } // for
178                } // if
179        } // if
180}
181
182void TypeSubstitution::Substituter::premutate( StructInstType * aggregateUseType ) {
183        handleAggregateType( aggregateUseType );
184}
185
186void TypeSubstitution::Substituter::premutate( UnionInstType *aggregateUseType ) {
187        handleAggregateType( aggregateUseType );
188}
189
190void TypeSubstitution::print( std::ostream &os, Indenter indent ) const {
191        os << indent << "Types:" << std::endl;
192        for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
193                os << indent+1 << i->first << " -> ";
194                i->second->print( os, indent+2 );
195                os << std::endl;
196        } // for
197        os << indent << "Non-types:" << std::endl;
198        for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) {
199                os << indent+1 << i->first << " -> ";
200                i->second->print( os, indent+2 );
201                os << std::endl;
202        } // for
203}
204
205std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) {
206        sub.print( out );
207        return out;
208}
209
210
211// Local Variables: //
212// tab-width: 4 //
213// mode: c++ //
214// compile-command: "make install" //
215// End: //
Note: See TracBrowser for help on using the repository browser.