~ubuntu-branches/ubuntu/quantal/aspectc++/quantal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003  The 'ac++' developers (see aspectc.org)
//                                                                
// This program is free software;  you can redistribute it and/or 
// modify it under the terms of the GNU General Public License as 
// published by the Free Software Foundation; either version 2 of 
// the License, or (at your option) any later version.            
//                                                                
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
// GNU General Public License for more details.                   
//                                                                
// You should have received a copy of the GNU General Public      
// License along with this program; if not, write to the Free     
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
// MA  02111-1307  USA                                            

#ifndef __Condition_h__
#define __Condition_h__

#include <set>
#include <iostream>
#include <string>
using namespace std;

#include "Puma/CClassInfo.h"
#include "JoinPointLoc.h"
#include "TransformInfo.h"
#include "Naming.h"
using namespace Puma;

class PointCutExpr;
class AdviceInfo;
class  JPL_Class;

typedef pair<CRecord*, string> TypeCheck;
typedef set<TypeCheck> TypeCheckSet;
typedef set<CRecord*> RecordSet;
typedef set<string> StringSet;

// this is the class, which users should apply to manage runtime conditions
// for joinpoints. It is responsible for the memory management.

class Condition {

  class Base {
  public:
    virtual ~Base () {}
    virtual void print (ostream&) const = 0;
    virtual void gen_check (ostream &, AdviceInfo*, const char*) const = 0;
    virtual void checks_for_that (TypeCheckSet &checks) const {}
    virtual void checks_for_target (TypeCheckSet &checks) const {}
    virtual void names_for_that (StringSet &names) const {}
    virtual void names_for_target (StringSet &names) const {}
  };

  class TypeCond : public Base {
    RecordSet _matching;
    string _name;
  public:
    TypeCond (const string &name) : _name (name) {}
    virtual ~TypeCond () {}
    void matches (CRecord *rec) { _matching.insert (rec); }
    const RecordSet &matching () const { return _matching; }
    const string &name () const { return _name; }
    void type_checks (TypeCheckSet &checks) const {
      for (RecordSet::const_iterator iter = _matching.begin ();
	   iter != _matching.end (); ++iter)
	checks.insert (TypeCheck (*iter, _name));
    }
  };
  
  class That : public TypeCond {
  public:
    That (const string &name) : TypeCond (name) {}
    virtual void print (ostream&) const;
    virtual void gen_check (ostream &, AdviceInfo*, const char*) const;
    virtual void checks_for_that (TypeCheckSet &checks) const {
      type_checks (checks);
    }
    virtual void names_for_that (StringSet &names) const {
      names.insert (name ());
    }
  };

  class Target : public TypeCond {
  public:
    Target (const string &name) : TypeCond (name) {}
    virtual void print (ostream&) const;
    virtual void gen_check (ostream &, AdviceInfo*, const char*) const;
    virtual void checks_for_target (TypeCheckSet &checks) const {
      type_checks (checks);
    }
    virtual void names_for_target (StringSet &names) const {
      names.insert (name ());
    }
  };

  class CFlow : public Base {
    int _index;
  public:
    CFlow (int i) : _index (i) {}
    virtual void print (ostream&) const;
    virtual void gen_check (ostream &, AdviceInfo*, const char*) const;
  };
  
  class Binary : public Base {
  protected:
    Base *_left;
    Base *_right;
  public:
    Binary (Base *left, Base *right) : _left (left), _right (right) {}
    virtual void checks_for_that (TypeCheckSet &result) const {
      _left->checks_for_that (result);
      _right->checks_for_that (result);
    }
    virtual void checks_for_target (TypeCheckSet &result) const {
      _left->checks_for_target (result);
      _right->checks_for_target (result);
    }
    virtual void names_for_that (StringSet &names) const {
      _left->names_for_that (names);
      _right->names_for_that (names);
    }
    virtual void names_for_target (StringSet &names) const {
      _left->names_for_that (names);
      _right->names_for_that (names);
    }
  };
  
  class And : public Binary {
  public:
    And (Base *left, Base *right) : Binary (left, right) {}
    virtual void print (ostream&) const;
    virtual void gen_check (ostream &, AdviceInfo*, const char*) const;
  };

