source: translator/Designators/Processor.cc @ 51587aa

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 51587aa was 51587aa, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

licencing: fourth groups of files

  • Property mode set to 100644
File size: 5.4 KB
Line 
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//
7// XXX.cc --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By :
12// Last Modified On :
13// Update Count     : 0
14//
15#include <vector>
16#include <algorithm>
17
18#include "Processor.h"
19#include "SynTree/Declaration.h"
20
21namespace 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            }
33        }
34    }
35
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++;
44        }
45        return true;
46    }
47
48    template< class InputIterator, class OutputIterator >
49    bool Matcher::slice(InputIterator begin, InputIterator end, OutputIterator out ) {
50        while ( begin != end )
51            if ( table.find( *begin ) != table.end() )
52                *out++ = declarations [ table[ *begin++ ] ];
53            else
54                return false; // throw 0;
55        return true;
56    }
57
58    template< class OutputIterator >
59    bool Matcher::get_reorderedCall( OutputIterator out ) {
60        // fill call with defaults, if need be
61        for (Matcher::iterator o = begin(); o != end(); o++ )
62            if ( alternatives[ table[ *o ] ] == 0 )
63                return false;
64            else
65                out++ = *alternatives[table[ *o ]];
66        return true;
67    }
68
69    bool fixDesignations( ResolvExpr::AlternativeFinder &finder, Expression *designation ) {
70        // Distribute `designation' over alternatives contained in `finder'
71        if ( ! designation) return false;
72        else
73            for ( ResolvExpr::AlternativeFinder::iterator alt = finder.begin(); alt != finder.end(); alt++ )
74                alt->expr->set_argName( designation );
75        return true;
76    }
77
78    template < class OutputIterator >
79    bool extractNames( Expression *expr, OutputIterator out, Matcher matcher ) {
80        Expression *designator = expr->get_argName();
81        if ( designator == 0 ) return false;
82
83        if ( NameExpr *ndes = dynamic_cast<NameExpr *>(designator) )
84            out++ = ndes->get_name();
85        else if ( TupleExpr *tdes = dynamic_cast<TupleExpr *>(designator) ) {
86            std::cerr << "Tuple designation" << std::endl;
87//      ObjectDecl *decl = extractTupleV(matcher, tdes); // xxx
88            // transform?
89            for ( std::list< Expression * >::iterator n = tdes->get_exprs().begin();
90                 n != tdes->get_exprs().end(); n++ ) {
91
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            }
98        }
99        return true;
100    }
101
102    std::string extractName( Expression *expr ) /* throw NoNameExtraction */ {
103        if ( NameExpr *name = dynamic_cast< NameExpr *>(expr) )
104            return name->get_name();
105        else /* if () */
106            throw 0;
107    }
108
109    DeclarationWithType *gensym( DeclarationWithType *, std::string prefix ) {
110        return 0;
111    }
112
113    ObjectDecl *extractTupleV( Matcher matcher, TupleExpr *nmTuple ) {
114        // extract a subtuple of the function `fun' argument list, corresponding to the tuple of names requested by
115        // `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;
124
125        return 0;//new ObjectDecl()
126    }
127
128    void check_alternative( FunctionType *fun, ResolvExpr::AltList &args ) {
129        using namespace ResolvExpr;
130
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            }
140        }
141        //AltList newArgs;
142        args.clear();
143        matcher.get_reorderedCall( back_inserter(args) );
144
145        return;
146    }
147#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;
157        }
158        return;
159    }
160#endif // 0
161} // namespaces Designators
162// Local Variables: //
163// tab-width: 4 //
164// mode: c++ //
165// compile-command: "make install" //
166// End: //
Note: See TracBrowser for help on using the repository browser.