| [51587aa] | 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 | // | 
|---|
| [a08ba92] | 7 | // Processor.cc -- | 
|---|
| [51587aa] | 8 | // | 
|---|
| [843054c2] | 9 | // Author           : Rodolfo G. Esteves | 
|---|
| [51587aa] | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [a08ba92] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Tue May 19 16:23:10 2015 | 
|---|
|  | 13 | // Update Count     : 2 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [a08ba92] | 15 |  | 
|---|
| [51b73452] | 16 | #include <vector> | 
|---|
|  | 17 | #include <algorithm> | 
|---|
|  | 18 |  | 
|---|
|  | 19 | #include "Processor.h" | 
|---|
|  | 20 | #include "SynTree/Declaration.h" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | namespace Designators { | 
|---|
| [a08ba92] | 23 | Matcher::Matcher( const std::list< DeclarationWithType * > &decls ) { | 
|---|
|  | 24 | int cnt = 0; | 
|---|
|  | 25 | for ( std::list< DeclarationWithType * >::const_iterator i = decls.begin(); | 
|---|
|  | 26 | i != decls.end(); i++, cnt++ ) { | 
|---|
|  | 27 | std::string newName = (*i)->get_name(); | 
|---|
|  | 28 | if ( table.find( newName ) == table.end() ) { | 
|---|
|  | 29 | table.insert( std::pair<std::string, int>(newName, cnt) ); | 
|---|
|  | 30 | order.push_back( newName ); | 
|---|
|  | 31 | declarations.push_back( *i ); | 
|---|
|  | 32 | alternatives.push_back( 0 ); | 
|---|
|  | 33 | } // if | 
|---|
|  | 34 | } // for | 
|---|
| [d9a0e76] | 35 | } | 
|---|
| [a08ba92] | 36 |  | 
|---|
|  | 37 | template< class InputIterator > | 
|---|
|  | 38 | bool Matcher::add(InputIterator begin, InputIterator end, ResolvExpr::Alternative &alt ) { | 
|---|
|  | 39 | while ( begin != end ) { | 
|---|
|  | 40 | if ( table.find( *begin ) != table.end() ) | 
|---|
|  | 41 | alternatives[ table[ *begin ] ] = new ResolvExpr::Alternative(alt); | 
|---|
|  | 42 | else | 
|---|
|  | 43 | return false; | 
|---|
|  | 44 | begin++; | 
|---|
|  | 45 | } // while | 
|---|
|  | 46 | return true; | 
|---|
|  | 47 | } | 
|---|
|  | 48 |  | 
|---|
|  | 49 | template< class InputIterator, class OutputIterator > | 
|---|
|  | 50 | bool Matcher::slice(InputIterator begin, InputIterator end, OutputIterator out ) { | 
|---|
|  | 51 | while ( begin != end ) | 
|---|
|  | 52 | if ( table.find( *begin ) != table.end() ) | 
|---|
|  | 53 | *out++ = declarations [ table[ *begin++ ] ]; | 
|---|
|  | 54 | else | 
|---|
|  | 55 | return false; // throw 0; | 
|---|
|  | 56 | return true; | 
|---|
| [d9a0e76] | 57 | } | 
|---|
|  | 58 |  | 
|---|
| [a08ba92] | 59 | template< class OutputIterator > | 
|---|
|  | 60 | bool Matcher::get_reorderedCall( OutputIterator out ) { | 
|---|
|  | 61 | // fill call with defaults, if need be | 
|---|
|  | 62 | for (Matcher::iterator o = begin(); o != end(); o++ ) | 
|---|
|  | 63 | if ( alternatives[ table[ *o ] ] == 0 ) | 
|---|
|  | 64 | return false; | 
|---|
|  | 65 | else | 
|---|
|  | 66 | out++ = *alternatives[table[ *o ]]; | 
|---|
|  | 67 | return true; | 
|---|
|  | 68 | } | 
|---|
|  | 69 |  | 
|---|
|  | 70 | bool fixDesignations( ResolvExpr::AlternativeFinder &finder, Expression *designation ) { | 
|---|
|  | 71 | // Distribute `designation' over alternatives contained in `finder' | 
|---|
|  | 72 | if ( ! designation) return false; | 
|---|
| [d9a0e76] | 73 | else | 
|---|
| [a08ba92] | 74 | for ( ResolvExpr::AlternativeFinder::iterator alt = finder.begin(); alt != finder.end(); alt++ ) | 
|---|
|  | 75 | alt->expr->set_argName( designation ); | 
|---|
|  | 76 | return true; | 
|---|
|  | 77 | } | 
|---|
|  | 78 |  | 
|---|
|  | 79 | template < class OutputIterator > | 
|---|
|  | 80 | bool extractNames( Expression *expr, OutputIterator out, Matcher matcher ) { | 
|---|
|  | 81 | Expression *designator = expr->get_argName(); | 
|---|
|  | 82 | if ( designator == 0 ) return false; | 
|---|
|  | 83 |  | 
|---|
|  | 84 | if ( NameExpr *ndes = dynamic_cast<NameExpr *>(designator) ) | 
|---|
|  | 85 | out++ = ndes->get_name(); | 
|---|
|  | 86 | else if ( TupleExpr *tdes = dynamic_cast<TupleExpr *>(designator) ) { | 
|---|
|  | 87 | std::cerr << "Tuple designation" << std::endl; | 
|---|
|  | 88 | //      ObjectDecl *decl = extractTupleV(matcher, tdes); // xxx | 
|---|
|  | 89 | // transform? | 
|---|
|  | 90 | for ( std::list< Expression * >::iterator n = tdes->get_exprs().begin(); | 
|---|
|  | 91 | n != tdes->get_exprs().end(); n++ ) { | 
|---|
|  | 92 |  | 
|---|
|  | 93 | if ( NameExpr *name = dynamic_cast<NameExpr *>(*n) ) | 
|---|
|  | 94 | out++ = name->get_name(); | 
|---|
|  | 95 | else | 
|---|
|  | 96 | // flatten nested Tuples | 
|---|
|  | 97 | throw SemanticError( "Invalid tuple designation." ); | 
|---|
|  | 98 | } // for | 
|---|
|  | 99 | } // if | 
|---|
|  | 100 | return true; | 
|---|
| [d9a0e76] | 101 | } | 
|---|
| [a08ba92] | 102 |  | 
|---|
|  | 103 | std::string extractName( Expression *expr ) /* throw NoNameExtraction */ { | 
|---|
|  | 104 | if ( NameExpr *name = dynamic_cast< NameExpr *>(expr) ) | 
|---|
|  | 105 | return name->get_name(); | 
|---|
|  | 106 | else /* if () */ | 
|---|
|  | 107 | throw 0; | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | DeclarationWithType *gensym( DeclarationWithType *, std::string prefix ) { | 
|---|
|  | 111 | return 0; | 
|---|
| [d9a0e76] | 112 | } | 
|---|
|  | 113 |  | 
|---|
| [a08ba92] | 114 | ObjectDecl *extractTupleV( Matcher matcher, TupleExpr *nmTuple ) { | 
|---|
|  | 115 | // extract a subtuple of the function `fun' argument list, corresponding to the tuple of names requested by | 
|---|
|  | 116 | // `nmTuple'. | 
|---|
|  | 117 | std::list< Expression * > &exprs = nmTuple->get_exprs(); | 
|---|
|  | 118 | std::cerr << "In extractTupleV, the tuple has " << exprs.size() << " components." << std::endl; | 
|---|
|  | 119 | std::list< std::string > names; | 
|---|
|  | 120 | std::transform( exprs.begin(), exprs.end(), back_inserter(names), extractName ); | 
|---|
|  | 121 | std::list< DeclarationWithType * > decls; | 
|---|
|  | 122 | matcher.slice( names.begin(), names.end(), back_inserter(decls) ); | 
|---|
|  | 123 | //std::for_each( decls.begin(), decls.end(), gensym ); | 
|---|
|  | 124 | std::cerr << "Returning declaration with " << decls.size() << " components." << std::endl; | 
|---|
|  | 125 |  | 
|---|
|  | 126 | return 0;//new ObjectDecl() | 
|---|
|  | 127 | } | 
|---|
|  | 128 |  | 
|---|
|  | 129 | void check_alternative( FunctionType *fun, ResolvExpr::AltList &args ) { | 
|---|
|  | 130 | using namespace ResolvExpr; | 
|---|
|  | 131 |  | 
|---|
|  | 132 | Matcher matcher( fun->get_parameters() ); | 
|---|
|  | 133 | for ( AltList::iterator a = args.begin(); a != args.end(); a++ ) { | 
|---|
|  | 134 | std::list< std::string > actNames; | 
|---|
|  | 135 | if ( ! extractNames( a->expr, back_inserter(actNames), matcher ) ) { | 
|---|
|  | 136 | return; // not a designated call, leave alternative alone | 
|---|
|  | 137 | } else { | 
|---|
|  | 138 | // see if there's a match | 
|---|
|  | 139 | matcher.add( actNames.begin(), actNames.end(), *a ); | 
|---|
|  | 140 | } // if | 
|---|
|  | 141 | } // for | 
|---|
|  | 142 | //AltList newArgs; | 
|---|
|  | 143 | args.clear(); | 
|---|
|  | 144 | matcher.get_reorderedCall( back_inserter(args) ); | 
|---|
|  | 145 |  | 
|---|
|  | 146 | return; | 
|---|
|  | 147 | } | 
|---|
| [d9a0e76] | 148 | #if 0 | 
|---|
| [a08ba92] | 149 | void pruneAlternatives( Expression *expr, ResolvExpr::AlternativeFinder &finder ) { | 
|---|
|  | 150 | if ( expr->get_argName() != 0 ) { | 
|---|
|  | 151 | // Looking at designated expression | 
|---|
|  | 152 | using namespace ResolvExpr; | 
|---|
|  | 153 | AltList &alternatives = finder.get_alternatives(); | 
|---|
|  | 154 | std::cerr << "Now printing alternatives: " << std::endl; | 
|---|
|  | 155 | for ( AltList::iterator a = alternatives.begin(); a != alternatives.end(); a++ ) | 
|---|
|  | 156 | a->expr->print( std::cerr ); | 
|---|
|  | 157 | //std::cerr << "Looking for constructions of length no more than: " << tdes->get_exprs().size() << "." << std::endl; | 
|---|
|  | 158 | } | 
|---|
|  | 159 | return; | 
|---|
| [d9a0e76] | 160 | } | 
|---|
|  | 161 | #endif // 0 | 
|---|
| [51b73452] | 162 | } // namespaces Designators | 
|---|
| [a08ba92] | 163 |  | 
|---|
| [51587aa] | 164 | // Local Variables: // | 
|---|
|  | 165 | // tab-width: 4 // | 
|---|
|  | 166 | // mode: c++ // | 
|---|
|  | 167 | // compile-command: "make install" // | 
|---|
|  | 168 | // End: // | 
|---|