File: Transformer.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 (1062 lines) | stat: -rw-r--r-- 35,124 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
// 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                                            

// AspectC++ includes
#include "Transformer.h"
#include "JPAdvice.h"
#include "PointCutEvaluator.h"
#include "PointCut.h"
#include "PointCutContext.h"
#include "OrderInfo.h"
#include "AdviceInfo.h"
#include "AspectInfo.h"
#include "IntroductionInfo.h"
#include "JoinPointPlan.h"
#include "Plan.h"
#include "Repository.h"
#include "PointCutContext.h"
#include "CFlow.h"
#include "BackEndProblems.h"
#include "ACConfig.h"
#include "Introducer.h"
#include "IncludeGraph.h"
#include "PointCutExpr.h"
#include "ModelBuilder.h"
#include "IntroductionUnit.h"
#ifdef ACMODEL
#include "XmlModelWriter.h"
#endif

// PUMA includes
#include "Puma/CProject.h"
#include "Puma/CCSemVisitor.h"
#include "Puma/VerboseMgr.h"
#include "Puma/CFunctionInfo.h"
#include "Puma/CArgumentInfo.h"
#include "Puma/CTranslationUnit.h"
#include "Puma/CSemDatabase.h"
#include "Puma/ACAspectInfo.h"
#include "Puma/ACAdviceInfo.h"
#include "Puma/ACIntroductionInfo.h"
#include "Puma/ACPointcutInfo.h"
#include "Puma/ACTree.h"
#include "Puma/CPrintVisitor.h"
#include "Puma/SysCall.h"

// C++ includes
#include <sstream>
using std::stringstream;
using std::endl;
#include <stdlib.h> // for getenv
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h> // for access()!
#endif // _MSC_VER
#include <fcntl.h>

void Transformer::work (Unit *unit, Token *primary_start, Token *primary_end) {

  // set the start and end token of the unit
  _code_weaver.init (primary_start, primary_end);
  
  // determine back-end compiler problems and setup code weaver
  BackEndProblems back_end_problems;
  back_end_problems._local_class       = _conf.problem_local_class ();
  back_end_problems._spec_scope        = _conf.problem_spec_scope ();
  back_end_problems._use_always_inline = !_conf.problem_force_inline ();
  back_end_problems._size_type         = _conf.size_type ();
  back_end_problems._warn_macro        = _conf.warn_macro();
  back_end_problems._warn_deprecated   = _conf.warn_deprecated();
  _code_weaver.problems (back_end_problems);
  
  // perform the transformation
  CTranslationUnit *tunit1 = 0;
  ModelBuilder jpm (_vm, _err, _conf);
  
  bool ok = (phase1 (unit, tunit1, jpm) &&
             phase2 (unit, tunit1, jpm));
  if (tunit1)
    delete tunit1;
  if (!ok)
    _vm << "Aborting" << endvm;
}

bool Transformer::phase1 (Unit *unit, CTranslationUnit *&tunit,
                          ModelBuilder &jpm) {

  _vm << "Inserting namespace AC" << endvm;
  // in front of start!
  _code_weaver.insert_namespace_ac_before ((Token*)unit->first ());
  _code_weaver.commit ();
  
  _vm << "Parsing ..." << endvm;
  // parse the translation unit, but ignore function bodies
  unsigned options = _parser.Options ();
  _parser.Options (options | CCParser::SKIP_FCT_BODY);
  tunit = _parser.parse (*unit, _project);
  _parser.Options (options);
  // do semantic analysis of expressions
  _sem_visitor.run (tunit->tree ());
  
  if (_err.severity () >= sev_error)
    return false;

//  tunit->unit ()->print(cout);
//  CPrintVisitor printer;
//  printer.print (tunit->tree (), cout);

  //    Some debugging code:
  //    tunit->unit ()->print(cout);
  //    CPrintVisitor printer;
  //    printer.print (tunit->tree (), cout);
  //    tunit->db ().Dump (cout, 2);
  
  // create the phase 1 join point model
  _vm << "Setting up join point model 1 ..." << endvm;
  _vm++;
  jpm.setup_phase1 (*tunit);
  _vm--;
  
  // set up the project repository
  _repo.setup (unit);

  _vm << "Setting Aspect Access Priviledges ..." << endvm;
  aspect_priviledges (jpm);
  
  _vm << "Weaving Introductions ..." << endvm;
  introductions (*tunit, jpm);
      
  _vm << "Weaving Advice Declarations ..." << endvm;
  advice (*tunit);
  
  _vm << "Weaving Singleton Aspects ..." << endvm;
  singleton_aspects (*tunit);

//  _vm << "Slice/Intro Includes ..." << endvm;
//  _code_weaver.slice_includes (_project, _primary_start);

  _vm << "Committing" << endvm;
  _code_weaver.commit ();

//    _vm << "Stage1 save!" << endvm;
//    _project.save();
//    _vm << " done." << endvm;
//	exit(1);

  return (_err.severity () < sev_error);
}

