summaryrefslogtreecommitdiff
path: root/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/struct/gen/VarType.java
blob: a58898a1906df080b22c4f693fa1c258ffe18f73 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.java.decompiler.struct.gen;

import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.util.InterpreterUtil;

public class VarType {  // TODO: optimize switch

  public static final int FALSEBOOLEAN = 1;

  public static final VarType VARTYPE_UNKNOWN = new VarType(CodeConstants.TYPE_UNKNOWN);
  public static final VarType VARTYPE_INT = new VarType(CodeConstants.TYPE_INT);
  public static final VarType VARTYPE_FLOAT = new VarType(CodeConstants.TYPE_FLOAT);
  public static final VarType VARTYPE_LONG = new VarType(CodeConstants.TYPE_LONG);
  public static final VarType VARTYPE_DOUBLE = new VarType(CodeConstants.TYPE_DOUBLE);
  public static final VarType VARTYPE_BYTE = new VarType(CodeConstants.TYPE_BYTE);
  public static final VarType VARTYPE_CHAR = new VarType(CodeConstants.TYPE_CHAR);
  public static final VarType VARTYPE_SHORT = new VarType(CodeConstants.TYPE_SHORT);
  public static final VarType VARTYPE_BOOLEAN = new VarType(CodeConstants.TYPE_BOOLEAN);
  public static final VarType VARTYPE_BYTECHAR = new VarType(CodeConstants.TYPE_BYTECHAR);
  public static final VarType VARTYPE_SHORTCHAR = new VarType(CodeConstants.TYPE_SHORTCHAR);

  public static final VarType VARTYPE_NULL = new VarType(CodeConstants.TYPE_NULL, 0, null);
  public static final VarType VARTYPE_GROUP2EMPTY = new VarType(CodeConstants.TYPE_GROUP2EMPTY);
  public static final VarType VARTYPE_STRING = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/String");
  public static final VarType VARTYPE_CLASS = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Class");
  public static final VarType VARTYPE_OBJECT = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Object");
  public static final VarType VARTYPE_VOID = new VarType(CodeConstants.TYPE_VOID);

  public int type;

  public int type_family;

  public int arraydim;

  public String value;

  public int stack_size;

  public int convinfo;

  public VarType(int type) {
    this.type = type;
    this.arraydim = 0;

    value = getChar(type);
    setStackSize(type);
    setFamily();
  }

  public VarType(int type, int arraydim) {
    this(type);
    this.arraydim = arraydim;
    setFamily();
  }

  public VarType(int type, int arraydim, String value) {
    this(type);
    this.arraydim = arraydim;
    this.value = value;
    setFamily();
  }

  public VarType(String strtype) {
    this(strtype, false);
  }

  public VarType(String strtype, boolean cltype) {
    parseTypeString(strtype, cltype);
    setStackSize(type);
    setFamily();
  }

  public void decArrayDim() {
    if (arraydim > 0) {
      arraydim--;
      setFamily();
    }
    else {
      // throw new RuntimeException("array dimension equals 0!"); FIXME: investigate this case
    }
  }

  public String toString() {
    String res = "";

    for (int i = 0; i < arraydim; i++) {
      res += "[";
    }

    if (type == CodeConstants.TYPE_OBJECT) {
      res += "L" + value + ";";
    }
    else {
      res += value;
    }

    return res;
  }

  public VarType copy() {
    VarType v = new VarType(type, arraydim, value);
    v.convinfo = convinfo;
    return v;
  }

  public boolean isFalseBoolean() {
    return (convinfo & FALSEBOOLEAN) != 0;
  }

  public boolean isSuperset(VarType val) {

    return this.equals(val) || this.isStrictSuperset(val);
  }

  public boolean isStrictSuperset(VarType val) {

    int valtype = val.type;

    if (valtype == CodeConstants.TYPE_UNKNOWN && type != CodeConstants.TYPE_UNKNOWN) {
      return true;
    }

    if (val.arraydim > 0) {
      return this.equals(VARTYPE_OBJECT);
    }
    else if (arraydim > 0) {
      return (valtype == CodeConstants.TYPE_NULL);
    }

    boolean res = false;

    switch (type) {
      case CodeConstants.TYPE_INT:
        res |= (valtype == CodeConstants.TYPE_SHORT ||
                valtype == CodeConstants.TYPE_CHAR);
      case CodeConstants.TYPE_SHORT:
        res |= (valtype == CodeConstants.TYPE_BYTE);
      case CodeConstants.TYPE_CHAR:
        res |= (valtype == CodeConstants.TYPE_SHORTCHAR);
      case CodeConstants.TYPE_BYTE:
      case CodeConstants.TYPE_SHORTCHAR:
        res |= (valtype == CodeConstants.TYPE_BYTECHAR);
      case CodeConstants.TYPE_BYTECHAR:
        res |= (valtype == CodeConstants.TYPE_BOOLEAN);
        break;
      case CodeConstants.TYPE_OBJECT:
        if (valtype == CodeConstants.TYPE_NULL) {
          return true;
        }
        else if (this.equals(VARTYPE_OBJECT)) {
          return valtype == CodeConstants.TYPE_OBJECT &&
                 !val.equals(VARTYPE_OBJECT);
        }
    }

    return res;
  }

