Ignore:
Timestamp:
Dec 16, 2014, 9:41:50 PM (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:
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/Designators/Processor.cc

    r3848e0e rd9a0e76  
    66
    77namespace Designators {
     8    Matcher::Matcher( const std::list< DeclarationWithType * > &decls ) {
     9        int cnt = 0;
     10        for ( std::list< DeclarationWithType * >::const_iterator i = decls.begin();
     11             i != decls.end(); i++, cnt++ ) {
     12            std::string newName = (*i)->get_name();
     13            if ( table.find( newName ) == table.end() ) {
     14                table.insert( std::pair<std::string, int>(newName, cnt) );
     15                order.push_back( newName );
     16                declarations.push_back( *i );
     17                alternatives.push_back( 0 );
     18            }
     19        }
     20    }
    821
    9   Matcher::Matcher( const std::list< DeclarationWithType * > &decls ) {
    10     int cnt = 0;
    11     for( std::list< DeclarationWithType * >::const_iterator i = decls.begin();
    12          i != decls.end(); i++, cnt++ ) {
    13       std::string newName = (*i)->get_name();
    14       if( table.find( newName ) == table.end() ) {
    15         table.insert( std::pair<std::string, int>(newName, cnt) );
    16         order.push_back( newName );
    17         declarations.push_back( *i );
    18         alternatives.push_back( 0 );
    19       }
     22    template< class InputIterator >
     23    bool Matcher::add(InputIterator begin, InputIterator end, ResolvExpr::Alternative &alt ) {
     24        while ( begin != end ) {
     25            if ( table.find( *begin ) != table.end() )
     26                alternatives[ table[ *begin ] ] = new ResolvExpr::Alternative(alt);
     27            else
     28                return false;
     29            begin++;
     30        }
     31        return true;
    2032    }
    21   }
    2233
    23   template< class InputIterator >
    24   bool Matcher::add(InputIterator begin, InputIterator end, ResolvExpr::Alternative &alt ) {
    25     while( begin != end ) {
    26       if ( table.find( *begin ) != table.end() )
    27         alternatives[ table[ *begin ] ] = new ResolvExpr::Alternative(alt);
    28       else
    29         return false;
    30       begin++;
     34    template< class InputIterator, class OutputIterator >
     35    bool Matcher::slice(InputIterator begin, InputIterator end, OutputIterator out ) {
     36        while ( begin != end )
     37            if ( table.find( *begin ) != table.end() )
     38                *out++ = declarations [ table[ *begin++ ] ];
     39            else
     40                return false; // throw 0;
     41        return true;
    3142    }
    32     return true;
    33   }
    3443
    35   template< class InputIterator, class OutputIterator >
    36   bool Matcher::slice(InputIterator begin, InputIterator end, OutputIterator out ) {
    37     while( begin != end )
    38       if ( table.find( *begin ) != table.end() )
    39         *out++ = declarations [ table[ *begin++ ] ];
    40       else
    41         return false; // throw 0;
    42     return true;
    43   }
     44    template< class OutputIterator >
     45    bool Matcher::get_reorderedCall( OutputIterator out ) {
     46        // fill call with defaults, if need be
     47        for (Matcher::iterator o = begin(); o != end(); o++ )
     48            if ( alternatives[ table[ *o ] ] == 0 )
     49                return false;
     50            else
     51                out++ = *alternatives[table[ *o ]];
     52        return true;
     53    }
    4454
    45   template< class OutputIterator >
    46   bool Matcher::get_reorderedCall( OutputIterator out ) {
    47     // fill call with defaults, if need be
    48     for (Matcher::iterator o = begin(); o != end(); o++ )
    49       if ( alternatives[ table[ *o ] ] == 0 )
    50         return false;
    51       else
    52         out++ = *alternatives[table[ *o ]];
    53     return true;
    54   }
     55    bool fixDesignations( ResolvExpr::AlternativeFinder &finder, Expression *designation ) {
     56        // Distribute `designation' over alternatives contained in `finder'
     57        if ( ! designation) return false;
     58        else
     59            for ( ResolvExpr::AlternativeFinder::iterator alt = finder.begin(); alt != finder.end(); alt++ )
     60                alt->expr->set_argName( designation );
     61        return true;
     62    }
    5563
    56   bool fixDesignations( ResolvExpr::AlternativeFinder &finder, Expression *designation ) {
    57     /* Distribute `designation' over alternatives contained in `finder' */
    58     if (!designation) return false;
    59     else
    60       for( ResolvExpr::AlternativeFinder::iterator alt = finder.begin(); alt != finder.end(); alt++ )
    61         alt->expr->set_argName( designation );
    62     return true;
    63   }
     64    template < class OutputIterator >
     65    bool extractNames( Expression *expr, OutputIterator out, Matcher matcher ) {
     66        Expression *designator = expr->get_argName();
     67        if ( designator == 0 ) return false;
    6468
    65   template < class OutputIterator >
    66   bool extractNames( Expression *expr, OutputIterator out, Matcher matcher ) {
    67     Expression *designator = expr->get_argName();
    68     if ( designator == 0 ) return false;
     69        if ( NameExpr *ndes = dynamic_cast<NameExpr *>(designator) )
     70            out++ = ndes->get_name();
     71        else if ( TupleExpr *tdes = dynamic_cast<TupleExpr *>(designator) ) {
     72            std::cerr << "Tuple designation" << std::endl;
     73//      ObjectDecl *decl = extractTupleV(matcher, tdes); // xxx
     74            // transform?
     75            for ( std::list< Expression * >::iterator n = tdes->get_exprs().begin();
     76                 n != tdes->get_exprs().end(); n++ ) {
    6977
    70     if( NameExpr *ndes = dynamic_cast<NameExpr *>(designator) )
    71         out++ = ndes->get_name();
    72     else if ( TupleExpr *tdes = dynamic_cast<TupleExpr *>(designator) ) {
    73       std::cerr << "Tuple designation" << std::endl;
    74 //      ObjectDecl *decl = extractTupleV(matcher, tdes); // xxx
    75       // transform?
    76       for( std::list< Expression * >::iterator n = tdes->get_exprs().begin();
    77            n != tdes->get_exprs().end(); n++ ) {
     78                if ( NameExpr *name = dynamic_cast<NameExpr *>(*n) )
     79                    out++ = name->get_name();
     80                else
     81                    // flatten nested Tuples
     82                    throw SemanticError( "Invalid tuple designation." );
     83            }
     84        }
     85        return true;
     86    }
    7887
    79         if ( NameExpr *name = dynamic_cast<NameExpr *>(*n) )
    80           out++ = name->get_name();
    81         else
    82           // flatten nested Tuples
    83           throw SemanticError( "Invalid tuple designation." );
    84       }
     88    std::string extractName( Expression *expr ) /* throw NoNameExtraction */ {
     89        if ( NameExpr *name = dynamic_cast< NameExpr *>(expr) )
     90            return name->get_name();
     91        else /* if () */
     92            throw 0;
    8593    }
    86     return true;
    87   }
    8894
    89   std::string extractName( Expression *expr ) /* throw NoNameExtraction */ {
    90     if( NameExpr *name = dynamic_cast< NameExpr *>(expr) )
    91       return name->get_name();
    92     else /* if() */
    93       throw 0;
    94   }
     95    DeclarationWithType *gensym( DeclarationWithType *, std::string prefix ) {
     96        return 0;
     97    }
    9598
    96   DeclarationWithType *gensym( DeclarationWithType *, std::string prefix ) {
    97     return 0;
    98   }
     99    ObjectDecl *extractTupleV( Matcher matcher, TupleExpr *nmTuple ) {
     100        // extract a subtuple of the function `fun' argument list, corresponding to the tuple of names requested by
     101        // `nmTuple'.
     102        std::list< Expression * > &exprs = nmTuple->get_exprs();
     103        std::cerr << "In extractTupleV, the tuple has " << exprs.size() << " components." << std::endl;
     104        std::list< std::string > names;
     105        std::transform( exprs.begin(), exprs.end(), back_inserter(names), extractName );
     106        std::list< DeclarationWithType * > decls;
     107        matcher.slice( names.begin(), names.end(), back_inserter(decls) );
     108        //std::for_each( decls.begin(), decls.end(), gensym );
     109        std::cerr << "Returning declaration with " << decls.size() << " components." << std::endl;
    99110
    100   ObjectDecl *extractTupleV( Matcher matcher, TupleExpr *nmTuple ) {
    101     /* extract a subtuple of the function `fun' argument list, corresponding to the tuple
    102        of names requested by `nmTuple'.
    103      */
    104     std::list< Expression * > &exprs = nmTuple->get_exprs();
    105     std::cerr << "In extractTupleV, the tuple has " << exprs.size() << " components." << std::endl;
    106     std::list< std::string > names;
    107     std::transform( exprs.begin(), exprs.end(), back_inserter(names), extractName );
    108     std::list< DeclarationWithType * > decls;
    109     matcher.slice( names.begin(), names.end(), back_inserter(decls) );
    110     //std::for_each( decls.begin(), decls.end(), gensym );
    111     std::cerr << "Returning declaration with " << decls.size() << " components." << std::endl;
     111        return 0;//new ObjectDecl()
     112    }
    112113
    113     return 0;//new ObjectDecl()
    114   }
     114    void check_alternative( FunctionType *fun, ResolvExpr::AltList &args ) {
     115        using namespace ResolvExpr;
    115116
    116   void check_alternative( FunctionType *fun, ResolvExpr::AltList &args ) {
    117     using namespace ResolvExpr;
     117        Matcher matcher( fun->get_parameters() );
     118        for ( AltList::iterator a = args.begin(); a != args.end(); a++ ) {
     119            std::list< std::string > actNames;
     120            if ( ! extractNames( a->expr, back_inserter(actNames), matcher ) ) {
     121                return; // not a designated call, leave alternative alone
     122            } else {
     123                // see if there's a match
     124                matcher.add( actNames.begin(), actNames.end(), *a );
     125            }
     126        }
     127        //AltList newArgs;
     128        args.clear();
     129        matcher.get_reorderedCall( back_inserter(args) );
    118130
    119     Matcher matcher( fun->get_parameters() );
    120     for ( AltList::iterator a = args.begin(); a != args.end(); a++ ) {
    121       std::list< std::string > actNames;
    122       if ( !extractNames( a->expr, back_inserter(actNames), matcher ) ) {
    123         return; // not a designated call, leave alternative alone
    124       } else {
    125         // see if there's a match
    126         matcher.add( actNames.begin(), actNames.end(), *a );
    127       }
     131        return;
    128132    }
    129     //AltList newArgs;
    130     args.clear();
    131     matcher.get_reorderedCall( back_inserter(args) );
    132 
    133     return;
    134   }
    135 
    136   /*
    137   void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder &finder ) {
    138     if ( expr->get_argName() != 0 ) {
    139       // Looking at designated expression
    140         using namespace ResolvExpr;
    141         AltList &alternatives = finder.get_alternatives();
    142         std::cerr << "Now printing alternatives: " << std::endl;
    143         for( AltList::iterator a = alternatives.begin(); a != alternatives.end(); a++ )
    144           a->expr->print( std::cerr );
    145         //std::cerr << "Looking for constructions of length no more than: " << tdes->get_exprs().size() << "." << std::endl;
    146       }
     133#if 0
     134    void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder &finder ) {
     135        if ( expr->get_argName() != 0 ) {
     136            // Looking at designated expression
     137            using namespace ResolvExpr;
     138            AltList &alternatives = finder.get_alternatives();
     139            std::cerr << "Now printing alternatives: " << std::endl;
     140            for ( AltList::iterator a = alternatives.begin(); a != alternatives.end(); a++ )
     141                a->expr->print( std::cerr );
     142            //std::cerr << "Looking for constructions of length no more than: " << tdes->get_exprs().size() << "." << std::endl;
     143        }
     144        return;
    147145    }
    148     return;
    149   }
    150   */
     146#endif // 0
    151147} // namespaces Designators
Note: See TracChangeset for help on using the changeset viewer.