Ignore:
Timestamp:
May 18, 2015, 11:20:23 AM (9 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
51587aa
Parents:
a32b204
Message:

licencing: third groups of files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • translator/SynTree/TypeSubstitution.h

    ra32b204 r0dd3a2f  
    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  */
     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//
    715
    8 #ifndef SYNTREE_TYPESUBSTITUTION_H
    9 #define SYNTREE_TYPESUBSTITUTION_H
     16#ifndef TYPESUBSTITUTION_H
     17#define TYPESUBSTITUTION_H
    1018
    1119#include <map>
     
    1725#include "SynTree/Expression.h"
    1826
     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();
    1954
    20 class TypeSubstitution : public Mutator
    21 {
    22     typedef Mutator Parent;
    23    
    24 public:
    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();
     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 );
    4977
    50     void print( std::ostream &os, int indent = 0 ) const;
    51     TypeSubstitution *clone() const { return new TypeSubstitution( *this ); }
    52    
    53 private:
    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;
     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;
    8286};
    8387
    8488template< typename FormalIterator, typename ActualIterator >
    85 void
    86 TypeSubstitution::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     }
     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
    112114}
    113115
     
    115117TypeSubstitution::TypeSubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actualBegin )
    116118{
    117     add( formalBegin, formalEnd, actualBegin );
     119        add( formalBegin, formalEnd, actualBegin );
    118120}
    119121
    120122template< typename SynTreeClass >
    121 int
    122 TypeSubstitution::apply( SynTreeClass *&input )
    123 {
    124     assert( input );
    125     subCount = 0;
    126     freeOnly = false;
    127     input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
    128     assert( input );
     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 );
    129129///     std::cout << "substitution result is: ";
    130130///     newType->print( std::cout );
    131131///     std::cout << std::endl;
    132     return subCount;
     132        return subCount;
    133133}
    134    
     134       
    135135template< typename SynTreeClass >
    136 int
    137 TypeSubstitution::applyFree( SynTreeClass *&input )
    138 {
    139     assert( input );
    140     subCount = 0;
    141     freeOnly = true;
    142     input = dynamic_cast< SynTreeClass *>( input->acceptMutator( *this ) );
    143     assert( input );
     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 );
    144142///     std::cout << "substitution result is: ";
    145143///     newType->print( std::cout );
    146144///     std::cout << std::endl;
    147     return subCount;
     145        return subCount;
    148146}
    149    
     147       
    150148template< typename TypeInstListIterator >
    151 void
    152 TypeSubstitution::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     }
     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
    161157}
    162158
    163159// helper function
    164160template< typename FormalIterator, typename ActualIterator, typename MemberIterator, typename OutputIterator >
    165 void
    166 applySubstitution( 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
     161void applySubstitution( FormalIterator formalBegin, FormalIterator formalEnd, ActualIterator actual, MemberIterator memberBegin, MemberIterator memberEnd, OutputIterator out ) {
     162        // Instantiate each member of the context given the actual parameters specified, and store the
     163        // instantiations for use by the indexer
    170164
    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     }
     165        TypeSubstitution sub = TypeSubstitution( formalBegin, formalEnd, actual );
     166        for ( std::list< Declaration* >::iterator i = memberBegin; i != memberEnd; ++i ) {
     167                Declaration *newdecl = (*i)->clone();
     168                sub.apply( newdecl );
     169                *out++ = newdecl;
     170        } // for
    177171}
    178172
     173#endif // TYPESUBSTITUTION_H
    179174
    180 #endif /* #ifndef SYNTREE_TYPESUBSTITUTION_H */
     175// Local Variables: //
     176// tab-width: 4 //
     177// mode: c++ //
     178// compile-command: "make install" //
     179// End: //
Note: See TracChangeset for help on using the changeset viewer.