source: src/SynTree/TypeSubstitution.h @ 6b0b624

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 6b0b624 was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change #ifndef to #pragma once

  • Property mode set to 100644
File size: 6.5 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.h --
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 : Sat Jul 22 09:52:24 2017
13// Update Count     : 3
14//
15
16#pragma once
17
18#include <map>
19#include <set>
20#include <cassert>
21
22#include "SynTree/Mutator.h"
23#include "SynTree/Declaration.h"
24#include "SynTree/Expression.h"
25
26class TypeSubstitution : public Mutator {
27        typedef Mutator Parent;
28  public:
29        TypeSubstitution();
30        template< typename FormalIterator, typename ActualIterator >
31        TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
32        TypeSubstitution( const TypeSubstitution &other );
33        virtual ~TypeSubstitution();
34
35        TypeSubstitution &operator=( const TypeSubstitution &other );
36
37        template< typename SynTreeClass > int apply( SynTreeClass *&input );
38        template< typename SynTreeClass > int applyFree( SynTreeClass *&input );
39
40        void add( std::string formalType, Type *actualType );
41        void add( const TypeSubstitution &other );
42        void remove( std::string formalType );
43        Type *lookup( std::string formalType ) const;
44        bool empty() const;
45
46        template< typename FormalIterator, typename ActualIterator >
47        void add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
48
49        /// this function is unused...
50        template< typename TypeInstListIterator >
51        void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result );
52
53        void normalize();
54
55        TypeSubstitution * acceptMutator( Mutator & mutator );
56
57        void print( std::ostream &os, int indent = 0 ) const;
58        TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
59  private:
60        virtual Type* mutate(TypeInstType *aggregateUseType);
61        virtual Expression* mutate(NameExpr *nameExpr);
62
63        /// Records type variable bindings from forall-statements
64        template< typename TypeClass > Type *handleType( TypeClass *type );
65        /// Records type variable bindings from forall-statements and instantiations of generic types
66        template< typename TypeClass > Type *handleAggregateType( TypeClass *type );
67
68        virtual Type* mutate(VoidType *basicType);
69        virtual Type* mutate(BasicType *basicType);
70        virtual Type* mutate(PointerType *pointerType);
71        virtual Type* mutate(ArrayType *arrayType);
72        virtual Type* mutate(FunctionType *functionType);
73        virtual Type* mutate(StructInstType *aggregateUseType);
74        virtual Type* mutate(UnionInstType *aggregateUseType);
75        virtual Type* mutate(EnumInstType *aggregateUseType);
76        virtual Type* mutate(TraitInstType *aggregateUseType);
77        virtual Type* mutate(TupleType *tupleType);
78        virtual Type* mutate(VarArgsType *varArgsType);
79        virtual Type* mutate(ZeroType *zeroType);
80        virtual Type* mutate(OneType *oneType);
81
82        // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
83
84        void initialize( const TypeSubstitution &src, TypeSubstitution &dest );
85
86        typedef std::map< std::string, Type* > TypeEnvType;
87        typedef std::map< std::string, Expression* > VarEnvType;
88        typedef std::set< std::string > BoundVarsType;
89        TypeEnvType typeEnv;
90        VarEnvType varEnv;
91        BoundVarsType boundVars;
92        int subCount;
93        bool freeOnly;
94};
95
96template< typename FormalIterator, typename ActualIterator >
97void TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin ) {
98        // FormalIterator points to a TypeDecl
99        // ActualIterator points to a Type
100        FormalIterator formalIt = formalBegin;
101        ActualIterator actualIt = actualBegin;
102        for ( ; formalIt != formalEnd; ++formalIt, ++actualIt ) {
103                if ( TypeDecl *formal = dynamic_cast< TypeDecl* >( *formalIt ) ) {
104                        if ( TypeExpr *actual = dynamic_cast< TypeExpr* >( *actualIt ) ) {
105                                if ( formal->get_name() != "" ) {
106                                        TypeEnvType::iterator i = typeEnv.find( formal->get_name() );
107                                        if ( i != typeEnv.end() ) {
108                                                delete i->second;
109                                        } // if
110                                        typeEnv[ formal->get_name() ] = actual->get_type()->clone();
111                                } // if
112                        } else {
113                                throw SemanticError( "Attempt to provide non-type parameter for type parameter", formal );
114                        } // if
115                } else {
116                        // TODO: type check the formal and actual parameters
117                        if ( (*formalIt)->get_name() != "" ) {
118                                varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone();
119                        } // if
120                } // if
121        } // for
122}
123
124template< typename FormalIterator, typename ActualIterator >
125TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
126{
127        add( formalBegin, formalEnd, actualBegin );
128}
129
130template< typename SynTreeClass >
131int TypeSubstitution::apply( SynTreeClass *&input ) {
132        assert( input );
133        subCount = 0;
134        freeOnly = false;
135        input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
136        assert( input );
137///     std::cout << "substitution result is: ";
138///     newType->print( std::cout );
139///     std::cout << std::endl;
140        return subCount;
141}
142
143template< typename SynTreeClass >
144int TypeSubstitution::applyFree( SynTreeClass *&input ) {
145        assert( input );
146        subCount = 0;
147        freeOnly = true;
148        input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
149        assert( input );
150///     std::cout << "substitution result is: ";
151///     newType->print( std::cout );
152///     std::cout << std::endl;
153        return subCount;
154}
155
156template< typename TypeInstListIterator >
157void TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result ) {
158        // xxx - this function doesn't extract varEnv - is this intentional?
159        while ( begin != end ) {
160                TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() );
161                if ( cur != typeEnv.end() ) {
162                        result.typeEnv[ cur->first ] = cur->second;
163                        typeEnv.erase( cur );
164                } // if
165        } // while
166}
167
168/// Instantiate each member of the context given the actual parameters specified, and store the
169/// instantiations for use by the indexer
170template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator >
171void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) {
172        TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual );
173        for ( std::list< Declaration* >::iterator i = memberBegin; i != memberEnd; ++i ) {
174                sub.apply( *i );
175                *out++ = *i;
176        } // for
177}
178
179std::ostream & operator<<( std::ostream & out, const TypeSubstitution & sub );
180
181// Local Variables: //
182// tab-width: 4 //
183// mode: c++ //
184// compile-command: "make install" //
185// End: //
Note: See TracBrowser for help on using the repository browser.