source: src/AST/TypeSubstitution.cpp @ 7ed7b4a

ADTast-experimental
Last change on this file since 7ed7b4a was 9e23b446, checked in by Fangren Yu <f37yu@…>, 23 months ago

add specialize pass

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