source: translator/Designators/Processor.cc @ 42dcae7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 42dcae7 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 5.0 KB
Line 
1#include <vector>
2#include <algorithm>
3
4#include "Processor.h"
5#include "SynTree/Declaration.h"
6
7namespace Designators {
8
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      }
20    }
21  }
22
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++;
31    }
32    return true;
33  }
34
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
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
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
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
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
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      }
85    }
86    return true;
87  }
88
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
96  DeclarationWithType *gensym( DeclarationWithType *, std::string prefix ) {
97    return 0;
98  }
99
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;
112
113    return 0;//new ObjectDecl()
114  }
115
116  void check_alternative( FunctionType *fun, ResolvExpr::AltList &args ) {
117    using namespace ResolvExpr;
118
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      }
128    }
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      }
147    }
148    return;
149  }
150  */
151} // namespaces Designators
Note: See TracBrowser for help on using the repository browser.