File: Naming.cc

package info (click to toggle)
aspectc%2B%2B 1.0pre4~svn.20090918-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 117,308 kB
  • ctags: 410,601
  • sloc: cpp: 1,883,007; ansic: 17,279; sh: 2,190; makefile: 1,088
file content (445 lines) | stat: -rw-r--r-- 12,181 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
// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003  The 'ac++' developers (see aspectc.org)
//
// This program is free software;  you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA  02111-1307  USA

#include <string.h>
#include <ctype.h>

#include "Naming.h"
#ifdef ACMODEL
#include "Utils.h"
#else
#include "JoinPointLoc.h"
#endif
#include "AdviceInfo.h"
#include "JoinPoint.h"
#include "AspectInfo.h"

#include "Puma/CFunctionInfo.h"
#include "Puma/CArgumentInfo.h"
#include "Puma/CClassInfo.h"
#include "Puma/ACAspectInfo.h"
#include "Puma/CTypeInfo.h"
#include "Puma/CLinkage.h"
#include "Puma/CScopeInfo.h"
#include "Puma/FileUnit.h"

void Naming::scope_name (ostream &out, CScopeInfo *scope) {
  assert (scope);
  // check if we reached the global scope
  if (scope->FileInfo ())
    return;

  // print the parent scope first
  if (scope->Scope ())
    scope_name (out, scope->Scope ());

  // print the scope name, preceeded by the length of the string
  if (scope->isAnonymous ())
    out << "4ANON"; // handle this better!
  else if (strcmp (scope->Name ().c_str (), "<unnamed>") == 0)
    out << "12_GLOBAL__N_1"; // same symbol as used by g++
  else
    out << strlen (scope->Name ()) << scope->Name ();
}

bool Naming::unary (CFunctionInfo *func) {
  return ((func->TypeInfo()->isMethod() && func->Arguments() == 0) ||
    (!func->TypeInfo()->isMethod() && func->Arguments() == 1));
}

void Naming::conv_name (ostream &out, CFunctionInfo *func)
 {
   out << "cv";
   func->ConversionType ()->Mangled (out);
 }

void Naming::op_name (ostream &out, CFunctionInfo *func)
 {
   const char *name = func->Name () + strlen ("operator ");

   if (strcmp (name, "new") == 0)
      out << "nw";
   else if (strcmp (name, "new[]") == 0)
      out << "na";
   else if (strcmp (name, "delete") == 0)
      out << "dl";
   else if (strcmp (name, "delete[]") == 0)
      out << "da";
   else if (strcmp (name, "-") == 0 && unary (func))
      out << "ng";
   else if (strcmp (name, "&") == 0 && unary (func))
      out << "ad";
   else if (strcmp (name, "*") == 0 && unary (func))
      out << "de";
   else if (strcmp (name, "~") == 0)
      out << "co";
   else if (strcmp (name, "+") == 0)
      out << "pl";
   else if (strcmp (name, "-") == 0 && !unary (func))
      out << "mi";
   else if (strcmp (name, "*") == 0 && !unary (func))
      out << "ml";
   else if (strcmp (name, "/") == 0)
      out << "dv";
   else if (strcmp (name, "%") == 0)
      out << "rm";
   else if (strcmp (name, "&") == 0 && !unary (func))
      out << "an";
   else if (strcmp (name, "|") == 0)
      out << "or";
   else if (strcmp (name, "^") == 0)
      out << "eo";
   else if (strcmp (name, "=") == 0)
      out << "aS";
   else if (strcmp (name, "+=") == 0)
      out << "pL";
   else if (strcmp (name, "-=") == 0)
      out << "mI";
   else if (strcmp (name, "*=") == 0)
      out << "mL";
   else if (strcmp (name, "/=") == 0)
      out << "dV";
   else if (strcmp (name, "%=") == 0)
      out << "rM";
   else if (strcmp (name, "&=") == 0)
      out << "aN";
   else if (strcmp (name, "|=") == 0)
      out << "oR";
   else if (strcmp (name, "^=") == 0)
      out << "eO";
   else if (strcmp (name, "<<") == 0)
      out << "ls";
   else if (strcmp (name, ">>") == 0)
      out << "rs";
   else if (strcmp (name, "<<=") == 0)
      out << "lS";
   else if (strcmp (name, ">>=") == 0)
      out << "rS";
   else if (strcmp (name, "==") == 0)
      out << "eq";
   else if (strcmp (name, "!=") == 0)
      out << "ne";
   else if (strcmp (name, "<") == 0)
      out << "lt";
   else if (strcmp (name, ">") == 0)
      out << "gt";
   else if (strcmp (name, "<=") == 0)
      out << "le";
   else if (strcmp (name, ">=") == 0)
      out << "ge";
   else if (strcmp (name, "!") == 0)
      out << "nt";
   else if (strcmp (name, "&&") == 0)
      out << "aa";
   else if (strcmp (name, "||") == 0)
      out << "oo";
   else if (strcmp (name, "++") == 0)
      out << "pp";
   else if (strcmp (name, "--") == 0)
      out << "mm";
   else if (strcmp (name, ",") == 0)
      out << "cm";
   else if (strcmp (name, "->*") == 0)
      out << "pm";
   else if (strcmp (name, "->") == 0)
      out << "pt";
   else if (strcmp (name, "()") == 0)
      out << "cl";
   else if (strcmp (name, "[]") == 0)
      out << "ix";
   else if (strcmp (name, "?") == 0)
      out << "qu";
   else
    {
      cout << "no mangling for unknown operator \"" << name << "\""
	   << endl;
      out << "unknown_operator";
    }
 }

