Signature.jrag 7.66 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
30
31
32
33
34
35
  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);
    // HERE BE DRAGONS. Is this correct for a NTA?
    setSignature(sig);
    return sig;
  }

  //syn nta SignatureList Signature.getSignatureList() {
  //  SignatureList result = new SignatureList();
  //  genSigLineForDecl(result, true);
  //  return result;
  //}
Anders Nilsson's avatar
Anders Nilsson committed
36

37
38
39
40
41
42
43
    public String SignatureLine.getIndentString() {
      StringBuffer result = new StringBuffer();
      int indent = getIndent();
      for (int i = 0 ; i < indent ; i++) {
        result.append("  ");
      }
      return result.toString();
Anders Nilsson's avatar
Anders Nilsson committed
44
45
    }

46
47
48
49
50
51
52
    //Very temporary kludge: the flat signature generation 
    //predates the SignatureLine class hierarchy. This hack
    //returns size zero arrays, which doesn't break the sig
    //generation
    //public byte[] getData() {
    syn byte[] SignatureLine.getData(int version) {
      return new byte[0];
Anders Nilsson's avatar
Anders Nilsson committed
53
54
    }

55
56
57
58
59
60
61
    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
62
63
    }

64
65
66
67
    public ByteArraySignatureLine.ByteArraySignatureLine(int indent, byte[] data, String comment) {
      super(indent, comment);
      setData(data);
    }
Anders Nilsson's avatar
Anders Nilsson committed
68

69
70
71
72
    public IntSignatureLine.IntSignatureLine(int indent, int data, String comment) {
      super(indent, comment);
      setData(data);
    }
Anders Nilsson's avatar
Anders Nilsson committed
73

74
75
    public void SignatureList.add(byte[] data, String comment) {
      addSignatureLine(new ByteArraySignatureLine(indent, data, comment));
76
77
    }

78
79
    public void SignatureList.addInt(int data, String comment) {
      addSignatureLine(new IntSignatureLine(indent, data, comment));
Anders Nilsson's avatar
Anders Nilsson committed
80
81
    }

82
83
84
    protected byte[] DataSignatureLine.getIntBytes(int value, int version) {
      byte data[];
      switch(version) {
85
        case 2006:             // Use old encoding with 32 bit integers
86
	    data = new byte[4];
87
88
89
	    for (int i = 0 ; i < 4 ; i++) {
	        data[3 - i] = (byte)((value >> (8 * i)) & 0xff);
	    }
90
	    //add(data, comment);
91
92
93
94
95
96
97
98
99
100
101
102
        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));
            }
103
104
	    //add(packed, comment);
            data = packed;
105
106
        break;
        default: 
107
            throw new RuntimeException("Unsupported version = "+version+". This should never happen.");
108
      }
109
      return data;
Anders Nilsson's avatar
Anders Nilsson committed
110
111
    }

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    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
131
      }
132
      return data;
Anders Nilsson's avatar
Anders Nilsson committed
133
134
    }

135
136
    public int SignatureList.size() {
      return getNumSignatureLine();
Anders Nilsson's avatar
Anders Nilsson committed
137
138
    }

139
    public String SignatureList.getIndent(int i) {
Anders Nilsson's avatar
Anders Nilsson committed
140
      StringBuffer result = new StringBuffer();
141
      int indent = getSignatureLine(i).getIndent();
Anders Nilsson's avatar
Anders Nilsson committed
142
143
144
145
146
147
      for (i = 0 ; i < indent ; i++) {
        result.append("  ");
      }
      return result.toString();
    }

148
149
    public byte[] SignatureList.getData(int i, int version) {
      return getSignatureLine(i).getData(version);
Anders Nilsson's avatar
Anders Nilsson committed
150
151
    }

152
153
    public String SignatureList.getComment(int i) {
      return getSignatureLine(i).getComment();
Anders Nilsson's avatar
Anders Nilsson committed
154
155
    }

156
157
158
    private int SignatureList.indent;

    public void SignatureList.indent() {
Anders Nilsson's avatar
Anders Nilsson committed
159
160
161
      indent++;
    }

162
    public void SignatureList.unindent() {
Anders Nilsson's avatar
Anders Nilsson committed
163
164
165
166
      indent--;
    }


167
  public void ASTNode.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
168
    throw new Error(this.getClass().getName() + 
169
                    ".genSigLineForDecl(SignatureList list)" + 
Anders Nilsson's avatar
Anders Nilsson committed
170
171
172
                    " not declared");
  }

173
174
175
176
177
178
179
  public void TypeDecl.genSigLineForDecl(SignatureList list, boolean decl) {
     System.out.println("************ TypeDecl.genSigLine("+decl+").... for "+getName());
    if(decl){
      getType().genSigLineForDecl(list, decl);
    }else{
      list.addTypeRef(this, "//TODO (from list.addTypeRef)");
    }
Anders Nilsson's avatar
Anders Nilsson committed
180
181
  }

182
183
184
  public void SampleDecl.genSigLineForDecl(SignatureList list, boolean decl) {
     System.out.println("************ SampleDecl.genSigLine("+decl+").... for "+getName());
    getType().genSigLineForDecl(list, decl);
Anders Nilsson's avatar
Anders Nilsson committed
185
186
  }

187
  public void VoidType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
188
189
190
191
    list.addInt(LABCOMM_STRUCT, "void");
    list.addInt(0, null);
  }

192
  public void PrimType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
193
194
195
    list.addInt(getToken(), null);
  }

196
197
198
199
200
201
202
203
204
205
206
207
  public void UserType.genSigLineForDecl(SignatureList list, boolean decl) {
    if(decl){
     System.out.println("************ UserType.genSigLine("+decl+").... for "+getName());
      TypeDecl thet=lookupType(getName());
      System.out.println("************ thet: "+thet.getName() +":"+thet.getType());
      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
208
209
  }

210
  public void ArrayType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
211
212
213
214
    list.addInt(LABCOMM_ARRAY, signatureComment());
    list.indent();
    list.addInt(getNumExp(), null);
    for (int i = 0 ; i < getNumExp() ; i++) {
215
      getExp(i).genSigLineForDecl(list, false);
Anders Nilsson's avatar
Anders Nilsson committed
216
    }
217
    getType().genSigLineForDecl(list, false);
Anders Nilsson's avatar
Anders Nilsson committed
218
219
220
221
    list.unindent();
    list.add(null, "}");
  }

222
  public void StructType.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
223
224
225
226
    list.addInt(LABCOMM_STRUCT, "struct { " + getNumField() + " fields");
    list.indent();
    list.addInt(getNumField(), null);
    for (int i = 0 ; i < getNumField() ; i++) {
227
      getField(i).genSigLineForDecl(list, false);
Anders Nilsson's avatar
Anders Nilsson committed
228
229
230
231
232
    }
    list.unindent();
    list.add(null, "}");
  }

233
  public void Field.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
234
    list.addString(getName(), signatureComment());
235
    getType().genSigLineForDecl(list, decl);
Anders Nilsson's avatar
Anders Nilsson committed
236
237
  }

238
  public void IntegerLiteral.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
239
240
241
    list.addInt(Integer.parseInt(getValue()), null);
  }

242
  public void VariableSize.genSigLineForDecl(SignatureList list, boolean decl) {
Anders Nilsson's avatar
Anders Nilsson committed
243
244
    list.addInt(0, null);
  }
245
}