Signature.jrag 7.21 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
import java.util.*;

aspect Signature {  

5
6
7
  syn boolean Decl.isSampleDecl();
  eq TypeDecl.isSampleDecl() = false;
  eq SampleDecl.isSampleDecl() = true;
Anders Nilsson's avatar
Anders Nilsson committed
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  syn boolean Decl.sendOnlyFlatSignatures(Java_env env) = (env.version==2006);

  eq Decl.getSignature().parentDecl() = this;
  eq Signature.getSignatureList().parentDecl() = parentDecl();

  inh Decl Signature.parentDecl();
  inh Decl SignatureList.parentDecl();


  syn nta Signature Decl.getSignature() { 
    SignatureList sl = new SignatureList();
    genSigLineForDecl(sl, true);
    SignatureList fsl = new SignatureList();
    flatSignature(fsl);
    Signature sig = new Signature();
    sig.setSignatureList(sl);
    sig.setFlatSignatureList(fsl);
    setSignature(sig);
    return sig;
  }

30
31
32
33
34
  public String SignatureLine.getIndentString() {
    StringBuffer result = new StringBuffer();
    int indent = getIndent();
    for (int i = 0 ; i < indent ; i++) {
      result.append("  ");
Anders Nilsson's avatar
Anders Nilsson committed
35
    }
36
37
    return result.toString();
  }
Anders Nilsson's avatar
Anders Nilsson committed
38

39
40
    syn byte[] SignatureLine.getData(int version) = null;
    //  return new byte[0];
Anders Nilsson's avatar
Anders Nilsson committed
41

42
43
44
45
46
47
48
    private Decl TypeRefSignatureLine.decl;
    public TypeRefSignatureLine.TypeRefSignatureLine(int indent, Decl decl, String comment) {
      super(indent, comment);
      this.decl = decl;
    }
    public void SignatureList.addTypeRef(Decl type, String comment) {
        addSignatureLine(new TypeRefSignatureLine(indent, type, comment));
Anders Nilsson's avatar
Anders Nilsson committed
49
50
    }

51
52
53
54
    public ByteArraySignatureLine.ByteArraySignatureLine(int indent, byte[] data, String comment) {
      super(indent, comment);
      setData(data);
    }
Anders Nilsson's avatar
Anders Nilsson committed
55

56
57
58
59
    public IntSignatureLine.IntSignatureLine(int indent, int data, String comment) {
      super(indent, comment);
      setData(data);
    }
Anders Nilsson's avatar
Anders Nilsson committed
60

61
62
    public void SignatureList.add(byte[] data, String comment) {
      addSignatureLine(new ByteArraySignatureLine(indent, data, comment));
63
64
    }

65
66
    public void SignatureList.addInt(int data, String comment) {
      addSignatureLine(new IntSignatureLine(indent, data, comment));
Anders Nilsson's avatar
Anders Nilsson committed
67
68
    }

69
70
71
    protected byte[] DataSignatureLine.getIntBytes(int value, int version) {
      byte data[];
      switch(version) {
72
        case 2006:             // Use old encoding with 32 bit integers
73
	    data = new byte[4];
74
75
76
	    for (int i = 0 ; i < 4 ; i++) {
	        data[3 - i] = (byte)((value >> (8 * i)) & 0xff);
	    }
77
	    //add(data, comment);
78
79
80
81
82
83
84
85
86
87
88
89
        break;
        case 2013:             // Use new encoding with varints
	    byte[] tmp = new byte[5];
 	    long v = value & 0xffffffff;
            int i, j;
            for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
		tmp[i] = (byte)(v & 0x7f);
            }
	    byte[] packed = new byte[i];
            for (i = i - 1, j = 0 ; i >= 0 ; i--, j++) {
		packed[j] = (byte)(tmp[i] | (i!=0?0x80:0x00));
            }
90
91
	    //add(packed, comment);
            data = packed;
92
93
        break;
        default: 
94
            throw new RuntimeException("Unsupported version = "+version+". This should never happen.");
95
      }
96
      return data;
Anders Nilsson's avatar
Anders Nilsson committed
97
98
    }

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    eq IntSignatureLine.getData(int version) {
      return getIntBytes(getData(), version);
    }

    public void SignatureList.addString(String data, String comment) {
      addSignatureLine(new StringSignatureLine(indent, comment, data));
    }
    eq StringSignatureLine.getData(int version) {
      byte[] lenBytes = getIntBytes(getData().length(), version);
      byte[] data = new byte[lenBytes.length+getData().length()];

      // first add the encoded length
      for (int i = 0 ; i < lenBytes.length ; i++) {
        data[i] = lenBytes[i];
      }
      // and then the actual chars
      for (int i = 0 ; i < getData().length() ; i++) {
        int idx = lenBytes.length + i;
        data[idx] = (byte)(getData().charAt(i) & 0xff);
Anders Nilsson's avatar
Anders Nilsson committed
118
      }
119
      return data;
Anders Nilsson's avatar
Anders Nilsson committed
120
121
    }

