File: ClangTransformInfo.h

package info (click to toggle)
aspectc%2B%2B 1%3A2.0%2Bsvn20160329-1~bpo8.0
  • links: PTS
  • area: main
  • in suites: jessie-backports
  • size: 16,132 kB
  • sloc: cpp: 106,806; ansic: 7,644; sh: 2,188; makefile: 1,269; pascal: 634; python: 402; xml: 349
file content (2362 lines) | stat: -rw-r--r-- 89,550 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
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
// 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 __ClangTransformInfo_h__
#define __ClangTransformInfo_h__

#include "ACModel/Elements.h"
#include "ACToken.h"
#include "ACFileID.h"
#include "ThisJoinPoint.h"
#include "PointCutExpr.h"
#include "SyntacticContext.h"
#include "CFlow.h"
#include "WeaverBase.h"
#include "ClangAdjustedTypePrinter.h"

#include "clang/Basic/Version.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Type.h"
#if !(CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && !defined(CLANG_VERSION_PATCHLEVEL)) && \
  !(CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && CLANG_VERSION_PATCHLEVEL == 2)
#include "clang/AST/ExprCXX.h"
#endif
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/CXXInheritance.h"


class TI_CodeAdvice : public ModelTransformInfo {
  // Condition is located here instead of in the AdviceInfo object because
  // there are cases where the conditions differ but the advice info
  // is the same.
  const Condition *_condition;
  AdviceInfo *_advice_info;
public:
  void set_condition (const Condition *c) { _condition = c; }
  const Condition *get_condition () const { return _condition; }
  void set_advice_info (AdviceInfo *ai) { _advice_info = ai; }
  AdviceInfo *get_advice_info () const { return _advice_info; }

  static TI_CodeAdvice *of (const ACM_CodeAdvice &loc) {
    return static_cast<TI_CodeAdvice*>(loc.transform_info ());
  }
};

class TransformInfo : public ModelTransformInfo {
public:
  virtual ModelNode &jpl () = 0;
  virtual clang::Decl *decl () const = 0;

  static inline const TransformInfo *of (const ModelNode &loc);
  static inline clang::Decl *decl (const ModelNode &loc);
  static inline Puma::Location location (const ModelNode &loc);

  static const WeavePos &get_pos_after_token (clang::SourceLocation loc,
      WeaverBase &wb, WeavePos::Pos pos = WeavePos::WP_AFTER) {
    return wb.get_pos_after_loc(loc, pos);
  }

  // parameter 'is_id': if true, the searched string is only replaced if the
  //                    character before and after the matching substring is
  //                    not in an identifier.
  //                    (example: don't replace 'shortcut' by 'short intcut')
  static void replace_in_string(std::string& subject, const std::string& search,
      const std::string& replace, bool is_id = false) {
    size_t pos = 0;
    while ((pos = subject.find(search, pos)) != std::string::npos) {
      bool id_before = (pos > 0 && (subject[pos - 1] == '_' || std::isalpha(subject[pos - 1])));
      bool id_after  = ((pos + search.length () < subject.length()) &&
          (subject[pos + search.length()] == '_' || std::isalnum(subject[pos + search.length()])));
      if (!is_id || (!id_before && !id_after)) {
        subject.replace(pos, search.length(), replace);
        pos += replace.length();
      }
      else
        pos += search.length();
    }
  }

  static bool needs_this (clang::FunctionDecl *func) {
    if (clang::CXXMethodDecl *m = llvm::dyn_cast<clang::CXXMethodDecl>(func))
      if (!m->isStatic())
        return true;

    return false;
  }

  // TODO: duplicate of function in ClangModelBuilder.cc
  template <typename T>
  static bool isTemplateInstantiation(T node) {
    return (node->getTemplateSpecializationKind() ==
                clang::TSK_ImplicitInstantiation ||
            node->getTemplateSpecializationKind() ==
                clang::TSK_ExplicitInstantiationDefinition);
  }

  // TODO: duplicate of function in ClangModelBuilder.cc
  static bool inside_template_instance (clang::DeclContext *scope) {
    if (llvm::isa<clang::TranslationUnitDecl>(scope))
      return false;

    if (clang::FunctionDecl *FD = llvm::dyn_cast<clang::FunctionDecl>(scope))
      if (isTemplateInstantiation(FD))
        return true;

    if (clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(scope))
      if (isTemplateInstantiation(VD))
        return true;

    if (clang::CXXRecordDecl *RD = llvm::dyn_cast<clang::CXXRecordDecl>(scope))
      if (isTemplateInstantiation(RD))
        return true;

    return inside_template_instance(scope->getParent());
  }

};

inline const TransformInfo *TransformInfo::of (const ModelNode &loc) {
  return loc.transform_info () ? (TransformInfo*)loc.transform_info () : 0;
}
//inline CTree *TransformInfo::tree (const ModelNode &loc) {
//  return loc.transform_info () ?
//    ((TransformInfo*)loc.transform_info ())->tree () : 0;
//}
//inline Unit *TransformInfo::unit (const ModelNode &loc) {
//  return loc.transform_info () ?
//    ((TransformInfo*)loc.transform_info ())->unit () : 0;
//}
inline clang::Decl *TransformInfo::decl (const ModelNode &loc) {
  return loc.transform_info () ?
    ((TransformInfo*)loc.transform_info ())->decl () : 0;
}
inline Puma::Location TransformInfo::location (const ModelNode &loc) {
  return Puma::Location (); // FIXME: implement for Clang
}



class TI_Type : public TransformInfo {
  clang::QualType _type;
public:
  void type (clang::QualType ti) { _type = ti; }
  clang::QualType type () const { return _type; }

  virtual clang::Decl *decl () const { return 0; }

  bool is_const () const { return _type.isConstQualified (); }
  bool is_reference () const { return _type.getTypePtr ()->isReferenceType (); }

  // Called from ClangModelBuilder::register_type(...) and ClangModelBuilder::register_arg(...)
  // and used for signature.
  static string name (clang::ASTContext &ctx, clang::QualType type_info) {
    // Get the type as a string, looking through template parameters
    // and typedefs and printing anonymous as unnamed:
    string type_str = TI_Type::get_type_text(type_info, &ctx, "?", TSEF_DONOTCHANGE, false, TSEF_DISABLE, true, true, false);

    int last = type_str.length() - 1;
    if (type_str[last] == '?') last--;
    while(type_str[last] == ' ') last--;

    return type_str.substr (0, last + 1);
  }

private:
  // Returns a printing policy setup according to given flags for adjusted type printing
  static inline clang::PrintingPolicy get_preset_printing_policy(const clang::ASTContext* ctx,
                                                                 bool unnamed) {
    // TODO: always create, setup and use own printing policy?
    clang::PrintingPolicy policy = ctx
        ? ctx->getPrintingPolicy()
        : clang::PrintingPolicy(clang::LangOptions());

    // Ensure some policy properties:
    policy.SuppressSpecifiers = false;
    policy.SuppressTag = false;
    // Suppress <anonymous> only if we do not want to replace it with
    // <unnamed> later
    policy.SuppressUnwrittenScope = !unnamed;
    policy.AnonymousTagLocations = unnamed;

    return policy;
  }

  // Unifies the types in the given string according to the given flags.
  static inline string& get_with_unified_types(string& input, bool unnamed) {
    if(unnamed) {
      // If requested, replace occurences of anonymous-variants with "<unnamed>":
      const char* unnamed_namespace_str =
#if (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && !defined(CLANG_VERSION_PATCHLEVEL)) || \
  (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && CLANG_VERSION_PATCHLEVEL == 2)
          "<anonymous>"
#else // C++ 11 interface
          "(anonymous namespace)"
#endif
      ;
      replace_in_string(input, unnamed_namespace_str, "<unnamed>");
    }

    // Unify some types: (taken from fix_type_in_signature(...)
    // and fixed "long int" -> "long int int" bug)
    replace_in_string(input, "long", "long int", true);
    replace_in_string(input, "long int long int", "long long int", true);
    replace_in_string(input, "long int int", "long int", true);
    replace_in_string(input, "long int double", "long double", true);
    replace_in_string(input, "short", "short int", true);
    replace_in_string(input, "short int int", "short int", true);
    // TODO: Is the following line necessary?
    replace_in_string(input, "<anonymous namespace>::", unnamed ? "<unnamed>::" : "");
    replace_in_string(input, "*restrict", "*");
    replace_in_string(input, " restrict", " ");

    return input;
  }

public:

  /** Returns the textual representation of a declaration name.
      *  \param decl The declaration
      *  \param ctx The ASTContext
      *  \param absolute_qualified Add all nested name specifiers as well as the
      *   root qualifier (ENABLE), do not change nested name specifiers (DONOTCHANGE)
      *   or remove all nested name specifiers (DISABLE).
      *  \param keep_typedef If the type is typedef'd, keep the typedef and return
      *   the typedef'd type (and not the underlying type)
      *  \param elaborated_type_spec Add elaborated type specifier before
      *                              class, union, and enumeration types.
      *  \param unnamed Do not suppress printing of unwritten scope and convert printed
      *   namespaces to '<unnamed>'
      *  \param remove_attributes Removes occurrences of attributes
      *    */
  static string get_decl_name_text(const clang::NamedDecl* decl,
                              TriStateEnableFeature absolute_qualified = TSEF_DONOTCHANGE,
                              bool keep_typedef = true,
                              TriStateEnableFeature elaborated_type_spec = TSEF_DONOTCHANGE,
                              bool unnamed = false,
                              bool remove_attributes = false
  ) {
    clang::PrintingPolicy policy = get_preset_printing_policy(&decl->getASTContext(), unnamed);
    string string_buffer;
    llvm::raw_string_ostream stream(string_buffer);

    AdjustedTypePrinter(policy, absolute_qualified, keep_typedef, elaborated_type_spec,
                               remove_attributes, false, 0)
        .adjusted_NamedDecl_printQualifiedName(decl, stream);
    return get_with_unified_types(stream.str(), unnamed);
  }

  /** Returns the textual representation of a list of template arguments.
     *  \param template_args The list of template arguments
     *  \param ctx The ASTContext
     *  \param absolute_qualified Add all nested name specifiers as well as the
     *   root qualifier (ENABLE), do not change nested name specifiers (DONOTCHANGE)
     *   or remove all nested name specifiers (DISABLE).
     *  \param keep_typedef If the type is typedef'd, keep the typedef and return
     *   the typedef'd type (and not the underlying type)
     *  \param elaborated_type_spec Add elaborated type specifier before
     *                              class, union, and enumeration types.
     *  \param unnamed Do not suppress printing of unwritten scope and convert printed
     *   namespaces to '<unnamed>'
     *  \param remove_attributes Removes occurrences of attributes
     *    */
  static string get_templ_arg_list_text(const clang::TemplateArgumentList& template_args,
                                           const clang::ASTContext* ctx,
                                           TriStateEnableFeature absolute_qualified = TSEF_ENABLE,
                                           bool keep_typedef = true,
                                           TriStateEnableFeature elaborated_type_spec = TSEF_DONOTCHANGE,
                                           bool unnamed = false,
                                           bool remove_attributes = false
  ) {
    clang::PrintingPolicy policy = get_preset_printing_policy(ctx, unnamed);
    string string_buffer;
    llvm::raw_string_ostream stream(string_buffer);

    AdjustedTypePrinter(policy, absolute_qualified, keep_typedef, elaborated_type_spec,
                           remove_attributes, false, ctx)
        .adjusted_PrintTemplateArgumentList(stream,
                                            template_args.data(),
                                            template_args.size());
    return get_with_unified_types(stream.str(), unnamed);
  }