bool Transformer::phase2 (Unit *unit, CTranslationUnit *tunit1,
                          ModelBuilder &jpm) {

  IncludeGraph ig (_project);
  ig.init (*tunit1);
//  ig.dump ();
  
  _vm << "Preparing introductions ..." << endvm;
  Plan plan (_err, jpm);

  // get all aspects from the plan (in some order)
  const Plan::AspectContainer &aspects = plan.aspect_infos ();
  for (Plan::AspectContainer::const_iterator i = aspects.begin ();
    i != aspects.end (); ++i) {
    
#ifdef ACMODEL
    ACM_Aspect &jpl_aspect = i->loc ();

    // add introductions to plan
    list<ACM_Introduction*> intros;
    collect_intros (jpl_aspect, intros);
    
    for (list<ACM_Introduction*>::iterator i = intros.begin ();
      i != intros.end (); ++i) {
      IntroductionInfo *ii = plan.addIntroduction (jpl_aspect, **i);
      ii->analyze_expr (_err, jpm);
    }

    // add order advices to plan
    list<ACM_Order*> orders;
    collect_orders (jpl_aspect, orders);
    
    for (list<ACM_Order*>::iterator i = orders.begin ();
      i != orders.end (); ++i) {
      OrderInfo *oi = plan.addOrder (jpl_aspect, **i);
      oi->analyze_exprs (_err, jpm);
      if (!getenv ("ACNEWREPO")) {
        _repo.update (*oi);
      }
    }
#else
    JPL_Aspect &jpl_aspect = i->loc ();

    // add introductions to plan
    list<JPL_Introduction*> intros;
    jpl_aspect.collect_intros (intros);
    
    for (list<JPL_Introduction*>::iterator i = intros.begin ();
      i != intros.end (); ++i) {
      IntroductionInfo *ii = plan.addIntroduction (jpl_aspect, **i);
      ii->analyze_expr (_err, jpm);
    }

    // add order advices to plan
    list<JPL_Order*> orders;
    jpl_aspect.collect_orders (orders);
    
    for (list<JPL_Order*>::iterator i = orders.begin ();
      i != orders.end (); ++i) {
      OrderInfo *oi = plan.addOrder (jpl_aspect, **i);
      oi->analyze_exprs (_err, jpm);
      if (!getenv ("ACNEWREPO")) {
        _repo.update (*oi);
      }
    }
#endif
  }

  _vm << "Parsing again ..." << endvm;
  
  // parse the translation unit, but ignore function bodies
  Introducer introducer (plan, _code_weaver, _parser, jpm, ig, _conf);
  _parser.introducer (&introducer);
  CTranslationUnit *tunit = _parser.parse (*unit, _project);
  _parser.introducer (0);

  // do semantic analysis of expressions
  _sem_visitor.run (tunit->tree ());
  list<CTree*> &ah_trees = introducer.ah_trees ();
  for (list<CTree*>::iterator i = ah_trees.begin (); i != ah_trees.end (); ++i)
    if (*i)
      _sem_visitor.run (*i);

  if (_err.severity () >= sev_error) {
    // TODO: delete takes too much time and has no real use for ac++
    // so we skip it for now 
    // delete tunit;
    return false;
  }

//#ifdef TRY_INTRODUCER
//  cout << "Printing semantic database..." << endl;
//  tunit->db ().Dump (cout, 10);
//#endif // TRY_INTRODUCER
  
  //    CPrintVisitor printer;
  //    printer.print (tunit->tree (), cout);
  
  _vm << "Updating repository intro part" << endvm;
  if (!getenv ("ACNEWREPO")) {
    update_intros_in_repo (jpm);
  }
  
  jpm.setup_phase2 (*tunit, ah_trees);
  
  _vm << "Weaving Join Points ..." << endvm;
  Plan plan2 (_err, jpm);
  join_points (*tunit, jpm, plan2);
      
  _vm << "Final cleanup" << endvm;
  cleanup (*tunit);

  if (_conf.dynamic ()) {
    _vm << "Preparing for dynamic weaving" << endvm;
    prepare_dynamic_weaving (jpm);  
  }

  // generate a string with aspect header include directives
  determine_aspect_includes (ig);
  
  _vm << "Committing" << endvm;
  _code_weaver.commit ();

  // TODO: delete takes too much time and has no real use for ac++
  // so we skip it for now 
  // delete tunit;
  
  _repo.cleanup ();

  return (_err.severity () < sev_error);
}

void Transformer::determine_aspect_includes (const IncludeGraph &ig) {
  // find all files that are included by aspect headers
  if (_conf.iterate_aspects ()) {
    // collect the names of aspect header files,
    // generate a unit with include statements for these files,
    PathIterator ah_iter (".*\\.ah$");
    while (_project.iterate (ah_iter))
      _aspect_includes += aspect_include_cluster (ah_iter.file (), ig);
  }
  else {
    // Get the names from the configuration object (-a options)
    for (int i = 0; i < _conf.aspect_headers (); i++)
      _aspect_includes += aspect_include_cluster (_conf.aspect_header (i), ig);
  }
}

