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