  /** Returns the textual representation of template argument.
    *  \param temp_arg The template argument
    *  \param ctx The ASTContext
    *  \param absolute_qualified Add all nested name specifiers as well as the
    *   root qualifier (ENABLE), do not change nested name specifiers (DONOTCHANGE)
    *   or remove all nested name specifiers (DISABLE).
    *  \param keep_typedef If the type is typedef'd, keep the typedef and return
    *   the typedef'd type (and not the underlying type)
    *  \param elaborated_type_spec Add elaborated type specifier before
    *                              class, union, and enumeration types.
    *  \param unnamed Do not suppress printing of unwritten scope and convert printed
    *   namespaces to '<unnamed>'
    *  \param remove_attributes Remove occurrences of attributes
    *    */
  static string get_templ_arg_text(const clang::TemplateArgument& temp_arg,
                       const clang::ASTContext* ctx,
                       TriStateEnableFeature absolute_qualified = TSEF_ENABLE,
                       bool keep_typedef = true,
                       TriStateEnableFeature elaborated_type_spec = TSEF_DONOTCHANGE,
                       bool unnamed = false,
                       bool remove_attributes = false
  ) {
    clang::PrintingPolicy policy = get_preset_printing_policy(ctx, unnamed);

    string temp_arg_string_buffer;
    llvm::raw_string_ostream temp_arg_stream(temp_arg_string_buffer);
    AdjustedTypePrinter(policy, absolute_qualified, keep_typedef, elaborated_type_spec,
                           remove_attributes, false, ctx)
        .adjusted_TemplateArgument_print(temp_arg, temp_arg_stream);
    return get_with_unified_types(temp_arg_stream.str(), unnamed);
  }

  /** Returns the textual representation of a type for the use in woven code.
      *  \param type The type
      *  \param ctx The ASTContext
      *  \param var_name If this is unequal zero, the type is returned
      *   together with the given variable name
      *   (e.g. void(*)(int) is returned as void(*example)(int) if "example" is
      *   provided as variable name.)
      *    */
  static string get_type_code_text(clang::QualType type, const clang::ASTContext* ctx,
       const char *var_name = (const char*)0
  ) {
    return get_type_text(type,
                         ctx,
                         var_name,
                         TSEF_ENABLE, // make type absolute
                         true, // keep typedef if possible (to keep woven code portable)
                         TSEF_DONOTCHANGE,
                         false, // No unwritten scopes
                         true, // Remove __attribute__s (TODO: There are cases where we can keep them?)
                         false); // Do not make signature parameter type adjustments (because the result is not used for signatures)
  }

  /** Returns the textual representation of a type for the use in AspectC++ signatures.
      *  \param type The type
      *  \param ctx The ASTContext
      *  \param var_name If this is unequal zero, the type is returned
      *   together with the given variable name
      *   (e.g. void(*)(int) is returned as void(*example)(int) if "example" is
      *   provided as variable name.)
      *  \param as_parameter_signature_type Return the type as adjusted for use in the
      *   signature of a function, decaying array and function types and removing top-level
      *   cv-qualifiers. This should be true if the type is associated to a parameter of a function.
      *    */
  static string get_type_sig_text(clang::QualType type, const clang::ASTContext* ctx,
      const char *var_name = (const char*)0,
      bool as_parameter_signature_type = false
  ) {
    // TODO: Common question: Always remove elaborated in signature?
    return get_type_text(type,
                         ctx,
                         var_name,
                         TSEF_DONOTCHANGE, // Do not change nested name specifiers
                         false, // Remove typedefs to make matching independent of typedefs
                         TSEF_DISABLE, // AspectC++ signatures do not contain elaborated specifiers. TODO: Is this always true?
                         false, // Do not add unwritten scopes
                         true, // remove __attribute__s
                         as_parameter_signature_type);
  }

  /** Returns the textual representation of a type.
    *  \param type The type
    *  \param ctx The ASTContext
    *  \param var_name If this is unequal zero, the type is returned
    *   together with the given variable name
    *   (e.g. void(*)(int) is returned as void(*example)(int) if "example" is
    *   provided as variable name.)
    *  \param absolute_qualified Add all nested name specifiers as well as the
    *   root qualifier (ENABLE), do not change nested name specifiers (DONOTCHANGE)
    *   or remove all nested name specifiers (DISABLE).
    *  \param keep_typedef If the type is typedef'd, keep the typedef and return
    *   the typedef'd type (and not the underlying type)
    *  \param elaborated_type_spec Add elaborated type specifier before
    *                              class, union, and enumeration types.
    *  \param unnamed Do not suppress printing of unwritten scope and convert printed
    *   namespaces to '<unnamed>'
    *  \param remove_attributes Remove occurrences of attributes
    *  \param as_parameter_signature_type Return the type as adjusted for use in the
    *   signature of a function, decaying array and function types and removing top-level
    *   cv-qualifiers.
    *    */
  static string get_type_text(clang::QualType type, const clang::ASTContext* ctx,
    const char *var_name = (const char*)0,
    TriStateEnableFeature absolute_qualified = TSEF_ENABLE,
    bool keep_typedef = true,
    TriStateEnableFeature elaborated_type_spec = TSEF_DONOTCHANGE,
    bool unnamed = false,
    bool remove_attributes = false,
    bool as_parameter_signature_type = false
  ) {

    // Check invalid combinations:
    assert(!(keep_typedef && elaborated_type_spec == TSEF_ENABLE) && "A typedef type can not be "
           "printed together with an elaborated type specifier.");
    assert(!(as_parameter_signature_type && !ctx) && "as_parameter_signature_type flag needs an ASTContext");
    // TODO: more!?

    clang::PrintingPolicy policy = get_preset_printing_policy(ctx, unnamed);


    // Start printing:
    string resulting_type_text = var_name ? var_name : "";

    string type_string_buffer;
    llvm::raw_string_ostream type_stream(type_string_buffer);
    AdjustedTypePrinter(policy, absolute_qualified, keep_typedef, elaborated_type_spec,
                        remove_attributes, as_parameter_signature_type, ctx)
        .print(type, type_stream, resulting_type_text);
    resulting_type_text = type_stream.str();

    return get_with_unified_types(resulting_type_text, unnamed);
  }

  // This static member-function checks whether the expression is a lvalue or
  // xvalue and if yes returns a appropriate reference-QualType while using
  // the given QualType as guideline. If not it returns a copy of the given type.
  static inline clang::QualType get_reference_type_if_necessary(const clang::QualType& type,
      const clang::Expr* const expression, const clang::ASTContext& context) {
    if(expression->isLValue() == true) {
      return context.getLValueReferenceType(type);
    }
    else if(expression->isXValue() == true) {
      return context.getRValueReferenceType(type);
    }
    else {
      return type;
    }
  }


  static const TI_Type *of (const ACM_Type &loc) {
    return static_cast<TI_Type*>(loc.transform_info ());
  }
};

class TI_Namespace : public TransformInfo {
  // pointer to the Clang namespace object (for transformation)
  clang::NamespaceDecl *_decl;
public:
  void decl (clang::NamespaceDecl *n) { _decl = n; }
  virtual clang::NamespaceDecl *decl () const { return _decl; }
};

class TI_Class : public TransformInfo {
  clang::RecordDecl *_decl;
  clang::SourceLocation _lbrace_loc;
  bool _has_replaced_arrays;

  static void get_member_contexts (const clang::RecordDecl *decl,
      list<ClangSyntacticContext> &member_contexts) {

    for (clang::RecordDecl::field_iterator i = decl->field_begin ();
        i != decl->field_end (); ++i) {

      // Only certain members are delivered.
      // If this is not the right choice for all use case, add filter flags to
      // the argument list of this function
      clang::FieldDecl *attr = *i;
      if (attr->getNameAsString ().empty ()) {
        const clang::RecordType *UT = attr->getType ()->getAsUnionType ();
        if (UT) { // members of anonymous unions in a record are also record members
          get_member_contexts (UT->getDecl (), member_contexts);
        }
        continue;
      }
//      if (attr->isStatic () || attr->isAnonymous () || attr->EnumeratorInfo ())
//        continue;

      member_contexts.push_back (ClangSyntacticContext (attr));
    }
  }

public:
  TI_Class () : _decl (0), _has_replaced_arrays( false ) {}

  bool valid () const { return _decl != 0; }

  void decl (clang::RecordDecl *c) { _decl = c; }
  virtual clang::RecordDecl *decl () const { return _decl; }

  void set_lbrace_loc(clang::SourceLocation loc) { _lbrace_loc = loc; }

  SyntacticContext get_def_context () const { return SyntacticContext (_decl); }

  void get_member_contexts (list<ClangSyntacticContext> &member_contexts) const {
    get_member_contexts (_decl, member_contexts);
  }

  enum SMKind { CONSTRUCTOR, COPY_CONSTRUCTOR, DESTRUCTOR };
  bool may_have_implicit (SMKind kind) {
    return may_have_implicit (kind, _decl);
  }

  bool may_have_implicit (SMKind kind, const clang::RecordDecl *decl) {
    const clang::CXXRecordDecl *d = llvm::cast<clang::CXXRecordDecl>(decl);
    if (!d)
      return false;
    if (kind == CONSTRUCTOR || kind == COPY_CONSTRUCTOR) {
      for (clang::CXXRecordDecl::ctor_iterator i = d->ctor_begin ();
          i != d->ctor_end (); ++i) {
        clang::CXXConstructorDecl *cd = *i;
        if (kind == CONSTRUCTOR && cd->isDefaultConstructor () &&
            cd->getAccess () == clang::AS_private)
          return false;
        if (kind == COPY_CONSTRUCTOR && cd->isCopyConstructor () &&
            cd->getAccess () == clang::AS_private)
          return false;
      }
    }
    else { // destructor
      const clang::CXXDestructorDecl *dd = d->getDestructor ();
      if (dd && kind == DESTRUCTOR && dd->getAccess () == clang::AS_private)
        return false;
    }

    for (clang::CXXRecordDecl::base_class_const_iterator i = d->bases_begin ();
        i != d->bases_end (); ++i) {
      const clang::CXXRecordDecl *bd = (*i).getType ()->getAsCXXRecordDecl ();
      if (bd && !may_have_implicit (kind, bd))
        return false;
    }

    for (clang::CXXRecordDecl::field_iterator i = d->field_begin ();
        i != d->field_end (); ++i) {
      const clang::FieldDecl *attr = *i;
      const clang::CXXRecordDecl *rd = attr->getType ()->getAsCXXRecordDecl ();
      if (rd && !may_have_implicit (kind, rd))
        return false;
    }
    return true;
  }

  // remember if planing found advice for the builtin_copy constructor
  void remember_builtin_copyconstructor_advice() {
    // if the constructor advice can be woven (parallel check to begining of CodeWeaver::gen_special_member_function)
    if( may_have_implicit( TI_Class::COPY_CONSTRUCTOR ) )
      _has_replaced_arrays = true; // remember that replacement will be done, to assure it is considerd when weaving on other joinpoints (array access)
  }
  bool has_replaced_arrays() const { return _has_replaced_arrays; }