string Transformer::aspect_include_cluster (const char* ah_file,
  const IncludeGraph &ig) {

  // find the corresponding unit object for the aspect header file name
  const Unit *unit = _project.unitManager ().getUnit (ah_file, true);
  if (!unit || !unit->isFile ()) {
    _err << sev_fatal << "Unit for \"" << ah_file << "\" not found or no file."
         << endMessage;
    return "";
  }

  // determine the aspect header cluster for this aspect header unit
  set<const Unit*> cluster_units;
  determine_aspect_cluster (unit, ig, cluster_units);
  
  // generate the code
  stringstream cluster;
  cluster << "#ifdef __ac_need_";
  Naming::mangle_file (cluster, (FileUnit*)unit);
  cluster << endl;

  for (set<const Unit*>::iterator i = cluster_units.begin ();
    i != cluster_units.end (); ++i) {
    const Unit *aspect_unit = *i;  
    Filename incname = _project.getInclString (aspect_unit->name ());
    cluster << "#ifndef __ac_have_";
    Naming::mangle_file (cluster, (FileUnit*)aspect_unit);
    cluster << endl;
    cluster << "#define __ac_have_";
    Naming::mangle_file (cluster, (FileUnit*)aspect_unit);
    cluster << endl;
    cluster << "#include \"" << incname << "\"" << endl;
    cluster << "#endif" << endl;
  }  
  
  cluster << "#endif" << endl;
  return cluster.str ();
}

void Transformer::determine_aspect_cluster (const Unit* ah_unit,
  const IncludeGraph &ig, set<const Unit*> &cluster) {
  
  // if the ah file is already a cluster member, we return immediately
  if (cluster.find (ah_unit) != cluster.end ())
    return;

  // otherwise the unit will be inserted
  cluster.insert (ah_unit);
      
  // find all header files that are included by this aspect header
  set<const Unit*> inc_units;
  ig.included_files (ah_unit, inc_units);
  
  // include all aspect headers that affect join points in these headers
  AspectIncludes &ais = _code_weaver.aspect_includes ();
  for (set<const Unit*>::iterator i = inc_units.begin ();
    i != inc_units.end (); ++i) {
    Unit *inc_unit = (Unit*)*i;
    AspectIncludes::const_iterator aii = ais.find (inc_unit);
    if (aii != ais.end ()) {
      const set<AspectRef> &aspect_refs = aii->second;
      for (set<AspectRef>::const_iterator ari = aspect_refs.begin ();
        ari != aspect_refs.end (); ++ari) {
#ifdef ACMODEL
        ACM_Aspect &jpl_aspect = ari->_aspect->loc ();
#else
        JPL_Aspect &jpl_aspect = ari->_aspect->loc ();
#endif
        Unit *aspect_unit = TI_Aspect::of (jpl_aspect)->unit ();
        // recursively analyze the cluster of this aspect header unit
        determine_aspect_cluster (aspect_unit, ig, cluster);
      }
    }
  }
}

