source: translator/SynTree/TypeSubstitution.h @ 134b86a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 134b86a was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*
2 * This file is part of the Cforall project
3 *
4 * $Id: TypeSubstitution.h,v 1.9 2005/08/29 20:59:26 rcbilson Exp $
5 *
6 */
7
8#ifndef SYNTREE_TYPESUBSTITUTION_H
9#define SYNTREE_TYPESUBSTITUTION_H
10
11#include <map>
12#include <set>
13#include <cassert>
14
15#include "SynTree/Mutator.h"
16#include "SynTree/Declaration.h"
17#include "SynTree/Expression.h"
18
19
20class TypeSubstitution : public Mutator
21{
22    typedef Mutator Parent;
23   
24public:
25    TypeSubstitution();
26    template< typename FormalIterator, typename ActualIterator >
27    TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
28    TypeSubstitution( const TypeSubstitution &other );
29    virtual ~TypeSubstitution();
30   
31    TypeSubstitution &operator=( const TypeSubstitution &other );
32   
33    template< typename SynTreeClass > int apply( SynTreeClass *&input );
34    template< typename SynTreeClass > int applyFree( SynTreeClass *&input );
35   
36    void add( std::string formalType, Type *actualType );
37    void add( const TypeSubstitution &other );
38    void remove( std::string formalType );
39    Type *lookup( std::string formalType ) const;
40    bool empty() const;
41   
42    template< typename FormalIterator, typename ActualIterator >
43    void add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin );
44   
45    template< typename TypeInstListIterator >
46    void extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result );
47   
48    void normalize();
49
50    void print( std::ostream &os, int indent = 0 ) const;
51    TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
52   
53private:
54    virtual Type* mutate(TypeInstType *aggregateUseType);
55    virtual Expression* mutate(NameExpr *nameExpr);
56   
57    template< typename TypeClass > Type *handleType( TypeClass *type );
58   
59    virtual Type* mutate(VoidType *basicType);
60    virtual Type* mutate(BasicType *basicType);
61    virtual Type* mutate(PointerType *pointerType);
62    virtual Type* mutate(ArrayType *arrayType);
63    virtual Type* mutate(FunctionType *functionType);
64    virtual Type* mutate(StructInstType *aggregateUseType);
65    virtual Type* mutate(UnionInstType *aggregateUseType);
66    virtual Type* mutate(EnumInstType *aggregateUseType);
67    virtual Type* mutate(ContextInstType *aggregateUseType);
68    virtual Type* mutate(TupleType *tupleType);
69   
70    // TODO: worry about traversing into a forall-qualified function type or type decl with assertions
71   
72    void initialize( const TypeSubstitution &src, TypeSubstitution &dest );
73
74    typedef std::map< std::string, Type* > TypeEnvType;
75    typedef std::map< std::string, Expression* > VarEnvType;
76    typedef std::set< std::string > BoundVarsType;
77    TypeEnvType typeEnv;
78    VarEnvType varEnv;
79    BoundVarsType boundVars;
80    int subCount;
81    bool freeOnly;
82};
83
84template< typename FormalIterator, typename ActualIterator >
85void 
86TypeSubstitution::add( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
87{
88    // FormalIterator points to a TypeDecl
89    // ActualIterator points to a Type
90    FormalIterator formalIt = formalBegin;
91    ActualIterator actualIt = actualBegin;
92    for( ; formalIt != formalEnd; ++formalIt, ++actualIt ) {
93        if( TypeDecl *formal = dynamic_cast< TypeDecl* >( *formalIt ) ) {
94            if( TypeExpr *actual = dynamic_cast< TypeExpr* >( *actualIt ) ) {
95                if( formal->get_name() != "" ) {
96                    TypeEnvType::iterator i = typeEnv.find( formal->get_name() );
97                    if( i != typeEnv.end() ) {
98                        delete i->second;
99                    }
100                    typeEnv[ formal->get_name() ] = actual->get_type()->clone();
101                }
102            } else {
103                throw SemanticError( "Attempt to provide non-type parameter for type parameter", formal );
104            }
105        } else {
106            // TODO: type check the formal and actual parameters
107            if( (*formalIt)->get_name() != "" ) {
108                varEnv[ (*formalIt)->get_name() ] = (*actualIt)->clone();
109            }
110        }
111    }
112}
113
114template< typename FormalIterator, typename ActualIterator >
115TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
116{
117    add( formalBegin, formalEnd, actualBegin );
118}
119
120template< typename SynTreeClass >
121int
122TypeSubstitution::apply( SynTreeClass *&input )
123{
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
137TypeSubstitution::applyFree( SynTreeClass *&input )
138{
139    assert( input );
140    subCount = 0;
141    freeOnly = true;
142    input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
143    assert( input );
144///     std::cout << "substitution result is: ";
145///     newType->print( std::cout );
146///     std::cout << std::endl;
147    return subCount;
148}
149   
150template< typename TypeInstListIterator >
151void
152TypeSubstitution::extract( TypeInstListIterator begin, TypeInstListIterator end, TypeSubstitution &result )
153{
154    while( begin != end ) {
155        TypeEnvType::iterator cur = typeEnv.find( (*begin++)->get_name() );
156        if( cur != typeEnv.end() ) {
157            result.typeEnv[ cur->first ] = cur->second;
158            typeEnv.erase( cur );
159        }
160    }
161}
162
163// helper function
164template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator >
165void
166applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out )
167{
168    // Instantiate each member of the context given the actual parameters specified, and store the
169    // instantiations for use by the indexer
170
171    TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual );
172    for( std::list< Declaration* >::iterator i = memberBegin; i != memberEnd; ++i ) {
173        Declaration *newdecl = (*i)->clone();
174        sub.apply( newdecl );
175        *out++ = newdecl;
176    }
177}
178
179
180#endif /* #ifndef SYNTREE_TYPESUBSTITUTION_H */
Note: See TracBrowser for help on using the repository browser.