source: src/SynTree/TypeSubstitution.h@ 23063ea

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 23063ea was e33f321, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

mutate env on each expression type to ensure type mappings are up to date

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