  // return the position behind the opening bracket of the class body
  const WeavePos &body_start_pos (WeaverBase &wb) const {
    // TODO: iterating over all decls and finding the one with the smallest
    // location is a terribly comlicated solution. However, I haven't found
    // a way to get the location of the opening bracket.
#if 0
    clang::SourceLocation min_loc;
    for (clang::DeclContext::decl_iterator i = _decl->decls_begin ();
        i != _decl->decls_end (); ++i) {
      clang::SourceLocation cur_loc = (*i)->getLocStart();
      if ((*i)->isImplicit () || !cur_loc.isValid ())
        continue;
      if (!min_loc.isValid () || (cur_loc < min_loc))
          min_loc = (*i)->getLocStart();
    }
    if (min_loc.isValid ())
      return wb.weave_pos(min_loc, WeavePos::WP_BEFORE);
    else
      return wb.weave_pos(_decl->getRBraceLoc(), WeavePos::WP_BEFORE);
#endif
    assert (_lbrace_loc.isValid());
    return wb.weave_pos(_lbrace_loc.getLocWithOffset(1), WeavePos::WP_AFTER);
  }

  // return the position in front of the closing bracket of the class body
  const WeavePos &body_end_pos (WeaverBase &wb) const {
    return wb.weave_pos(_decl->getRBraceLoc(), WeavePos::WP_BEFORE);
  }

  // return the position of the first token of the class definition
  const WeavePos &objdecl_start_pos (WeaverBase &wb) const {
    return wb.weave_pos (_decl->getLocStart(), WeavePos::WP_BEFORE);
  }

  // return the position after the ";" of the class definition
  const WeavePos &objdecl_end_pos (WeaverBase &wb) const {
    // FIXME: This relies on the lack of spaces between the closing '}' and ';'.
    return wb.weave_pos (_decl->getLocEnd().getLocWithOffset(1), WeavePos::WP_AFTER);
  }

  // check whether this is a class and not a struct
  bool is_class () const { return _decl->isClass(); }

  // check whether this is a struct (more restrictive than 'is_class')
  bool is_struct () const { return _decl->isStruct(); }

  // check whether the class is defined (=has a body) and not only declared
  bool is_defined () const { return _decl->isCompleteDefinition (); }

  // checks whether this class is a template *instance*
  bool is_template_instance () const {
    const clang::CXXRecordDecl *d = llvm::cast<clang::CXXRecordDecl>(_decl);
    return d && isTemplateInstantiation (d);
  }

  // check whether the class is defined in a extern "C" block
  bool is_extern_c () const {
    return is_extern_c (_decl);
  }

  static bool is_extern_c (clang::RecordDecl *d) {
    clang::DeclContext *dc = d;
    while (dc->getDeclKind() != clang::Decl::TranslationUnit) {
      if (dc->getDeclKind() == clang::Decl::LinkageSpec)
        return clang::cast<clang::LinkageSpecDecl>(dc)->getLanguage() ==
            clang::LinkageSpecDecl::lang_c;
      dc = dc->getParent();
    }
    return false;
    // Future clang versions will support this: return _decl->isExternCContext ();
    // TODO: current version support now: return d->isExternCContext();
  }

  // checks whther the class is defined within a template instance
  bool is_in_template_instance () const { return inside_template_instance(_decl); }

  static string name(clang::RecordDecl *ci) {
    return TI_Type::get_decl_name_text(ci, TSEF_DISABLE, false, TSEF_DISABLE, false, true);
  }

  static TI_Class *of (const ACM_Class &loc) {
    return static_cast<TI_Class*>(loc.transform_info ());
  }
};

class TI_Aspect : public TI_Class {
public:

  clang::FunctionDecl *aspectof () const {

    clang::CXXRecordDecl *d = llvm::cast<clang::CXXRecordDecl>(decl());
    for (clang::CXXRecordDecl::decl_iterator di = d->decls_begin(),
                                             de = d->decls_end();
         di != de; ++di) {
      clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl> (*di);
      if (!nd)
        continue;
      std::string name = nd->getNameAsString();
      if (name != "aspectof" && name != "aspectOf")
        continue;
      if (nd->getKind () == clang::Decl::FunctionTemplate)
        return llvm::dyn_cast<clang::FunctionTemplateDecl>(nd)->getTemplatedDecl ();
      else if (nd->getKind () == clang::Decl::CXXMethod)
        return llvm::dyn_cast<clang::CXXMethodDecl>(nd);
    }
    return 0;
  }

  static const TI_Aspect *of (const ACM_Aspect &loc) {
    return static_cast<TI_Aspect*>(loc.transform_info ());
  }

};

class TI_Function : public TransformInfo {
  mutable vector<ClangSyntacticContext> _contexts;
  clang::FunctionDecl *_decl;
public:

  const vector<ClangSyntacticContext> &syntactic_contexts () const {
    if (_contexts.size() == 0) {
      // Store a pointer to each declaration and the definition (if one exists)
      for (clang::FunctionDecl::redecl_iterator ri = _decl->redecls_begin(),
                                                re = _decl->redecls_end();
           ri != re; ++ri)
        _contexts.push_back(ClangSyntacticContext(*ri));
    }
    return _contexts;
  }

  void decl (clang::FunctionDecl *c) { _decl = c; }
  virtual clang::FunctionDecl *decl () const { return _decl; }
  void add_decl (clang::FunctionDecl *c) {
    _contexts.push_back(ClangSyntacticContext(c));
  }

  static string name(clang::FunctionDecl *func_info) {
    string out;

    clang::CXXConversionDecl *conv =
        llvm::dyn_cast_or_null<clang::CXXConversionDecl>(func_info);
    if (conv) {
      clang::ASTContext &ctx = func_info->getASTContext();
      clang::QualType t = conv->getConversionType ();
      out = "operator ";
      out += TI_Type::get_type_sig_text(t, &ctx, 0, true);
      return out;
    }

    out = func_info->getNameAsString();
    // Add a space after "operator" for Puma compatibility.
    if (out.size() > 8 && llvm::StringRef(out).startswith("operator") &&
        out[8] != ' ' && out[8] != '_' && !isalnum(out[8]))
      out.insert(out.begin() + 8, ' ');

    clang::FunctionTemplateSpecializationInfo *ftsi =
        func_info->getTemplateSpecializationInfo ();
    if (ftsi) {
      if (out[out.length() - 1] == '<') // e.g. operator <<
        out += " ";
      out += TI_Type::get_templ_arg_list_text(*ftsi->TemplateArguments,
                                              &func_info->getASTContext(),
                                              TSEF_DONOTCHANGE,
                                              false,
                                              TSEF_DISABLE,
                                              false,
                                              true);
    }
    return out;
  }

  static string signature(clang::FunctionDecl *func_info) {
    std::string str;
    llvm::raw_string_ostream out(str);
    clang::ASTContext &ctx = func_info->getASTContext();

    out << '(';
    for (clang::FunctionDecl::param_iterator i = func_info->param_begin(),
                                             e = func_info->param_end();
         i != e; ++i) {
      if (i != func_info->param_begin())
        out << ',';
      out << TI_Type::get_type_sig_text((*i)->getType(), &ctx, 0, true);
    }
    out << ')';

    // add CV qualifiers
    if (clang::CXXMethodDecl *m =
            llvm::dyn_cast<clang::CXXMethodDecl>(func_info)) {
      if (m->isConst())
        out << " const";
      if (m->isVolatile())
        out << " volatile";
    }

    return name(func_info) + out.str ();
  }

  bool is_const () const {
    clang::CXXMethodDecl *m = llvm::dyn_cast<clang::CXXMethodDecl>(_decl);
    return m && m->isConst ();
  }

  bool is_conversion_operator () const {
    return (llvm::dyn_cast_or_null<clang::CXXConversionDecl>(_decl) != 0);
  }

  static const TI_Function *of (const ACM_Function &loc) {
    return static_cast<TI_Function*>(loc.transform_info ());
  }
};

class TI_Variable : public TransformInfo {
  clang::DeclaratorDecl *_decl;
public:
  TI_Variable () : _decl (0) {}

  void decl (clang::DeclaratorDecl *oi) { _decl = oi; }
  virtual clang::DeclaratorDecl *decl () const { return _decl; }
public:
  static const TI_Variable *of (const ACM_Variable &loc) {
    return static_cast<TI_Variable*>(loc.transform_info ());
  }
};


class TI_Arg : public TransformInfo {
  clang::QualType _type;
public:
  void type (clang::QualType ti) { _type = ti; }
  clang::QualType type () const { return _type; }

  virtual clang::Decl *decl () const { return 0; }

  static const TI_Arg *of (const ACM_Arg &loc) {
    return static_cast<TI_Arg*>(loc.transform_info ());
  }
};

class TI_Code : public TransformInfo {
  bool _is_planned;
  bool _has_implicit;
  CFlowList _triggers;
public:
  TI_Code() : _is_planned( false ), _has_implicit( false ) {};

  // remember if this joinpoint is planned for weaving
  // ( decisions for other joinpoints depend on this info )
  void remember_planned() { _is_planned = true; }
  bool is_planned() { return _is_planned; }

  // remember implicit joinpoints needing to be considered
  void remember_implicit() { _has_implicit = true; }
  bool has_implicit_joinpoints() { return _has_implicit; }

  // consider a necessary cflow trigger at this join point
  bool consider (const CFlow &cflow) {
    _triggers.push_back (cflow);
    return true;
  }

  // return the list of cflows that must be entered/left here
  const CFlowList &cflows () const { return _triggers; }

  // that types (for the JoinPoint-API)
  virtual std::string that_type_string () const { return "void"; }

  // target type (for the JoinPoint-API)
  virtual std::string target_type_string () const { return "void"; }

  // argument type (for the JoinPoint-API)
  virtual std::string arg_type_string (unsigned no) const { return "void"; }

  // entity type (for the JoinPoint-API)
  virtual std::string entity_type_string() const {
    return format_type( decl() );
  }

  // helper functions for derived classes
  static std::string get_type_string (const clang::NamedDecl *obj, bool normalise = false) {

    // if the 'obj' refers to a record or member function, we have to print a record 'r'
    const clang::CXXRecordDecl *r = llvm::dyn_cast<const clang::CXXRecordDecl>(obj);
    const clang::CXXMethodDecl *m = llvm::dyn_cast<const clang::CXXMethodDecl>(obj);
    if (m)
      r = m->getParent ();

    if (!r)
      if (const clang::ParmVarDecl *pd = llvm::dyn_cast<const clang::ParmVarDecl>(obj))
        r = pd->getType()->getAsCXXRecordDecl();

    if (r) {
      string qualifiers;
      if (m) {
        // preserve const volatile qualifiers of member functions
        // we could also print m->getThisType(obj->getASTContext())
        if(m->isConst())
          qualifiers = "const ";
        if (m->isVolatile())
          qualifiers = "volatile ";
      }
      return qualifiers + TI_Type::get_decl_name_text(r, TSEF_ENABLE, true, TSEF_DONOTCHANGE, false, true);
    }
    else if (const clang::ParmVarDecl *pd = llvm::dyn_cast<const clang::ParmVarDecl>(obj)) {
      return TI_Type::get_type_code_text(pd->getType(), &obj->getASTContext(), 0);
    }
    else
      return "void";
  }

  static std::string format_type( clang::Decl *obj ) {
    clang::ValueDecl *typed_obj = llvm::dyn_cast_or_null<clang::ValueDecl>( obj );
    if( ! typed_obj )
      return "void";

    return format_type( typed_obj->getType(), obj->getASTContext() );
  }

