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

import java.util.*;

public class CFunc {

    String original;

    CType ftype;
    String fname;

    List<String> argNames = new ArrayList<String>();
    List<CType> argTypes = new ArrayList<CType>();

    boolean hasPointerArg = false;
    boolean hasTypedPointerArg = false;

    public CFunc(String original) {
        this.original = original;
    }

    public String getOriginal() {
        return original;
    }

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

    public String getName() {
        return fname;
    }

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

    public CType getType() {
        return ftype;
    }

    public void addArgument(String argName, CType argType) {
        argNames.add(argName);
        argTypes.add(argType);

        if (argType.isPointer()) {
            hasPointerArg = true;
        }
        if (argType.isTypedPointer()) {
            hasTypedPointerArg = true;
        }
    }

    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 CType getArgType(int index) {
        return argTypes.get(index);
    }

    public boolean hasPointerArg() {
        return hasPointerArg;
    }

    public boolean hasTypedPointerArg() {
        return hasTypedPointerArg;
    }

    @Override
    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;
    }

    public static CFunc parseCFunc(String s) {
        CFunc cfunc = new CFunc(s);
        String[] tokens = s.split("\\s");

        int i = 0;
        CType ftype = new CType();
        String ftypeName = tokens[i++];
        if (ftypeName.equals("const")) {
            ftype.setIsConst(true);
            ftypeName = tokens[i++];
        }
        ftype.setBaseType(ftypeName);

        String fname = tokens[i++];
        if (fname.equals("*")) {
            ftype.setIsPointer(true);
            fname = tokens[i++];
        }

        cfunc.setName(fname);
        cfunc.setType(ftype);

        while (i < tokens.length) {
            String tok = tokens[i++];

            if (tok.equals("(")) {
                continue;
            }
            if (tok.equals(")")) {
                break;
            }

            CType argType = new CType();

            String argTypeName = tok;
            String argName = "";

            if (argTypeName.equals("const")) {
                argType.setIsConst(true);
                argTypeName = tokens[i++];
            }
            argType.setBaseType(argTypeName);

            if (argTypeName.equals("void")) {
                break;
            }

            argName = tokens[i++];
            if (argName.startsWith("*")) {
                argType.setIsPointer(true);
                argName = argName.substring(1, argName.length());
            }
            if (argName.endsWith(",")) {
                argName = argName.substring(0, argName.length() - 1);
            }

            cfunc.addArgument(argName, argType);
        }

        return cfunc;
    }
}