  class Or : public Binary {
  public:
    Or (Base *left, Base *right) : Binary (left, right) {}
    virtual void print (ostream&) const;
    virtual void gen_check (ostream &, AdviceInfo*, const char*) const;
  };

  class Not : public Base {
    Base *_arg;
  public:
    Not (Base *arg) : _arg (arg) {}
    virtual void print (ostream&) const;
    virtual void gen_check (ostream &, AdviceInfo*, const char*) const;
    virtual void checks_for_that (TypeCheckSet &result) const {
      _arg->checks_for_that (result);
    }
    virtual void checks_for_target (TypeCheckSet &result) const {
      _arg->checks_for_target (result);
    }
    virtual void names_for_that (StringSet &names) const {
      _arg->names_for_that (names);
    }
    virtual void names_for_target (StringSet &names) const {
      _arg->names_for_target (names);
    }
  };
  
  Base *_cond;
  
  Condition (Base *cond) : _cond (cond) {}
  
public:
  // create an unused condition
  Condition () : _cond (0) {}
  
  // delete a condition
  ~Condition () {
    // heap cleanup
    if (_cond)
      delete _cond;
  }
  
  // operator bool returns whether a condition is used
  operator bool () const { return _cond != 0; }

  // print the condition in a human readable way
  void print (ostream &out) {
    if (_cond)
      _cond->print (out);
    else
      out << "no condition";
  }

  void gen_check (ostream &out, AdviceInfo* ai, const char *srcthis) const {
    if (_cond)
      _cond->gen_check (out, ai, srcthis);
    else
      out << "true";
  }

  // return the set of checks needed for 'that' and target
  void checks_for_that (TypeCheckSet &result) const {
    if (_cond) _cond->checks_for_that (result);
  }
  void checks_for_target (TypeCheckSet &result) const {
    if (_cond) _cond->checks_for_target (result);
  }

  // return the names of all 'that' and 'target' checks in this condition
  void names_for_that (StringSet &names) const {
    if (_cond) _cond->names_for_that (names);
  }
  void names_for_target (StringSet &names) const {
    if (_cond) _cond->names_for_target (names);
  }
    
  // create a 'that' condition, i.e. the current object is an instance
  // of a specific class that matches the argument of 'that'
  void that (JPL_Class *in_class, const string &mangled_check) {
    TI_Class *ti = (TI_Class*)in_class->transform_info();
    stringstream cond_name;
    Naming::type_check_func (cond_name, ti->class_info (), mangled_check.c_str ());
    _cond = new That (cond_name.str ());
  }

  // the target object is an instance of a specific class
  void target (JPL_Class *in_class, const string &mangled_check) {
    TI_Class *ti = (TI_Class*)in_class->transform_info();
    stringstream cond_name;
    Naming::type_check_func (cond_name, ti->class_info (), mangled_check.c_str ());
    _cond = new Target (cond_name.str ());
  }
  
  // we are currently running in a control flow from ...
  void cflow (int index) {
    _cond = new CFlow (index);
  }
  
  // assgin a condition
  void assign (Condition &other) {
    _cond = other._cond;
    other._cond = 0;
  }
  
  // combine two conditions with &&
  void op_and (Condition &other) {
    _cond = new And (_cond, other._cond);
    other._cond = 0;
  }

  // combine two conditions with ||
  void op_or (Condition &other) {
    _cond = new Or (_cond, other._cond);
    other._cond = 0;
  }

  // check if a condition is *not* true
  void op_not () {
    _cond = new Not (_cond);
  }

  void matches (const JPL_Class &cls_loc) {
    // TODO: matching should work without parser information
    CRecord *rec = ((TI_Class*)cls_loc.transform_info ())->class_info ();
    // this is a hack! I assume that the top-level node is a TypeCond.
    if (_cond)
      ((TypeCond*)_cond)->matches (rec);
  }

};

inline ostream& operator<< (ostream& o, Condition &cond) {
  cond.print (o);
  return o;
}

#endif // __Condition_h__