Backend.jadd 62.2 KB
Newer Older
1
aspect BackendAbstractGrammar {
2

Johannes Mey's avatar
Johannes Mey committed
3
  public static String ASTNode.listClass = "ArrayList";
4
5
  public static String ASTNode.jastAddListType = "List";

6
7
  public static boolean ASTNode.resolverHelper = false;
  public static boolean ASTNode.serializer = false;
8
  public static boolean ASTNode.useJastAddNames = false;
Johannes Mey's avatar
Johannes Mey committed
9
10
11
12
13
14
15
16
17
18
19
20
21

  public String Program.generateAbstractGrammar() {
    StringBuilder sb = new StringBuilder();
    generateAbstractGrammar(sb);
    return sb.toString();
  }

  public void Program.generateAbstractGrammar(StringBuilder sb) {
    for (TypeDecl td: getTypeDecls()) {
      td.generateAbstractGrammar(sb);
    }
  }

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
  public void TypeDecl.generateUnresolvedClass(StringBuilder sb) {
    if (getAbstract()) {
      sb.append(ind(1) + "abstract ");
    } else {
      sb.append(ind(1));
    }
    sb.append("class " + "Unresolved$" + getID() + " extends " + getID() + "  implements Unresolved$Node {\n");

    sb.append(ind(2) + "private String unresolved$Token;\n");
    sb.append(ind(2) + "public String getUnresolved$Token() {\n");
    sb.append(ind(3) + "return unresolved$Token;\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "void setUnresolved$Token(String token) {\n");
    sb.append(ind(3) + "this.unresolved$Token = token;\n");
    sb.append(ind(2) + "}\n");

    sb.append(ind(2) + "private boolean unresolved$ResolveOpposite;\n");
    sb.append(ind(2) + "public boolean getUnresolved$ResolveOpposite() {\n");
    sb.append(ind(3) + "return unresolved$ResolveOpposite;\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "void setUnresolved$ResolveOpposite(boolean resolveOpposite) {\n");
    sb.append(ind(3) + "this.unresolved$ResolveOpposite = resolveOpposite;\n");
    sb.append(ind(2) + "}\n");

    sb.append(ind(1) + "}\n");

    sb.append(ind(1) + "Unresolved$Node " + getID() + ".as$Unresolved() {\n");
    sb.append(ind(2) + "return null;\n");
    sb.append(ind(1) + "}\n");
    sb.append(ind(1) + "Unresolved$Node Unresolved$" + getID() + ".as$Unresolved() {\n");
    sb.append(ind(2) + "return this;\n");
    sb.append(ind(1) + "}\n");

    sb.append(ind(1) + "boolean " + getID() + ".is$Unresolved() {\n");
    sb.append(ind(2) + "return false;\n");
    sb.append(ind(1) + "}\n");
    sb.append(ind(1) + "boolean Unresolved$" + getID() + ".is$Unresolved() {\n");
    sb.append(ind(2) + "return true;\n");
    sb.append(ind(1) + "}\n");
  }

Johannes Mey's avatar
Johannes Mey committed
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
  public void TypeDecl.generateAbstractGrammar(StringBuilder sb) {
    if (getAbstract()) {
      sb.append("abstract ");
    }
    sb.append(getID());
    if (hasSuper()) {
      sb.append(" : " + getSuper());
    }

    if (getNumComponent() > 0 || relationComponents().size() > 0) {
      sb.append(" ::=");
    }
    for (Component c: getComponents()) {
      sb.append(" ");
      sb.append(c.generateAbstractGrammar());
    }
    for (RelationComponent c: relationComponents()) {
      sb.append(" ");
      sb.append(c.generateAbstractGrammar());
    }

    sb.append(";\n");
  }

  public String Component.generateAbstractGrammar() {
    if (getID().equals(getTypeUse().toString())) {
      return getTypeUse().toString();
    } else {
      return getID() + ":" + getTypeUse();
    }
  }
  public String ListComponent.generateAbstractGrammar() {
    return super.generateAbstractGrammar() + "*";
  }
  public String OptComponent.generateAbstractGrammar() {
    return "[" + super.generateAbstractGrammar() + "]";
  }
  public String NTAComponent.generateAbstractGrammar() {
    return "/" + super.generateAbstractGrammar() + "/";
  }
  public String TokenComponent.generateAbstractGrammar() {
    return "<" + getID() + ":" + getTypeUse() + ">";
  }

  public String RelationComponent.generateAbstractGrammar() {
    return "<" + getImplAttributeName() + ":" + ofTypeDecl() + ">";
  }
  public String ManyRelationComponent.generateAbstractGrammar() {
    return "<" + getImplAttributeName() + ":" + ASTNode.listClass + "<" + ofTypeDecl() + ">>";
  }

  public String RelationComponent.getImplAttributeName() {
    return "_impl_" + getID();
  }
117
118
119
120
121
122
123
124
125
126

  public String RelationComponent.getImplAttributeField() {
    //  tt.bind("TypeInSignature", ASTNode.convTypeNameToSignature(type()));
    return "token" + ofTypeDecl() + "__impl_" + getID();
  }

  public String ManyRelationComponent.getImplAttributeField() {
    //  tt.bind("TypeInSignature", ASTNode.convTypeNameToSignature(type()));
    return "token" + listClass + "_" + ofTypeDecl() + "___impl_" + getID();
  }
127
128
129
}

aspect BackendAspect {
Johannes Mey's avatar
Johannes Mey committed
130
131
132
133
134
135
136
137
138
  public String Program.generateAspect() {
    StringBuilder sb = new StringBuilder();
    generateAspect(sb);
    return sb.toString();
  }

  public void Program.generateAspect(StringBuilder sb) {
    sb.append("import java.util.ArrayList;\n");
    sb.append("import java.util.Collections;\n");
139
140
    sb.append("import java.time.Instant;\n");
    sb.append("import java.time.Period;\n");
Johannes Mey's avatar
Johannes Mey committed
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
    sb.append("aspect RelAstAPI {\n");

    for (TypeDecl td: getTypeDecls()) {
      if (td.needsConstructor()) {
        td.generateConstructor(sb);
      }
    }
    for (Relation r: getRelations()) {
      r.generateAPI(sb);
    }

    generateLowerBoundCheck(sb);

    sb.append(ind(1) + "public static void ASTNode.assertNotNull(Object obj) {\n");
    sb.append(ind(2) + "if (obj == null) {\n");
    sb.append(ind(3) + "throw new NullPointerException();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
    sb.append("}\n");
  }

  public void TypeDecl.generateConstructor(StringBuilder sb) {
    sb.append(ind(1) + "public " + getID() + "." + getID() + "(");
    int i = 0;
    for (Component c: componentsTransitive()) {
      sb.append(c.constructorParameter());
      if (++i < componentsTransitive().size()) {
        sb.append(", ");
      }
    }
    sb.append(") {\n");
    for (Component c: componentsTransitive()) {
      sb.append(ind(2) + c.constructorSetMethod() + "(" + c.getID() + ");\n");
    }
    sb.append(ind(1) + "}\n");
  }
  public String Component.constructorParameter() {
    return getTypeUse() + " " + getID();
  }
  public String ListComponent.constructorParameter() {
181
    return ASTNode.jastAddListType + "<" + getTypeUse() + "> " + getID();
Johannes Mey's avatar
Johannes Mey committed
182
183
184
185
186
187
188
189
190
191
192
193
194
  }
  public String OptComponent.constructorParameter() {
    return "Opt<" + getTypeUse() + "> " + getID();
  }
  public String Component.constructorSetMethod() {
    return "set" + getID();
  }
  public String ListComponent.constructorSetMethod() {
    return "set" + getID() + "List";
  }
  public String OptComponent.constructorSetMethod() {
    return "set" + getID() + "Opt";
  }
195
196
197
}

aspect BackendAPI {
Johannes Mey's avatar
Johannes Mey committed
198
199
200
201
202
203
  public void Relation.generateAPI(StringBuilder sb) {
    sb.append(ind(1) + "// " + prettyPrint() + "\n");
    getDirection().generateAPI(sb);
    sb.append("\n");
  }
  public abstract void Direction.generateAPI(StringBuilder sb);
204
205


Johannes Mey's avatar
Johannes Mey committed
206
207
208
  inh Relation Direction.relation();
  eq Relation.getChild().relation() = this;
  eq Program.getChild().relation() = null;
209

Johannes Mey's avatar
Johannes Mey committed
210
211
212
  public String RelationComponent.nameCapitalized() {
    return name().substring(0,1).toUpperCase() + name().substring(1);
  }
213
}
214

215
aspect BackendDirectedAPI {
Johannes Mey's avatar
Johannes Mey committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  public void RightDirection.generateAPI(StringBuilder sb) {
    relation().getLeft().generateDirectedAPI(sb);
  }

  public abstract void RelationComponent.generateDirectedAPI(StringBuilder sb);
  public void OneRelationComponent.generateDirectedAPI(StringBuilder sb) {
    generateDirectedZeroOneAPI(sb, false);
  }
  public void OptionalRelationComponent.generateDirectedAPI(StringBuilder sb) {
    generateDirectedZeroOneAPI(sb, true);

    generateExtraOptAPI(sb);
  }
  public void RelationComponent.generateDirectedZeroOneAPI(StringBuilder sb, boolean optional) {
    // Get
    generateGetOne(sb);

    // Set
234
    sb.append(ind(1) + "public " + toTypeDecl() + " " + toTypeDecl());
Johannes Mey's avatar
Johannes Mey committed
235
236
237
238
239
    sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    if (!optional) {
      sb.append(ind(2) + "assertNotNull(o);\n");
    }
    sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
240
    sb.append(ind(2) + "return this;\n");
Johannes Mey's avatar
Johannes Mey committed
241
242
243
244
245
    sb.append(ind(1) + "}\n");
  }

  public void ManyRelationComponent.generateDirectedAPI(StringBuilder sb) {
    // Get
246
247
248
249
250
251
    sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl() + ".");
    if (useJastAddNames) {
      // getXs
      sb.append("get" + nameCapitalized() + "s() {\n");
      sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
      sb.append(ind(1) + "}\n");
252

253
254
255
256
257
258
      // getXList
      sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl());
      sb.append(".get" + nameCapitalized() + "List() {\n");
    } else {
      sb.append(name() + "() {\n");
    }
259
260
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
    // resolve the entire list
261
    if (resolverHelper | serializer) {
262
      sb.append(ind(2) + "if (l != null) {\n");
263
        sb.append(ind(3) + "boolean changed = false;\n");
264
265
        sb.append(ind(3) + "for (int i = 0; i < l.size(); i++) {\n");
          sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
266
          sb.append(ind(4) + "if (element.is$Unresolved()) {\n");
267
            sb.append(ind(5) + "changed = true;\n");
268
            sb.append(ind(5) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "ByToken(element.as$Unresolved().getUnresolved$Token(), i);\n");
269
270
271
            sb.append(ind(5) + "l.set(i, resolvedElement);\n");
          sb.append(ind(4) + "}\n");
        sb.append(ind(3) + "}\n");
272
273
274
        sb.append(ind(3) + "if (changed) {\n");
          sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
        sb.append(ind(3) + "}\n");
275
276
277
278
      sb.append(ind(2) + "}\n");
    }
    sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
    sb.append(ind(1) + "}\n");
Johannes Mey's avatar
Johannes Mey committed
279
280
281

    // Add
    sb.append(ind(1) + "public void " + toTypeDecl() + ".add");
282
283
284
    if (!useJastAddNames) {
      sb.append("To");
    }
Johannes Mey's avatar
Johannes Mey committed
285
286
    sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    sb.append(ind(2) + "assertNotNull(o);\n");
287
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
288
289
290
291
292
293
294
    sb.append(ind(2) + "if (list == null) {\n");
    sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "list.add(o);\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(1) + "}\n");

295
    // Insert / add at specific position
296
297
298
299
300
    sb.append(ind(1) + "public void " + toTypeDecl() + ".add");
    if (!useJastAddNames) {
      sb.append("To");
    }
    sb.append(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
301
    sb.append(ind(2) + "assertNotNull(o);\n");
302
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
303
304
305
306
307
308
309
    sb.append(ind(2) + "if (list == null) {\n");
    sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "list.add(index, o);\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(1) + "}\n");

Johannes Mey's avatar
Johannes Mey committed
310
311
    // Remove
    sb.append(ind(1) + "public void " + toTypeDecl() + ".remove");
312
313
314
    if (!useJastAddNames) {
      sb.append("From");
    }
Johannes Mey's avatar
Johannes Mey committed
315
316
    sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    sb.append(ind(2) + "assertNotNull(o);\n");
317
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
318
319
320
321
322
323
324
    sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
    sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }

  public void RelationComponent.generateGetOne(StringBuilder sb) {
325
326
327
328
329
330
331
    sb.append(ind(1) + "public " + ofTypeDecl() + " " + toTypeDecl() + ".");
    if (useJastAddNames) {
      sb.append("get" + nameCapitalized());
    } else {
      sb.append(name());
    }
    sb.append("() {\n");
332
    if (resolverHelper | serializer) {
333
334
335
      sb.append(ind(2) + "if (" + getImplAttributeField() + " != null && " + getImplAttributeField() + ".is$Unresolved()) {\n");
        sb.append(ind(3) + "if (" + getImplAttributeField() + ".as$Unresolved().getUnresolved$ResolveOpposite()) {\n");
          sb.append(ind(4) + "set" + nameCapitalized() + "(resolve" + nameCapitalized() + "ByToken(" + getImplAttributeField() + ".as$Unresolved().getUnresolved$Token()));\n");
336
        sb.append(ind(3) + "} else {\n");
337
          sb.append(ind(4) + "set" + getImplAttributeName() + "(resolve" + nameCapitalized() + "ByToken(" + getImplAttributeField() + ".as$Unresolved().getUnresolved$Token()));\n");
338
        sb.append(ind(3) + "}\n");
339
340
      sb.append(ind(2) + "}\n");
    }
Johannes Mey's avatar
Johannes Mey committed
341
342
343
344
345
346
347
348
    sb.append(ind(2) + "return get" + getImplAttributeName() + "();\n");
    sb.append(ind(1) + "}\n");
  }

  public void RelationComponent.generateExtraOptAPI(StringBuilder sb) {
    // has
    sb.append(ind(1) + "public boolean " + toTypeDecl());
    sb.append(".has" + nameCapitalized() + "() {\n");
349
350
351
352
353
354
355
    sb.append(ind(2) + "return ");
    if (useJastAddNames) {
      sb.append("get" + nameCapitalized());
    } else {
      sb.append(name());
    }
    sb.append("() != null;\n");
Johannes Mey's avatar
Johannes Mey committed
356
357
358
359
360
361
362
363
    sb.append(ind(1) + "}\n");

    // clear
    sb.append(ind(1) + "public void " + toTypeDecl());
    sb.append(".clear" + nameCapitalized() + "() {\n");
    sb.append(ind(2) + "set" + nameCapitalized() + "(null);\n");
    sb.append(ind(1) + "}\n");
  }
364
}
365

366
aspect BackendBidirectionalAPI {
Johannes Mey's avatar
Johannes Mey committed
367
368
369
370
371
372
373
374
375
376
377
378
379
  public void Bidirectional.generateAPI(StringBuilder sb) {
    RelationComponent l = relation().getLeft();
    RelationComponent r = relation().getRight();

    if (l.multiplicityOne()) {
      if (r.multiplicityOne()) {
        l.generateBiOneOne(sb, false);
        r.generateBiOneOne(sb, false);
      } else if (r.multiplicityOpt()) {
        l.generateBiOneOne(sb, false);
        r.generateBiOneOne(sb, true);
      } else if (r.multiplicityMany()) {
        l.generateBiOneMany(sb, false);
Johannes Mey's avatar
Johannes Mey committed
380
        r.generateBiManyOne(sb, l);
Johannes Mey's avatar
Johannes Mey committed
381
382
383
384
385
386
387
388
389
390
      }
    } else if (l.multiplicityOpt()) {
      if (r.multiplicityOne()) {
        l.generateBiOneOne(sb, true);
        r.generateBiOneOne(sb, false);
      } else if (r.multiplicityOpt()) {
        l.generateBiOneOne(sb, true);
        r.generateBiOneOne(sb, true);
      } else if (r.multiplicityMany()) {
        l.generateBiOneMany(sb, true);
Johannes Mey's avatar
Johannes Mey committed
391
        r.generateBiManyOne(sb, l);
Johannes Mey's avatar
Johannes Mey committed
392
393
394
      }
    } else if (l.multiplicityMany()) {
      if (r.multiplicityOne()) {
Johannes Mey's avatar
Johannes Mey committed
395
        l.generateBiManyOne(sb, r);
Johannes Mey's avatar
Johannes Mey committed
396
397
        r.generateBiOneMany(sb, false);
      } else if (r.multiplicityOpt()) {
Johannes Mey's avatar
Johannes Mey committed
398
        l.generateBiManyOne(sb, r);
Johannes Mey's avatar
Johannes Mey committed
399
400
        r.generateBiOneMany(sb, true);
      } else if (r.multiplicityMany()) {
Johannes Mey's avatar
Johannes Mey committed
401
402
        l.generateBiManyMany(sb, r);
        r.generateBiManyMany(sb, l);
Johannes Mey's avatar
Johannes Mey committed
403
404
405
406
407
408
409
410
411
      }
    }
  }

  public void RelationComponent.generateBiOneOne(StringBuilder sb, boolean isOpt) {
    // Get
    generateGetOne(sb);

    // Set
412
    sb.append(ind(1) + "public " + toTypeDecl() + " " + toTypeDecl());
Johannes Mey's avatar
Johannes Mey committed
413
414
415
416
    sb.append(".set" + nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    if (!isOpt) {
      sb.append(ind(2) + "assertNotNull(o);\n");
    }
417
    // unset the old opposite
418
419
    sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n");
    sb.append(ind(3) + "" + getImplAttributeField() + ".set" + otherSide().getImplAttributeName() + "(null);\n");
Johannes Mey's avatar
Johannes Mey committed
420
    sb.append(ind(2) + "}\n");
421
    if (resolverHelper | serializer) {
422
      sb.append(ind(2) + "if (o != null && !o.is$Unresolved() && o." + otherSide().getImplAttributeField() + " != null) {\n");
423
    } else {
424
      sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n");
425
    }
426
    sb.append(ind(3) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(null);\n");
Johannes Mey's avatar
Johannes Mey committed
427
428
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");
429
    if (resolverHelper | serializer) {
430
      sb.append(ind(2) + "if (o == null || !o.is$Unresolved()) {\n");
431
432
433
434
435
436
437
      if (isOpt) {
        sb.append(ind(3) + "if (o != null) {\n");
        sb.append(ind(4) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
        sb.append(ind(3) + "}\n");
      } else {
        sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
      }
Johannes Mey's avatar
Johannes Mey committed
438
439
      sb.append(ind(2) + "}\n");
    } else {
440
441
442
443
444
445
446
      if (isOpt) {
        sb.append(ind(2) + "if (o != null) {\n");
        sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
        sb.append(ind(2) + "}\n");
      } else {
        sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
      }
Johannes Mey's avatar
Johannes Mey committed
447
    }
448

449
    sb.append(ind(2) + "return this;\n");
Johannes Mey's avatar
Johannes Mey committed
450
451
452
453
454
455
456
    sb.append(ind(1) + "}\n");

    if (isOpt) {
      generateExtraOptAPI(sb);
    }
  }

Johannes Mey's avatar
Johannes Mey committed
457
  public void RelationComponent.generateBiManyMany(StringBuilder sb, RelationComponent opposite) {
Johannes Mey's avatar
Johannes Mey committed
458
    // Get
459
460
461
462
463
464
    sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl() + ".");
    if (useJastAddNames) {
      // getXs
      sb.append("get" + nameCapitalized() + "s() {\n");
      sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
      sb.append(ind(1) + "}\n");
465

466
467
468
469
470
471
      // getXList
      sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl());
      sb.append(".get" + nameCapitalized() + "List() {\n");
    } else {
      sb.append(name() + "() {\n");
    }
472
473
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
    // resolve the entire list
474
    if (resolverHelper | serializer) {
475
      sb.append(ind(2) + "if (l != null) {\n");
476
        sb.append(ind(3) + "boolean changed = false;\n");
477
478
        sb.append(ind(3) + "for (int i = 0; i < l.size(); i++) {\n");
          sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
479
          sb.append(ind(4) + "if (element.is$Unresolved()) {\n");
480
            sb.append(ind(5) + "changed = true;\n");
481
482
            sb.append(ind(5) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "ByToken(element.as$Unresolved().getUnresolved$Token(), i);\n");
            sb.append(ind(5) + "if (resolvedElement != null && element.as$Unresolved().getUnresolved$ResolveOpposite()) {\n");
483
              sb.append(ind(6) + ASTNode.listClass + "<" + toTypeDecl() + "> otherList = resolvedElement." + opposite.getImplAttributeField() + ";\n");
484
              sb.append(ind(6) + "if (otherList == null) {\n");
485
                sb.append(ind(7) + "otherList = new " + listClass + "<>();\n");
486
487
              sb.append(ind(6) + "}\n");
              sb.append(ind(6) + "otherList.add(this);\n");
Johannes Mey's avatar
Johannes Mey committed
488
              sb.append(ind(6) + "resolvedElement.set" + opposite.getImplAttributeName() + "(otherList);\n");
489
490
491
492
            sb.append(ind(5) + "}\n");
            sb.append(ind(5) + "l.set(i, resolvedElement);\n");
          sb.append(ind(4) + "}\n");
        sb.append(ind(3) + "}\n");
493
494
495
        sb.append(ind(3) + "if (changed) {\n");
          sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
        sb.append(ind(3) + "}\n");
496
497
498
499
      sb.append(ind(2) + "}\n");
    }
    sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
    sb.append(ind(1) + "}\n");
Johannes Mey's avatar
Johannes Mey committed
500
501
502

    // Add
    sb.append(ind(1) + "public void " + toTypeDecl() + ".add");
503
504
505
    if (!useJastAddNames) {
      sb.append("To");
    }
Johannes Mey's avatar
Johannes Mey committed
506
507
    sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    sb.append(ind(2) + "assertNotNull(o);\n");
508
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
509
510
511
    sb.append(ind(2) + "if (list == null) {\n");
    sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
512
    sb.append(ind(2) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
513
514
515
516
517
518
519
520
521
    sb.append(ind(2) + "if (list2 == null) {\n");
    sb.append(ind(3) + "list2 = new "+ ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "list.add(o);\n");
    sb.append(ind(2) + "list2.add(this);\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
    sb.append(ind(1) + "}\n");

522
    // Insert / add at specific position
523
524
525
526
527
    sb.append(ind(1) + "public void " + toTypeDecl() + ".add");
    if (!useJastAddNames) {
      sb.append("To");
    }
    sb.append(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
528
    sb.append(ind(2) + "assertNotNull(o);\n");
529
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
530
531
532
    sb.append(ind(2) + "if (list == null) {\n");
    sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
533
534
    sb.append(ind(2) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o."
    + otherSide().getImplAttributeField() + ";\n");
535
536
537
538
539
540
541
542
543
    sb.append(ind(2) + "if (list2 == null) {\n");
    sb.append(ind(3) + "list2 = new "+ ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "list.add(index, o);\n");
    sb.append(ind(2) + "list2.add(this);\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
    sb.append(ind(1) + "}\n");

Johannes Mey's avatar
Johannes Mey committed
544
545
    // Remove
    sb.append(ind(1) + "public void " + toTypeDecl() + ".remove");
546
547
548
    if (!useJastAddNames) {
      sb.append("From");
    }
Johannes Mey's avatar
Johannes Mey committed
549
550
    sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    sb.append(ind(2) + "assertNotNull(o);\n");
551
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
552
    sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
553
554
    sb.append(ind(3) + ASTNode.listClass + "<" + otherSide().ofTypeDecl() + "> list2 = o."
      + otherSide().getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
555
556
557
558
559
560
561
562
    sb.append(ind(3) + "if (list2 != null) list2.remove(this);\n");
    sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(3) + "o.set" + otherSide().getImplAttributeName() + "(list2);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }


Johannes Mey's avatar
Johannes Mey committed
563
  public void RelationComponent.generateBiManyOne(StringBuilder sb, RelationComponent opposite) {
Johannes Mey's avatar
Johannes Mey committed
564
    // Get
565
566
567
568
569
570
    sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl() + ".");
    if (useJastAddNames) {
      // getXs
      sb.append("get" + nameCapitalized() + "s() {\n");
      sb.append(ind(2) + "return get" + nameCapitalized() + "List();\n");
      sb.append(ind(1) + "}\n");
571

572
573
574
575
576
577
      // getXList
      sb.append(ind(1) + "public java.util.List<" + ofTypeDecl() + "> " + toTypeDecl());
      sb.append(".get" + nameCapitalized() + "List() {\n");
    } else {
      sb.append(name() + "() {\n");
    }
578
579
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> l = get" + getImplAttributeName() + "();\n");
    // resolve the entire list
580
    if (resolverHelper | serializer) {
581
      sb.append(ind(2) + "if (l != null) {\n");
582
        sb.append(ind(3) + "boolean changed = false;\n");
583
584
        sb.append(ind(3) + "for (int i = 0; i < l.size(); i++) {\n");
          sb.append(ind(4) + ofTypeDecl() + " element = l.get(i);\n");
585
          sb.append(ind(4) + "if (element.is$Unresolved()) {\n");
586
            sb.append(ind(5) + "changed = true;\n");
587
588
            sb.append(ind(5) + ofTypeDecl() + " resolvedElement = resolve" + nameCapitalized() + "ByToken(element.as$Unresolved().getUnresolved$Token(), i);\n");
            sb.append(ind(5) + "if (element.as$Unresolved().getUnresolved$ResolveOpposite()) {\n");
589
              sb.append(ind(6) + toTypeDecl() + " oldTarget = resolvedElement." + opposite.getImplAttributeField() + ";\n");
590
              sb.append(ind(6) + "if (oldTarget != null && oldTarget != this) {\n");
591
                sb.append(ind(7) + "oldTarget." + getImplAttributeField() + ".remove(resolvedElement);\n");
592
593
594
595
596
597
598
599
              sb.append(ind(6) + "}\n");
              sb.append(ind(6) + "if (oldTarget == this) {\n");
                sb.append(ind(7) + "l.remove(i);\n");
                sb.append(ind(7) + "i--;\n");
              sb.append(ind(6) + "} else {\n");
                sb.append(ind(7) + "resolvedElement.set" + opposite.getImplAttributeName() + "(this);\n");
                sb.append(ind(7) + "l.set(i, resolvedElement);\n");
              sb.append(ind(6) + "}\n");
600
601
602
603
604
            sb.append(ind(5) + "} else {\n");
              sb.append(ind(6) + "l.set(i, resolvedElement);\n");
            sb.append(ind(5) + "}\n");
          sb.append(ind(4) + "}\n");
        sb.append(ind(3) + "}\n");
605
606
607
        sb.append(ind(3) + "if (changed) {\n");
          sb.append(ind(4) + "set" + getImplAttributeName() + "(l);\n");
        sb.append(ind(3) + "}\n");
608
609
610
611
      sb.append(ind(2) + "}\n");
    }
    sb.append(ind(2) + "return l != null ? Collections.unmodifiableList(l) : Collections.emptyList();\n");
    sb.append(ind(1) + "}\n");
Johannes Mey's avatar
Johannes Mey committed
612
613
614

    // Add
    sb.append(ind(1) + "public void " + toTypeDecl() + ".add");
615
616
617
    if (!useJastAddNames) {
      sb.append("To");
    }
Johannes Mey's avatar
Johannes Mey committed
618
619
    sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    sb.append(ind(2) + "assertNotNull(o);\n");
620
621
622
    sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n");
    sb.append(ind(3) + ASTNode.listClass + "<" + ofTypeDecl() + "> list2 = o."
      + otherSide().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
623
    sb.append(ind(3) + "if (list2.remove(o))\n");
624
625
    sb.append(ind(4) + "o." + otherSide().getImplAttributeField()
      + ".set" + getImplAttributeName() + "(list2);\n");
Johannes Mey's avatar
Johannes Mey committed
626
    sb.append(ind(2) + "}\n");
627
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
628
629
630
631
632
633
634
635
    sb.append(ind(2) + "if (list == null) {\n");
    sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "list.add(o);\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
    sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
    sb.append(ind(1) + "}\n");

636
    // Insert / add at specific position
637
638
639
640
641
    sb.append(ind(1) + "public void " + toTypeDecl() + ".add");
    if (!useJastAddNames) {
      sb.append("To");
    }
    sb.append(nameCapitalized() + "(int index, " + ofTypeDecl() + " o) {\n");
642
      sb.append(ind(2) + "assertNotNull(o);\n");
643
644
      sb.append(ind(2) + "if (o != null && o." + otherSide().getImplAttributeField() + " != null) {\n");
        sb.append(ind(3) + ASTNode.listClass + "<" + ofTypeDecl() + "> list2 = o." + otherSide().getImplAttributeField() + "." + getImplAttributeField() + ";\n");
645
        sb.append(ind(3) + "if (list2.remove(o))\n");
646
          sb.append(ind(4) + "o." + otherSide().getImplAttributeField() + ".set" + getImplAttributeName() + "(list2);\n");
647
      sb.append(ind(2) + "}\n");
648
      sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
649
650
651
652
653
654
655
656
      sb.append(ind(2) + "if (list == null) {\n");
        sb.append(ind(3) + "list = new " + ASTNode.listClass + "<>();\n");
      sb.append(ind(2) + "}\n");
      sb.append(ind(2) + "list.add(index, o);\n");
      sb.append(ind(2) + "set" + getImplAttributeName() + "(list);\n");
      sb.append(ind(2) + "o.set" + otherSide().getImplAttributeName() + "(this);\n");
    sb.append(ind(1) + "}\n");

Johannes Mey's avatar
Johannes Mey committed
657
658
    // Remove
    sb.append(ind(1) + "public void " + toTypeDecl() + ".remove");
659
660
661
    if (!useJastAddNames) {
      sb.append("From");
    }
Johannes Mey's avatar
Johannes Mey committed
662
663
    sb.append(nameCapitalized() + "(" + ofTypeDecl() + " o) {\n");
    sb.append(ind(2) + "assertNotNull(o);\n");
664
    sb.append(ind(2) + ASTNode.listClass + "<" + ofTypeDecl() + "> list = " + getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
665
666
    sb.append(ind(2) + "if (list != null && list.remove(o)) {\n");
    sb.append(ind(3) + "set" + getImplAttributeName() + "(list);\n");
667
    sb.append(ind(3) + "if (o." + otherSide().getImplAttributeField() + " == this) {\n");
Johannes Mey's avatar
Johannes Mey committed
668
669
670
671
672
673
674
675
676
677
678
    sb.append(ind(4) + "o.set" + otherSide().getImplAttributeName() + "(null);\n");
    sb.append(ind(3) + "}\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }

  public void RelationComponent.generateBiOneMany(StringBuilder sb, boolean isOpt) {
    // Get
    generateGetOne(sb);

    // Set
679
    sb.append(ind(1) + "public " + toTypeDecl() + " " + toTypeDecl() + ".set" + nameCapitalized()
Johannes Mey's avatar
Johannes Mey committed
680
681
682
683
      + "(" + ofTypeDecl() + " o) {\n");
    if (!isOpt) {
      sb.append(ind(2) + "assertNotNull(o);\n");
    }
684
685
686
    sb.append(ind(2) + "if (" + getImplAttributeField() + " != null) {\n");
    sb.append(ind(3) + ASTNode.listClass + "<" + toTypeDecl() + "> list2 = " + getImplAttributeField()
      + "." + otherSide().getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
687
    sb.append(ind(3) + "list2.remove(this);\n");
688
    sb.append(ind(3) + getImplAttributeField() + "." + "set"
Johannes Mey's avatar
Johannes Mey committed
689
690
691
692
693
694
695
696
      + otherSide().getImplAttributeName() + "(list2);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(2) + "set" + getImplAttributeName() + "(o);\n");

    int ind = isOpt ? 3 : 2;
    if (isOpt) {
      sb.append(ind(2) + "if (o != null) {\n");
    }
697
698
    sb.append(ind(ind) + ASTNode.listClass + "<" + toTypeDecl() + "> list = o."
      + otherSide().getImplAttributeField() + ";\n");
Johannes Mey's avatar
Johannes Mey committed
699
700
701
702
703
704
705
706
    sb.append(ind(ind) + "if (list == null) {\n");
    sb.append(ind(ind+1) + "list = new " + ASTNode.listClass + "<>();\n");
    sb.append(ind(ind) + "}\n");
    sb.append(ind(ind) + "list.add(this);\n");
    sb.append(ind(ind) + "o.set" + otherSide().getImplAttributeName() + "(list);\n");
    if (isOpt) {
      sb.append(ind(2) + "}\n");
    }
707
    sb.append(ind(2) + "return this;\n");
Johannes Mey's avatar
Johannes Mey committed
708
709
710
711
712
713
    sb.append(ind(1) + "}\n");

    if (isOpt) {
      generateExtraOptAPI(sb);
    }
  }
714
715
}

716
aspect LowerBoundCheck {
Johannes Mey's avatar
Johannes Mey committed
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
  public void Program.generateLowerBoundCheck(StringBuilder sb) {
    sb.append(ind(1) + "public boolean ASTNode.violatesLowerBounds() {\n");
    sb.append(ind(2) + "return !getLowerBoundsViolations().isEmpty();\n");
    sb.append(ind(1) + "}\n");

    sb.append(ind(1) + "public java.util.List<Pair<ASTNode, String>> "
      + "ASTNode.getLowerBoundsViolations() {\n");
    sb.append(ind(2) + "ArrayList<Pair<ASTNode, String>> list = new ArrayList<>();\n");
    sb.append(ind(2) + "computeLowerBoundsViolations(list);\n");
    sb.append(ind(2) + "return list;\n");
    sb.append(ind(1) + "}\n");

    sb.append(ind(1) + "public void ASTNode.computeLowerBoundsViolations("
      + "java.util.List<Pair<ASTNode, String>> list) {\n");
    sb.append(ind(2) + "for (int i = 0; i < getNumChildNoTransform(); i++) {\n");
    sb.append(ind(3) + "getChildNoTransform(i).computeLowerBoundsViolations(list);\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");

    for (TypeDecl td: getTypeDecls()) {
      td.generateLowerBoundCheck(sb);
    }

    generatePairClass(sb);
  }

  public void TypeDecl.generateLowerBoundCheck(StringBuilder sb) {
    if (!oneRelationComponents().isEmpty()) {
      sb.append(ind(1) + "public void " + getID() + ".computeLowerBoundsViolations(" +
        "java.util.List<Pair<ASTNode, String>> list) {\n");
      for (OneRelationComponent o: oneRelationComponents()) {
        o.generateLowerBoundCheck(sb);
      }
      sb.append(ind(2) + "super.computeLowerBoundsViolations(list);\n");
      sb.append(ind(1) + "}\n");
    }
  }

  public void OneRelationComponent.generateLowerBoundCheck(StringBuilder sb) {
756
757
758
759
760
761
762
    sb.append(ind(2) + "if (");
    if (useJastAddNames) {
      sb.append("get" + nameCapitalized());
    } else {
      sb.append(name());
    }
    sb.append("() == null) {\n");
Johannes Mey's avatar
Johannes Mey committed
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
    sb.append(ind(3) + "list.add(new Pair<>(this, \"" + name() + "\"));\n");
    sb.append(ind(2) + "}\n");
  }


  public void Program.generatePairClass(StringBuilder sb) {
    sb.append(ind(1) + "public class Pair<T1, T2> {\n");
    sb.append(ind(2) + "public final T1 _1;\n");
    sb.append(ind(2) + "public final T2 _2;\n");
    // Constructor
    sb.append(ind(2) + "public Pair(T1 _1, T2 _2) {\n");
    sb.append(ind(3) + "ASTNode.assertNotNull(_1);\n");
    sb.append(ind(3) + "ASTNode.assertNotNull(_2);\n");
    sb.append(ind(3) + "this._1 = _1;\n");
    sb.append(ind(3) + "this._2 = _2;\n");
    sb.append(ind(2) + "}\n");
    // equals
    sb.append(ind(2) + "public boolean equals(Object other) {\n");
    sb.append(ind(3) + "if (other instanceof Pair) {\n");
    sb.append(ind(4) + "Pair<?,?> p = (Pair<?,?>) other;\n");
    sb.append(ind(4) + "return _1.equals(p._1) && _2.equals(p._2);\n");
    sb.append(ind(3) + "} else {\n");
    sb.append(ind(4) + "return false;\n");
    sb.append(ind(3) + "}\n");
    sb.append(ind(2) + "}\n");
    // hashCode
    sb.append(ind(2) + "public int hashCode() {\n");
    sb.append(ind(3) + "return 31*_1.hashCode() + _2.hashCode();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
  }
794
}
795

796
797
798
799
800
801
802
803
aspect NameResolutionHelper {

  public String Program.generateRewriteToSuperTypeStub() {
    StringBuilder sb = new StringBuilder();
    generateRewriteToSuperTypeStub(sb);
    return sb.toString();
  }

804
805
806
807
808
809
810
  public String Program.generateResolverStubs() {
    StringBuilder sb = new StringBuilder();
    generateResolverStubs(sb);
    return sb.toString();
  }

  public void Program.generateResolverStubs(StringBuilder sb) {
811
812
813
814
815
816
    sb.append("aspect RefResolverStubs {\n\n");

    for (Relation r: getRelations()) {
      r.generateContextDependentNameResolution(sb);
    }

817
818
819
820
821
    if (resolverHelper) {
      for (TypeDecl decl : getTypeDeclList()) {
        decl.generateContextIndependentNameResolution(sb);
        sb.append("\n");
      }
822
823
    }
    sb.append("}\n\n");
824
825
826
  }

  public void Program.generateRewriteToSuperTypeStub(StringBuilder sb) {
827
828
829
830

    sb.append("aspect ReferenceCreation {\n\n");

    for (TypeDecl decl : getTypeDeclList()) {
831
      decl.createReferenceCreator(sb);
832
833
834
835
    }

    sb.append("}\n\n");

836
837
838
839
840
    sb.append("aspect ResolverTrigger {\n\n");

    resolveAll(sb);

    for (TypeDecl decl : getTypeDeclList()) {
841
      decl.resolveAll(sb);
842
843
844
845
    }

    sb.append("}\n\n");

846
847
    sb.append("aspect RefResolverHelpers {\n\n");

848
849
850
    sb.append(ind(1) + "interface Unresolved$Node {\n");
    sb.append(ind(2) + "String getUnresolved$Token();\n");
    sb.append(ind(2) + "boolean getUnresolved$ResolveOpposite();\n");
851
852
    sb.append(ind(1) + "}\n\n");

853
854
855
    for (TypeDecl td: getTypeDecls()) {
      if (td.needUnresolvedClass()) {
        td.generateUnresolvedClass(sb);
856
857
858
859
860
861
      }
    }

    sb.append("\n}\n");
  }

862
  public void TypeDecl.createReferenceCreator(StringBuilder sb) {
863

864
865
866
    TypeDecl instantiableSubType = instantiableSubType();
    if (instantiableSubType == null) {
      throw new RuntimeException("unable to find instantiable subtype for " + getID());
867
868
    }

869
870
871
872
    sb.append(ind(1) + "public static " + getID() + " " + getID() + ".createRef(String ref) {\n");
      sb.append(ind(2) + "Unresolved$" + instantiableSubType.getID() + " unresolvedNode = new Unresolved$" + instantiableSubType.getID() + "();\n");
      sb.append(ind(2) + "unresolvedNode.setUnresolved$Token(ref);\n");
      sb.append(ind(2) + "unresolvedNode.setUnresolved$ResolveOpposite(true);\n");
873
874
875
      sb.append(ind(2) + "return unresolvedNode;\n");
    sb.append(ind(1) + "}\n");

876
877
878
879
    sb.append(ind(1) + "public static " + getID() + " " + getID() + ".createRefDirection(String ref) {\n");
      sb.append(ind(2) + "Unresolved$" + instantiableSubType.getID() + " unresolvedNode = new Unresolved$" + instantiableSubType.getID() + "();\n");
      sb.append(ind(2) + "unresolvedNode.setUnresolved$Token(ref);\n");
      sb.append(ind(2) + "unresolvedNode.setUnresolved$ResolveOpposite(false);\n");
880
881
882
883
      sb.append(ind(2) + "return unresolvedNode;\n");
    sb.append(ind(1) + "}\n");
  }

884
  public void TypeDecl.generateContextIndependentNameResolution(StringBuilder sb) {
885
    sb.append(ind(1) + "// context-independent name resolution\n");
886
    sb.append(ind(1) + "uncache ASTNode.globallyResolve" + getID() + "ByToken(String id);\n");
887
    sb.append(ind(1) + "syn " + getID() + " ASTNode.globallyResolve" + getID() + "ByToken(String id) {\n");
888
    if (serializer) {
889
      sb.append(ind(2) + "return (" + getID() + ") globallyResolveASTNodeByUID(id);\n");
890
    } else {
891
      sb.append(ind(2) + "// perform context independent name resolution here using the id\n");
892
      sb.append(ind(2) + "throw new RuntimeException(\"Context-independent name resolution for " + getID() + " not implemented.\");\n");
893
    }
894
895
896
897
898
899
900
901
902
903
904
905
906
907
    sb.append(ind(1) + "}\n");
  }

  public void Relation.generateContextDependentNameResolution(StringBuilder sb) {
    sb.append(ind(1) + "// " + prettyPrint() + "\n");
    getDirection().generateContextDependentNameResolution(sb);
    sb.append("\n");
  }

  public abstract void Direction.generateContextDependentNameResolution(StringBuilder sb);
  public void RightDirection.generateContextDependentNameResolution(StringBuilder sb) {
    relation().getLeft().generateContextDependentNameResolution(sb);
  }
  public void Bidirectional.generateContextDependentNameResolution(StringBuilder sb) {
908
909
    relation().getLeft().generateContextDependentNameResolution(sb);
    relation().getRight().generateContextDependentNameResolution(sb);
910
911
912
913
914
915
916
  }

  public abstract void RelationComponent.generateContextDependentNameResolution(StringBuilder sb);
  public void OneRelationComponent.generateContextDependentNameResolution(StringBuilder sb) {
    generateDirectedContextDependentNameResolution(sb);
  }
  public void OptionalRelationComponent.generateContextDependentNameResolution(StringBuilder sb) {
917
918
919
    // optional relations are resolved in the same way as mandatory relations
    // TODO maybe, there should be a check if the id to be solved is empty or null
    generateDirectedContextDependentNameResolution(sb);
920
921
  }
  public void ManyRelationComponent.generateContextDependentNameResolution(StringBuilder sb) {
922
923
924
925
926
927
928
929
930
931
932
933
934

    if (serializer && !resolverHelper) {
      sb.append(ind(1) + ofTypeDecl() + " " + toTypeDecl() + ".resolve" + nameCapitalized() + "ByToken(String id, int position) {\n");
        sb.append(ind(2) + "return (" + ofTypeDecl() + ") globallyResolveASTNodeByUID(id);\n");
      sb.append(ind(1) + "}\n");
    } else {
      sb.append(ind(1) + "// context-dependent name resolution\n");
      sb.append(ind(1) + "uncache " + toTypeDecl() + ".resolve" + nameCapitalized() + "ByToken(String id, int position);\n");
      sb.append(ind(1) + "syn " + ofTypeDecl() + " " + toTypeDecl() + ".resolve" + nameCapitalized() + "ByToken(String id, int position) {\n");
        sb.append(ind(2) + "// default to context-independent name resolution\n");
        sb.append(ind(2) + "return globallyResolve" + ofTypeDecl() + "ByToken(id);\n");
      sb.append(ind(1) + "}\n");
    }
935
936
937
  }

  public void RelationComponent.generateDirectedContextDependentNameResolution(StringBuilder sb) {
938
939
940
941
942
943
944
945
946
947
948
949
    if (serializer && !resolverHelper) {
      sb.append(ind(1) + ofTypeDecl() + " " + toTypeDecl() + ".resolve" + nameCapitalized() + "ByToken(String id) {\n");
        sb.append(ind(2) + "return (" + ofTypeDecl() + ") globallyResolveASTNodeByUID(id);\n");
      sb.append(ind(1) + "}\n");
    } else {
      sb.append(ind(1) + "// context-dependent name resolution\n");
      sb.append(ind(1) + "uncache " + toTypeDecl() + ".resolve" + nameCapitalized() + "ByToken(String id);\n");
      sb.append(ind(1) + "syn " + ofTypeDecl() + " " + toTypeDecl() + ".resolve" + nameCapitalized() + "ByToken(String id) {\n");
        sb.append(ind(2) + "// default to context-independent name resolution\n");
        sb.append(ind(2) + "return globallyResolve" + ofTypeDecl() + "ByToken(id);\n");
      sb.append(ind(1) + "}\n");
    }
950
  }
951
952
953
954
955
956
957
958
959

  public void Program.resolveAll(StringBuilder sb) {
    sb.append(ind(1) + "// enforce resolving of all non-containment relations of the current non-terminal\n");
    sb.append(ind(1) + "public void ASTNode.resolveAll() {\n");
    sb.append(ind(1) + "}\n\n");

    sb.append(ind(1) + "// enforce resolving in the entire subtree\n");
    sb.append(ind(1) + "public void ASTNode.treeResolveAll() {\n");
      sb.append(ind(2) + "if (children != null) {\n");
960
        sb.append(ind(3) + "for (int i = 0; i < numChildren; ++i) {\n");
961
962
963
964
965
966
967
968
969
970
971
972
973
974
          sb.append(ind(4) + "ASTNode child = children[i];\n");
          sb.append(ind(4) + "if (child != null) {\n");
            sb.append(ind(5) + "child.treeResolveAll();\n");
          sb.append(ind(4) + "}\n");
        sb.append(ind(3) + "}\n");
      sb.append(ind(2) + "}\n");
      sb.append(ind(2) + "resolveAll();\n");
    sb.append(ind(1) + "}\n");
  }

  public void TypeDecl.resolveAll(StringBuilder sb) {
    sb.append(ind(1) + "// enforce resolving of all non-containment relations of the current non-terminal\n");
    sb.append(ind(1) + "public void " + getID() + ".resolveAll() {\n");
    for (RelationComponent relationComponent : relationComponents()) {
975
976
977
978
979
980
981
      sb.append(ind(2));
      if (useJastAddNames) {
        sb.append("get" + relationComponent.nameCapitalized());
      } else {
        sb.append(relationComponent.name());
      }
      sb.append(relationComponent.isMany() && useJastAddNames ? "List" : "").append("();\n");
982
983
984
985
    }
      sb.append(ind(2) + "super.resolveAll();\n");
    sb.append(ind(1) + "}\n");
  }
986
987
}

988
989
990
991
992
993
994
995
996
997
998
999
1000
aspect Serializer {

  protected static String ASTNode.jsonTypeKey = "type";
  protected static String ASTNode.jsonNodeType = "com.fasterxml.jackson.databind.JsonNode";
  protected static String ASTNode.jsonNodeTypeAccessor = ".get(\"" + jsonTypeKey + "\").asText()";
  public String Program.generateSerializer() {
    StringBuilder sb = new StringBuilder();
    generateFromJson(sb);
    generateToJson(sb);
    writeUID(sb);
    return sb.toString();
  }