File: Plan.cc

package info (click to toggle)
aspectc%2B%2B 1.0pre4~svn.20090918-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 117,308 kB
  • ctags: 410,601
  • sloc: cpp: 1,883,007; ansic: 17,279; sh: 2,190; makefile: 1,088
file content (489 lines) | stat: -rw-r--r-- 15,316 bytes parent folder | download | duplicates (2)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// 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                                            

#include "Plan.h"
#include "AspectInfo.h"
#include "AdviceInfo.h"
#include "OrderInfo.h"
#include "IntroductionInfo.h"
#include "PointCutContext.h"
#include "PointCutEvaluator.h"
#include "PointCutExpr.h"
#include "JoinPointModel.h"
#include "JoinPointPlan.h"

#include "Puma/ACAspectInfo.h"
#include "Puma/ACTree.h"
#include "Puma/ErrorSink.h"

#ifdef ACMODEL
Plan::Plan (ErrorStream &e, ProjectModel &jpm) : _err (e), _jpm (jpm) {
#else
Plan::Plan (ErrorStream &e, JoinPointModel &jpm) : _err (e), _jpm (jpm) {
#endif
  
  // get all aspects from the join point model
#ifdef ACMODEL
  ProjectModel::Selection all_aspects;
  _jpm.select (JPT_Aspect, all_aspects);
#else
  JoinPointModel::Selection all_aspects;
  _jpm.select (JoinPointLoc::Aspect, all_aspects);
#endif
  
  // sort them by inserting them into a set
#ifdef ACMODEL
  for (ProjectModel::Selection::iterator iter = all_aspects.begin ();
       iter != all_aspects.end (); ++iter) {
    ACM_Aspect &jpl_aspect = (ACM_Aspect&)**iter;
#else
  for (JoinPointModel::Selection::iterator iter = all_aspects.begin ();
       iter != all_aspects.end (); ++iter) {
    JPL_Aspect &jpl_aspect = (JPL_Aspect&)**iter;
#endif
    ACAspectInfo *acai = ((TI_Aspect*)jpl_aspect.transform_info ())->aspect_info();

    // Don't weave advice of abstract aspects
    if (acai->is_abstract ())
      continue;
    
    AspectInfo *aspect_info = addAspect (jpl_aspect);
    
    // traverse inheritance tree and collect advice declarations
#ifdef ACMODEL
    list<ACM_AdviceCode*> advices;
    collect_advice_codes (jpl_aspect, advices);

    // handle advice
    for (list<ACM_AdviceCode*>::const_iterator i = advices.begin ();
      i != advices.end (); ++i) {
      ACM_AdviceCode *code = *i;
#else
    list<JPL_AdviceCode*> advices;
    jpl_aspect.collect_advice_codes (advices);

    // handle advice
    for (list<JPL_AdviceCode*>::const_iterator i = advices.begin ();
      i != advices.end (); ++i) {
      JPL_AdviceCode *code = *i;
#endif
      AdviceInfo *advice_info = addAdvice (*aspect_info, *code);
      aspect_info->advice_infos ().push_back (advice_info);
    }
  }
}

Plan::~Plan () {
  for (int i = 0; i < (int)_advice_infos.length (); i++)
    delete _advice_infos[i];
  for (int i = 0; i < (int)_exec_jpls.length (); i++)
    delete _exec_jpls[i]->plan ();
  for (int i = 0; i < (int)_call_jpls.length (); i++)
    delete _call_jpls[i]->plan ();
  for (int i = 0; i < (int)_cons_jpls.length (); i++)
    delete _cons_jpls[i]->plan ();
  for (int i = 0; i < (int)_dest_jpls.length (); i++)
    delete _dest_jpls[i]->plan ();
  for (list<IntroductionInfo*>::iterator i = _introduction_infos.begin ();
       i != _introduction_infos.end (); ++i)
    delete *i;
  for (list<OrderInfo*>::iterator i = _order_infos.begin ();
       i != _order_infos.end (); ++i)
    delete *i;
}

#ifdef ACMODEL
AspectInfo *Plan::addAspect (ACM_Aspect &a) {
#else
AspectInfo *Plan::addAspect (JPL_Aspect &a) {
#endif
  pair<AspectContainer::iterator, bool> res = _aspect_infos.insert (AspectInfo (a));
  return (AspectInfo*)&*res.first;
}

#ifdef ACMODEL
AdviceInfo *Plan::addAdvice (AspectInfo &ai, ACM_AdviceCode &code) {
#else
AdviceInfo *Plan::addAdvice (AspectInfo &ai, JPL_AdviceCode &code) {
#endif
  AdviceInfo *result = new AdviceInfo (ai, code);
  _advice_infos.append (result);
  return result;
}

#ifdef ACMODEL
IntroductionInfo *Plan::addIntroduction (ACM_Aspect &jpl_aspect, ACM_Introduction &intro) {
#else
IntroductionInfo *Plan::addIntroduction (JPL_Aspect &jpl_aspect, JPL_Introduction &intro) {
#endif
  IntroductionInfo *result = new IntroductionInfo (jpl_aspect, intro);
  _introduction_infos.push_back (result);
  return result;
}

#ifdef ACMODEL
OrderInfo *Plan::addOrder (ACM_Aspect &a, ACM_Order &o) {
#else
OrderInfo *Plan::addOrder (JPL_Aspect &a, JPL_Order &o) {
#endif
  OrderInfo *result = new OrderInfo (a, o);
  _order_infos.push_back (result);
  return result;
}

// consider a join point and advice in the plan
#ifdef ACMODEL
void Plan::consider (ACM_Any *jpl, const Condition &cond, AdviceInfo *ai) {
  ACM_Code *jp_code = (ACM_Code*)jpl;
  if (!is_pseudo(*jp_code)) {
#else
void Plan::consider (JoinPointLoc *jpl, const Condition &cond, AdviceInfo *ai) {
  JPL_Code *jp_code = (JPL_Code*)jpl;
  if (!jp_code->is_pseudo ()) {
#endif
    JPP_Code *jp_plan = (JPP_Code*)jpl->plan ();
    if (!jp_plan) {
#ifdef ACMODEL
      switch (jpl->type_val ()) {
      case JPT_Execution:
        jp_plan = new JPP_Code;
        _exec_jpls.append (jpl);
        break;
      case JPT_Call:
        jp_plan = new JPP_Code;
        _call_jpls.append (jpl);
        break;
      case JPT_Construction:
        jp_plan = new JPP_Code;
        _cons_jpls.append (jpl);
        break;
      case JPT_Destruction:
        jp_plan = new JPP_Code;
        _dest_jpls.append (jpl);
        break;
#else
        switch (jpl->type ()) {
        case JoinPointLoc::Method:
          jp_plan = new JPP_Code;
          _exec_jpls.append (jpl);
          break;
        case JoinPointLoc::MethodCall:
          jp_plan = new JPP_Code;
          _call_jpls.append (jpl);
          break;
        case JoinPointLoc::Construction:
          jp_plan = new JPP_Code;
          _cons_jpls.append (jpl);
          break;
        case JoinPointLoc::Destruction:
          jp_plan = new JPP_Code;
          _dest_jpls.append (jpl);
          break;
#endif
      default:
        _err << sev_error
             << "internal problem, invalid join point type in plan" 
             << endMessage;
        return;
      }
      jpl->plan (jp_plan);
    }
    jp_plan->consider (ai, cond);
  }

  if (cond) {
    CObjectInfo *assoc_obj = TransformInfo::of (*jpl)->assoc_obj ();
    CFunctionInfo *that_func = (assoc_obj ? assoc_obj->FunctionInfo () : 0);
#ifdef ACMODEL
    CFunctionInfo *target_func = ((jpl->type_val () == JPT_Call) ?
      ((TI_MethodCall*)jpl->transform_info ())->called () : 0);
#else
    CFunctionInfo *target_func = ((jpl->type () == JoinPointLoc::MethodCall) ?
      ((TI_MethodCall*)jpl->transform_info ())->called () : 0);
#endif
    cond.checks_for_that (_type_checks_true);
    cond.checks_for_target (_type_checks_true);

    StringSet check_names_that, check_names_target;

    if (that_func) {
      cond.names_for_that (check_names_that);
      for (StringSet::iterator iter = check_names_that.begin ();
           iter != check_names_that.end (); ++iter) {
        _type_checks_false.insert (TypeCheck (that_func->ClassScope (),
                                   *iter));
      }
    }

    if (target_func) {
      cond.names_for_target (check_names_target);
      for (StringSet::iterator iter = check_names_target.begin ();
	   iter != check_names_target.end (); ++iter) {
	_type_checks_false.insert (TypeCheck (target_func->ClassScope (), 
					      *iter));
      }
    }
  }
}

#ifdef ACMODEL
void Plan::consider (ACM_Any *jpl, const CFlow &cflow) {
  ACM_Code *jp_code = (ACM_Code*)jpl;
  if (!jp_code->get_parent ()) // TODO: need is_pseudo?
#else
void Plan::consider (JoinPointLoc *jpl, const CFlow &cflow) {
  JPL_Code *jp_code = (JPL_Code*)jpl;
  if (jp_code->is_pseudo ())
#endif
    return;
    
  JPP_Code *jp_plan = (JPP_Code*)jpl->plan ();
  if (!jp_plan) {
#ifdef ACMODEL 
    switch (jpl->type_val ()) {
    case JPT_Execution:
      jp_plan = new JPP_Code;
      _exec_jpls.append (jpl);
      break;
    case JPT_Call:
      jp_plan = new JPP_Code;
      _call_jpls.append (jpl);
      break;
    case JPT_Construction:
      jp_plan = new JPP_Code;
      _cons_jpls.append (jpl);
      break;
    case JPT_Destruction:
      jp_plan = new JPP_Code;
      _dest_jpls.append (jpl);
      break;
#else
      switch (jpl->type ()) {
      case JoinPointLoc::Method:
        jp_plan = new JPP_Code;
        _exec_jpls.append (jpl);
        break;
      case JoinPointLoc::MethodCall:
        jp_plan = new JPP_Code;
        _call_jpls.append (jpl);
        break;
      case JoinPointLoc::Construction:
        jp_plan = new JPP_Code;
        _cons_jpls.append (jpl);
        break;
      case JoinPointLoc::Destruction:
        jp_plan = new JPP_Code;
        _dest_jpls.append (jpl);
        break;
#endif
    default:
      _err << sev_error
           << "internal problem, invalid join point type in plan" 
           << endMessage;
      return;
    }
    jpl->plan (jp_plan);
  }
  jp_plan->consider (cflow);
}

// consider a join point for an introduction in the plan
#ifdef ACMODEL
  JPP_Class *Plan::consider (ACM_Any *jpl, ACM_Introduction *intro) {
#else
  JPP_Class *Plan::consider (JoinPointLoc *jpl, JPL_Introduction *intro) {
#endif
  JPP_Class *jp_plan = (JPP_Class*)jpl->plan ();

  if (!jp_plan) {
#ifdef ACMODEL
    switch (jpl->type_val ()) {
    case JPT_Class:
    case JPT_Aspect:
#else
    switch (jpl->type ()) {
    case JoinPointLoc::Class:
    case JoinPointLoc::Aspect:
#endif
      jp_plan = new JPP_Class;
      _class_jpls.append (jpl);
      break;
    default:
      _err << sev_error
           << "internal problem, invalid join point type for intro in plan" 
           << endMessage;
      return 0;
    }
    jpl->plan (jp_plan);
  }
  
  jp_plan->consider (intro);
  return jp_plan;
}
  
// consider ordering information in the plan
#ifdef ACMODEL
void Plan::consider(ACM_Any* jpl, ACM_Aspect &hi, ACM_Aspect &lo) {
#else
void Plan::consider(JoinPointLoc* jpl, JPL_Aspect &hi, JPL_Aspect &lo) {
#endif
  JoinPointPlan* jp_plan = jpl->plan();
  if (jp_plan) {
    ACAspectInfo *h = TI_Aspect::of (hi)->aspect_info ();
    ACAspectInfo *l = TI_Aspect::of (lo)->aspect_info ();
    jp_plan->consider(*h, *l);
  }
}

// calculate the order for a single join points
#ifdef ACMODEL
void Plan::order (ACM_Any *jpl) {
#else
void Plan::order (JoinPointLoc *jpl) {
#endif
  PointCutContext context (_jpm);

  const list<OrderInfo*> &orders = order_infos ();
  for (list<OrderInfo*>::const_iterator i = orders.begin ();
       i != orders.end (); ++i) {
    OrderInfo *order = *i;

    // check if the current join point location is matched by the pointcut
    // expression of the order advice
    context.concrete_aspect (order->aspect ());
    Binding binding;     // binding and condition not used for intros
    Condition condition;
    if (!order->jp_pce ()->evaluate (*jpl, context, binding, condition))
      continue;
    // remember the partial orderings in the plan for this join point
    const list<PointCutExpr*> &pces = order->pces ();
#ifdef ACMODEL
    list<ACM_Aspect*> *hi = 0;
    list<ACM_Aspect*> *lo = 0;
#else
    list<JPL_Aspect*> *hi = 0;
    list<JPL_Aspect*> *lo = 0;
#endif
    bool start = true;
    for (list<PointCutExpr*>::const_iterator i2 = pces.begin ();
         i2 != pces.end (); ++i2) {
      PointCutExpr *pce = *i2;

      // match all relevant aspects with the current match expression
#ifdef ACMODEL
      list<ACM_Aspect*> *matched = new list<ACM_Aspect*>;
#else
      list<JPL_Aspect*> *matched = new list<JPL_Aspect*>;
#endif
      // TODO: this is inefficient. Not all aspects are relevant for each jp
      // get all aspects from the join point model
#ifdef ACMODEL
      ProjectModel::Selection all_aspects;
      _jpm.select (JPT_Aspect, all_aspects);
      for (ProjectModel::Selection::iterator iter = all_aspects.begin ();
        iter != all_aspects.end (); ++iter) {
        ACM_Aspect &jpl_aspect = (ACM_Aspect&)**iter;
#else
        JoinPointModel::Selection all_aspects;
        _jpm.select (JoinPointLoc::Aspect, all_aspects);
        for (JoinPointModel::Selection::iterator iter = all_aspects.begin ();
          iter != all_aspects.end (); ++iter) {
          JPL_Aspect &jpl_aspect = (JPL_Aspect&)**iter;
#endif
        if (pce->evaluate (jpl_aspect, context, binding, condition))
          matched->push_back (&jpl_aspect);
      }
      if (matched->size () == 0) {
	delete matched;
      }
      else if (start) {
        lo    = matched; // will become high in the next loop
	start = false;
      }
      else {
        if (hi) delete hi;
        hi = lo;
        lo = matched;

        // now we can consider the ordering info in the plan
#ifdef ACMODEL
        for (list<ACM_Aspect*>::iterator ihi = hi->begin ();
             ihi != hi->end (); ++ihi) {
          for (list<ACM_Aspect*>::iterator ilo = lo->begin ();
#else
          for (list<JPL_Aspect*>::iterator ihi = hi->begin ();
               ihi != hi->end (); ++ihi) {
            for (list<JPL_Aspect*>::iterator ilo = lo->begin ();
#endif
                 ilo != lo->end (); ++ilo) {
//              cout << (*ihi)->signature () << " -> " << (*ilo)->signature () << endl;
            consider (jpl, **ihi, **ilo);
          }
        }
      }
    }
    if (lo) delete lo;
  }
  
}
  
// calculate the order for all join points
void Plan::order () {
  for (int i = 0; i < (int)_exec_jpls.length (); i++)
    order (_exec_jpls[i]);
  for (int i = 0; i < (int)_call_jpls.length (); i++)
    order (_call_jpls[i]);
  for (int i = 0; i < (int)_cons_jpls.length (); i++)
    order (_cons_jpls[i]);
  for (int i = 0; i < (int)_dest_jpls.length (); i++)
    order (_dest_jpls[i]);
  for (int i = 0; i < (int)_class_jpls.length (); i++) 
    order (_class_jpls[i]);
}  

// check the plan for a specific join point
#ifdef ACMODEL
void Plan::check (ACM_Any *jpl) {
#else
void Plan::check (JoinPointLoc *jpl) {
#endif
  JoinPointPlan *plan = jpl->plan ();
  if (plan) {
    if (!plan->check (_err))
#ifdef ACMODEL
      _err << " for " << jpl->type_str () << "(\"" << "<sig: TODO in Plan.cc>" << "\")"
#else
      _err << " for " << jpl->type_str () << "(\"" << jpl->signature () << "\")"
#endif
      << TransformInfo::location (*jpl) << endMessage;
  }
}

// check the final plan -> messages to the error sink
void Plan::check () {
  for (int i = 0; i < (int)_exec_jpls.length (); i++)
    check (_exec_jpls[i]);
  for (int i = 0; i < (int)_call_jpls.length (); i++)
    check (_call_jpls[i]);
  for (int i = 0; i < (int)_cons_jpls.length (); i++)
    check (_cons_jpls[i]);
  for (int i = 0; i < (int)_dest_jpls.length (); i++)
    check (_dest_jpls[i]);
  for (int i = 0; i < (int)_class_jpls.length (); i++) 
    check (_class_jpls[i]);
}