  static std::string format_type( clang::QualType type, clang::ASTContext& ctx ) {
    return TI_Type::get_type_code_text(type, &ctx, 0);
  }

  static TI_Code *of (const ACM_Code &loc) {
    return static_cast<TI_Code*>(loc.transform_info ());
  }
};

class TI_Method : public TI_Code {
  clang::FunctionDecl *_decl;

public:
  TI_Method () : _decl (0) {}

  void decl (clang::FunctionDecl *f) { _decl = f; }
  virtual clang::Decl *decl () const { return _decl; }

  // that type (for the JoinPoint-API)
  virtual std::string that_type_string() const {
    return get_type_string(_decl);
  }

  // target type (for the JoinPoint-API)
  virtual std::string target_type_string() const {
    return get_type_string(_decl);
  }

  virtual std::string arg_type_string (unsigned no) const {
    return get_type_string (_decl->getParamDecl (no), true);
  }
};

// forward declaring child class, as we need to reference the pointer type already
// class TI_Builtin : public TI_Access
class TI_Builtin;

class TI_Access : public TI_Code {
private:
  clang::DeclaratorDecl *_entity;

  clang::Expr *_node;
  clang::Expr *_ref_node;

  clang::Decl *_origin;

  clang::Expr *_target_expr;
public:
  TI_Access() : _entity(0), _node(0), _ref_node(0), _origin(0), _target_expr(0) {}

protected: // make setter protected as derived class might need one with different signature
  void entity( clang::DeclaratorDecl *ent ) { _entity = ent; }
public:
  clang::DeclaratorDecl *entity() const { return _entity; }
  virtual clang::Decl *decl() const { return _entity; } // defined in TransformInfo

  bool entity_is_const() const { return false; }

protected: // make setter protected as derived class might need one with different signature
  void tree_node( clang::Expr *n ) { _node = n; };
public:
  clang::Expr *tree_node() const { return _node; };

  void ref_node( clang::Expr *ref ) {
    _ref_node = ref;

    _target_expr = find_target_expr(); // init caching var
  };
  clang::Expr *ref_node() const { return _ref_node; };

  // checks if the original access uses a qualified target entity name
  bool is_qualified () const {
    clang::MemberExpr *me = clang::dyn_cast_or_null<clang::MemberExpr>( _ref_node );
    if (me && me->hasQualifier())
      return true;
    clang::DeclRefExpr *dre = clang::dyn_cast_or_null<clang::DeclRefExpr>( _ref_node );
    if (dre && dre->hasQualifier())
      return true;
    return false;
  }

  void origin( clang::Decl *o ) { _origin = o; }
  clang::Decl *origin() const { return _origin; }

  bool target_is_ptr() const { return _target_expr && _target_expr->getType().getTypePtr()->isPointerType(); }
  bool target_is_implicit() const { return _target_expr && _target_expr->isImplicitCXXThis(); }
  bool target_is_const() const {
    if (!_target_expr)
      return false;
    // in case of calls to const member functions Clang adds an implicit cast that turn the object
    // into a const object even if it was non-const. So we have to ignore implicit casts here.
    clang::Expr *real_target = _target_expr->IgnoreImpCasts ();
    // now check whether the object type was const before any implicit casts
    clang::QualType t = real_target->getType();
    // if the target is a pointer we need check the object's const-ness and ignore the pointer.
    if (t.getTypePtr()->isPointerType())
      t = t.getTypePtr()->getPointeeType();
    return t.isConstQualified ();
  }
  bool has_target_expr() const { return _target_expr && ! target_is_implicit(); }
  clang::Expr *target_expr() const {
    if( ! target_is_implicit() ) // by convention only explicit expr are returned
      return _target_expr;
    else
      return 0;
  }
  // no setter as it is done internally
  virtual clang::Expr *find_target_expr() const {
    // analog to clang::CXXMemberCallExpr::getImplicitObjectArgument()
    if( const clang::MemberExpr *member = llvm::dyn_cast<clang::MemberExpr>( _ref_node ) )
      return member->getBase()->IgnoreImpCasts();
    else if( const clang::BinaryOperator *op = llvm::dyn_cast<clang::BinaryOperator>( _ref_node ) )
      if( op->getOpcode() == clang::BO_PtrMemD || op->getOpcode() == clang::BO_PtrMemI )
        return op->getLHS();

    return 0;
  }

  // target type (for the JoinPoint-API)
  virtual const clang::RecordDecl *target_class () const = 0;
  // type of target expr (for qualifing names, might differ from target_class)
  virtual const clang::RecordDecl *targetexpr_class() const = 0;
  const clang::RecordDecl *defining_class() const { return clang::dyn_cast<clang::RecordDecl>( _entity->getDeclContext() ); }

  const bool entity_from_baseclass() const {
    const clang::CXXRecordDecl *defining = llvm::dyn_cast_or_null<clang::CXXRecordDecl>( defining_class() );
    const clang::CXXRecordDecl *target = llvm::dyn_cast_or_null<clang::CXXRecordDecl>( target_class() );
    return defining && target && target->isDerivedFrom( defining );
  }

  static TI_Access *of( const ACM_Access &loc ) {
    return static_cast<TI_Access *>( loc.transform_info() );
  }

  const SyntacticContext access_context () const { return SyntacticContext( _origin ); }
  const SyntacticContext entity_context () const { return SyntacticContext( _entity ); }

  // that type (for the JoinPoint-API)
  virtual std::string that_type_string() const {
    if( clang::CXXMethodDecl *m = llvm::dyn_cast<clang::CXXMethodDecl>( _origin ) )
      return get_type_string( m->getParent() );
    else if( clang::VarDecl *vd = llvm::dyn_cast<clang::VarDecl>( _origin ) )
      if( clang::CXXRecordDecl *r = llvm::dyn_cast<clang::CXXRecordDecl>( vd->getDeclContext() ) )
        return get_type_string( r );
    return "void";
  }

  // target type (for the JoinPoint-API)
  virtual std::string target_type_string() const {
    const clang::NamedDecl *tc = target_class();
    return ( tc ? get_type_string( tc ) : string( "void" ));
  }

  // type of target expr (for qualifing names, might differ from target_class)
  virtual std::string targetexpr_type_string() const {
    const clang::NamedDecl *tc = targetexpr_class();
    return ( tc ? get_type_string( tc ) : string( "void" ));
  }

  virtual bool has_result() const = 0;
  virtual clang::QualType result_type() const = 0;
  virtual std::string result_type_string() const { return format_type( result_type(), _origin->getASTContext() ); }

  virtual unsigned int entity_index_count() const { return 0; } // derived override if they have indices
  virtual unsigned long int entity_index_dimension( unsigned int i ) const { return 0; } // derived override if they have indices
  virtual std::string entity_index_type( unsigned int i ) const { return "void"; } // derived override if they have indices
  virtual TI_Builtin *entity_src() const { return 0; }
  enum SpliceMode { SpliceMode_None, SpliceMode_Packed, SpliceMode_Pack, SpliceMode_FullSplice };
  virtual SpliceMode entity_src_splice_mode() const { return SpliceMode_None; }

  static const clang::Expr * skipTransparent( clang::Expr * start ) {
    const clang::Expr *oldE = start;
    const clang::Expr *newE = start->IgnoreImplicit()->IgnoreParens();
    while( oldE != newE ) {
      oldE = newE;
      if( const clang::BinaryOperator *bo = llvm::dyn_cast<clang::BinaryOperator>( newE ) ) {
        if( bo->getOpcode() == clang::BO_Comma ) // jump inside right part as this is the relevant part for the joinpoint in the parent
          newE = bo->getRHS();
      }
      newE = newE->IgnoreImplicit()->IgnoreParens();
    }
    return newE;
  }

  const WeavePos &before_pos (WeaverBase &wb) {
    // jump into parenthesis and ignore exprs that don't change anything
    // cant do this early (or _node in general as ImplicitCast can be skipped here but not for type compuatations)
    return wb.weave_pos( skipTransparent( _node )->getLocStart(), WeavePos::WP_BEFORE );
  }
  const WeavePos &after_pos (WeaverBase &wb) {
    // jump into parenthesis and ignore exprs that don't change anything
    // cant do this early (or _node in general as ImplicitCast can be skipped here but not for type compuatations)
    return get_pos_after_token( skipTransparent( _node )->getLocEnd(), wb );
  }
  const WeavePos &entity_before_pos( WeaverBase &wb ) {
    return wb.weave_pos( ref_node()->getLocStart(), WeavePos::WP_BEFORE );
  }
  const WeavePos &entity_after_pos( WeaverBase &wb ) {
    return get_pos_after_token( ref_node()->getLocEnd(), wb, WeavePos::WP_AFTER );
  }
  const WeavePos &entity_op_before_pos( WeaverBase &wb ) {
    assert( clang::isa<clang::MemberExpr>( ref_node() ) );
    clang::MemberExpr *me = clang::dyn_cast<clang::MemberExpr>( ref_node() );
    // clang3.4 seems to miss getOperatorLoc()
    //return wb.weave_pos( me->getOperatorLoc(), WeavePos::WP_BEFORE );
    return get_pos_after_token( me->getBase()->getLocEnd(), wb, WeavePos::WP_BEFORE );
  }
  const WeavePos &entity_op_after_pos( WeaverBase &wb ) {
    assert( clang::isa<clang::MemberExpr>( ref_node() ) );
    clang::MemberExpr *me = clang::dyn_cast<clang::MemberExpr>( ref_node() );
    if( me->hasQualifier() )
      return wb.weave_pos( me->getQualifierLoc().getBeginLoc(), WeavePos::WP_AFTER );
    else
      return wb.weave_pos( me->getMemberLoc(), WeavePos::WP_AFTER );
  }

  struct PH : public clang::PrinterHelper {
    virtual bool  handledStmt (clang::Stmt *node, llvm::raw_ostream &os) {
      clang::CXXOperatorCallExpr *ce = clang::dyn_cast<clang::CXXOperatorCallExpr> (node);
      clang::FunctionDecl *fd = (ce ? ce->getDirectCallee () : 0);
      if (ce && ce->getNumArgs () == 1 &&
          fd->getNameAsString () == "operator->") {
        clang::ASTContext &ctx = fd->getASTContext();
        os << "(";
        ce->getArg (0)->printPretty(os, this, ctx.getPrintingPolicy(), 0);
        os << ").operator->()";
        return true;
      }
      // WORKAROUND for Clang 3.4 problem:
      // Implicit calls to conversion operators are printed explicitly, but
      // the object argument is not put into brackets. For example:
      // "a & b" might become "a & b.operator int()"
      // This fix generates the brackets: "(a & b).operator int()"
      clang::CXXMemberCallExpr *mce = clang::dyn_cast<clang::CXXMemberCallExpr> (node);
      fd = (mce ? mce->getDirectCallee () : 0);
      if (fd && clang::dyn_cast<clang::CXXConversionDecl>(fd)) {
        clang::MemberExpr *me = clang::dyn_cast<clang::MemberExpr>(mce->getCallee ());
        if (me) {
          os << "(";
          clang::ASTContext &ctx = fd->getASTContext();
          me->getBase ()->printPretty(os, this, ctx.getPrintingPolicy(), 0);
          os << ")" << (me->isArrow () ? "->" : ".");
          os << TI_Function::name(fd) << "()";
          return true;
        }
      }
      return false;
    }
  };

