source: src/SynTree/TypeSubstitution.cc @ 986dd36

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 986dd36 was 2ec65ad, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Refactor trimEnv into TypeSubstitution::newFromExpr

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