void Naming::constr_name (ostream &out, CFunctionInfo *func) {
  out << "C1";
}

void Naming::destr_name (ostream &out, CFunctionInfo *func) {
  out << "D1";
}

void Naming::mangle (ostream &out, CObjectInfo *obj) {

  // check if this object is a function:
  CFunctionInfo *func = obj->FunctionInfo ();

  // determine the scope
  CScopeInfo *scope = obj->ClassScope (); // class scope
  if (!scope)
    scope = obj->Scope (); // lexical scope

  // objects with C linkage and global namespace variables are not mangled
  if (obj->Language () == CLanguage::LANG_C ||
      (scope->GlobalScope () && !obj->FunctionInfo ())) {
    out << obj->Name ();
    return;
  }

  // mangle the function name
  out << "_Z";

  // print the (possibly nested) scope of the function
  if (scope) {
    out << "N";
    // possible CV qualifier of member functions must be printed here
    if (func) {
      if (func->TypeInfo ()->isConst ())
        out << "K";
      if (func->TypeInfo ()->isVolatile ())
        out << "V";
    }
    scope_name (out, scope);
  }

  if (func) {
    // print the function name (special handling)
    if (func->isOperator ())
      op_name (out, func);
    else if (func->isConversion ())
      conv_name (out, func);
    else if (func->isConstructor ())
      constr_name (out, func);
    else if (func->isDestructor ())
      destr_name (out, func);
    else
      out << strlen (func->Name ()) << func->Name ();
  }
  else {
    out << strlen (obj->Name ()) << obj->Name ();
  }

  // if this was a nested name, add the 'E'
  if (scope)
    out << "E";

  if (func) {
    // print the argument types (also mangled)
    if (func->Arguments () == 0)
      out << "v";
    else {
      for (unsigned a = 0; a < func->Arguments (); a++)
        func->Argument (a)->TypeInfo ()->Mangled (out);
    }
  }
}

