source: translator/InitTweak/Association.h @ 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: 7.5 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#ifndef _ASSOCIATE_H_
16#define _ASSOCIATE_H_
17
18#include <map>
19#include <list>
20#include <string>
21#include <vector>
22#include <iostream>
23#include <stdexcept>
24#include <algorithm>
25
26#include "SynTree/Expression.h"
27#include "diet_map.h"
28
29class Association;
30class SingleName;
31class PointAssociation;
32class RangeAssociation;
33
34// ** exceptions
35class AssocException : public std::exception {
36public:
37  AssocException() {}
38  AssocException( std::string _what ) : what( _what ) {}
39  ~AssocException() throw () {}
40
41  std::string get_what() const { return what; }
42  void set_what( std::string newValue ) { what = newValue; }
43private:
44  std::string what;
45};
46
47// ** visitors
48class AssociationVisitor {
49public:
50  AssociationVisitor() {}
51  virtual ~AssociationVisitor() {}
52
53  virtual void visit( SingleName * ) = 0;
54  virtual void visit( PointAssociation * ) = 0;
55  virtual void visit( RangeAssociation * ) = 0;
56};
57
58// ** containers
59class Association {
60 public:
61  virtual ~Association();
62
63  virtual Association *clone() = 0;
64  virtual long int add_single( std::string, Expression *) = 0;
65  virtual long int add_single( long int, Expression *expr) = 0;
66
67  virtual Association *operator[]( int idx ) = 0;
68  virtual Association *operator[]( std::string ) = 0;
69
70  //  virtual AssociationIterator *get_iterator() = 0;
71
72  virtual void accept( AssociationVisitor & ) = 0;
73  virtual void display( std::ostream & ) = 0;
74};
75
76class SingleName : public Association {
77public:
78  SingleName( Expression *initExpr = 0 ) : expr( initExpr ) {}
79  virtual ~SingleName();
80
81  virtual SingleName *clone() {
82    return 0; // XXX!
83  }
84
85  virtual long int add_single( long int idx, Expression *newExpr) {
86    if ( expr == 0 ) //|| *expr == *newExpr )
87      expr = newExpr;
88    return 0;
89  }
90
91  virtual long int add_single( std::string str, Expression *newExpr) {
92    if ( expr == 0 ) //|| *expr == *newExpr )
93      expr = newExpr;
94    return 0;
95  }
96
97  virtual Association *operator[]( int idx ) { assert(false); }
98  virtual Association *operator[]( std::string idx ) { assert(false); }
99
100  virtual void accept( AssociationVisitor &v ) { v.visit( this ); }
101  virtual void display( std::ostream &os ) {
102    os << "Single association" << std::endl;
103  }
104
105  Expression *get_expr() const { return expr; }
106
107private:
108  Expression *expr;
109  Expression *deflt;
110};
111
112class PointAssociation : public Association {
113public:
114  typedef std::map< std::string, std::pair< long int, Association *> > map_type;
115
116  PointAssociation() {}
117  PointAssociation( const PointAssociation &other ) {
118    copy( other.anonym.begin(), other.anonym.end(), back_inserter( anonym ));
119  }
120
121  virtual ~PointAssociation();
122
123  virtual PointAssociation *clone() {
124    return ( new PointAssociation( *this ) );
125  }
126
127  virtual long int add_single( long int idx, Expression *expr) {
128    long int ret;
129
130    if ( idx >= (long int)ordering.size() ) throw AssocException("extra (spurious) members");
131
132    if ( ordering[ idx ] == "")
133      std::cerr << "Checkpoint 2" << std::endl;
134    else {
135      assert( table[ordering[idx]].second != 0 );
136      ret = idx;
137      table[ ordering[idx] ].second->add_single("", expr );
138    }
139    return ret;
140  }
141
142  virtual long int add_single( std::string idx, Expression *expr) {
143    if ( idx == "" )
144      std::cerr << "Checkpoint 1" << std::endl;
145    else {
146      map_type::iterator j;
147      if (  (j = table.find( idx )) == table.end() )  // this doesn't amount for reachable members deeper down the structure, fix
148        throw AssocException("No such member");
149      else
150        return add_single( j->second.first, expr );
151    }
152
153    return -1;
154  }
155
156  void add_member( std::string str ) {
157    if ( table.find( str ) != table.end() ) return;
158    ordering.push_back( str );
159    if ( str != "" ) {
160      std::pair<long int, Association *> p( ordering.size() - 1, 0 );
161      table.insert( std::pair< std::string, std::pair<long int, Association *> >(str, p) );
162    }
163    return;
164  }
165
166  virtual void set_member( std::string str, Association *assoc ) {
167    if ( str == "" )
168      anonym.push_back( assoc );
169    else  if ( table.find( str ) == table.end() )
170      throw AssocException( "no such member" );
171    else
172      table[ str ] = std::pair<long int, Association *>(ordering.size() - 1, assoc);
173
174    return;
175  }
176
177  virtual Association *operator[]( int idx ) {
178    if ( ordering[idx] == "" ) {
179      std::cerr << "Error, anonymous members not implemented yet" << std::endl;
180      throw 0;
181    } else
182      return table[ ordering[idx] ].second;
183  }
184
185  virtual Association *operator[]( std::string idx ) {
186    if ( table.find( idx ) == table.end() )
187      throw AssocException("Member not found");
188    else
189      return table[ idx ].second;
190  }
191
192  /*
193  virtual AssociationIterator *get_iterator() {
194    PointAssocIterator *it;
195    return it;
196  }
197  */
198
199  void accept( AssociationVisitor &v ) { v.visit( this ); }
200
201  virtual void display( std::ostream &os ) {
202    os << "Point association: " << std::endl;
203    for ( map_type::iterator i = table.begin(); i != table.end(); i++ ) {
204      os << "Member [" << i->first << ", index = " << i->second.first << "]";
205      if ( i->second.second != 0 )
206        i->second.second->display( os );
207      else
208        std::cerr << "No recursive association" << std::endl;
209
210      os << std::endl;
211    }
212  }
213
214  const int size() const { return ordering.size(); }
215
216private:
217  PointAssociation &operator=(const PointAssociation &);
218  std::vector<std::string> ordering;
219  std::list< Association * > anonym;
220  std::map< std::string, std::pair<long int, Association *> > table;
221};
222
223class RangeAssociation : public Association {
224public:
225  static const int UNDEF;
226  RangeAssociation( int _hi= UNDEF ) : hi( _hi ) {
227    std::cerr << "Constructed RangeAssociation with: [" << hi << "]" << std::endl;
228  }
229
230  virtual ~RangeAssociation();
231
232  virtual RangeAssociation *clone() {
233    return 0; // XXX !!!!
234  }
235
236  virtual Association *operator[]( int idx ) {
237    return 0; // XXX !!!
238  }
239
240  virtual Association *operator[]( std::string idx ) { assert(false); return 0; }
241
242  /*
243  virtual AssociationIterator *get_iterator() {
244    RangeAssocIterator *it;
245    return it;
246  }
247  */
248
249  virtual long int add_single( long int idx, Expression *newExpr) { return 0; }
250  virtual long int add_single( std::string, Expression *) { return 0; }
251  void accept( AssociationVisitor &v ) { v.visit( this ); }
252  virtual void display( std::ostream &os ) {
253    os << "Range association, with limit: " << std::endl;
254  }
255
256private:
257  int hi;
258  diet::diet_tree<int> tree;
259  /*
260  for ( diet_tree<int>::iterator i = tree.begin(); i != tree.end(); i++ )
261    std::cout << "--(" << (*i).first << ", " << (*i).second << ")--" << std::endl;
262  diet_tree<int> tree;
263  tree.insert(100,200);
264  */
265};
266
267// ** builders
268class AssociationBuilder {
269public:
270  /* AssociationBuilder( Declaration * ) */
271  virtual ~AssociationBuilder() {}
272  virtual Association *get_assoc() = 0;
273  virtual Association *grab_assoc() = 0;
274  virtual void set_assoc(   Association * ) = 0;
275};
276
277class AssociationFiller {
278public:
279  // AssociationFiller( Declaration * ) {}
280  virtual ~AssociationFiller() {}
281  virtual Association *get_assoc() = 0;
282  virtual void set_assoc( Association * ) = 0;
283};
284
285#endif //#define _ASSOCIATE_H_
286
287/*
288  Local Variables:
289  mode: c++
290  End:
291*/
292// Local Variables: //
293// tab-width: 4 //
294// mode: c++ //
295// compile-command: "make install" //
296// End: //
Note: See TracBrowser for help on using the repository browser.