File: JoinPointPlan.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 (473 lines) | stat: -rw-r--r-- 15,281 bytes parent folder | download
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
// 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 "JoinPointPlan.h"
#include "ACUnit.h"
#include "IntroductionUnit.h"
#include "AspectInfo.h"
#include "WeaverBase.h"
#include "LineDirectiveMgr.h"
#ifdef ACMODEL
#include "Utils.h"
#endif

#include "Puma/CProtection.h"
#include "Puma/ErrorSink.h"
#include "Puma/ManipCommander.h"
#include "Puma/CFunctionInfo.h"
#include "Puma/CArgumentInfo.h"
#include "Puma/ACSliceInfo.h"
#include "Puma/CSemDatabase.h"

// issue an advice in the plan for this code join point
void JPP_Code::issue (int index) {
  AdviceInfo *ai = _unsorted[index];
  const Condition &cond = *_conds[index];
  switch (ai->type()) {
#ifdef ACMODEL
  case ACT_BEFORE:
#else
  case JPL_AdviceCode::ADVICE_BEFORE:
#endif
    _jpa.do_before (ai, cond);
    break;
#ifdef ACMODEL
  case ACT_AROUND:
#else
  case JPL_AdviceCode::ADVICE_AROUND:
#endif
    _jpa.do_around (ai, cond);
    break;
#ifdef ACMODEL
  case ACT_AFTER:
#else
  case JPL_AdviceCode::ADVICE_AFTER:
#endif
    _jpa.do_after (ai, cond);
    break;
  }
}

// consider an advice in the plan for this code join point
bool JPP_Code::consider (AdviceInfo *ai, const Condition &cond) {
  _unsorted.append (ai);
  _conds.append (&cond);
  return true;
}

bool JPP_Code::check(ErrorSink &err) {
  if (!plan ()) {
    err << sev_error << "order directive not resolveable";
    return false;
  }
  for (int i = 0; i < items (); i++) {
    ACAspectInfo* ai = &item (i);
    for (int j = 0; j < _unsorted.length(); j++) {
      AdviceInfo* a = _unsorted[j];
      if (a && (TI_Aspect::of (a->aspect())->acnode() == ai)) {
        issue(j);
        _unsorted[j] = 0;
      }
    }
  }
  for (int j = 0; j < _unsorted.length(); j++)
    if (_unsorted[j] != 0) {
      issue(j);
    }
  return true;
}

