source: src/AST/TypeSubstitution.cpp @ 943bfad

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 943bfad was 3e5dd913, checked in by Fangren Yu <f37yu@…>, 3 years ago

reimplement function type and eliminate deep copy

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