Analysis.jrag 7.42 KB
Newer Older
Niklas Fors's avatar
Niklas Fors committed
1
2
3
import java.util.*;


Niklas Fors's avatar
Niklas Fors committed
4
aspect TypeAnalysis {
Johannes Mey's avatar
Johannes Mey committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  public abstract TypeUse Component.getTypeUse();

  syn TypeDecl TypeUse.decl() = lookupType(getID());
  inh TypeDecl TypeUse.lookupType(String name);
  eq Program.getChild().lookupType(String name) {
    for (TypeDecl td: getTypeDecls()) {
      if (td.getID().equals(name)) {
        return td;
      }
    }
    return null;
  }
  syn boolean TypeDecl.isAlreadyDeclared()
    = lookupType(getID()) != this;
  inh TypeDecl TypeDecl.lookupType(String name);
20
21
22
23
24
25
26
27

  syn TypeDecl TypeDecl.mostGeneralSuperType() {
    if (!hasSuper()) {
      return this;
    } else {
      return getSuper().decl().mostGeneralSuperType();
    }
  }
Niklas Fors's avatar
Niklas Fors committed
28
}
Niklas Fors's avatar
Niklas Fors committed
29

Niklas Fors's avatar
Niklas Fors committed
30
aspect ComponentAnalysis {
Johannes Mey's avatar
Johannes Mey committed
31
  syn boolean Component.isTargetOfDirectedRelation() = false;
32
  eq RelationComponent.isTargetOfDirectedRelation() = isTargetOfRightDirection() | isTargetOfLeftDirection();
Johannes Mey's avatar
Johannes Mey committed
33
34
35
36
  inh boolean RelationComponent.isTargetOfRightDirection();
  eq Relation.getRight().isTargetOfRightDirection()
    = getDirection() instanceof RightDirection;
  eq Program.getChild().isTargetOfRightDirection() = false;
37
38
39
40
  inh boolean RelationComponent.isTargetOfLeftDirection();
  eq Relation.getLeft().isTargetOfLeftDirection()
    = getDirection() instanceof LeftDirection;
  eq Program.getChild().isTargetOfLeftDirection() = false;
Johannes Mey's avatar
Johannes Mey committed
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

  syn String Component.name() = getID();

  syn TypeDecl Component.toTypeDecl() = enclosingTypeDecl();
  eq RelationComponent.toTypeDecl() = getTypeUse().decl();
  inh TypeDecl Component.enclosingTypeDecl();
  eq TypeDecl.getChild().enclosingTypeDecl() = this;
  eq Program.getChild().enclosingTypeDecl() = null;

  inh RelationComponent RelationComponent.otherSide();
  eq Relation.getLeft().otherSide() = getRight();
  eq Relation.getRight().otherSide() = getLeft();
  eq Program.getChild().otherSide() = null;

  syn TypeDecl RelationComponent.ofTypeDecl() = otherSide().toTypeDecl();

  syn boolean Component.isAlreadyDeclared()
    = !isTargetOfDirectedRelation()
      && toTypeDecl() != null
      && lookupComponent(toTypeDecl(), name()) != this;
  inh Component Component.lookupComponent(TypeDecl td, String name);
  eq Program.getChild().lookupComponent(TypeDecl td, String name)
    = lookupComponentSyn(td, name);
  syn Component Program.lookupComponentSyn(TypeDecl td, String name) {
    for (Component c: td.getComponents()) {
      if (c.name().equals(name)) {
        return c;
      }
    }

    for (Relation r: getRelations()) {
      Component c = r.getLeft().lookup(td, name);
      if (c != null) return c;
      c = r.getRight().lookup(td, name);
      if (c != null) return c;
    }

    return null;
  }

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
  syn boolean Component.isInvalidRedefinition() {
    if (toTypeDecl() == null) return false;

    TypeDecl td = toTypeDecl();

    while (td.hasSuper() && td.getSuper().decl() != null) {
      td = td.getSuper().decl();
      Component c = lookupComponent(td, getID());
      if (c != null && isTargetOfDirectedRelation()) return true;

      if (c != null && !this.isEqual(c)) return true;
    }
    return false;
  }

  syn boolean Component.isEqual(Component c) = this.getClass() == c.getClass() && getTypeUse().isEqual(c.getTypeUse());

  syn boolean TypeUse.isEqual(TypeUse u);
  eq SimpleTypeUse.isEqual(TypeUse u) = u instanceof SimpleTypeUse && getID().equals(u.getID());
  eq ParameterizedTypeUse.isEqual(TypeUse u) {
    if (!getID().equals(u.getID())) return false;
    if (!(u instanceof ParameterizedTypeUse)) return false;
    ParameterizedTypeUse pu = (ParameterizedTypeUse) u;
    if (getNumTypeUse() != pu.getNumTypeUse()) return false;
    for (int i = 0; i < getNumTypeUse(); i++) {
      if (!getTypeUse(i).isEqual(pu.getTypeUse(i))) return false;
    }
    return true;
  }

Johannes Mey's avatar
Johannes Mey committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  syn RelationComponent RelationComponent.lookup(TypeDecl td, String name)
    = !isTargetOfDirectedRelation() && toTypeDecl() == td && name().equals(name)
    ? this
    : null;


  coll Set<RelationComponent> TypeDecl.relationComponents()
    [new HashSet<RelationComponent>()]
    root Program;
  RelationComponent contributes this
    when !isTargetOfDirectedRelation() && toTypeDecl() != null
    to TypeDecl.relationComponents()
    for toTypeDecl();

125
126
127
128
129
130
131
132
133
  syn Collection<RelationComponent> TypeDecl.relationComponentsTransitive() {
    ArrayList<RelationComponent> list = new ArrayList<>();
    if (hasSuper() && getSuper().decl() != null) {
      list.addAll(getSuper().decl().relationComponentsTransitive());
    }
    list.addAll(relationComponents());
    return list;
  }

Johannes Mey's avatar
Johannes Mey committed
134
135
136
137
138
139
140
141
142
  syn Set<OneRelationComponent> TypeDecl.oneRelationComponents() {
    Set<OneRelationComponent> set = new HashSet<>();
    for (RelationComponent rc: relationComponents()) {
      if (rc instanceof OneRelationComponent) {
        set.add((OneRelationComponent) rc);
      }
    }
    return set;
  }
143
144
145
146
147
148

  syn boolean TypeDecl.needUnresolvedClass() {
    // a TypeDecl needs an unresolved class, if it can appear in a relation
    // TODO
    return true;
  }
149
150
151
152
153
154
155
156
157

  syn boolean Component.isList() = false;
  eq ListComponent.isList() = true;

  syn boolean Component.isOpt() = false;
  eq OptComponent.isOpt() = true;

  syn boolean Component.isNullable() = false;
  eq TokenComponent.isNullable() = !"float double int short long char byte boolean".contains(getTypeUse().getID());
Niklas Fors's avatar
Niklas Fors committed
158
159
}

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
aspect InstanceSupplier {

  inh Program TypeDecl.program();
  eq Program.getTypeDecl(int i).program() = this;

  syn Collection<TypeDecl> TypeDecl.subTypeDecls() {
    java.util.List<TypeDecl> subDecls = new ArrayList();
    for (TypeDecl decl : program().getTypeDeclList()) {
      if (decl.hasSuper() && decl.getSuper().getID().equals(getID())) {
        subDecls.add(decl);
      }
    }
    return subDecls;
  }

175
  syn TypeDecl TypeDecl.instantiableSubType() {
176
    if (getAbstract() == false) {
177
      return this;
178
179
180
    } else {
      for (TypeDecl sub : subTypeDecls()) {
        if (sub.getAbstract() == false) {
181
          return sub;
182
        } else {
183
          TypeDecl subInstance = sub.instantiableSubType();
184
          if (subInstance != null) {
185
            return subInstance;
186
187
188
189
190
191
192
193
          }
        }
      }
    }
    return null;
  }
}