  string code () const {
    PH ph;
    clang::ASTContext &ctx = _origin->getASTContext();
    std::string buf;
    llvm::raw_string_ostream out (buf);
    _node->printPretty(out, &ph, ctx.getPrintingPolicy(), 0);
    return buf;
  }

  // returns true if the access needs special rights
  bool needs_rights () const {
    // no member function => no accessibility problem
    if( ! target_class() )
      return false;

    // static member => no problem only if public
    clang::CXXMethodDecl *md = clang::dyn_cast<clang::CXXMethodDecl>(_entity);
    clang::VarDecl *vd = clang::dyn_cast<clang::VarDecl>(_entity);
    if ((md && md->isStatic ()) || (vd && vd->isStaticDataMember ()))
      return (_entity->getAccess () != clang::AS_public);

    // normal member function => look up the accessibility
    const clang::CXXRecordDecl *base = clang::dyn_cast<clang::CXXRecordDecl>( defining_class() );
    const clang::CXXRecordDecl *target = clang::dyn_cast<clang::CXXRecordDecl>(target_class ());
    clang::CXXBasePaths paths;
    if (target->isDerivedFrom (base, paths)) {
      for (clang::CXXBasePaths::const_paths_iterator i = paths.begin (); i != paths.end (); ++i)
        if (i->Access != clang::AS_public)
          return true;
    }

    if( _entity->getAccess () == clang::AS_public )
      return false;

    return true;
  }
};

// This abstract class represents a call in general and exists just as interface for
// actions that are (mostly) the same between calls of user-defined (member-)functions
// and calls of built-in operators.
class TI_CommonCall : public TI_Access {
protected:
  TI_CommonCall () {}
public:
  virtual string operator_kind_string() const = 0;

  virtual const clang::Expr* arg(unsigned int index) const = 0;
  virtual unsigned int arg_count() const = 0;

  virtual bool is_unary_expr() const = 0;
  virtual bool is_binary_expr() const = 0;
  virtual bool is_ternary_expr() const = 0;
  virtual bool is_postfix_expr() const = 0;
  virtual bool is_index_expr() const = 0;
  virtual bool is_arrow_class_member_access_expr() const = 0;
  virtual bool is_implicit_conversion() const = 0;

  virtual clang::SourceLocation get_operator_location() const = 0;

  // returns the position directly in front of the operator
  virtual const WeavePos &op_before_pos (WeaverBase &wb) {
    return wb.weave_pos(get_operator_location(), WeavePos::WP_BEFORE);
  }

  // returns the position directly behind the operator
  virtual const WeavePos &op_after_pos (WeaverBase &wb) {
    return get_pos_after_token(get_operator_location(), wb);
  }

  // The following member-functions may be called if is_index_expr() is true
  // returns the position directly in front of the opening bracket [
  virtual const WeavePos &index_open_before_pos (WeaverBase &wb) {
    assert (is_index_expr ());
    return get_pos_after_token(arg(0)->getLocEnd(), wb, WeavePos::WP_BEFORE);
  }
  // returns the position directly behind the opening bracket [
  virtual const WeavePos &index_open_after_pos (WeaverBase &wb) {
    assert (is_index_expr ());
    return wb.weave_pos(arg(1)->getLocStart (), WeavePos::WP_AFTER);
  }
  // returns the position directly in front of the closing bracket ]
  virtual const WeavePos &index_close_before_pos (WeaverBase &wb) {
    assert (is_index_expr ());
    return get_pos_after_token(arg(1)->getLocEnd (), wb, WeavePos::WP_BEFORE);
  }
  // returns the position directly behind the closing bracket ]
  virtual const WeavePos &index_close_after_pos (WeaverBase &wb) {
    assert (is_index_expr ());
    return get_pos_after_token(TI_Access::tree_node()->getLocEnd (), wb, WeavePos::WP_AFTER);
  }

  // The following member-functions return the corresponding weave-positions of ternary operators
  // (first delimiter = "?", second delimiter = ":")
  virtual const WeavePos& ternary_op_first_delim_before_pos(WeaverBase &wb) {
    assert (is_ternary_expr());
    return get_pos_after_token(arg(0)->getLocEnd(), wb, WeavePos::WP_BEFORE);
  }
  virtual const WeavePos& ternary_op_first_delim_after_pos(WeaverBase &wb) {
    assert (is_ternary_expr());
    return wb.weave_pos(arg(1)->getLocStart(), WeavePos::WP_AFTER);
  }
  virtual const WeavePos& ternary_op_second_delim_before_pos(WeaverBase &wb) {
    assert (is_ternary_expr());
    return get_pos_after_token(arg(1)->getLocEnd(), wb, WeavePos::WP_BEFORE);
  }
  virtual const WeavePos& ternary_op_second_delim_after_pos(WeaverBase &wb) {
    assert (is_ternary_expr());
    return wb.weave_pos(arg(2)->getLocStart(), WeavePos::WP_AFTER);
  }
};

class TI_MethodCall : public TI_CommonCall {
  clang::FunctionDecl *_called_func;
public:
  TI_MethodCall() : _called_func( 0 ) {}

  void called (clang::FunctionDecl *f) { _called_func = f; entity( f ); }
  clang::FunctionDecl *called () const { return _called_func; }

  void tree_node( clang::Expr *n ) {
    // In this class the node-object always has the type clang::CallExpr*
    assert(!n || llvm::isa<clang::CallExpr>(n));
    TI_Access::tree_node( n );
    if( n ) {
      ref_node( static_cast<clang::CallExpr*>(n)->getCallee()->IgnoreParenImpCasts() );
    }
  }

  // In this class the node-object always has the type clang::CallExpr*
  const clang::CallExpr* tree_node() const {
    assert(llvm::isa<clang::CallExpr>(TI_Access::tree_node()));
    return static_cast<clang::CallExpr*>(TI_Access::tree_node());
  }

  static TI_MethodCall *of (const ACM_Call &loc) {
    return static_cast<TI_MethodCall*>(loc.transform_info ());
  }

  bool uses_ADN_lookup () const {
    return (_called_func &&
        _called_func->getLexicalDeclContext () != _called_func->getDeclContext () &&
        _called_func->getFriendObjectKind () != clang::Decl::FOK_None);
  }

  virtual string operator_kind_string() const {
    if( clang::CXXOperatorCallExpr *oc =llvm::dyn_cast<clang::CXXOperatorCallExpr>( TI_Access::tree_node() ) )
      return oc->getDirectCallee ()->getNameAsString ().substr (8);

    assert( false && "Unknown expr type" );
    return "<?>"; // dummy
  }

  static unsigned int arg_count( const clang::CallExpr *node ) {
    unsigned int args = node->getNumArgs();
    // for calls to % T::operator() (...) on an object t of type T,
    // e.g t(42), Clang says that the number of args is 2. In AspectC++ we
    // regard t is the target pointer and only 42 as an argument.
    if (is_call_op(node))
      args--;
    return args;
  }

  virtual unsigned int arg_count() const {
    return arg_count( tree_node() );
  }

  // This method takes an argument-index as unsigned int and returns the correspondent argument-clang::Expr-pointer.
  static const clang::Expr* arg( const clang::CallExpr *node, unsigned int index ) {
    // for calls to % T::operator() (...) on an object t of type T,
    // e.g t(42), Clang says that the number of args is 2. In AspectC++ we
    // regard t is the target pointer and only 42 as an argument.
    if (is_call_op(node))
      index++;
    return node->getArg(index);
  }

  // This method takes an argument-index as unsigned int and returns the correspondent argument-clang::Expr-pointer.
  virtual const clang::Expr* arg(unsigned int index) const {
    return arg( tree_node(), index );
  }

  static std::string arg_type_string( const clang::CallExpr *node, clang::ASTContext &ctx, unsigned no ) {
    return TI_Type::get_type_code_text(arg( node, no )->getType(), &ctx, 0);
  }

  virtual std::string arg_type_string (unsigned no) const {
    // If the requested argument is no variadic argument we take it
    // from the function declaration:
    if( no < _called_func->getNumParams () )
      return get_type_string (_called_func->getParamDecl (no), true);

    assert(_called_func->isVariadic());
    // Otherwise we take it from the call expression:
    clang::ASTContext &ctx = origin()->getASTContext();
    return arg_type_string( tree_node(), ctx, no );
  }

  bool is_operator_call() const {
    return llvm::isa<clang::CXXOperatorCallExpr>( TI_Access::tree_node() );
  }

  virtual bool is_unary_expr () const {
    clang::CXXOperatorCallExpr *ce = clang::dyn_cast<clang::CXXOperatorCallExpr> (TI_Access::tree_node());
    return (ce && ce->getNumArgs () == 1 && !is_call_op());
  }

  virtual bool is_binary_expr () const {
    clang::CXXOperatorCallExpr *ce = clang::dyn_cast<clang::CXXOperatorCallExpr> (TI_Access::tree_node());
    return (ce && ce->getNumArgs () == 2 && !is_index_expr () && !is_postfix_expr () && !is_call_op());
  }

  virtual bool is_index_expr () const {
    clang::CXXOperatorCallExpr *ce = clang::dyn_cast<clang::CXXOperatorCallExpr> (TI_Access::tree_node());
    return (ce && ce->getNumArgs () == 2 && _called_func->getNameAsString () == "operator[]");
  }

  virtual bool is_postfix_expr () const {
    clang::CXXOperatorCallExpr *ce = clang::dyn_cast<clang::CXXOperatorCallExpr> (TI_Access::tree_node());
    return (ce && ce->getNumArgs () == 2 &&
        (_called_func->getNameAsString () == "operator++" ||
            _called_func->getNameAsString () == "operator--"));
  }

  virtual bool is_arrow_class_member_access_expr() const {
    return _called_func->getNameAsString () == "operator->";
  }

  virtual bool is_ternary_expr() const {
    return false;
  }

  virtual bool is_implicit_conversion () const {
    if (!llvm::isa<clang::CXXConversionDecl> (_called_func))
      return false;
    clang::CXXMemberCallExpr *mce = clang::dyn_cast<clang::CXXMemberCallExpr>( TI_Access::tree_node() );
    if (!mce)
      return false;
    // TODO: is there a better way to distinguish 'c' from 'c.operator int*()'?
    return (mce->getCallee ()->getLocEnd () == TI_Access::tree_node()->getLocEnd ());
  }

  static bool is_call_op (const clang::CallExpr *node) {
    return (clang::dyn_cast<clang::CXXOperatorCallExpr>(node) &&
        node->getDirectCallee ()->getNameAsString () == "operator()");
  }

  bool is_call_op () const {
    return is_call_op (tree_node());
  }

  // This method returns the clang::SourceLocation of the operator according to the operator-type.
  virtual clang::SourceLocation get_operator_location() const {
    if( clang::CXXOperatorCallExpr *ce = clang::dyn_cast<clang::CXXOperatorCallExpr> ( TI_Access::tree_node() ) )
      return ce->getOperatorLoc ();

    assert( false && "This is no operator." );
    return TI_Access::tree_node()->getLocStart(); // dummy
  }

  const WeavePos &args_open_before_pos (WeaverBase &wb) {
    if (is_call_op ())
      return wb.weave_pos (tree_node()->getCallee ()->getLocStart (), WeavePos::WP_BEFORE);
    else
      return get_pos_after_token(tree_node()->getCallee ()->getLocEnd (), wb, WeavePos::WP_BEFORE);
  }

