summaryrefslogtreecommitdiff
path: root/src/fjbg/ch/epfl/lamp/fjbg/JMethodType.java
blob: ca9cbb6ba4b2b4da27f15b6bf522d7929ebb6494 (plain) (blame)
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
/* FJBG -- Fast Java Bytecode Generator
 * Copyright 2002-2012 LAMP/EPFL
 * @author  Michel Schinz
 */

package ch.epfl.lamp.fjbg;

/**
 * Type for Java methods. These types do not really exist in Java, but
 * are provided here because they are useful in several places.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class JMethodType extends JType {
    protected final JType returnType;
    protected final JType[] argTypes;
    protected String signature = null;

    public final static JMethodType ARGLESS_VOID_FUNCTION =
        new JMethodType(JType.VOID, JType.EMPTY_ARRAY);

    public JMethodType(JType returnType, JType[] argTypes) {
        this.returnType = returnType;
        this.argTypes = argTypes;
    }

    public JType getReturnType() { return returnType; }
    public JType[] getArgumentTypes() { return argTypes; }

    public int getSize() {
        throw new UnsupportedOperationException();
    }

    public String getSignature() {
        if (signature == null) {
            StringBuffer buf = new StringBuffer();
            buf.append('(');
            for (int i = 0; i < argTypes.length; ++i)
                buf.append(argTypes[i].getSignature());
            buf.append(')');
            buf.append(returnType.getSignature());
            signature = buf.toString();
        }
        return signature;
    }

    public int getTag() { return T_UNKNOWN; }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append('(');
        for (int i = 0; i < argTypes.length; ++i)
            buf.append(argTypes[i].toString());
        buf.append(')');
        buf.append(returnType.toString());
        return buf.toString();
    }

    public int getArgsSize() {
        int size = 0;
        for (int i = 0; i < argTypes.length; ++i)
            size += argTypes[i].getSize();
        return size;
    }

    public int getProducedStack() {
        return returnType.getSize() - getArgsSize();
    }

    public boolean isCompatibleWith(JType other) {
        return false;
    }
    public boolean equals(Object o) {
        if (o instanceof JMethodType)
            return ((JMethodType)o).getSignature().equals(this.getSignature());
        else
            return false;
    }
    public int hashCode() {
        if (signature == null)
            return 0;
        else
            return signature.hashCode();
    }
}