  // type1 and type2 must not be null
  public static VarType getCommonMinType(VarType type1, VarType type2) {

    if (type1.type == CodeConstants.TYPE_BOOLEAN && type2.type == CodeConstants.TYPE_BOOLEAN) { // special case booleans
      return type1.isFalseBoolean() ? type2 : type1;
    }

    if (type1.isSuperset(type2)) {
      return type2;
    }
    else if (type2.isSuperset(type1)) {
      return type1;
    }
    else if (type1.type_family == type2.type_family) {
      switch (type1.type_family) {
        case CodeConstants.TYPE_FAMILY_INTEGER:
          if ((type1.type == CodeConstants.TYPE_CHAR && type2.type == CodeConstants.TYPE_SHORT)
              || (type1.type == CodeConstants.TYPE_SHORT && type2.type == CodeConstants.TYPE_CHAR)) {
            return VARTYPE_SHORTCHAR;
          }
          else {
            return VARTYPE_BYTECHAR;
          }
        case CodeConstants.TYPE_FAMILY_OBJECT:
          return VARTYPE_NULL;
      }
    }

    return null;
  }

  // type1 and type2 must not be null
  public static VarType getCommonSupertype(VarType type1, VarType type2) {

    if (type1.type == CodeConstants.TYPE_BOOLEAN && type2.type == CodeConstants.TYPE_BOOLEAN) { // special case booleans
      return type1.isFalseBoolean() ? type1 : type2;
    }

    if (type1.isSuperset(type2)) {
      return type1;
    }
    else if (type2.isSuperset(type1)) {
      return type2;
    }
    else if (type1.type_family == type2.type_family) {
      switch (type1.type_family) {
        case CodeConstants.TYPE_FAMILY_INTEGER:
          if ((type1.type == CodeConstants.TYPE_SHORTCHAR && type2.type == CodeConstants.TYPE_BYTE)
              || (type1.type == CodeConstants.TYPE_BYTE && type2.type == CodeConstants.TYPE_SHORTCHAR)) {
            return VARTYPE_SHORT;
          }
          else {
            return VARTYPE_INT;
          }
        case CodeConstants.TYPE_FAMILY_OBJECT:
          return VARTYPE_OBJECT;
      }
    }

    return null;
  }

  public static VarType getMinTypeInFamily(int family) {
    switch (family) {
      case CodeConstants.TYPE_FAMILY_BOOLEAN:
        return VARTYPE_BOOLEAN;
      case CodeConstants.TYPE_FAMILY_INTEGER:
        return VARTYPE_BYTECHAR;
      case CodeConstants.TYPE_FAMILY_OBJECT:
        return VARTYPE_NULL;
      case CodeConstants.TYPE_FAMILY_FLOAT:
        return VARTYPE_FLOAT;
      case CodeConstants.TYPE_FAMILY_LONG:
        return VARTYPE_LONG;
      case CodeConstants.TYPE_FAMILY_DOUBLE:
        return VARTYPE_DOUBLE;
      case CodeConstants.TYPE_FAMILY_UNKNOWN:
        return VARTYPE_UNKNOWN;
      default:
        throw new RuntimeException("invalid type family!");
    }
  }

  public boolean equals(Object o) {

    if (o == this) {
      return true;
    }

    if (o == null || !(o instanceof VarType)) {
      return false;
    }

    VarType vt = (VarType)o;
    return type == vt.type && arraydim == vt.arraydim && InterpreterUtil.equalObjects(value, vt.value);
  }

  private void parseTypeString(String strtype, boolean cltype) {

    for (int i = 0; i < strtype.length(); i++) {
      switch (strtype.charAt(i)) {
        case '[':
          arraydim++;
          break;
        case 'L':
          if (strtype.charAt(strtype.length() - 1) == ';') {
            type = CodeConstants.TYPE_OBJECT;
            value = strtype.substring(i + 1, strtype.length() - 1);
            return;
          }
        default:
          value = strtype.substring(i, strtype.length());
          if ((cltype && i == 0) || value.length() > 1) {
            type = CodeConstants.TYPE_OBJECT;
          }
          else {
            type = getType(value.charAt(0));
          }
          return;
      }
    }
  }

