Ignore:
Timestamp:
Dec 16, 2014, 9:41:50 PM (10 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:
17cd4eb
Parents:
3848e0e
Message:

remove Parser.old, add -XCFA to driver, copy ptrdiff_t from stddef.h in preclude, remove casts from initialization constants, adjust formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • translator/ResolvExpr/Resolver.cc

    r3848e0e rd9a0e76  
    1 /*
    2  * This file is part of the Cforall project
    3  *
    4  * $Id: Resolver.cc,v 1.19 2005/08/29 20:14:16 rcbilson Exp $
    5  *
    6  */
    7 
    81#include "Resolver.h"
    92#include "AlternativeFinder.h"
     
    1811#include "utility.h"
    1912
     13#include <iostream>
     14using namespace std;
     15
    2016namespace ResolvExpr {
    21 
    22 class Resolver : public SymTab::Indexer
    23 {
    24 public:
    25   Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
    26  
    27   virtual void visit( FunctionDecl *functionDecl );
    28   virtual void visit( ObjectDecl *functionDecl );
    29   virtual void visit( TypeDecl *typeDecl );
    30 
    31   virtual void visit( ExprStmt *exprStmt );
    32   virtual void visit( IfStmt *ifStmt );
    33   virtual void visit( WhileStmt *whileStmt );
    34   virtual void visit( ForStmt *forStmt );
    35   virtual void visit( SwitchStmt *switchStmt );
    36   virtual void visit( ChooseStmt *switchStmt );
    37   virtual void visit( CaseStmt *caseStmt );
    38   virtual void visit( ReturnStmt *returnStmt );
    39 
    40   virtual void visit( SingleInit *singleInit );
    41 
    42 private:
    43   std::list< Type* > functionReturn;
    44   Type* initContext;
    45   Type *switchType;
    46 };
    47 
    48 void
    49 resolve( std::list< Declaration* > translationUnit )
    50 {
    51   Resolver resolver;
    52   acceptAll( translationUnit, resolver );
    53 ///   for( std::list< Declaration* >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
    54 ///     (*i)->print( std::cerr );
    55 ///     (*i)->accept( resolver );
    56 ///   }
    57 }
    58 
    59 Expression *
    60 resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer )
    61 {
    62   TypeEnvironment env;
    63   return resolveInVoidContext( expr, indexer, env );
    64 }
    65 
    66 namespace {
    67 
    68   void
    69   finishExpr( Expression *expr, const TypeEnvironment &env )
    70   {
    71     expr->set_env( new TypeSubstitution );
    72     env.makeSubstitution( *expr->get_env() );
    73   }
    74 
    75   Expression*
    76   findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer )
    77   {
    78     global_renamer.reset();
    79     TypeEnvironment env;
    80     Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
    81     finishExpr( newExpr, env );
    82     return newExpr;
    83   }
    84  
    85   Expression*
    86   findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer )
    87   {
    88     TypeEnvironment env;
    89     AlternativeFinder finder( indexer, env );
    90     finder.find( untyped );
    91 ///     if( finder.get_alternatives().size() != 1 ) {
    92 ///       std::cout << "untyped expr is ";
    93 ///       untyped->print( std::cout );
    94 ///       std::cout << std::endl << "alternatives are:";
    95 ///       for( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
    96 ///         i->print( std::cout );
    97 ///       }
    98 ///     }
    99     assert( finder.get_alternatives().size() == 1 );
    100     Alternative &choice = finder.get_alternatives().front();
    101     Expression *newExpr = choice.expr->clone();
    102     finishExpr( newExpr, choice.env );
    103     return newExpr;
    104   }
    105 
    106   bool
    107   isIntegralType( Type *type )
    108   {
    109     if( dynamic_cast< EnumInstType* >( type ) ) {
    110       return true;
    111     } else if( BasicType *bt = dynamic_cast< BasicType* >( type ) ) {
    112       return bt->isInteger();
    113     } else {
    114       return true;
    115     }
    116   }
    117  
    118   Expression*
    119   findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer )
    120   {
    121     TypeEnvironment env;
    122     AlternativeFinder finder( indexer, env );
    123     finder.find( untyped );
    124 ///     if( finder.get_alternatives().size() != 1 ) {
    125 ///       std::cout << "untyped expr is ";
    126 ///       untyped->print( std::cout );
    127 ///       std::cout << std::endl << "alternatives are:";
    128 ///       for( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
    129 ///         i->print( std::cout );
    130 ///       }
    131 ///     }
    132     Expression *newExpr = 0;
    133     const TypeEnvironment *newEnv = 0;
    134     for( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
    135       if( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
    136         if( newExpr ) {
    137           throw SemanticError( "Too many interpretations for switch control expression", untyped );
    138         } else {
    139           newExpr = i->expr->clone();
    140           newEnv = &i->env;
    141         }
    142       }
    143     }
    144     if( !newExpr ) {
    145       throw SemanticError( "Too many interpretations for switch control expression", untyped );
    146     }
    147     finishExpr( newExpr, *newEnv );
    148     return newExpr;
    149   }
    150  
    151 }
    152  
    153 void
    154 Resolver::visit( ObjectDecl *objectDecl )
    155 {
    156   Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
    157   objectDecl->set_type( new_type );
    158   initContext = new_type;
    159   SymTab::Indexer::visit( objectDecl );
    160 }
    161  
    162 void
    163 Resolver::visit( TypeDecl *typeDecl )
    164 {
    165   if( typeDecl->get_base() ) {
    166     Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
    167     typeDecl->set_base( new_type );
    168   }
    169   SymTab::Indexer::visit( typeDecl );
    170 }
    171  
    172 void
    173 Resolver::visit( FunctionDecl *functionDecl )
    174 {
    175 ///   std::cout << "resolver visiting functiondecl ";
    176 ///   functionDecl->print( std::cout );
    177 ///   std::cout << std::endl;
    178   Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
    179   functionDecl->set_type( new_type );
    180   std::list< Type* > oldFunctionReturn = functionReturn;
    181   functionReturn.clear();
    182   for( std::list< DeclarationWithType* >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
    183     functionReturn.push_back( (*i)->get_type() );
    184   }
    185   SymTab::Indexer::visit( functionDecl );
    186   functionReturn = oldFunctionReturn;
    187 }
    188 
    189 void
    190 Resolver::visit( ExprStmt *exprStmt )
    191 {
    192   if( exprStmt->get_expr() ) {
    193     Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
    194     delete exprStmt->get_expr();
    195     exprStmt->set_expr( newExpr );
    196   }
    197 }
    198 
    199 void
    200 Resolver::visit( IfStmt *ifStmt )
    201 {
    202   Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
    203   delete ifStmt->get_condition();
    204   ifStmt->set_condition( newExpr );
    205   Visitor::visit( ifStmt );
    206 }
    207 
    208 void
    209 Resolver::visit( WhileStmt *whileStmt )
    210 {
    211   Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
    212   delete whileStmt->get_condition();
    213   whileStmt->set_condition( newExpr );
    214   Visitor::visit( whileStmt );
    215 }
    216 
    217 void
    218 Resolver::visit( ForStmt *forStmt )
    219 {
    220   Expression *newExpr;
    221   if( forStmt->get_condition() ) {
    222     newExpr = findSingleExpression( forStmt->get_condition(), *this );
    223     delete forStmt->get_condition();
    224     forStmt->set_condition( newExpr );
    225   }
    226  
    227   if( forStmt->get_increment() ) {
    228     newExpr = findVoidExpression( forStmt->get_increment(), *this );
    229     delete forStmt->get_increment();
    230     forStmt->set_increment( newExpr );
    231   }
    232  
    233   Visitor::visit( forStmt );
    234 }
    235 
    236 template< typename SwitchClass >
    237 void
    238 handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor )
    239 {
    240   Expression *newExpr;
    241   newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
    242   delete switchStmt->get_condition();
    243   switchStmt->set_condition( newExpr );
    244  
    245   visitor.Visitor::visit( switchStmt );
    246 }
    247 
    248 void
    249 Resolver::visit( SwitchStmt *switchStmt )
    250 {
    251   handleSwitchStmt( switchStmt, *this );
    252 }
    253 
    254 void
    255 Resolver::visit( ChooseStmt *switchStmt )
    256 {
    257   handleSwitchStmt( switchStmt, *this );
    258 }
    259 
    260 void
    261 Resolver::visit( CaseStmt *caseStmt )
    262 {
    263   Visitor::visit( caseStmt );
    264 }
    265 
    266 void
    267 Resolver::visit( ReturnStmt *returnStmt )
    268 {
    269   if( returnStmt->get_expr() ) {
    270     CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
    271     cloneAll( functionReturn, castExpr->get_results() );
    272     Expression *newExpr = findSingleExpression( castExpr, *this );
    273     delete castExpr;
    274     returnStmt->set_expr( newExpr );
    275   }
    276 }
    277 
    278 void
    279 Resolver::visit( SingleInit *singleInit )
    280 {
    281   if( singleInit->get_value() ) {
    282     CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
    283     Expression *newExpr = findSingleExpression( castExpr, *this );
    284     delete castExpr;
    285     singleInit->set_value( newExpr );
    286   }
    287   singleInit->get_value()->accept( *this );
    288 }
    289 
     17    class Resolver : public SymTab::Indexer {
     18      public:
     19        Resolver() : SymTab::Indexer( false ), switchType( 0 ) {}
     20 
     21        virtual void visit( FunctionDecl *functionDecl );
     22        virtual void visit( ObjectDecl *functionDecl );
     23        virtual void visit( TypeDecl *typeDecl );
     24
     25        virtual void visit( ExprStmt *exprStmt );
     26        virtual void visit( IfStmt *ifStmt );
     27        virtual void visit( WhileStmt *whileStmt );
     28        virtual void visit( ForStmt *forStmt );
     29        virtual void visit( SwitchStmt *switchStmt );
     30        virtual void visit( ChooseStmt *switchStmt );
     31        virtual void visit( CaseStmt *caseStmt );
     32        virtual void visit( ReturnStmt *returnStmt );
     33
     34        virtual void visit( SingleInit *singleInit );
     35        virtual void visit( ListInit *listInit );
     36      private:
     37        std::list< Type * > functionReturn;
     38        Type *initContext;
     39        Type *switchType;
     40    };
     41
     42    void resolve( std::list< Declaration * > translationUnit ) {
     43        Resolver resolver;
     44        acceptAll( translationUnit, resolver );
     45#if 0
     46        for ( std::list< Declaration * >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
     47            (*i)->print( std::cerr );
     48            (*i)->accept( resolver );
     49        }
     50#endif
     51    }
     52
     53    Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
     54        TypeEnvironment env;
     55        return resolveInVoidContext( expr, indexer, env );
     56    }
     57
     58    namespace {
     59        void finishExpr( Expression *expr, const TypeEnvironment &env ) {
     60            expr->set_env( new TypeSubstitution );
     61            env.makeSubstitution( *expr->get_env() );
     62        }
     63
     64        Expression *findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
     65            global_renamer.reset();
     66            TypeEnvironment env;
     67            Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
     68            finishExpr( newExpr, env );
     69            return newExpr;
     70        }
     71 
     72        Expression *findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
     73            TypeEnvironment env;
     74            AlternativeFinder finder( indexer, env );
     75            finder.find( untyped );
     76#if 0
     77            if ( finder.get_alternatives().size() != 1 ) {
     78                std::cout << "untyped expr is ";
     79                untyped->print( std::cout );
     80                std::cout << std::endl << "alternatives are:";
     81                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
     82                    i->print( std::cout );
     83                }
     84            }
     85#endif
     86            assert( finder.get_alternatives().size() == 1 );
     87            Alternative &choice = finder.get_alternatives().front();
     88            Expression *newExpr = choice.expr->clone();
     89            finishExpr( newExpr, choice.env );
     90            return newExpr;
     91        }
     92
     93        bool isIntegralType( Type *type ) {
     94            if ( dynamic_cast< EnumInstType * >( type ) ) {
     95                return true;
     96            } else if ( BasicType *bt = dynamic_cast< BasicType * >( type ) ) {
     97                return bt->isInteger();
     98            } else {
     99                return true;
     100            }
     101        }
     102 
     103        Expression *findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) {
     104            TypeEnvironment env;
     105            AlternativeFinder finder( indexer, env );
     106            finder.find( untyped );
     107#if 0
     108            if ( finder.get_alternatives().size() != 1 ) {
     109                std::cout << "untyped expr is ";
     110                untyped->print( std::cout );
     111                std::cout << std::endl << "alternatives are:";
     112                for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
     113                    i->print( std::cout );
     114                }
     115            }
     116#endif
     117            Expression *newExpr = 0;
     118            const TypeEnvironment *newEnv = 0;
     119            for ( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
     120                if ( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) {
     121                    if ( newExpr ) {
     122                        throw SemanticError( "Too many interpretations for switch control expression", untyped );
     123                    } else {
     124                        newExpr = i->expr->clone();
     125                        newEnv = &i->env;
     126                    }
     127                }
     128            }
     129            if ( !newExpr ) {
     130                throw SemanticError( "Too many interpretations for switch control expression", untyped );
     131            }
     132            finishExpr( newExpr, *newEnv );
     133            return newExpr;
     134        }
     135 
     136    }
     137 
     138    void Resolver::visit( ObjectDecl *objectDecl ) {
     139        Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
     140        objectDecl->set_type( new_type );
     141        initContext = new_type;
     142        SymTab::Indexer::visit( objectDecl );
     143    }
     144 
     145    void Resolver::visit( TypeDecl *typeDecl ) {
     146        if ( typeDecl->get_base() ) {
     147            Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
     148            typeDecl->set_base( new_type );
     149        }
     150        SymTab::Indexer::visit( typeDecl );
     151    }
     152 
     153    void Resolver::visit( FunctionDecl *functionDecl ) {
     154#if 0
     155        std::cout << "resolver visiting functiondecl ";
     156        functionDecl->print( std::cout );
     157        std::cout << std::endl;
     158#endif
     159        Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
     160        functionDecl->set_type( new_type );
     161        std::list< Type * > oldFunctionReturn = functionReturn;
     162        functionReturn.clear();
     163        for ( std::list< DeclarationWithType * >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) {
     164            functionReturn.push_back( (*i)->get_type() );
     165        }
     166        SymTab::Indexer::visit( functionDecl );
     167        functionReturn = oldFunctionReturn;
     168    }
     169
     170    void Resolver::visit( ExprStmt *exprStmt ) {
     171        if ( exprStmt->get_expr() ) {
     172            Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
     173            delete exprStmt->get_expr();
     174            exprStmt->set_expr( newExpr );
     175        }
     176    }
     177
     178    void Resolver::visit( IfStmt *ifStmt ) {
     179        Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
     180        delete ifStmt->get_condition();
     181        ifStmt->set_condition( newExpr );
     182        Visitor::visit( ifStmt );
     183    }
     184
     185    void Resolver::visit( WhileStmt *whileStmt ) {
     186        Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
     187        delete whileStmt->get_condition();
     188        whileStmt->set_condition( newExpr );
     189        Visitor::visit( whileStmt );
     190    }
     191
     192    void Resolver::visit( ForStmt *forStmt ) {
     193        Expression *newExpr;
     194        if ( forStmt->get_condition() ) {
     195            newExpr = findSingleExpression( forStmt->get_condition(), *this );
     196            delete forStmt->get_condition();
     197            forStmt->set_condition( newExpr );
     198        }
     199 
     200        if ( forStmt->get_increment() ) {
     201            newExpr = findVoidExpression( forStmt->get_increment(), *this );
     202            delete forStmt->get_increment();
     203            forStmt->set_increment( newExpr );
     204        }
     205 
     206        Visitor::visit( forStmt );
     207    }
     208
     209    template< typename SwitchClass >
     210    void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) {
     211        Expression *newExpr;
     212        newExpr = findIntegralExpression( switchStmt->get_condition(), visitor );
     213        delete switchStmt->get_condition();
     214        switchStmt->set_condition( newExpr );
     215 
     216        visitor.Visitor::visit( switchStmt );
     217    }
     218
     219    void Resolver::visit( SwitchStmt *switchStmt ) {
     220        handleSwitchStmt( switchStmt, *this );
     221    }
     222
     223    void Resolver::visit( ChooseStmt *switchStmt ) {
     224        handleSwitchStmt( switchStmt, *this );
     225    }
     226
     227    void Resolver::visit( CaseStmt *caseStmt ) {
     228        Visitor::visit( caseStmt );
     229    }
     230
     231    void Resolver::visit( ReturnStmt *returnStmt ) {
     232        if ( returnStmt->get_expr() ) {
     233            CastExpr *castExpr = new CastExpr( returnStmt->get_expr() );
     234            cloneAll( functionReturn, castExpr->get_results() );
     235            Expression *newExpr = findSingleExpression( castExpr, *this );
     236            delete castExpr;
     237            returnStmt->set_expr( newExpr );
     238        }
     239    }
     240
     241    void Resolver::visit( SingleInit *singleInit ) {
     242        // if ( singleInit->get_value() ) {
     243        //     CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() );
     244        //     Expression *newExpr = findSingleExpression( castExpr, *this );
     245        //     delete castExpr;
     246        //     singleInit->set_value( newExpr );
     247        // }
     248        // singleInit->get_value()->accept( *this );
     249    }
     250
     251    void Resolver::visit( ListInit *listInit ) {
     252        // no cast necessary
     253    }
    290254} // namespace ResolvExpr
Note: See TracChangeset for help on using the changeset viewer.