  const WeavePos &args_open_after_pos (WeaverBase &wb) {
    if (call_args () > 0)
      return wb.weave_pos (arg (0)->getLocStart (), WeavePos::WP_AFTER);
    else
      return wb.weave_pos (TI_Access::tree_node()->getLocEnd (), WeavePos::WP_AFTER);
  }

  const WeavePos &args_close_before_pos (WeaverBase &wb) {
    return wb.weave_pos(TI_Access::tree_node()->getLocEnd (), WeavePos::WP_BEFORE);
  }

  const WeavePos &args_close_after_pos (WeaverBase &wb) {
    return get_pos_after_token(TI_Access::tree_node()->getLocEnd (), wb, WeavePos::WP_AFTER);
  }

  const WeavePos &callee_before_pos (WeaverBase &wb) {
    if (has_target_expr()) {
      const clang::Expr *callee = tree_node()->getCallee ();
      while (clang::dyn_cast<clang::ImplicitCastExpr> (callee))
        callee = clang::dyn_cast<clang::ImplicitCastExpr> (callee)->getSubExpr ();
      while (clang::dyn_cast<clang::ParenExpr> (callee))
        callee = clang::dyn_cast<clang::ParenExpr> (callee)->getSubExpr ();
      const clang::MemberExpr *me = clang::dyn_cast<clang::MemberExpr> (callee);
      assert (me);
      return get_pos_after_token(me->getBase ()->getLocEnd (), wb, WeavePos::WP_BEFORE);
    }
    else {
      if (is_call_op ())
        return wb.weave_pos (TI_Access::tree_node()->getLocStart (), WeavePos::WP_BEFORE);
      else
        return wb.weave_pos (tree_node()->getCallee ()->getLocStart (), WeavePos::WP_BEFORE);
    }
  }

  const WeavePos &callee_after_pos (WeaverBase &wb) {
    if (is_call_op ())
      return wb.weave_pos (tree_node()->getCallee ()->getLocStart (), WeavePos::WP_AFTER);
    else {
      const clang::Expr *callee = tree_node()->getCallee ();
      while (clang::dyn_cast<clang::ParenExpr> (callee))
        callee = clang::dyn_cast<clang::ParenExpr> (callee)->getSubExpr ();
      return get_pos_after_token(callee->getLocEnd (), wb, WeavePos::WP_AFTER);
    }
  }

  // return the number of arguments, not including the object in case of
  // member function calls and not including implicitly passed default arguments
  unsigned call_args () const {
    unsigned args = 0;
    const clang::CallExpr* call_node = tree_node();
    while (args < call_node->getNumArgs ()) {
      if( clang::dyn_cast<clang::CXXDefaultArgExpr>( call_node->getArg( args ) ) )
        break;
      args++;
    }
    // in case of calls to operator() the number has to be decremented once
    if (is_call_op())
      args--;
    return args;
  }

  virtual bool has_result () const {
#if (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && !defined(CLANG_VERSION_PATCHLEVEL)) || \
  (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && CLANG_VERSION_PATCHLEVEL == 2)
    return !_called_func->getResultType()->isVoidType ();
#else // C++ 11 interface
    return !_called_func->getReturnType()->isVoidType ();
#endif
  }

  virtual clang::QualType result_type() const {
#if (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && !defined(CLANG_VERSION_PATCHLEVEL)) || \
  (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && CLANG_VERSION_PATCHLEVEL == 2)
    return _called_func->getResultType ();
#else // C++ 11 interface
    return _called_func->getReturnType ();
#endif
  }

  // target type (for the JoinPoint-API)
  virtual const clang::RecordDecl *target_class () const {
    return targetexpr_class();
  }

  virtual const clang::RecordDecl *targetexpr_class () const {
    const clang::RecordDecl *result = 0;

    const clang::CXXMethodDecl *md = clang::dyn_cast_or_null<clang::CXXMethodDecl> (_called_func);
    if( has_target_expr() ) {
      const clang::Type *type = target_expr()->getType ().getTypePtr ();
      result = type->getPointeeCXXRecordDecl();
      if (!result)
        result = type->getAsCXXRecordDecl ();
    }
    else if (md) {
      result = md->getParent();
      if (clang::CXXMethodDecl *caller = llvm::dyn_cast<clang::CXXMethodDecl>(origin())) {
        if (!md->isStatic())
          result = caller->getParent ();
      }
    }
    return result;
  }

  // the target object of the call or NULL
  virtual clang::Expr *find_target_expr() const {

    // check if this call has a target object
    clang::CXXMethodDecl *md = clang::dyn_cast_or_null<clang::CXXMethodDecl> (_called_func);
    if (!md /* || md->isStatic ()*/)
      return 0;

    clang::Expr *result = 0;

    // an ordinary member function call, e.g. foo->bar()
    clang::CXXMemberCallExpr *mce =
        clang::dyn_cast<clang::CXXMemberCallExpr> (TI_Access::tree_node());
    // .. or an operator call, e.g. !foo or foo+bar
    clang::CXXOperatorCallExpr *oce =
        clang::dyn_cast<clang::CXXOperatorCallExpr> (TI_Access::tree_node());
    if (mce)
      result = mce->getImplicitObjectArgument ();
    else if (oce && md->getParent ())
      result = oce->getArg (0);
    else {
      // it might still be a static member function call with unused target expr,
      // e.g. foo->static_bar()
      const clang::Expr *callee = tree_node()->getCallee();
      if (clang::dyn_cast<clang::ImplicitCastExpr> (callee)) {
        callee = clang::dyn_cast<clang::ImplicitCastExpr> (callee)->getSubExpr ();
        if (clang::dyn_cast<clang::MemberExpr> (callee)) {
          result = clang::dyn_cast<clang::MemberExpr> (callee)->getBase();
        }
      }
    }

    // TODO: check if implicit calls are handled correctly here
    return result;
  }

  // checks whether the call uses explicit template parameters
  bool has_explicit_template_params () const {
    if( clang::DeclRefExpr *dre = llvm::dyn_cast<clang::DeclRefExpr>( ref_node() ) )
      return dre->hasExplicitTemplateArgs();
    else if( clang::MemberExpr *me = llvm::dyn_cast<clang::MemberExpr>( ref_node() ) )
      return me->hasExplicitTemplateArgs();
    else
      return false;
  }

  const clang::TemplateArgumentLoc *get_explicit_template_params() const {
    if( clang::DeclRefExpr *dre = llvm::dyn_cast<clang::DeclRefExpr>( ref_node() ) )
      return dre->getTemplateArgs();
    else if( clang::MemberExpr *me = llvm::dyn_cast<clang::MemberExpr>( ref_node() ) )
      return me->getTemplateArgs();
    else
      return 0;
  }

  unsigned int num_explicit_template_params() const {
    if( clang::DeclRefExpr *dre = llvm::dyn_cast<clang::DeclRefExpr>( ref_node() ) )
      return dre->getNumTemplateArgs();
    else if( clang::MemberExpr *me = llvm::dyn_cast<clang::MemberExpr>( ref_node() ) )
      return me->getNumTemplateArgs();
    else
      return 0;
  }
};

// This class represents a call of a built-in operator.
class TI_Builtin : public TI_CommonCall {
  TI_Builtin *_forwarded_src;
  ACM_Access *_packed_forward_requester;
public:
  TI_Builtin() : _forwarded_src( 0 ), _packed_forward_requester( 0 ) {}

  void tree_node( clang::Expr *n ) {
    assert(is_builtin_operator(n));
    TI_Access::tree_node( n );
  }

  static TI_Builtin *of (const ACM_Builtin &loc) {
    return static_cast<TI_Builtin*>(loc.transform_info ());
  }

  static bool is_builtin_operator(clang::Expr* node) {
    return llvm::isa<clang::UnaryOperator>( node )
        || llvm::isa<clang::BinaryOperator>( node )
        || llvm::isa<clang::ArraySubscriptExpr>( node )
        || llvm::isa<clang::ConditionalOperator>( node );
  }

  static string operator_kind_string( clang::Expr *node ) {
    assert( is_builtin_operator( node ) );
    if( clang::UnaryOperator *uo = llvm::dyn_cast<clang::UnaryOperator>( node ) )
      return clang::UnaryOperator::getOpcodeStr( uo->getOpcode() ).str();
    else if( clang::BinaryOperator * bo = llvm::dyn_cast<clang::BinaryOperator>( node ) )
      return clang::BinaryOperator::getOpcodeStr( bo->getOpcode() ).str();
    else if( llvm::isa<clang::ArraySubscriptExpr>( node ) )
      return "[]";
    else if( llvm::isa<clang::ConditionalOperator>( node ) )
      return "?:";

    assert( false && "Unknown expr type" );
    return "<?>"; // dummy
  }

  virtual string operator_kind_string() const {
    return( operator_kind_string( TI_Access::tree_node() ) );
  }

  static unsigned int arg_count( const clang::Expr *node ) {
    if( const clang::UnaryOperator *uo = llvm::dyn_cast<clang::UnaryOperator>( node ) ) {
      if( uo->isPostfix() )
        return 2;
      else
        return 1;
    }
    else if( llvm::isa<clang::BinaryOperator>( node ) )
      return 2;
    else if( llvm::isa<clang::ArraySubscriptExpr>( node ) )
      return 2;
    else if( llvm::isa<clang::ConditionalOperator>( node ) )
      return 3;

    assert( false && "Unknown expr type" );
    return 0; // dummy
  }

  virtual unsigned int arg_count() const {
    return arg_count( TI_Access::tree_node() );
  }

  // This method takes an argument-index as unsigned int and returns the correspondent argument-clang::Expr-pointer.
  static const clang::Expr* arg( const clang::Expr *node, unsigned int index ) {
    // Valid index?
    assert( index < arg_count( node ) );
    if( const clang::UnaryOperator *uo = llvm::dyn_cast<clang::UnaryOperator>( node ) ) {
      if( uo->isPostfix() && index == 1 )
        return 0;
      else
        return uo->getSubExpr();
    }
    else if( const clang::BinaryOperator * bo = llvm::dyn_cast<clang::BinaryOperator>( node ) )
      return index == 0 ? bo->getLHS() : bo->getRHS();
    else if( const clang::ArraySubscriptExpr *ase = llvm::dyn_cast<clang::ArraySubscriptExpr>( node ) )
      return ( index == 0 ) ? ase->getLHS() : ase->getRHS();
    else if( const clang::ConditionalOperator *co = llvm::dyn_cast<clang::ConditionalOperator>( node ) )
      return ( index == 0 ) ? co->getCond() : ( ( index == 1 ) ? co->getTrueExpr() : co->getFalseExpr() );

    assert( false && "Unknown expr type" );
    return 0; // dummy
  }

  // This method takes an argument-index as unsigned int and returns the correspondent argument-clang::Expr-pointer.
  virtual const clang::Expr* arg(unsigned int index) const {
    return arg( TI_Access::tree_node(), index );
  }

  static clang::QualType arg_type( const clang::Expr *node,  clang::ASTContext &ctx, unsigned no ) {
    if( const clang::UnaryOperator * uo = llvm::dyn_cast<clang::UnaryOperator>( node ) ) {
      if( uo->isPostfix() && ( no == 1 ) )
        return ctx.IntTy; // Dummy argument of postfix ops
    }
    const clang::Expr *argument = arg( node, no );
    return TI_Type::get_reference_type_if_necessary(argument->getType(), argument, ctx);
  }