  private void setStackSize(int type) {
    if (arraydim > 0) {
      stack_size = 1;
    }
    else {
      stack_size = (type == CodeConstants.TYPE_DOUBLE ||
                    type == CodeConstants.TYPE_LONG) ? 2 :
                   ((type == CodeConstants.TYPE_VOID ||
                     type == CodeConstants.TYPE_GROUP2EMPTY) ? 0 : 1);
    }
  }

  private static int getType(char c) {
    switch (c) {
      case 'B':
        return CodeConstants.TYPE_BYTE;
      case 'C':
        return CodeConstants.TYPE_CHAR;
      case 'D':
        return CodeConstants.TYPE_DOUBLE;
      case 'F':
        return CodeConstants.TYPE_FLOAT;
      case 'I':
        return CodeConstants.TYPE_INT;
      case 'J':
        return CodeConstants.TYPE_LONG;
      case 'S':
        return CodeConstants.TYPE_SHORT;
      case 'Z':
        return CodeConstants.TYPE_BOOLEAN;
      case 'V':
        return CodeConstants.TYPE_VOID;
      case 'G':
        return CodeConstants.TYPE_GROUP2EMPTY;
      case 'N':
        return CodeConstants.TYPE_NOTINITIALIZED;
      case 'A':
        return CodeConstants.TYPE_ADDRESS;
      case 'X':
        return CodeConstants.TYPE_BYTECHAR;
      case 'Y':
        return CodeConstants.TYPE_SHORTCHAR;
      case 'U':
        return CodeConstants.TYPE_UNKNOWN;
      default:
        throw new RuntimeException("Invalid type");
    }
  }

  private static String getChar(int type) {
    switch (type) {
      case CodeConstants.TYPE_BYTE:
        return "B";
      case CodeConstants.TYPE_CHAR:
        return "C";
      case CodeConstants.TYPE_DOUBLE:
        return "D";
      case CodeConstants.TYPE_FLOAT:
        return "F";
      case CodeConstants.TYPE_INT:
        return "I";
      case CodeConstants.TYPE_LONG:
        return "J";
      case CodeConstants.TYPE_SHORT:
        return "S";
      case CodeConstants.TYPE_BOOLEAN:
        return "Z";
      case CodeConstants.TYPE_VOID:
        return "V";
      case CodeConstants.TYPE_GROUP2EMPTY:
        return "G";
      case CodeConstants.TYPE_NOTINITIALIZED:
        return "N";
      case CodeConstants.TYPE_ADDRESS:
        return "A";
      case CodeConstants.TYPE_BYTECHAR:
        return "X";
      case CodeConstants.TYPE_SHORTCHAR:
        return "Y";
      case CodeConstants.TYPE_UNKNOWN:
        return "U";
      case CodeConstants.TYPE_NULL:
      case CodeConstants.TYPE_OBJECT:
        return null;
      default:
        throw new RuntimeException("Invalid type");
    }
  }

  public void setFamily() {

    if (arraydim > 0) {
      this.type_family = CodeConstants.TYPE_FAMILY_OBJECT;
      return;
    }

    switch (type) {
      case CodeConstants.TYPE_BYTE:
      case CodeConstants.TYPE_BYTECHAR:
      case CodeConstants.TYPE_SHORTCHAR:
      case CodeConstants.TYPE_CHAR:
      case CodeConstants.TYPE_SHORT:
      case CodeConstants.TYPE_INT:
        this.type_family = CodeConstants.TYPE_FAMILY_INTEGER;
        break;
      case CodeConstants.TYPE_DOUBLE:
        this.type_family = CodeConstants.TYPE_FAMILY_DOUBLE;
        break;
      case CodeConstants.TYPE_FLOAT:
        this.type_family = CodeConstants.TYPE_FAMILY_FLOAT;
        break;
      case CodeConstants.TYPE_LONG:
        this.type_family = CodeConstants.TYPE_FAMILY_LONG;
        break;
      case CodeConstants.TYPE_BOOLEAN:
        this.type_family = CodeConstants.TYPE_FAMILY_BOOLEAN;
        break;
      case CodeConstants.TYPE_NULL:
      case CodeConstants.TYPE_OBJECT:
        this.type_family = CodeConstants.TYPE_FAMILY_OBJECT;
        break;
      default:
        this.type_family = CodeConstants.TYPE_FAMILY_UNKNOWN;
    }
  }
}