NameAnalysis.jrag 1.57 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4

aspect NameAnalysis {

  inh String Decl.lookupName(String name);
5
  eq Specification.getDecl(int index).lookupName(String name) {
Anders Nilsson's avatar
Anders Nilsson committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    for (int i = 0; i < index; i++) {
      String s = getDecl(i).getName();
      if (s.equals(name)) {
      	return s;
      }
    }
    return null;
  }
  inh String Field.lookupName(String name);
  eq StructType.getField(int index).lookupName(String name) {
    for (int i = 0; i < index; i++) {
      String s = getField(i).getName();
      if (s.equals(name)) {
      	return s;
      }
    }
    return null;
  }

  inh TypeDecl Decl.lookupType(String name);
  inh TypeDecl UserType.lookupType(String name);
27
  eq Specification.getDecl(int index).lookupType(String name) {
Anders Nilsson's avatar
Anders Nilsson committed
28
29
30
31
32
33
34
35
36
    for(int i = 0; i < index; i++) {
      Decl d = getDecl(i);  
      if(d instanceof TypeDecl && d.getName().equals(name)) {
	return (TypeDecl)d;
      }
    }
    return null;
  }

37
38
  syn TypeDecl DataType.decl(); 
  eq DataType.decl() = null;
39
40
  eq UserType.decl() = lookupType(getName());
  eq PrimType.decl() = null; //HERE BE DRAGONS XXX
Anders Nilsson's avatar
Anders Nilsson 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
  
  
  public void ASTNode.nameCheck() {
    for (int i = 0; i < getNumChild(); i++) {
      getChild(i).nameCheck();
    }
  }
  
  public void Decl.nameCheck() {
    if (lookupType(getName()) != null || lookupName(getName()) != null) {
      error(getName() + " multiply declared");
    }
  }
  
  public void Field.nameCheck() {
    if(lookupName(getName()) != null) {
      error(getName() + " multiply declared");
    }
  }
  
  public void UserType.nameCheck() {
    if (decl() == null) {
      error("Use of undeclared type");
    } 
  }

}