void Transformer::prepare_dynamic_weaving (ModelBuilder &jpm) {

  // mark all operations that access introduced attributes
  const list<AccessInfo> &access_infos = jpm.access_infos ();
  for (list<AccessInfo>::const_iterator i = access_infos.begin ();
    i != access_infos.end (); ++i) {
    if (i->_info->isStatic () || i->_info->isAnonymous ())
      continue;
    Unit *unit = (Unit*)i->_info->Tree ()->token ()->belonging_to ();
    while (unit->isMacroExp ())
      unit = ((MacroUnit*)unit)->CallingUnit ();
    if (IntroductionUnit::cast (unit)) {
      Token *tok_before = i->_tree->token ();
      const WeavePos &before = _code_weaver.weave_pos (tok_before, WeavePos::WP_BEFORE);
      _code_weaver.paste (before, string ("/*** +access ") +
        string (i->_info->QualName ()) + string (" ***/\n"));      
      Token *tok_after = i->_tree->end_token ();
      const WeavePos &after = _code_weaver.weave_pos (tok_after, WeavePos::WP_AFTER);
      _code_weaver.paste (after, string ("/*** -access ") +
        string (i->_info->QualName ()) + string (" ***/\n"));      
      if (i->_tree->NodeName () == CT_MembPtrExpr::NodeId () ||
          i->_tree->NodeName () == CT_MembRefExpr::NodeId ()) {
        CTree *op = i->_tree->Son (1);
        Token *tok_before = op->token ();
        const WeavePos &before = _code_weaver.weave_pos (tok_before, WeavePos::WP_BEFORE);
        _code_weaver.paste (before, string ("/*** op ") +
          string (i->_info->QualName ()) + string (" ***/\n"));      
      }
    }
  }
  
  // mark all classes that contain dynamically introduced attributes
#ifdef ACMODEL
  ProjectModel::Selection classes;
  jpm.select ((JoinPointType)(JPT_Class|JPT_Aspect), classes);
  for (ProjectModel::Selection::iterator i = classes.begin ();
    i != classes.end (); ++i) {
    ACM_Class *cls = (ACM_Class*)*i;
    if (!cls->get_intro_target ())
#else
      JoinPointModel::Selection classes;
      jpm.select ((JoinPointLoc::join_point_type)(JoinPointLoc::Class|
        JoinPointLoc::Aspect), classes);
      for (JoinPointModel::Selection::iterator i = classes.begin ();
        i != classes.end (); ++i) {
        JPL_Class *cls = (JPL_Class*)*i;
        if (!cls->intro_target ())
#endif
      continue;
    bool mark_class = false;
    const TI_Class *ti = TI_Class::of (*cls);
    CClassInfo *ci = ti->class_info ();
    for (unsigned i = 0; i < ci->Attributes (); i++) {
      CAttributeInfo *attr = ci->Attribute (i);
      if (attr->isStatic () || attr->isAnonymous ())
        continue;
      Unit *unit = (Unit*)attr->Tree ()->token ()->belonging_to ();
      IntroductionUnit *iunit = IntroductionUnit::cast (unit);
      if (iunit) {
        mark_class = true;
#ifdef ACMODEL
        ACM_Name *jpl_aspect = (ACM_Name*)iunit->intro ()->get_parent ();
#else
        JPL_Name *jpl_aspect = iunit->intro ()->parent ();
#endif
        Token *tok_before = attr->Tree ()->ObjDecl ()->token ();
        const WeavePos &before = _code_weaver.weave_pos (tok_before, WeavePos::WP_BEFORE);
        _code_weaver.paste (before, string ("/*** +intro ") +
#ifdef ACMODEL
        signature (*jpl_aspect) + string (" ***/\n"));      
#else
        string (jpl_aspect->signature ()) + string (" ***/\n"));      
#endif
        Token *tok_after = attr->Tree ()->ObjDecl ()->end_token ();
        const WeavePos &after = _code_weaver.weave_pos (tok_after, WeavePos::WP_AFTER);
        _code_weaver.paste (after, string ("/*** -intro ") +
#ifdef ACMODEL
        signature (*jpl_aspect) + string (" ***/\n"));      
#else
        string (jpl_aspect->signature ()) + string (" ***/\n"));      
#endif
      }
    }
    if (mark_class) {
      Token *tok_before = ((CT_ClassDef*)ci->Tree ())->ObjDecl ()->token ();
      const WeavePos &before = _code_weaver.weave_pos (tok_before, WeavePos::WP_BEFORE);
      _code_weaver.paste (before, string ("/*** +class ") +
#ifdef ACMODEL
      signature (*cls) + string (" ***/\n"));      
#else
      string (cls->signature ()) + string (" ***/\n"));      
#endif
      
      Token *tok_after = ((CT_ClassDef*)ci->Tree ())->Members ()->end_token ();
      const WeavePos &after = _code_weaver.weave_pos (tok_after, WeavePos::WP_BEFORE);
      _code_weaver.paste (after, string ("/*** -class ") +
#ifdef ACMODEL
      signature (*cls) + string (" ***/\n"));      
#else
      string (cls->signature ()) + string (" ***/\n"));
#endif
    }
  }
}


void Transformer::update_intros_in_repo (ModelBuilder &jpm1) {
#ifdef ACMODEL
  ProjectModel::Selection intros;
  jpm1.select (JPT_Introduction, intros);
  for (ProjectModel::Selection::iterator i = intros.begin ();
       i != intros.end (); ++i) {
    ACM_Introduction &intro = *(ACM_Introduction*)*i;
    _repo.consider (intro);
  }
  ProjectModel::Selection classes;
  jpm1.select ((JoinPointType)(JPT_Class|JPT_Aspect), classes);
  for (ProjectModel::Selection::iterator i = classes.begin ();
       i != classes.end (); ++i) {
    ACM_Class &cls = *(ACM_Class*)*i;
    // get the weaving plan of this intro
    JPP_Class *plan = (JPP_Class*)cls.plan ();
    // collect the intros into this class
    if (plan) {
      set<ACM_Introduction*> intros;
      for (int b = 0; b < plan->baseIntros (); b++)
        intros.insert (plan->baseIntro (b));
      for (int o = 0; o < plan->otherIntros (); o++)
        intros.insert (plan->otherIntro (o));
      // now update the repository with the intro information
      for (set<ACM_Introduction*>::iterator intro_iter = intros.begin ();
           intro_iter != intros.end (); ++intro_iter) {
        _repo.update (**intro_iter, cls);
      }
    }
  }
#else
  ModelBuilder::Selection intros;
  jpm1.select (JoinPointLoc::Introduction, intros);
  for (JoinPointModel::Selection::iterator i = intros.begin ();
       i != intros.end (); ++i) {
    JPL_Introduction &intro = *(JPL_Introduction*)*i;
    _repo.consider (intro);
  }
  ModelBuilder::Selection classes;
  jpm1.select ((JoinPointLoc::join_point_type)(JoinPointLoc::Class|
    JoinPointLoc::Aspect), classes);
  for (JoinPointModel::Selection::iterator i = classes.begin ();
       i != classes.end (); ++i) {
    JPL_Class &cls = *(JPL_Class*)*i;
    // get the weaving plan of this intro
    JPP_Class *plan = (JPP_Class*)cls.plan ();
    // collect the intros into this class
    if (plan) {
      set<JPL_Introduction*> intros;
      for (int b = 0; b < plan->baseIntros (); b++)
        intros.insert (plan->baseIntro (b));
      for (int o = 0; o < plan->otherIntros (); o++)
        intros.insert (plan->otherIntro (o));
      // now update the repository with the intro information
      for (set<JPL_Introduction*>::iterator intro_iter = intros.begin ();
           intro_iter != intros.end (); ++intro_iter) {
        _repo.update (**intro_iter, cls);
      }
    }
  }
#endif
}

