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