source: src/SynTree/TypeSubstitution.cc @ 843054c2

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 843054c2 was 843054c2, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: seventh groups of files

  • Property mode set to 100644
File size: 5.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 : Mon May 18 11:10:04 2015
13// Update Count     : 2
14//
15
16#include "Type.h"
17#include "TypeSubstitution.h"
18
19TypeSubstitution::TypeSubstitution() {
20}
21
22TypeSubstitution::TypeSubstitution( const TypeSubstitution &other ) {
23        initialize( other, *this );
24}
25
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        }
33}
34
35TypeSubstitution &TypeSubstitution::operator=( const TypeSubstitution &other ) {
36        if ( this == &other ) return *this;
37        initialize( other, *this );
38        return *this;
39}
40
41void TypeSubstitution::initialize( const TypeSubstitution &src, TypeSubstitution &dest ) {
42        dest.typeEnv.clear();
43        dest.varEnv.clear();
44        dest.add( src );
45}
46
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
54}
55
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();
62}
63
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
70}
71
72Type *TypeSubstitution::lookup( std::string formalType ) const {
73        TypeEnvType::const_iterator i = typeEnv.find( formalType );
74        if ( i == typeEnv.end() ) {
75                return 0;
76        } else {
77                return i->second;
78        } // if
79}
80
81bool TypeSubstitution::empty() const {
82        return typeEnv.empty() && varEnv.empty();
83}
84
85void TypeSubstitution::normalize() {
86        do {
87                subCount = 0;
88                freeOnly = true;
89                for ( TypeEnvType::iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
90                        i->second = i->second->acceptMutator( *this );
91                }
92        } while ( subCount );
93}
94
95Type * TypeSubstitution::mutate( TypeInstType *inst ) {
96        BoundVarsType::const_iterator bound = boundVars.find( inst->get_name() );
97        if ( bound != boundVars.end() ) return inst;
98       
99        TypeEnvType::const_iterator i = typeEnv.find( inst->get_name() );
100        if ( i == typeEnv.end() ) {
101                return inst;
102        } else {
103///         std::cout << "found " << inst->get_name() << ", replacing with ";
104///         i->second->print( std::cout );
105///         std::cout << std::endl;
106                subCount++;
107                Type *newtype = i->second->clone();
108                newtype->get_qualifiers() += inst->get_qualifiers();
109                delete inst;
110                return newtype;
111        } // if
112}
113
114Expression * TypeSubstitution::mutate( NameExpr *nameExpr ) {
115        VarEnvType::const_iterator i = varEnv.find( nameExpr->get_name() );
116        if ( i == varEnv.end() ) {
117                return nameExpr;
118        } else {
119                subCount++;
120                delete nameExpr;
121                return i->second->clone();
122        } // if
123}
124
125template< typename TypeClass >
126Type *TypeSubstitution::handleType( TypeClass *type ) {
127        BoundVarsType oldBoundVars( boundVars );
128        if ( freeOnly ) {
129                for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
130                        boundVars.insert( (*tyvar )->get_name() );
131                } // for
132        } // if
133        Type *ret = Mutator::mutate( type );
134        boundVars = oldBoundVars;
135        return ret;
136}
137
138Type * TypeSubstitution::mutate( VoidType *basicType ) {
139        return handleType( basicType );
140}
141
142Type * TypeSubstitution::mutate( BasicType *basicType ) {
143        return handleType( basicType );
144}
145
146Type * TypeSubstitution::mutate( PointerType *pointerType ) {
147        return handleType( pointerType );
148}
149
150Type * TypeSubstitution::mutate( ArrayType *arrayType ) {
151        return handleType( arrayType );
152}
153
154Type * TypeSubstitution::mutate( FunctionType *functionType ) {
155        return handleType( functionType );
156}
157
158Type * TypeSubstitution::mutate( StructInstType *aggregateUseType ) {
159        return handleType( aggregateUseType );
160}
161
162Type * TypeSubstitution::mutate( UnionInstType *aggregateUseType ) {
163        return handleType( aggregateUseType );
164}
165
166Type * TypeSubstitution::mutate( EnumInstType *aggregateUseType ) {
167        return handleType( aggregateUseType );
168}
169
170Type * TypeSubstitution::mutate( ContextInstType *aggregateUseType ) {
171        return handleType( aggregateUseType );
172}
173
174Type * TypeSubstitution::mutate( TupleType *tupleType ) {
175        return handleType( tupleType );
176}
177
178void TypeSubstitution::print( std::ostream &os, int indent ) const {
179        os << std::string( indent, ' ' ) << "Types:" << std::endl;
180        for ( TypeEnvType::const_iterator i = typeEnv.begin(); i != typeEnv.end(); ++i ) {
181                os << std::string( indent+2, ' ' ) << i->first << " -> ";
182                i->second->print( os, indent+4 );
183                os << std::endl;
184        } // for
185        os << std::string( indent, ' ' ) << "Non-types:" << std::endl;
186        for ( VarEnvType::const_iterator i = varEnv.begin(); i != varEnv.end(); ++i ) {
187                os << std::string( indent+2, ' ' ) << i->first << " -> ";
188                i->second->print( os, indent+4 );
189                os << std::endl;
190        } // for
191}
192
193// Local Variables: //
194// tab-width: 4 //
195// mode: c++ //
196// compile-command: "make install" //
197// End: //
Note: See TracBrowser for help on using the repository browser.