122
123
    public int SignatureList.size() {
      return getNumSignatureLine();
Anders Nilsson's avatar
Anders Nilsson committed
124
125
    }

126
    public String SignatureList.getIndent(int i) {
Anders Nilsson's avatar
Anders Nilsson committed
127
      StringBuffer result = new StringBuffer();
128
      int indent = getSignatureLine(i).getIndent();
Anders Nilsson's avatar
Anders Nilsson committed
129
130
131
132
133
134
      for (i = 0 ; i < indent ; i++) {
        result.append("  ");
      }
      return result.toString();
    }

135
136
    public byte[] SignatureList.getData(int i, int version) {
      return getSignatureLine(i).getData(version);
Anders Nilsson's avatar
Anders Nilsson committed
137
138
    }

139
140
    public String SignatureList.getComment(int i) {
      return getSignatureLine(i).getComment();
Anders Nilsson's avatar
Anders Nilsson committed
141
142
    }

143
144
145
    private int SignatureList.indent;

    public void SignatureList.indent() {
Anders Nilsson's avatar
Anders Nilsson committed
146
147
148
      indent++;
    }

149
    public void SignatureList.unindent() {
Anders Nilsson's avatar
Anders Nilsson committed
150
151
152
153
      indent--;
    }


154
  public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
155
    throw new Error(this.getClass().getName() + 
156
                    ".genSigLineForDecl(SignatureList list)" + 
Anders Nilsson's avatar
Anders Nilsson committed
157
158
159
                    " not declared");
  }

160
  public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl) {
161
     //System.out.println("************ TypeDecl.genSigLine("+decl+").... for "+getName());
162
163
164
165
166
    if(decl){
      getType().genSigLineForDecl(list, decl);
    }else{
      list.addTypeRef(this, "//TODO (from list.addTypeRef)");
    }
Anders Nilsson's avatar
Anders Nilsson committed
167
168
  }

169
  public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl) {
170
     //System.out.println("************ SampleDecl.genSigLine("+decl+").... for "+getName());
171
    getType().genSigLineForDecl(list, decl);
Anders Nilsson's avatar
Anders Nilsson committed
172
173
  }

174
  public void VoidType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
175
176
177
178
    list.addInt(LABCOMM_STRUCT, "void");
    list.addInt(0, null);
  }

179
  public void PrimType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
180
181
182
    list.addInt(getToken(), null);
  }

183
184
  public void UserType.genSigLineForDecl(SignatureList list, boolean decl) {
    if(decl){
185
     //System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
186
      TypeDecl thet=lookupType(getName());
187
      //System.out.println("************ thet: "+thet.getName() +":"+thet.getType());
188
189
190
191
192
193
194
      thet.genSigLineForDecl(list, decl);
    }else{
     //System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
      TypeDecl thet = lookupType(getName());
     // System.out.println("************ thet: "+thet.getName() +":"+thet.getType());
      list.addTypeRef(thet, null);
    }
Anders Nilsson's avatar
Anders Nilsson committed
195
196
  }

197
  public void ArrayType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
198
199
200
201
    list.addInt(LABCOMM_ARRAY, signatureComment());
    list.indent();
    list.addInt(getNumExp(), null);
    for (int i = 0 ; i < getNumExp() ; i++) {
202
      getExp(i).genSigLineForDecl(list, false);
Anders Nilsson's avatar
Anders Nilsson committed
203
    }
204
    getType().genSigLineForDecl(list, false);
Anders Nilsson's avatar
Anders Nilsson committed
205
206
207
208
    list.unindent();
    list.add(null, "}");
  }

209
  public void StructType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
210
211
212
213
    list.addInt(LABCOMM_STRUCT, "struct { " + getNumField() + " fields");
    list.indent();
    list.addInt(getNumField(), null);
    for (int i = 0 ; i < getNumField() ; i++) {
214
      getField(i).genSigLineForDecl(list, false);
Anders Nilsson's avatar
Anders Nilsson committed
215
216
217
218
219
    }
    list.unindent();
    list.add(null, "}");
  }

220
  public void Field.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
221
    list.addString(getName(), signatureComment());
222
    getType().genSigLineForDecl(list, decl);
Anders Nilsson's avatar
Anders Nilsson committed
223
224
  }

225
  public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
226
227
228
    list.addInt(Integer.parseInt(getValue()), null);
  }

229
  public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
230
231
    list.addInt(0, null);
  }
232
}