source: src/SynTree/TypeSubstitution.cc @ e6cee92

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 e6cee92 was c7a3081, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

only bind generic parameters in TypeSubstitution? in freeOnly mode, fix generic instantiation order [fixes #6]

  • Property mode set to 100644
File size: 7.5 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
16#include "Type.h"
17#include "TypeSubstitution.h"
18
[0dd3a2f]19TypeSubstitution::TypeSubstitution() {
20}
[51b7345]21
[0dd3a2f]22TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) {
23        initialize( other, *this );
[51b7345]24}
25
[0dd3a2f]26TypeSubstitution::~TypeSubstitution() {
27        for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
28                delete( i->second );
29        }
30        for ( VarEnvType::iterator i = varEnv.begin(); i != varEnv.end(); ++i ) {
31                delete( i->second );
32        }
[51b7345]33}
34
[0dd3a2f]35TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
36        if ( this == &other ) return *this;
37        initialize( other, *this );
38        return *this;
[51b7345]39}
40
[0dd3a2f]41void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
42        dest.typeEnv.clear();
43        dest.varEnv.clear();
44        dest.add( src );
[51b7345]45}
46
[0dd3a2f]47void TypeSubstitution::add( const TypeSubstitution &other ) {
48        for ( TypeEnvType::const_iterator i = other.typeEnv.begin(); i != other.typeEnv.end(); ++i ) {
49                typeEnv[ i->first ] = i->second->clone();
50        } // for
51        for ( VarEnvType::const_iterator i = other.varEnv.begin(); i != other.varEnv.end(); ++i ) {
52                varEnv[ i->first ] = i->second->clone();
53        } // for
[51b7345]54}
55
[0dd3a2f]56void TypeSubstitution::add( std::string formalType, Type *actualType ) {
57        TypeEnvType::iterator i = typeEnv.find( formalType );
58        if ( i != typeEnv.end() ) {
59                delete i->second;
60        } // if
61        typeEnv[ formalType ] = actualType->clone();
[51b7345]62}
63
[0dd3a2f]64void TypeSubstitution::remove( std::string formalType ) {
65        TypeEnvType::iterator i = typeEnv.find( formalType );
66        if ( i != typeEnv.end() ) {
67                delete i->second;
68                typeEnv.erase( formalType );
69        } // if
[51b7345]70}
71
[0dd3a2f]72Type *TypeSubstitution::lookup( std::string formalType ) const {
73        TypeEnvType::const_iterator i = typeEnv.find( formalType );
[8c49c0e]74
[084184b]75        // break on not in substitution set
76        if ( i == typeEnv.end() ) return 0;
[8c49c0e]77
[084184b]78        // attempt to transitively follow TypeInstType links.
79        while ( TypeInstType *actualType = dynamic_cast< TypeInstType* >( i->second ) ) {
80                const std::string& typeName = actualType->get_name();
[8c49c0e]81
[084184b]82                // break cycles in the transitive follow
83                if ( formalType == typeName ) break;
[8c49c0e]84
[084184b]85                // Look for the type this maps to, returning previous mapping if none-such
86                i = typeEnv.find( typeName );
87                if ( i == typeEnv.end() ) return actualType;
88        }
[8c49c0e]89
[084184b]90        // return type from substitution set
91        return i->second;
[8c49c0e]92
[084184b]93#if 0
[0dd3a2f]94        if ( i == typeEnv.end() ) {
95                return 0;
96        } else {
97                return i->second;
98        } // if
[084184b]99#endif
[51b7345]100}
101
[0dd3a2f]102bool TypeSubstitution::empty() const {
103        return typeEnv.empty() && varEnv.empty();
[51b7345]104}
105
[0dd3a2f]106void TypeSubstitution::normalize() {
107        do {
108                subCount = 0;
109                freeOnly = true;
110                for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
111                        i->second = i->second->acceptMutator( *this );
112                }
113        } while ( subCount );
[51b7345]114}
115
[0dd3a2f]116Type * TypeSubstitution::mutate( TypeInstType *inst ) {
117        BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() );
118        if ( bound != boundVars.end() ) return inst;
[5382492]119
[0dd3a2f]120        TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() );
121        if ( i == typeEnv.end() ) {
122                return inst;
123        } else {
[51b7345]124///         std::cout << "found " << inst->get_name() << ", replacing with ";
125///         i->second->print( std::cout );
126///         std::cout << std::endl;
[0dd3a2f]127                subCount++;
128                Type *newtype = i->second->clone();
[6f95000]129                newtype->get_qualifiers() |= inst->get_qualifiers();
[0dd3a2f]130                delete inst;
131                return newtype;
132        } // if
133}
134
135Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) {
136        VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() );
137        if ( i == varEnv.end() ) {
138                return nameExpr;
139        } else {
140                subCount++;
141                delete nameExpr;
142                return i->second->clone();
143        } // if
[51b7345]144}
145
146template< typename TypeClass >
[0dd3a2f]147Type *TypeSubstitution::handleType( TypeClass *type ) {
148        BoundVarsType oldBoundVars( boundVars );
[37a3b8f9]149        // bind type variables from forall-qualifiers
[0dd3a2f]150        if ( freeOnly ) {
[8c49c0e]151                for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
[0dd3a2f]152                        boundVars.insert( (*tyvar )->get_name() );
153                } // for
154        } // if
155        Type *ret = Mutator::mutate( type );
156        boundVars = oldBoundVars;
157        return ret;
[51b7345]158}
159
[37a3b8f9]160template< typename TypeClass >
161Type *TypeSubstitution::handleAggregateType( TypeClass *type ) {
162        BoundVarsType oldBoundVars( boundVars );
163        // bind type variables from forall-qualifiers
164        if ( freeOnly ) {
[8c49c0e]165                for ( Type::ForallList::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
[37a3b8f9]166                        boundVars.insert( (*tyvar )->get_name() );
167                } // for
[c7a3081]168                // bind type variables from generic type instantiations
169                std::list< TypeDecl* > *baseParameters = type->get_baseParameters();
170                if ( baseParameters && ! type->get_parameters().empty() ) {
171                        for ( std::list< TypeDecl* >::const_iterator tyvar = baseParameters->begin(); tyvar != baseParameters->end(); ++tyvar ) {
172                                boundVars.insert( (*tyvar)->get_name() );
173                        } // for
174                } // if
[ed94eac]175        } // if
[37a3b8f9]176        Type *ret = Mutator::mutate( type );
177        boundVars = oldBoundVars;
178        return ret;
179}
180
[89e6ffc]181Type * TypeSubstitution::mutate( VoidType *voidType ) {
182        return handleType( voidType );
[51b7345]183}
184
[0dd3a2f]185Type * TypeSubstitution::mutate( BasicType *basicType ) {
186        return handleType( basicType );
[51b7345]187}
188
[0dd3a2f]189Type * TypeSubstitution::mutate( PointerType *pointerType ) {
190        return handleType( pointerType );
[51b7345]191}
192
[0dd3a2f]193Type * TypeSubstitution::mutate( ArrayType *arrayType ) {
194        return handleType( arrayType );
[51b7345]195}
196
[0dd3a2f]197Type * TypeSubstitution::mutate( FunctionType *functionType ) {
198        return handleType( functionType );
[51b7345]199}
200
[0dd3a2f]201Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) {
[37a3b8f9]202        return handleAggregateType( aggregateUseType );
[51b7345]203}
204
[0dd3a2f]205Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) {
[37a3b8f9]206        return handleAggregateType( aggregateUseType );
[51b7345]207}
208
[0dd3a2f]209Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) {
210        return handleType( aggregateUseType );
[51b7345]211}
212
[4040425]213Type * TypeSubstitution::mutate( TraitInstType *aggregateUseType ) {
[0dd3a2f]214        return handleType( aggregateUseType );
[51b7345]215}
216
[0dd3a2f]217Type * TypeSubstitution::mutate( TupleType *tupleType ) {
218        return handleType( tupleType );
[51b7345]219}
220
[44b7088]221Type * TypeSubstitution::mutate( VarArgsType *varArgsType ) {
222        return handleType( varArgsType );
223}
224
[89e6ffc]225Type * TypeSubstitution::mutate( ZeroType *zeroType ) {
226        return handleType( zeroType );
227}
228
229Type * TypeSubstitution::mutate( OneType *oneType ) {
230        return handleType( oneType );
231}
232
[e33f321]233TypeSubstitution * TypeSubstitution::acceptMutator( Mutator & mutator ) {
234        for ( auto & p : typeEnv ) {
235                p.second = maybeMutate( p.second, mutator );
236        }
237        for ( auto & p : varEnv ) {
238                p.second = maybeMutate( p.second, mutator );
239        }
240        return this;
241}
242
[0dd3a2f]243void TypeSubstitution::print( std::ostream &os, int indent ) const {
244        os << std::string( indent, ' ' ) << "Types:" << std::endl;
245        for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
246                os << std::string( indent+2, ' ' ) << i->first << " -> ";
247                i->second->print( os, indent+4 );
248                os << std::endl;
249        } // for
250        os << std::string( indent, ' ' ) << "Non-types:" << std::endl;
251        for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) {
252                os << std::string( indent+2, ' ' ) << i->first << " -> ";
253                i->second->print( os, indent+4 );
254                os << std::endl;
255        } // for
[51b7345]256}
257
[5382492]258std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub ) {
259        sub.print( out );
260        return out;
261}
262
263
[0dd3a2f]264// Local Variables: //
265// tab-width: 4 //
266// mode: c++ //
267// compile-command: "make install" //
268// End: //
Note: See TracBrowser for help on using the repository browser.