void Transformer::aspect_priviledges (ModelBuilder &jpm) {
  // TODO: access to template classes

  // get all aspects from the join point model
#ifdef ACMODEL
  ProjectModel::Selection all_aspects;
  jpm.select (JPT_Aspect, all_aspects);

  // remember that these aspects should become friend of all classes
  list<CClassInfo*> friends;
  for (ProjectModel::Selection::iterator iter = all_aspects.begin ();
       iter != all_aspects.end (); ++iter) {
    ACM_Aspect &jpl = (ACM_Aspect&)**iter;
    friends.push_back (((TI_Aspect*)jpl.transform_info ())->ClassInfo());
  }
#else
  JoinPointModel::Selection all_aspects;
  jpm.select (JoinPointLoc::Aspect, all_aspects);

  // remember that these aspects should become friend of all classes
  list<CClassInfo*> friends;
  for (JoinPointModel::Selection::iterator iter = all_aspects.begin ();
       iter != all_aspects.end (); ++iter) {
    JPL_Aspect &jpl = (JPL_Aspect&)**iter;
    friends.push_back (((TI_Aspect*)jpl.transform_info ())->ClassInfo());
  }
#endif
  
  if (!friends.empty ()) {
    // get all classes and aspects from the join point model
#ifdef ACMODEL
    ProjectModel::Selection all_classes;
    jpm.select ((JoinPointType)(JPT_Aspect|JPT_Class), all_classes);
  
    // perform the transformation
    for (ProjectModel::Selection::iterator iter = all_classes.begin ();
         iter != all_classes.end (); ++iter) {
      ACM_Class &jpl = (ACM_Class&)**iter;
#else
      JoinPointModel::Selection all_classes;
      jpm.select ((JoinPointLoc::join_point_type)
        (JoinPointLoc::Aspect|JoinPointLoc::Class), all_classes);
    
      // perform the transformation
      for (JoinPointModel::Selection::iterator iter = all_classes.begin ();
           iter != all_classes.end (); ++iter) {
        JPL_Class &jpl = (JPL_Class&)**iter;
#endif
      CClassInfo *cls = ((TI_Class*)jpl.transform_info ())->class_info();

      // TODO: also ignore classes that do no belong to the project
      if (!cls->isDefined ())
        continue;

      // Don't add friend declarations in the AC namespace definitions
      CScopeInfo *scope = cls->Scope ();
      while (!scope->GlobalScope ()) {
        if (strcmp (scope->QualName (), "AC") == 0)
          break;
        scope = scope->Parent ();
      }
      if (!scope->GlobalScope ())
        continue;
      _code_weaver.declare_friends (cls, friends);
    }
  }
}
      

void Transformer::cleanup (CTranslationUnit &tunit) {
  CSemDatabase &db = tunit.db ();

  // replace keyword "aspect" with "class" and remove advice
  for (int a = 0; a < db.AspectInfos (); a++) {
    ACAspectInfo *acai = db.AspectInfo (a);

    // remove advice declarations
    for (int adv = 0; adv < acai->AdviceNodes (); adv++)
      _code_weaver.kill (acai->AdviceNode (adv));

    _code_weaver.to_class (acai);
  }
      
  // delete all pointcut definitions that are left
  for (int p = 0; p < db.PointcutInfos (); p++)
    _code_weaver.kill (db.PointcutInfo (p)->def_node ());
}


void Transformer::introductions (CTranslationUnit &tunit,
  ModelBuilder &jpm) {

  // remove the introductions from the code
#ifdef ACMODEL
  ProjectModel::Selection intros;
  jpm.select (JPT_Introduction, intros);
  for (ProjectModel::Selection::iterator i = intros.begin ();
       i != intros.end (); ++i) {
    ACM_Introduction &intro = *(ACM_Introduction*)*i;
    _code_weaver.kill (TI_Introduction::of (intro)->tree ());
  }
#else
  ModelBuilder::Selection intros;
  jpm.select (JoinPointLoc::Introduction, intros);
  for (JoinPointModel::Selection::iterator i = intros.begin ();
       i != intros.end (); ++i) {
    JPL_Introduction &intro = *(JPL_Introduction*)*i;
    _code_weaver.kill (TI_Introduction::of (intro)->tree ());
  }
#endif
  
  // now delete all slice definitions
  CSemDatabase &db = tunit.db ();
  for (int s = 0; s < db.SliceInfos (); s++) {
    ACSliceInfo *acsi = db.SliceInfo (s);
    // first all members
    for (int sm = 0; sm < acsi->members (); sm++) {
      CT_Intro *member = acsi->member (sm);
      _code_weaver.kill (member);
    }
    // now the main definition
    if (!acsi->in_advice ())
      _code_weaver.kill (acsi->def_node ());
  }
}

void Transformer::orderings (CTranslationUnit &tunit, Plan& plan,
                             ModelBuilder &jpm, bool purge) {

  // let the plan object calculate the right order
  plan.order ();

  // remove the introductions from the code
  if (purge) {
    // Iterate through order advice declarations
#ifdef ACMODEL
    ProjectModel::Selection orders;
    jpm.select (JPT_Order, orders);
    for (ProjectModel::Selection::iterator i = orders.begin ();
       i != orders.end (); ++i) {
      ACM_Order &order = *(ACM_Order*)*i;
#else
      ModelBuilder::Selection orders;
      jpm.select (JoinPointLoc::Order, orders);
      for (JoinPointModel::Selection::iterator i = orders.begin ();
         i != orders.end (); ++i) {
        JPL_Order &order = *(JPL_Order*)*i;
#endif
      TI_Order  &ti   = *TI_Order::of (order);
      _code_weaver.kill (ti.tree ());
    }
  }
}


void Transformer::advice (CTranslationUnit &tunit)
 {
   CSemDatabase &db = tunit.db ();
   for (int a = 0; a < db.AspectInfos (); a++)
    {
      ACAspectInfo *ai = db.AspectInfo (a);

      _vm++;
      // handle all advice of this aspect
      for (int i = 0; i < ai->AdviceNodes (); i++) {
	CT_AdviceDecl *ad = ai->AdviceNode (i);
	CFunctionInfo *advice_func = ((CT_FctDef*)ad->Decl ())->Object ()->
	  FunctionInfo ();
	
	// handle only real advice here
	if (strncmp (advice_func->Name (), "%a", 2) != 0)
	  continue;
	
	// don't handle inherited advice here. This is done in the base class
	if (advice_func->BaseObject ())
	  continue;
	
	_vm << ai->ClassInfo ()->QualName () << "::" 
	    << advice_func->Name () << endvm;
	
	_code_weaver.declare_function (advice_func, ad); // first phase
      }
      _vm--;
    }

   return;
 }


void Transformer::singleton_aspects (CTranslationUnit &tunit) {
  CSemDatabase &db = tunit.db ();
  for (int a = 0; a < db.AspectInfos (); a++) {
    ACAspectInfo *ai = db.AspectInfo (a);

    if (ai->is_abstract ())
      continue;

    _code_weaver.singleton (ai);
  }
}

void Transformer::join_points (CTranslationUnit &tunit,
                               ModelBuilder &jpm, Plan &plan) {
  _vm++;

  _vm << "Advicecode manipulation" << endvm;
  // Iterate through advice
#ifdef ACMODEL
  ProjectModel::Selection advice_codes;
  jpm.select (JPT_AdviceCode, advice_codes);
  for (ProjectModel::Selection::iterator i = advice_codes.begin ();
       i != advice_codes.end (); ++i) {
    ACM_AdviceCode &code = *(ACM_AdviceCode*)*i;
#else
    ModelBuilder::Selection advice_codes;
    jpm.select (JoinPointLoc::AdviceCode, advice_codes);
    for (JoinPointModel::Selection::iterator i = advice_codes.begin ();
         i != advice_codes.end (); ++i) {
      JPL_AdviceCode &code = *(JPL_AdviceCode*)*i;
#endif
    TI_AdviceCode  &ti   = *TI_AdviceCode::of (code);
    // setup ThisJoinPoint object of this advice code
    ti.this_join_point ().setup (ti.function ());
    _code_weaver.provide_tjp (ti.function (), ti.this_join_point ());
  }

  _vm << "Collecting Advice" << endvm;
  // Iterate through advice
  _vm++;

  // Create a pointcut evaluator
  PointCutContext context (jpm);
  PointCutEvaluator eva (_err, context);

  Plan::AspectContainer &aspects = plan.aspect_infos ();
  for (Plan::AspectContainer::iterator i = aspects.begin ();
    i != aspects.end (); ++i) {
    AspectInfo &aspect_info = (AspectInfo&)*i;
#ifdef ACMODEL
    ACM_Aspect &jpl_aspect = aspect_info.loc ();
#else
    JPL_Aspect &jpl_aspect = aspect_info.loc ();
#endif
    
    context.concrete_aspect (jpl_aspect);
    int index = 0; // CFlow index (each CFlow has a unique index per aspect)

    // setup thisJoinPoint for aspectOf function, if there is one
    CFunctionInfo *aspect_of_func = TI_Aspect::of (jpl_aspect)->aspectof();
    if (aspect_of_func) {
      _vm << "Setting up thisJoinPoint for aspectof" << endvm;
     	aspect_info.aspectof_this_join_point ().setup (aspect_of_func);
      _vm << "Supplying aspectof() with JoinPoint and tjp if needed" << endvm;
      _code_weaver.provide_tjp (aspect_of_func,
        aspect_info.aspectof_this_join_point ());
    }

    // handle the advice for the current aspect
    list<AdviceInfo*> advices = aspect_info.advice_infos ();

    // handle advice
    for (list<AdviceInfo*>::const_iterator i = advices.begin ();
      i != advices.end (); ++i) {
      AdviceInfo *advice_info = *i;
#ifdef ACMODEL
      ACM_AdviceCode *code = &advice_info->code ();
#else
      JPL_AdviceCode *code = &advice_info->code ();
#endif
      CFunctionInfo *adfunc = TI_AdviceCode::of (*code)->function ();

#ifdef ACMODEL
      _vm << signature (jpl_aspect) << ": "  << adfunc->Name () << endvm;
#else
      _vm << jpl_aspect.signature () << ": "  << adfunc->Name () << endvm;
#endif
      _vm++;

    	// evaluate the pointcut, even inherited
    	_vm << "Evaluate Pointcut" << endvm;
    	PointCut &pc = advice_info->pointcut ();
      Binding &binding = advice_info->binding ();
    	context.func (adfunc);
#ifdef ACMODEL
      eva.work (pc, binding, TI_PointcutExpr::of(*code->get_expr ())->tree(), JPT_Code);
#else
      eva.work (pc, binding, code->expr (), JoinPointLoc::Code);
#endif
      if (_err.severity () >= sev_error) {
  	    _vm--;
        continue;
      }

    	// Build a weaving plan for the join points
      for (PointCut::iterator iter = pc.begin ();
           iter != pc.end (); ++iter) {
        const JoinPoint &jp = *iter;

        // consider this joinpoint in the big plan
        plan.consider (jp.location (), jp.condition (), advice_info);

        // remember units for inclusion of aspect headers
        _code_weaver.add_aspect_include (jp.location (), aspect_info,
                                         AspectRef::AR_ADVICE);
      }

      // consider the cflow trigger needed for this advice in the plan
      const list<PointCut*> &trigger_pcs = pc.cflow_triggers();
      for (list<PointCut*>::const_iterator iter = trigger_pcs.begin ();
           iter != trigger_pcs.end (); ++iter, ++index) {
        PointCut *trigger_pc = *iter;
        // Consider a cflow trigger for every joinpoint is pointcut
        for (PointCut::iterator iter = trigger_pc->begin ();
             iter != trigger_pc->end (); ++iter) {
          const JoinPoint &jp = *iter;

          // consider this joinpoint in the big plan
          plan.consider (jp.location (), CFlow (advice_info, index));
          
          // remember units for inclusion of aspect headers
          _code_weaver.add_aspect_include (jp.location (), aspect_info,
                                           AspectRef::AR_DECL);
          
        }
      }
      
      // update the project repository
      if (getenv ("ACNEWREPO") == 0)
        _repo.update (*advice_info, pc);

      _vm--;
    }
    
    // add order advices to plan
#ifdef ACMODEL
    list<ACM_Order*> orders;
    collect_orders (jpl_aspect, orders);
    for (list<ACM_Order*>::iterator i = orders.begin ();
#else
    list<JPL_Order*> orders;
    jpl_aspect.collect_orders (orders);
    for (list<JPL_Order*>::iterator i = orders.begin ();
#endif
    
      i != orders.end (); ++i) {
      OrderInfo *oi = plan.addOrder (jpl_aspect, **i);
      oi->analyze_exprs (_err, jpm);
    }
    
    ACAspectInfo *acai = TI_Aspect::of (jpl_aspect)->aspect_info();
    _code_weaver.invocation_functions (acai,
      aspect_info.ifct_decls (_code_weaver.problems ()),
      aspect_info.ifct_defs (_code_weaver.problems ()));
  }
  _vm--;

  _vm << "Aspect ordering ..." << endvm;
  orderings(tunit, plan, jpm, true);

  // now do the final checks on the accumulated plan
  _vm << "Final checks before weaving code join points" << endvm;
  plan.check ();

  // Don't weave if there were errors in the planning phase
  if (_err.severity () >= sev_error)
    return;

  char *repo_file = getenv ("ACNEWREPO");
  if (repo_file) {
#ifdef _MSC_VER
    if (!_access (repo_file, 04)) {
#else
    if (!access (repo_file, R_OK)) {
#endif // _MSC_VER

#ifndef ACMODEL
      // TODO: implement loading and merging for AC model
      _vm << "Opening new project repository '" << repo_file << "'" << endvm;
      int fd = SysCall::open_excl (repo_file, O_RDWR, &_err);
      JoinPointModel ljpm;
      JoinPointModel::FileOpResult res = ljpm.load (fd, repo_file);
      if (res == JoinPointModel::JPM_LOAD_ERR) {
        _err << sev_error << "project repository '" << repo_file << "' cannot be opened"
        " or is invalid" << endMessage;
        return;
      }
      if (res == JoinPointModel::JPM_VERSION) {
        _err << sev_warning << "project file version differs from ac++ version"
            << endMessage;
      }
      ljpm.reconcile (jpm);
      lseek (fd, 0, SEEK_SET);
      if (ftruncate (fd, 0) != 0)
        perror ("truncate");
      ljpm.save (fd, repo_file);
      SysCall::close_excl (fd, &_err);
#endif
    }
    else {
#ifdef ACMODEL
      XmlModelWriter writer;
      writer.write (jpm, repo_file);
#else
      int fd = SysCall::create_excl (repo_file, 0600, &_err);
      jpm.save (fd, repo_file);
      SysCall::close_excl  (fd, &_err);
#endif
    }
  }

  _vm << "Type Check Functions" << endvm;
  _vm++;
  const TypeCheckSet &checks_false = plan.type_checks_false ();
  for (TypeCheckSet::const_iterator iter = checks_false.begin ();
       iter != checks_false.end (); ++iter) {
    _vm << "check for " << iter->second << " in "
        << iter->first->QualName () << " is false" << endvm;
    _code_weaver.type_check (iter->first, iter->second, false);
  }
  const TypeCheckSet &checks_true = plan.type_checks_true ();
  for (TypeCheckSet::const_iterator iter = checks_true.begin ();
       iter != checks_true.end (); ++iter) {
    _vm << "check for " << iter->second << " in "
        << iter->first->QualName () << " is true" << endvm;
    _code_weaver.type_check (iter->first, iter->second, true);
  }
  _vm--;
  
   _vm << "Call Join Points" << endvm;
   _vm++;
   for (int i = 0; i < plan.call_jp_plans (); i++) {
      JPP_Code &jp_plan = plan.call_jp_plan (i);
#ifdef ACMODEL
      ACM_Call &jp_loc = plan.call_jp_loc (i);
      _vm << signature (jp_loc) << endvm;
#else
      JPL_MethodCall &jp_loc = plan.call_jp_loc (i);
      _vm << jp_loc.signature () << endvm;
#endif

      // handle call joinpoint itself
      _code_weaver.call_join_point (&jp_loc, jp_plan);
   }
   _vm--;

   _vm << "Execution Join Points" << endvm;
   _vm++;
   for (int i = 0; i < plan.exec_jp_plans (); i++)
    {
      JPP_Code &jp_plan = plan.exec_jp_plan (i);
#ifdef ACMODEL
      ACM_Execution &jp_loc = plan.exec_jp_loc (i);
      _vm << signature (jp_loc) << endvm;
#else
      JPL_Method &jp_loc = plan.exec_jp_loc (i);
      _vm << jp_loc.signature () << endvm;
#endif

      // handle exec joinpoint itself
      _code_weaver.exec_join_point (&jp_loc, jp_plan);
    }
   _vm--;

   _vm << "Construction Join Points" << endvm;
   _vm++;
   for (int i = 0; i < plan.cons_jp_plans (); i++)
    {
      JPP_Code &jp_plan = plan.cons_jp_plan (i);
#ifdef ACMODEL
      ACM_Construction &jp_loc = plan.cons_jp_loc (i);
      _vm << signature (jp_loc) << endvm;
#else
      JPL_Construction &jp_loc = plan.cons_jp_loc (i);
      _vm << jp_loc.signature () << endvm;
#endif

      // handle construction joinpoint itself
      _code_weaver.cons_join_point (&jp_loc, jp_plan);
    }
   _vm--;

   _vm << "Destruction Join Points" << endvm;
   _vm++;
   for (int i = 0; i < plan.dest_jp_plans (); i++)
    {
      JPP_Code &jp_plan = plan.dest_jp_plan (i);
#ifdef ACMODEL
      ACM_Destruction &jp_loc = plan.dest_jp_loc (i);
      _vm << signature (jp_loc) << endvm;
#else
      JPL_Destruction &jp_loc = plan.dest_jp_loc (i);
      _vm << jp_loc.signature () << endvm;
#endif

      // handle destruction joinpoint itself
      _code_weaver.dest_join_point (&jp_loc, jp_plan);
    }
   _vm--;

   _vm--;

   _vm << "Aspect Includes ..." << endvm;
   _code_weaver.aspect_includes (_project);
 }