  static std::string arg_type_string( const clang::Expr *node, clang::ASTContext &ctx, unsigned no ) {
    return TI_Type::get_type_code_text(arg_type( node, ctx, no ), &ctx, 0);
  }

  virtual std::string arg_type_string (unsigned no) const {
    return arg_type_string( TI_Access::tree_node(), origin()->getASTContext(), no );
  }

  // returns whether the type of the argument with the given number is a reference-type
  bool arg_is_ref( unsigned no ) const {
    return arg_type( TI_Access::tree_node(), origin()->getASTContext(), no )->isReferenceType();
  }

  // returns whether the argument with the given number is NOT available in the call-wrapper
  // (e.g. due to short-circuit-evaluation)
  bool arg_is_unavailable( unsigned no, int wrapper ) {
    // First argument is always available
    if(no == 0) {
        return false;
    }
    // Second argument of && and ||
    if( clang::BinaryOperator *bo = llvm::dyn_cast<clang::BinaryOperator>( TI_Access::tree_node() ) ) {
      if( bo->getOpcode() == clang::BO_LAnd )
        return wrapper == 1 && no == 1;
      else if( bo->getOpcode() == clang::BO_LOr )
        return wrapper == 0 && no == 1;
      else
        return false;
    }
    // second or third argument of ?:
    else if( llvm::isa<clang::ConditionalOperator>( TI_Access::tree_node() ) )
      return no == static_cast<unsigned>( 2 - wrapper );
    else
      return false;
  }

  // returns whether this is a unary operator
  virtual bool is_unary_expr () const {
    return llvm::isa<clang::UnaryOperator>( TI_Access::tree_node() ) && ! is_postfix_expr();
  }

  // returns whether this is a postfix operator (increment oder decrement)
  virtual bool is_postfix_expr () const {
    if( clang::UnaryOperator *uo = llvm::dyn_cast<clang::UnaryOperator>( TI_Access::tree_node() ) )
      return uo->isPostfix();
    return false;
  }

  // returns whether this a binary operator ( but no array subscript operator
  virtual bool is_binary_expr () const {
    return llvm::isa<clang::BinaryOperator>( TI_Access::tree_node() );
  }

  // returns whether this is the binary array subscript operator
  virtual bool is_index_expr () const {
    return llvm::isa<clang::ArraySubscriptExpr>( TI_Access::tree_node() );
  }

  // returns whether this is a operator that changes and assigns a value
  bool is_compound_assignment() const {
    if( llvm::isa<clang::CompoundAssignOperator>( TI_Access::tree_node() ) )
      return true;
    else if( clang::UnaryOperator *uo = llvm::dyn_cast<clang::UnaryOperator>( TI_Access::tree_node() ) )
      return uo->isIncrementDecrementOp();
    else
      return false;
  }

  // returns whether this is the arrow class member access operator "->"
  virtual bool is_arrow_class_member_access_expr() const {
    // built-in operator "->" is currently not supported:
    return false;
  }

  // returns whether this a ternary operator (currently only conditional operator)
  virtual bool is_ternary_expr() const {
    return llvm::isa<clang::ConditionalOperator>( TI_Access::tree_node() );
  }

  virtual bool is_implicit_conversion () const {
    // built-in conversion operators are currently not supported
    return false;
  }

  // This member-function returns whether this call uses the short-circuit evaluation. Currently only
  // the built-in operators "&&", "||" and "?:".
  bool is_short_circuiting() const {
    if( clang::BinaryOperator * bo = llvm::dyn_cast<clang::BinaryOperator>( TI_Access::tree_node() ) ) {
      return bo->getOpcode() == clang::BO_LAnd || bo->getOpcode() == clang::BO_LOr;
    }
    else {
      return llvm::isa<clang::ConditionalOperator>( TI_Access::tree_node() );
    }
  }

  // check if operator is normally forwarding the first arg as result (usually references)
  bool is_forwarding() const {
    if( llvm::isa<clang::CompoundAssignOperator>( TI_Access::tree_node() ) )
      return true;
    else if( clang::BinaryOperator * bo = llvm::dyn_cast<clang::BinaryOperator>( TI_Access::tree_node() ) )
      return bo->getOpcode() == clang::BO_Assign;
    else if( clang::UnaryOperator *uo = llvm::dyn_cast<clang::UnaryOperator>( TI_Access::tree_node() ) )
      return uo->isIncrementDecrementOp() && uo->isPrefix();
    else if( llvm::isa<clang::ArraySubscriptExpr>( TI_Access::tree_node() ) )
      return true;
    else
      return false;
  }

  // This method returns the clang::SourceLocation of the operator according to the operator-type.
  virtual clang::SourceLocation get_operator_location() const {
    clang::Expr *node = TI_Access::tree_node();
    if( clang::UnaryOperator *uo = clang::dyn_cast<clang::UnaryOperator>( node ) ) {
      return uo->getOperatorLoc();
    }
    else if( clang::BinaryOperator *bo = clang::dyn_cast<clang::BinaryOperator>( node ) ) {
      return bo->getOperatorLoc();
    }

    assert( false && "The expr does not have THE ONE operator-location." );
    return node->getLocStart(); // dummy
  }

  // returns whether the current operator returns something
  virtual bool has_result () const {
    // All currently supported operators have a return value:
    return true;
  }

  // returns the type of the return value of the operator
  virtual clang::QualType result_type() const {
    const clang::Expr *node = TI_Access::tree_node();
    return TI_Type::get_reference_type_if_necessary(node->getType(), node, origin()->getASTContext());
  }

  // Built-in operators are global functions and therefore have no object/target on
  // which they are called. Hence we have no target expression and thus there
  // is no class of the type of this expression.
  virtual clang::Expr *find_target_expr() const {
    return 0;
  }
  virtual const clang::RecordDecl *target_class () const {
    return 0;
  }
  virtual const clang::RecordDecl *targetexpr_class () const {
    return 0;
  }

  void forwarded_src( TI_Builtin *src ) {
    _forwarded_src = src;
  }

  TI_Builtin *forwarded_src() {
    return _forwarded_src;
  }

  void request_packed_forward( ACM_Access *requester ) {
    if( _packed_forward_requester == 0 ) {
      if( _forwarded_src )
        _forwarded_src->request_packed_forward( requester );

      _packed_forward_requester = requester;
    }
  }

  bool forwarded_is_packed() const {
    return _packed_forward_requester != 0;
  }

  ACM_Access * packed_forward_requester() const {
    return _packed_forward_requester;
  }

  bool forwarded_needs_packing() {
    // check src for planned advice, which already packed it
    TI_Builtin *check = _forwarded_src;
    while( check ) {
      if( check->is_planned() )
        return false;
      check = check->_forwarded_src;
    }

    return true;
  }

  unsigned int forwarded_index_count() const {
    unsigned int result = 0;
    if( _forwarded_src )
      result = _forwarded_src->forwarded_index_count();

    if( is_index_expr() )
      result++;

    return result;
  }

  std::string forwarded_index_type( unsigned int i ) const {
    assert( forwarded_index_count() > i  );
    if( is_index_expr() ) {
      if( i == 0 )
        return arg_type_string( 1 );
      else
        i--;
    }

    assert( _forwarded_src );
    return _forwarded_src->forwarded_index_type( i );
  }
};


class TI_VariableAccess : public TI_Access {
private:
  TI_Builtin *_entity_src;
public:
  TI_VariableAccess() : _entity_src( 0 ) {}

  // unprotect function by forwarding (we need no special sig)
  void entity( clang::DeclaratorDecl *v ) { TI_Access::entity( v ); }
  void variable( clang::DeclaratorDecl *v ) { entity( v ); }
  clang::DeclaratorDecl *variable() const { return TI_Access::entity(); }

  virtual clang::QualType entity_type() const {
    clang::QualType type = TI_Access::entity()->getType();
    unsigned int unwrap = entity_index_count();
    while( unwrap > 0 && type->isArrayType() ) {
      type = clang::dyn_cast<clang::ArrayType>( type.getTypePtr() )->getElementType();
      unwrap--;
    }
    assert( unwrap == 0 ); // we should not meet any non array types above

    return type;
  }

  virtual std::string entity_type_string() const {
    return format_type( entity_type(), TI_Access::entity()->getASTContext() );
  }

  void entity_src( TI_Builtin *src ) { _entity_src = src; }
  virtual TI_Builtin *entity_src() const { return _entity_src; }

  virtual SpliceMode entity_src_splice_mode() const {
    TI_Builtin *src = _entity_src;
    while( src ) {
      if( src->is_planned() )
        return SpliceMode_Packed; // already done while weaving of src
      if( ( src->is_binary_expr() && ( src->operator_kind_string() == "=" ) ) || src->is_compound_assignment() )
        return SpliceMode_Pack; // this forwarding operators have side effects we cant splice, so we need to pack
      src = src->forwarded_src();
    }

    return SpliceMode_FullSplice; // no reasons to not do it, at least none found here
  }

  bool entity_is_const() const {
    clang::DeclaratorDecl *var = variable();
    return var && var->getType().isConstQualified();
  }

  virtual unsigned int entity_index_count() const {
    if( _entity_src )
      return _entity_src->forwarded_index_count();
    else
      return 0;
  }
  virtual unsigned long int entity_index_dimension( unsigned int i ) const {
    const clang::ConstantArrayType *type = llvm::dyn_cast<clang::ConstantArrayType>( variable()->getType().getCanonicalType().getTypePtr() );
    while( i > 0 )
      type = clang::dyn_cast<clang::ConstantArrayType>( type->getElementType().getTypePtr() ), i--;

    return type->getSize().getZExtValue();
  }
  virtual std::string entity_index_type( unsigned int i ) const {
    if( _entity_src )
      return _entity_src->forwarded_index_type( i );
    else
      return "void";
  }

  // unprotect function by forwarding (we need no special sig)
  void tree_node( clang::Expr* n ) { TI_Access::tree_node( n ); }

  // target type (for the JoinPoint-API)
  virtual const clang::RecordDecl *target_class () const {
    const clang::RecordDecl *result = targetexpr_class();

    // if variable is from a virtual base, we need to "downgrade" the target as member pointers cant represent that
    if( llvm::isa<clang::FieldDecl>( variable() ) ) {
      const clang::CXXRecordDecl *defining = llvm::dyn_cast_or_null<clang::CXXRecordDecl>( defining_class() );
      const clang::CXXRecordDecl *target = llvm::dyn_cast_or_null<clang::CXXRecordDecl>( result );
      if( defining && target && target->isVirtuallyDerivedFrom( defining ) )
        result = defining_class();
    }

    return result;
  }

  virtual const clang::RecordDecl *targetexpr_class() const {
    const clang::RecordDecl *result = 0;

    if( target_is_implicit() ) {
      clang::CXXMethodDecl *origin = llvm::dyn_cast<clang::CXXMethodDecl>( this->origin() );
      assert( origin );

      result = origin->getParent();
    }
    else if( has_target_expr() ) {
      const clang::Type *type = target_expr()->getType ().getTypePtr();
      result = type->getPointeeCXXRecordDecl();
      if( !result )
        result = type->getAsCXXRecordDecl();
    }
    else if( const clang::VarDecl *vd = llvm::dyn_cast<clang::VarDecl>( variable() ) ) {
      if( vd->isStaticDataMember() ) {
        const clang::DeclContext *dc = vd->getDeclContext();
        assert( dc->isRecord() );

        result = llvm::dyn_cast<clang::RecordDecl>( dc );
      }
      else
        result = 0;
    }

    return result;
  }
};

