NameAnalysis.jrag 1.45 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
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

aspect NameAnalysis {

  inh String Decl.lookupName(String name);
  eq Program.getDecl(int index).lookupName(String name) {
    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);
  eq Program.getDecl(int index).lookupType(String name) {
    for(int i = 0; i < index; i++) {
      Decl d = getDecl(i);  
      if(d instanceof TypeDecl && d.getName().equals(name)) {
	return (TypeDecl)d;
      }
    }
    return null;
  }

  syn TypeDecl UserType.decl() = lookupType(getName());
  
  
  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");
    } 
  }

}