// issue an introduction in the plan for this class join point
#ifdef ACMODEL
void JPP_Class::issue (ACM_Introduction *ii) {
  switch (ii->get_slice ()->get_kind ()) {
  case CST_DEP_BASE:
    _base.append (ii);
    break;
  case CST_DEP_NORMAL:
    _other.append (ii);
    break;
  case CST_NORMAL: { // slices might contribute new members and base classes
    CObjectInfo *obj = TI_ClassSlice::of (*ii->get_slice())->assoc_obj ();
    assert (obj && obj->Tree ());
    assert (obj->Tree ()->NodeName () == CT_ClassSliceDecl::NodeId ());
    CT_ClassSliceDecl *csd = (CT_ClassSliceDecl*)obj->Tree ();
    if (csd->base_clause ())
      _base.append (ii);
    if (csd->members () && csd->members ()->Entries ())
      _other.append (ii);
    }
    break;
  case CST_ERROR:
  case CST_UNKNOWN:
    break;
  }
}
#else
void JPP_Class::issue (JPL_Introduction *ii) {
  switch (ii->introduced ()->slice_type ()) {
  case JPL_ClassSlice::CS_OLD_BASE:
    _base.append (ii);
    break;
  case JPL_ClassSlice::CS_OLD_OTHER:
    _other.append (ii);
    break;
  case JPL_ClassSlice::CS_NORMAL: { // slices might contribute new members and base classes
    CObjectInfo *obj = TI_ClassSlice::of (*ii->introduced ())->assoc_obj ();
    assert (obj && obj->Tree ());
    assert (obj->Tree ()->NodeName () == CT_ClassSliceDecl::NodeId ());
    CT_ClassSliceDecl *csd = (CT_ClassSliceDecl*)obj->Tree ();
    if (csd->base_clause ())
      _base.append (ii);
    if (csd->members () && csd->members ()->Entries ())
      _other.append (ii);
    }
    break;
  case JPL_ClassSlice::CS_ERROR:
    break;
  }
}
#endif
// consider an introduction in the plan for this class join point
#ifdef ACMODEL
bool JPP_Class::consider (ACM_Introduction *ii) {
  switch (ii->get_slice ()->get_kind()) {
  case CST_DEP_BASE:
  case CST_DEP_NORMAL:
  case CST_NORMAL:
#else
bool JPP_Class::consider (JPL_Introduction *ii) {
  switch (ii->introduced ()->slice_type ()) {
  case JPL_ClassSlice::CS_OLD_BASE:
  case JPL_ClassSlice::CS_OLD_OTHER:
  case JPL_ClassSlice::CS_NORMAL:
#endif
    _unsorted.append (ii);
    return true;
  default:
    return false;
  }
}

bool JPP_Class::check(ErrorSink &err) {
  if (!plan ()) {
    err << sev_error << "order directive not resolveable";
    return false;
  }
  for (int i = 0; i < items (); i++) {
    ACAspectInfo* ai = &item (i);
    for (int j = 0; j < _unsorted.length(); j++) {
#ifdef ACMODEL
      if (_unsorted[j] && (((TI_Aspect*)_unsorted[j]->get_parent()->transform_info ())->acnode() == ai)) {
#else
      if (_unsorted[j] && (((TI_Aspect*)_unsorted[j]->parent()->transform_info ())->acnode() == ai)) {
#endif
        issue(_unsorted[j]);
        _unsorted[j] = 0;
      }
    }
  }
  for (int j = 0; j < _unsorted.length(); j++)
    if (_unsorted[j] != 0)
      issue(_unsorted[j]);

  return true;
}


void JPP_Class::gen_intros (list<Unit*> &units, ErrorStream &err,
  CStructure *target, LineDirectiveMgr &lmgr, bool non_inline) const {

  // ... some unit for formatting
  ACUnit ws (err);
  ws << " " << endu;
    
  // handle all intros
  for (int i = 0; i < otherIntros (); i++) {
#ifdef ACMODEL
    ACM_Introduction *ii = otherIntro (i);
#else
    JPL_Introduction *ii = otherIntro (i);
#endif
    
    // create the new unit
    IntroductionUnit *unit = 
      new IntroductionUnit (err, (Unit*)target->Tree ()->token ()->belonging_to ());
    unit->intro (ii);

    // at least on token is needed in the unit for 'move' (see below)
    unit->append (*((Token*)ws.first ())->duplicate ());

    // TODO: member intros from slices are ignored here!
#ifdef ACMODEL
    if (ii->get_slice ()->get_kind() == CST_NORMAL) {
      CObjectInfo *obj = TI_ClassSlice::of (*ii->get_slice ())->assoc_obj ();
#else
    if (ii->introduced ()->slice_type () == JPL_ClassSlice::CS_NORMAL) {
      CObjectInfo *obj = TI_ClassSlice::of (*ii->introduced ())->assoc_obj ();
#endif
      assert (obj && obj->Tree ());
      assert (obj->Tree ()->NodeName () == CT_ClassSliceDecl::NodeId ());
      ACSliceInfo *acsi = obj->SemDB ()->SliceInfo (obj)->definition ();
      if (non_inline)
        gen_intro_non_inline (unit, ii, acsi, err, lmgr, target);
      else
        gen_intro_inline (unit, ii, acsi, err, lmgr, target);
    }
#ifdef ACMODEL
    else if ((ii->get_slice()->get_prot () == SP_UNKNOWN && non_inline) ||
             (ii->get_slice()->get_prot () != SP_UNKNOWN && !non_inline)) {
#else
    else if ((ii->introduced ()->prot () == CProtection::PROT_NONE && non_inline) ||
              (ii->introduced ()->prot () != CProtection::PROT_NONE && !non_inline)) {
#endif
      if (!non_inline) {
        // add privat/public/protected
        ACUnit prot (err);
#ifdef ACMODEL
        prot << "\n" << slice_protection_str(ii->get_slice()->get_prot ()) << ":\n  " << endu;
#else
        prot << WeaverBase::protection_string (ii->introduced ()->prot ()) << "  " << endu;
#endif
        unit->move ((Token*)unit->last (), prot);
      }
      
      // obtain the pattern for this intro
#ifdef ACMODEL
      const Unit &pattern = TI_ClassSlice::of(*ii->get_slice())->pattern ();
#else
      const Unit &pattern = ii->introduced ()->pattern ();
#endif
      CT_Intro *intro = (CT_Intro*)TI_Introduction::of (*ii)->tree ()->Decl ();
      Token *curr = (Token*)pattern.first ();
      ACUnit dir (err);
      lmgr.directive (dir, (Unit*)&pattern, curr);
      dir << endu;
      if (!dir.empty ())
        unit->move ((Token*)unit->last (), dir);
      Location loc = ((Token*)pattern.first ())->location ();
      instantiate_intro (err, unit, intro, pattern, target, loc);
    }

    // if there was no introduction return 0
    if (unit->first () == unit->last ())
      delete unit;
    else
      units.push_back (unit);
  }
}


#ifdef ACMODEL
void JPP_Class::gen_intro_inline (Unit *unit, ACM_Introduction *ii,
  ACSliceInfo *acsi, ErrorStream &err, LineDirectiveMgr &lmgr,
  CStructure *target) const {
  CObjectInfo *obj = TI_ClassSlice::of (*ii->get_slice ())->assoc_obj ();
#else
  void JPP_Class::gen_intro_inline (Unit *unit, JPL_Introduction *ii,
    ACSliceInfo *acsi, ErrorStream &err, LineDirectiveMgr &lmgr,
    CStructure *target) const {
    CObjectInfo *obj = TI_ClassSlice::of (*ii->introduced ())->assoc_obj ();
#endif
  CT_ClassSliceDecl *csd = (CT_ClassSliceDecl*)obj->Tree ();
  
  // add "private:\n"
  ACUnit slice_start (err);
#ifdef ACMODEL
  if (ii->get_slice()->get_prot () == SP_PRIVATE)
#else
  if (ii->introduced ()->prot () == CProtection::PROT_PRIVATE)
#endif
    slice_start << "  private:" << endl;
  else
    slice_start << "  public:" << endl;
  // add "  typedef <target-name> <slice-name>;\n"
  if (!obj->isAnonymous ()) {
    slice_start << "  typedef " << target->Name () << " "
                << obj->Name () << ";" << endl;
  }
  slice_start << endu;
  unit->move ((Token*)unit->last (), slice_start);
  // generate the intro itself from the stored pattern
  const Unit &pattern = *acsi->pattern ();
  Token *curr = (Token*)pattern.first ();
  Location loc = curr->location ();
  ACUnit dir (err);
  lmgr.directive (dir, (Unit*)&pattern, curr);
  dir << endu;
  if (!dir.empty ())
    unit->move ((Token*)unit->last (), dir);
  for (int m = 0; m < csd->members ()->Entries (); m++) {
    Unit tmp_pattern;
    tmp_pattern.name (pattern.name ());
    CT_Intro *intro = (CT_Intro*)csd->members ()->Entry (m);
    for (int i = 0; i < intro->Entries (); i++) {
      tmp_pattern.append (*curr->duplicate ());
      curr = (Token*)pattern.next (curr);
    }
    instantiate_intro (err, unit, intro, tmp_pattern, target, loc);
  }
}

#ifdef ACMODEL
void JPP_Class::gen_intro_non_inline (Unit *unit, ACM_Introduction *ii,
  ACSliceInfo *acsi, ErrorStream &err, LineDirectiveMgr &lmgr,
  CStructure *target) const {
#else
void JPP_Class::gen_intro_non_inline (Unit *unit, JPL_Introduction *ii,
  ACSliceInfo *acsi, ErrorStream &err, LineDirectiveMgr &lmgr,
  CStructure *target) const {
#endif
  // generate the intros from the stored non-inline member patterns
  for (int m = 0; m < acsi->members (); m++) {
    CT_Intro *intro = acsi->member (m);
    const Unit &pattern = *acsi->member_pattern (m);
    Token *curr = (Token*)pattern.first ();
    Location loc = curr->location ();
    ACUnit dir (err);
    lmgr.directive (dir, (Unit*)&pattern, curr);
    dir << endu;
    if (!dir.empty ())
      unit->move ((Token*)unit->last (), dir);
    instantiate_intro (err, unit, intro, pattern, target, loc);
  }
}


#ifdef ACMODEL
void JPP_Class::gen_base_intro (IntroductionUnit &intro_unit,
  ACM_Introduction *ii, bool first) const {
#else
void JPP_Class::gen_base_intro (IntroductionUnit &intro_unit,
  JPL_Introduction *ii, bool first) const {
#endif
  // generate the code for this entry
  intro_unit << (first ? ": " : ", ");

#ifdef ACMODEL
  if (ii->get_slice()->get_kind() == CST_NORMAL) {
    // slice-based base class intro
    CObjectInfo *obj = TI_ClassSlice::of (*ii->get_slice())->assoc_obj ();
#else
  if (ii->introduced ()->slice_type () == JPL_ClassSlice::CS_NORMAL) {
    // slice-based base class intro
    CObjectInfo *obj = TI_ClassSlice::of (*ii->introduced ())->assoc_obj ();
#endif
    assert (obj && obj->Tree ());
    assert (obj->Tree ()->NodeName () == CT_ClassSliceDecl::NodeId ());
    ACSliceInfo *acsi = obj->SemDB ()->SliceInfo (obj)->definition ();
    const Unit &base = *acsi->base_pattern ();
    for (Token *curr = (Token*)base.first (); curr;
      curr = (Token*)base.next (curr)) {
      intro_unit << *curr << " ";
    }
  }
  else {
    // old-style base class intro with 'baseclass(virtual Bla)'
    // get all the needed information
#ifdef ACMODEL
    CObjectInfo *obj = TI_ClassSlice::of (*ii->get_slice())->assoc_obj ();
#else
    CObjectInfo *obj = TI_ClassSlice::of (*ii->introduced ())->assoc_obj ();
#endif
    CProtection::Type prot = obj->Protection ();
    bool virt = obj->isVirtual ();
    CTypeInfo *type = obj->FunctionInfo ()->Argument (0u)->TypeInfo ();
      
    if (virt) intro_unit << "virtual ";
    switch (prot) {
      case CProtection::PROT_PRIVATE:   intro_unit << "private "; break;
      case CProtection::PROT_PROTECTED: intro_unit << "protected "; break;
      case CProtection::PROT_PUBLIC:    intro_unit << "public "; break;
      default:
        assert (false);
    }
    intro_unit << *type;
  }
  intro_unit << endu;
}


void JPP_Class::gen_base_intros (list<Unit*> &units, ErrorStream &err,
  CClassInfo *target, LineDirectiveMgr &lmgr) const {

  for (int i = 0; i < _base.length (); i++) {
    // get the current introduction
#ifdef ACMODEL
    ACM_Introduction *ii = _base.lookup (i);
#else
    JPL_Introduction *ii = _base.lookup (i);
#endif
    
    // create the new unit
    IntroductionUnit *unit = 
      new IntroductionUnit (err, (Unit*)target->Tree ()->token ()->belonging_to ());
    unit->intro (ii);

    // generate the code for this base class introduction
    gen_base_intro (*unit, ii, (i == 0));

    // store the result for the caller
    units.push_back (unit);
  }
}

void JPP_Class::instantiate_intro (ErrorStream &err, Unit *unit,
  CT_Intro *intro, const Unit &pattern, CStructure *target,
  Location &loc) const {
    
  // create a unit with the target class name
  ACUnit target_name (err);
  target_name << target->Name () << endu;
  ACUnit target_qual_name (err);
  target_qual_name << target->QualName () << endu;

  // ... some units for formatting
  ACUnit nl (err);
  nl << endl << endu;
  ACUnit ws (err);
  ws << " " << endu;
  
  int i = 0, e = 0;
  // create a formatted unit and replace the advice name with the target name
  for (Token *curr = (Token*)pattern.first (); curr;
       curr = (Token*)pattern.next (curr), e++) {
    Token *tok = curr;
    if (curr->location ().line () != loc.line ()) {
      for (int l = loc.line (); l < curr->location ().line (); l++)
        unit->append (*((Token*)nl.first ())->duplicate ());
      loc = curr->location ();
    }
    else
      unit->append (*((Token*)ws.first ())->duplicate ());
    if (i < intro->NameIndices () && e == intro->NameIndex (i)) {
      // target class name is used instead of intro token
      if (intro->NameQual (i))
        *unit += target_qual_name;
      else
        *unit += target_name;
      while (e < intro->NameToIndex (i)) {
        e++;
        curr = (Token*)pattern.next (curr);
      }
      i++;
    }
    else
      unit->append (*tok->duplicate ());
  }
}