class TI_Get : public TI_VariableAccess {
public:
  TI_Get() {}

  static TI_Get *of( const ACM_Get &loc ) {
    return static_cast<TI_Get *>(loc.transform_info());
  }

  virtual bool has_result() const {
    return true;
  }

  virtual clang::QualType result_type() const {
    return entity_type();
  }
};

class TI_Set : public TI_VariableAccess {
public:
  TI_Set() {}

  static TI_Set *of( const ACM_Set &loc ) {
    return static_cast<TI_Set *>(loc.transform_info());
  }

  virtual std::string arg_type_string (unsigned no) const {
    assert( no == 0 );

    return entity_type_string();
  }

  virtual bool has_result() const {
    return false;
  }

  virtual clang::QualType result_type() const {
    clang::ASTContext& ctx = origin()->getASTContext();
    return ctx.VoidTy;
  }
};

class TI_Ref : public TI_VariableAccess {
public:
  TI_Ref() {}

  static TI_Ref *of( const ACM_Ref &loc ) {
    return static_cast<TI_Ref *>(loc.transform_info());
  }

  // emulate index on ArrayToPointerDecay
  virtual unsigned int entity_index_count() const {
    unsigned int result = TI_VariableAccess::entity_index_count();
    if( is_implicit_arraydecay() )
      result++;

    return result;
  }
  virtual std::string entity_index_type( unsigned int i ) const {
    if( is_implicit_arraydecay() && i == ( entity_index_count() - 1 ) )
      return "unsigned int";
    else
      return TI_VariableAccess::entity_index_type( i );
  }

  virtual bool has_result() const {
    return true;
  }

  virtual clang::QualType result_type() const {
    clang::Expr *node = TI_Access::tree_node();
    return TI_Type::get_reference_type_if_necessary(node->getType(), node, origin()->getASTContext());
  }

  bool result_is_ptr() const {
    return result_type().getTypePtr()->isPointerType();
  }

  const bool is_explicit_operator() const {
    if( clang::UnaryOperator *uo = clang::dyn_cast<clang::UnaryOperator>( TI_Access::tree_node() ) )
      if( uo->getOpcode() == clang::UO_AddrOf )
        return true;

    return false;
  }

  bool is_implicit_arraydecay() const {
    if( clang::ImplicitCastExpr *ice = clang::dyn_cast<clang::ImplicitCastExpr>( TI_Access::tree_node() ) )
      if( ice->getCastKind() == clang::CK_ArrayToPointerDecay )
        return true;

    return false;
  }

  const WeavePos &op_before_pos( WeaverBase &wb ) const {
    assert( clang::isa<clang::UnaryOperator>( TI_Access::tree_node() ) );
    return wb.weave_pos( clang::dyn_cast<clang::UnaryOperator>( TI_Access::tree_node() )->getOperatorLoc(), WeavePos::WP_BEFORE );
  }

  const WeavePos &op_after_pos( WeaverBase &wb ) const {
    assert( clang::isa<clang::UnaryOperator>( TI_Access::tree_node() ) );
    return get_pos_after_token( clang::dyn_cast<clang::UnaryOperator>( TI_Access::tree_node() )->getOperatorLoc(), wb );
  }
};

class TI_RefAccess : public TI_Access {
public:
  TI_RefAccess() {}

  bool entity_is_const() const {
    return entity_type().isConstQualified();
  }

  // unprotect function by forwarding (we need no special sig)
  void tree_node( clang::Expr* n ) { TI_Access::tree_node( n ); }

  // target type (for the JoinPoint-API)
  virtual const clang::RecordDecl *target_class () const {
    return 0; // refs dont have a target type currently, it has to be recovered from runtime info
  }
  virtual const clang::RecordDecl *targetexpr_class () const {
    return 0; // accesses by reference  dont have a target expr
  }

  // common function to determine entity type
  clang::QualType entity_type() const {
    return TI_Access::ref_node()->getType().getNonReferenceType();
  }

  // entity type (for the JoinPoint-API)
  virtual std::string entity_type_string() const {
    return format_type( entity_type(), TI_Access::origin()->getASTContext() );
  }
};

class TI_GetRef : public TI_RefAccess {
public:
  TI_GetRef() {}

  static const TI_GetRef *of( const ACM_GetRef &loc ) {
    return static_cast<TI_GetRef *>(loc.transform_info());
  }

  virtual bool has_result() const {
    return true;
  }

  virtual clang::QualType result_type() const {
    return entity_type();
  }
};

class TI_SetRef : public TI_RefAccess {
public:
  TI_SetRef() {}

  static const TI_SetRef *of( const ACM_SetRef &loc ) {
    return static_cast<TI_SetRef *>(loc.transform_info());
  }

  virtual std::string arg_type_string (unsigned no) const {
    assert( no == 0 );

    return entity_type_string();
  }

  virtual bool has_result() const {
    return false;
  }

  virtual clang::QualType result_type() const {
    clang::ASTContext& ctx = origin()->getASTContext();
    return ctx.VoidTy;
  }
};

class TI_Construction : public TI_Code {
  clang::FunctionDecl *_decl;
  clang::CXXRecordDecl *_that_decl;

public:
  TI_Construction () : _decl (0), _that_decl (0) {}

  void decl (clang::FunctionDecl *f) { _decl = f; }
  virtual clang::Decl *decl () const { return _that_decl; }
  void that_decl (clang::CXXRecordDecl *r) { _that_decl = r; }
  virtual clang::CXXRecordDecl *that_decl () const { return _that_decl; }
  
  // that type (for the JoinPoint-API)
  virtual std::string that_type_string() const {
    return get_type_string(_that_decl);
  }

  // target type (for the JoinPoint-API)
  virtual std::string target_type_string() const {
    return get_type_string(_that_decl);
  }

  virtual std::string arg_type_string (unsigned no) const {
    if (_decl) { // user-defined constructor
      return get_type_string (_decl->getParamDecl (no), true);
    }
    else { // built-in constructor
      assert (no == 0); // may have at most one argument
      string result;
      if (_that_decl->hasCopyConstructorWithConstParam ())
        result += "const ";
      result += get_type_string(_that_decl);
      result += "&";
      return result;
    }
  }

  // entity type (for the JoinPoint-API)
  virtual std::string entity_type_string() const {
    return format_type( _decl );
  }
};

class TI_Destruction : public TI_Code {
  clang::FunctionDecl *_decl;
  clang::CXXRecordDecl *_that_decl;

public:
  TI_Destruction () : _decl (0), _that_decl (0) {}

  void decl (clang::FunctionDecl *f) { _decl = f; }
  virtual clang::Decl *decl () const { return _that_decl; }
  void that_decl (clang::CXXRecordDecl *r) { _that_decl = r; }
  virtual clang::CXXRecordDecl *that_decl () const { return _that_decl; }

  // that type (for the JoinPoint-API)
  virtual std::string that_type_string() const {
    return get_type_string(_that_decl);
  }

  // target type (for the JoinPoint-API)
  virtual std::string target_type_string() const {
    return get_type_string(_that_decl);
  }

  // entity type (for the JoinPoint-API)
  virtual std::string entity_type_string() const {
    return format_type( _decl );
  }
};

class TI_AdviceCode : public TransformInfo {
  clang::FunctionDecl *_decl;
  ThisJoinPoint _this_join_point;
  
public:
  TI_AdviceCode () : _decl (0) {}
  
  void decl(clang::FunctionDecl *f) { _decl = f; }
  virtual clang::FunctionDecl *decl () const { return _decl; }

  clang::DeclContext *Scope () const {
    return _decl ? _decl->getParent() : 0;
  }
  string name () const {
    return _decl ? _decl->getNameAsString() : "";
  }
  string qual_name () {
    return _decl ? _decl->getQualifiedNameAsString() : "";
  }
  
  ThisJoinPoint &this_join_point () { return _this_join_point; }
  const ThisJoinPoint &this_join_point () const { return _this_join_point; }

  static TI_AdviceCode *of (const ACM_AdviceCode &loc) {
    return static_cast<TI_AdviceCode*>(loc.transform_info ());
  }
};

class TI_Introduction : public TransformInfo {
public:
  virtual clang::Decl *decl () const { return 0; }

  static TI_Introduction *of (const ACM_Introduction &loc) {
    return static_cast<TI_Introduction*>(loc.transform_info ());
  }
};

class TI_Order : public TransformInfo {
public:
  virtual clang::Decl *decl () const { return 0; }

  static TI_Order *of (const ACM_Order &loc) {
    return static_cast<TI_Order*>(loc.transform_info ());
  }
};

class TI_Pointcut : public TransformInfo {
  clang::FunctionDecl *_decl;
  int _phase;
  PointCutExpr *_pce;
  clang::FullSourceLoc _loc;
public:
  TI_Pointcut () : _decl(0), _phase (0), _pce (0) {}
  ~TI_Pointcut () { PointCutExpr::destroy(_pce); }

  void decl (clang::FunctionDecl *c) { _decl = c; }
  virtual clang::Decl *decl () const { return _decl; }

  void phase (int p) { _phase = p; }
  int phase () const { return _phase; }
  void set_pce (PointCutExpr *pce) { _pce = pce; }
  PointCutExpr *get_pce () const { return _pce; }
  void set_location (clang::FullSourceLoc loc) { _loc = loc; }
  clang::FullSourceLoc get_location () const { return _loc; }

  static TI_Pointcut *of (const ACM_Pointcut &loc) {
    return static_cast<TI_Pointcut*>(loc.transform_info ());
  }
};

class TI_ClassSlice : public TransformInfo {
public:
  struct SliceBody {
    enum InsertType {
      TARGET_NAME,
      TARGET_QUAL_NAME,
      JP_NAME
    };
    std::string text;
    std::vector<std::pair<size_t, InsertType> > positions;
  };

private:
  ACFileID _slice_unit;

  // new phase 1 implementation:
  SliceBody _tokens; // class slice body
  std::string _base_intro;
  bool _has_base_intro, _has_member_intro;
  std::list<SliceBody> _non_inline_members; // members defined outside of body
  std::vector<ACFileID> _non_inline_member_units; // corresponding source units

public:

  TI_ClassSlice () : _slice_unit (0), _has_base_intro (false),
                     _has_member_intro (false) {}

  // new phase 1 implementation:
  void set_tokens (const SliceBody &body, const std::string &base_intro,
                   bool has_base_intro, bool has_member_intro) {
    _tokens = body;
    _base_intro = base_intro;
    _has_base_intro = has_base_intro;
    _has_member_intro = has_member_intro;
  }
  const SliceBody &get_tokens () const { return _tokens; }
  std::list<SliceBody> &non_inline_members () { return _non_inline_members; }
  std::vector<ACFileID> &non_inline_member_units () { return _non_inline_member_units; }
  void analyze_tokens (bool &has_base_intro, bool &has_member_intro) {
    has_base_intro = _has_base_intro;
    has_member_intro = _has_member_intro;
  }
  const std::string &base_intro () const {
    return _base_intro;
  }
  // end - new phase 1 implementation

  virtual clang::Decl *decl () const { return 0; }
  void slice_unit (ACFileID su) { _slice_unit = su; }
  ACFileID slice_unit () const { return _slice_unit; }

  static TI_ClassSlice *of (const ACM_ClassSlice &loc) {
    return static_cast<TI_ClassSlice*>(loc.transform_info ());
  }
};

#endif // __ClangTransformInfo_h__