source: src/SynTree/TypeSubstitution.h@ d60ccbf

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since d60ccbf was 82dd287, checked in by Aaron Moss <a3moss@…>, 10 years ago

Doc comments

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