summaryrefslogtreecommitdiffstats
path: root/opengl/tools/glgen/src/JFunc.java
blob: 42d466c33ed6d4e8e6637378f756b5761da55aed (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

import java.util.ArrayList;
import java.util.List;

public class JFunc {

    String className = "com.google.android.gles_jni.GL11Impl";

    CFunc cfunc;
    JType ftype;
    String fname;

    List<String> argNames = new ArrayList<String>();
    List<JType> argTypes = new ArrayList<JType>();
    List<Integer> argCIndices = new ArrayList<Integer>();

    boolean hasBufferArg = false;
    boolean hasTypedBufferArg = false;
    ArrayList<String> bufferArgNames = new ArrayList<String>();

    public JFunc(CFunc cfunc) {
        this.cfunc = cfunc;
    }

    public CFunc getCFunc() {
        return cfunc;
    }

    public void setName(String fname) {
        this.fname = fname;
    }

    public String getName() {
        return fname;
    }

    public void setType(JType ftype) {
        this.ftype = ftype;
    }

    public JType getType() {
        return ftype;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getClassName() {
        return className;
    }
    
    public boolean hasBufferArg() {
        return hasBufferArg;
    }

    public boolean hasTypedBufferArg() {
        return hasTypedBufferArg;
    }

    public String getBufferArgName(int index) {
        return bufferArgNames.get(index);
    }

    public void addArgument(String argName, JType argType, int cindex) {
        argNames.add(argName);
        argTypes.add(argType);
        argCIndices.add(new Integer(cindex));

        if (argType.isBuffer()) {
            hasBufferArg = true;
            bufferArgNames.add(argName);
        }
        if (argType.isTypedBuffer()) {
            hasTypedBufferArg = true;
            bufferArgNames.add(argName);
        }
    }

    public int getNumArgs() {
        return argNames.size();
    }

    public int getArgIndex(String name) {
        int len = argNames.size();
        for (int i = 0; i < len; i++) {
            if (name.equals(argNames.get(i))) {
                return i;
            }
        }
        return -1;
    }

    public String getArgName(int index) {
        return argNames.get(index);
    }

    public JType getArgType(int index) {
        return argTypes.get(index);
    }

    public int getArgCIndex(int index) {
        return argCIndices.get(index).intValue();
    }

    public static JFunc convert(CFunc cfunc, boolean useArray) {
        JFunc jfunc = new JFunc(cfunc);
        jfunc.setName(cfunc.getName());
        jfunc.setType(JType.convert(cfunc.getType(), false));
	
        int numArgs = cfunc.getNumArgs();
        int numOffsets = 0;
        for (int i = 0; i < numArgs; i++) {
            CType cArgType = cfunc.getArgType(i);
            if (cArgType.isTypedPointer() && useArray) {
                ++numOffsets;
            }
        }

        for (int i = 0; i < numArgs; i++) {
            String cArgName = cfunc.getArgName(i);
            CType cArgType = cfunc.getArgType(i);

            jfunc.addArgument(cArgName, JType.convert(cArgType, useArray), i);
            if (cArgType.isTypedPointer() && useArray) {
                if (numOffsets > 1) {
                    jfunc.addArgument(cArgName + "Offset", new JType("int"), i);
                } else {
                    jfunc.addArgument("offset", new JType("int"), i);
                }
            }
        }

        return jfunc;
    }

    public String toString() {
        String s =  "Function " + fname + " returns " + ftype + ": ";
        for (int i = 0; i < argNames.size(); i++) {
            if (i > 0) {
                s += ", ";
            }
            s += argTypes.get(i) + " " + argNames.get(i);
        }
        return s;
    }

}