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


Niklas Fors's avatar
Niklas Fors committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
aspect TypeAnalysis {
	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);
}
Niklas Fors's avatar
Niklas Fors committed
19

Niklas Fors's avatar
Niklas Fors committed
20
aspect ComponentAnalysis {
21
22
23
24
25
26
27
	syn boolean Component.isDeclaringName() = true;
	eq RelationComponent.isDeclaringName() = !isTargetOfRightDirection();
	inh boolean RelationComponent.isTargetOfRightDirection();
	eq Relation.getRight().isTargetOfRightDirection()
		= getDirection() instanceof RightDirection;
	eq Program.getChild().isTargetOfRightDirection() = false;

Niklas Fors's avatar
Niklas Fors committed
28
	syn String Component.name() {
29
30
31
		if (!isDeclaringName()) {
			return "";
		}
Niklas Fors's avatar
Niklas Fors committed
32
33
34
35
36
37
38
39
40
41
42
43
44
		if (!getID().isEmpty()) {
			return getID();
		}
		return otherRelationSideName();
	}
	inh String Component.otherRelationSideName();
	eq Relation.getLeft().otherRelationSideName()
		= getRight().getTypeUse().getID();
	eq Relation.getRight().otherRelationSideName()
		= getLeft().getTypeUse().getID();
	eq Program.getChild().otherRelationSideName()
		= "";

45
46
	syn TypeDecl Component.toTypeDecl() = enclosingTypeDecl();
	eq RelationComponent.toTypeDecl() = getTypeUse().decl();
Niklas Fors's avatar
Niklas Fors committed
47
48
49
	inh TypeDecl Component.enclosingTypeDecl();
	eq TypeDecl.getChild().enclosingTypeDecl() = this;
	eq Program.getChild().enclosingTypeDecl() = null;
50
51
52
53
	inh TypeDecl RelationComponent.ofTypeDecl();
	eq Relation.getLeft().ofTypeDecl() = getRight().toTypeDecl();
	eq Relation.getRight().ofTypeDecl() = getLeft().toTypeDecl();
	eq Program.getChild().ofTypeDecl() = null;
Niklas Fors's avatar
Niklas Fors committed
54
55

	syn boolean Component.isAlreadyDeclared()
56
		= isDeclaringName() && lookupComponent(toTypeDecl(), name()) != this;
Niklas Fors's avatar
Niklas Fors committed
57
58
59
60
61
62
63
64
65
66
	inh Component Component.lookupComponent(TypeDecl td, String name);
	eq Program.getChild().lookupComponent(TypeDecl td, String name) {
		if (td != null) {
			for (Component c: td.getComponents()) {
				if (c.name().equals(name)) {
					return c;
				}
			}
		}
		for (Relation r: getRelations()) {
67
68
69
70
			Component c = r.getLeft().lookup(td, name);
			if (c != null) return c;
			c = r.getRight().lookup(td, name);
			if (c != null) return c;
Niklas Fors's avatar
Niklas Fors committed
71
72
73
		}
		return null;
	}
74
75
76
77
78
79
80
81
82
83
84
85
86
87

	syn RelationComponent RelationComponent.lookup(TypeDecl td, String name)
		= isDeclaringName() && toTypeDecl() == td && name().equals(name)
		? this
		: null;


	coll Set<RelationComponent> TypeDecl.relationComponents()
		[new HashSet<RelationComponent>()]
		root Program;
	RelationComponent contributes this
		when isDeclaringName() && toTypeDecl() != null
		to TypeDecl.relationComponents()
		for toTypeDecl();
Niklas Fors's avatar
Niklas Fors committed
88
89
90
91
92
93
94
95
96
97
}

aspect Utils {
	public String TypeUse.toString() {
		return getID();
	}
	public String TypeDecl.toString() {
		return getID();
	}
}