194
aspect Constructors {
Johannes Mey's avatar
Johannes Mey committed
195
196
197
198
199
200
  syn Collection<Component> TypeDecl.componentsTransitive() {
    ArrayList<Component> list = new ArrayList<>();
    if (hasSuper() && getSuper().decl() != null) {
      list.addAll(getSuper().decl().componentsTransitive());
    }
    for (Component c: getComponents()) {
201
202
203
      if (c.inConstructor()) {
        list.add(c);
      }
Johannes Mey's avatar
Johannes Mey committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    }
    return list;
  }

  syn boolean TypeDecl.needsConstructor() {
    if (componentsTransitive().isEmpty()) {
      return false;
    }
    if (!relationComponents().isEmpty()) {
      return true;
    }
    return hasSuper()
      && getSuper().decl() != null
      && getSuper().decl().needsConstructor();
  }
219
220
221
222
223
224

  /**
   * @return true, if the component should be added to the constructor (i.e., is not an NTA)
   */
  syn boolean Component.inConstructor() = true;
  eq NTAComponent.inConstructor() = false;
225
226
}

Niklas Fors's avatar
Niklas Fors committed
227
aspect Utils {
228
229
230
231

  syn boolean RelationComponent.isMany() = false;
  eq ManyRelationComponent.isMany() = true;

Johannes Mey's avatar
Johannes Mey committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  public String SimpleTypeUse.toString() {
    return getID();
  }
  public String ParameterizedTypeUse.toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(getID()).append("<");
    int i = 0;
    for (TypeUse u: getTypeUses()) {
      sb.append(u.toString());
      if (++i < getNumTypeUse()) {
        sb.append(", ");
      }
    }
    sb.append(">");
    return sb.toString();
  }
  public String TypeDecl.toString() {
    return getID();
  }
Niklas Fors's avatar
Niklas Fors committed
251
}