#ifdef ACMODEL
void Naming::call_wrapper (ostream& out, ACM_Call *jpl, unsigned depth) {
#else
void Naming::call_wrapper (ostream& out, JPL_MethodCall *jpl, unsigned depth) {
#endif
  out << "__call_";
  mangle (out, TransformInfo::of (*jpl)->assoc_obj ());
#ifdef ACMODEL
  if (jpl->get_lid () >= 0)
    out << "_" << jpl->get_lid ();
#else
  if (jpl->lid () >= 0)
    out << "_" << jpl->lid ();
#endif
  out << "_" << depth;
}

#ifdef ACMODEL
void Naming::exec_inner (ostream& out, ACM_Any *jpl) {
#else
void Naming::exec_inner (ostream& out, JoinPointLoc *jpl) {
#endif
  CFunctionInfo *func = TransformInfo::of (*jpl)->assoc_obj ()->FunctionInfo ();
  assert (func);
  out << "__exec_old_";
  if (func->isConstructor ())
    constr_name (out, func);
  else if (func->isDestructor ())
    destr_name (out, func);
  else if (func->isOperator ())
    op_name (out, func);
  else if (func->isConversion ())
    conv_name (out, func);
  else
    out << func->Name ();
}

#ifdef ACMODEL
void Naming::action_wrapper (ostream &out, ACM_Any *loc, unsigned depth) {
  out << "__action_func";
}

void Naming::exec_advice (ostream& out, ACM_Execution *jpl, AdviceInfo *adv) {
  out << "__" << (adv->name () + 1);
}

void Naming::call_advice (ostream& out, ACM_Call *jpl, AdviceInfo *adv) {
  out << "__" << (adv->name () + 1);
}
#else
void Naming::action_wrapper (ostream &out, JoinPointLoc *loc, unsigned depth) {
  out << "__action_func";
}

void Naming::exec_advice (ostream& out, JPL_Method *jpl, AdviceInfo *adv) {
  out << "__" << (adv->name () + 1);
}

void Naming::call_advice (ostream& out, JPL_MethodCall *jpl, AdviceInfo *adv) {
  out << "__" << (adv->name () + 1);
}
#endif

void Naming::call_func (ostream& out, CStructure *in, AdviceInfo *ad) {
   out << "__call_checked_";
   out << in->Name ();
   out << "_";
   out << ad->Scope ()->Name ();
   out << "_";
   out << (ad->name () + 1);
}

//void Naming::aspectof(ostream& out, JoinPointLoc *loc, AspectInfo *ai) {
//  out << "::" << ai->name () << "::aspectof";
//}

#ifdef ACMODEL
void Naming::local_id (ostream& out, ACM_Any *jpl) {
  if (jpl->type_val () == JPT_Call) {
    ACM_Call *jpl_code = (ACM_Call*)jpl;
    if (jpl_code->get_lid () >= 0)
      out << "_" << jpl_code->get_lid();
  }
}

void Naming::tjp_struct(ostream& out, ACM_Any *loc, int depth) {
  out << "TJP_";
  mangle(out, TransformInfo::of (*loc)->assoc_obj());
  local_id (out, loc);
  out << "_" << depth;
}

void Naming::tjp_instance(ostream& out, ACM_Any *loc) {
  out << "tjp";
}

void Naming::tjp_args_array(ostream& out, ACM_Any *loc) {
  out << "args_";
  mangle(out, TransformInfo::of (*loc)->assoc_obj ());
  local_id (out, loc);
}

void Naming::tjp_argtypes(ostream& out, ACM_Any *loc) {
  out << "argtypes_";
  mangle(out, TransformInfo::of (*loc)->assoc_obj());
  local_id (out, loc);
}

void Naming::cflow (ostream& out, ACM_Aspect &jpl_aspect, int index) {
  out << "::AC::CFlow<" << signature(jpl_aspect) << "," << index << ">";
}
#else
void Naming::local_id (ostream& out, JoinPointLoc *jpl) {
  if (jpl->type () & JoinPointLoc::Code) {
    JPL_Code *jpl_code = (JPL_Code*)jpl;
    if (jpl_code->lid () >= 0)
      out << "_" << jpl_code->lid();
  }
}

void Naming::tjp_struct(ostream& out, JoinPointLoc *loc, int depth) {
  out << "TJP_";
  mangle(out, TransformInfo::of (*loc)->assoc_obj());
  local_id (out, loc);
  out << "_" << depth;
}

void Naming::tjp_instance(ostream& out, JoinPointLoc *loc) {
  out << "tjp";
}

void Naming::tjp_args_array(ostream& out, JoinPointLoc *loc) {
  out << "args_";
  mangle(out, TransformInfo::of (*loc)->assoc_obj ());
  local_id (out, loc);
}

void Naming::tjp_argtypes(ostream& out, JoinPointLoc *loc) {
  out << "argtypes_";
  mangle(out, TransformInfo::of (*loc)->assoc_obj());
  local_id (out, loc);
}

void Naming::cflow (ostream& out, JPL_Aspect &jpl_aspect, int index) {
  out << "::AC::CFlow<" << jpl_aspect.signature () << "," << index << ">";
}
#endif

bool Naming::is_tjp_object (const char *candidate) {
  return strcmp (candidate, "tjp") == 0 ||
    strcmp (candidate, "thisJoinPoint") == 0;
}

void Naming::tjp_typedef (ostream& out, const char *name) {
  out << "__JP_" << name;
}

void Naming::type_check_function (ostream &out, CRecord *in, CRecord *cfor) {
  out << "__ac_";
  scope_name (out, in);
  out << "is";
  scope_name (out, cfor);
}

void Naming::type_check_func (ostream &out, CRecord *in, const string &name) {
  out << "__ac_";
  scope_name (out, in);
  out << "is_" << name;
}

void Naming::guard (ostream &out, FileUnit *unit) {
  out << "__ac_guard_";
  mangle_file (out, unit);
}

void Naming::mangle_file (ostream &out, FileUnit *unit) {
  // TODO: better is unit->absolutePath() but the implementation is strange and
  // has to be changed.
  char *name = FileUnit::absolutePath (unit->Unit::name ());
  mangle_file (out, name);
  if (name)
    delete[] name;
}

void Naming::mangle_file (ostream &out, const char *name) {
  const char *curr = name;
  while (*curr) {
    if (*curr == '_' || isalnum (*curr))
      out << *curr;
    else if (*curr == '/' || *curr == '\\' || *curr == ':' || *curr == '.')
      out << "_";
    else
      out << (int)*curr;
    curr++;
  }
  out << "__";
}