summaryrefslogtreecommitdiffstats
path: root/tags/cpp-0.1.incubating-M1-RC3b/sca/tools/scagen/src/org/apache/tuscany/sca/cpp/tools/common/Signature.java
blob: 3650839cabec81e6339b692788b095179af21702 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  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.
 */

/*
 *  Branched from the original class that was also contributed to the 
 *  org.apache.axis.tools.common package.
 *  
 */
package org.apache.tuscany.sca.cpp.tools.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * A C or C++ method signature with the ability to parse it. TODO: properly
 * support variable length argument lists using "..." TODO: passing or returning
 * function pointers (hopefully not needed) TODO: Cope with ~ <space>Classname()
 */
public class Signature {
    private String originalText;

    private String attributes;

    private String className = null;
    
    private String namespace = null;

    private String methodName = null;

    private Parameter returnType = null;

    private Parameter[] params = null;

    private String trailingAttributes;

    private String scope = "public";

    private boolean failed = false;

    private boolean traceable = true;

    private final static Set knownAttrs = new HashSet(Arrays
            .asList(new Object[] { "public", "private", "extern", "\"C\"",
                    "virtual", "static", "inline" }));

    private final static Set specialOperators = new HashSet(
            Arrays.asList(new Object[] { "(", ")", "*", ",", "&", "]", "[",
                    "=", "~" }));

    /**
     * Takes an unparsed signature string and parses it.
     * 
     * TODO: Should optionally pass in the className here in case it's an inline
     * method implementation inside the class{}. Just so the className comes out
     * in the trace.
     */
    Signature(String s) {
        originalText = s;

        try {
            List tokens = tokenise(s);

            ArrayList alAttrs = new ArrayList();
            ArrayList alName = new ArrayList();
            ArrayList alParms = new ArrayList();
            ArrayList alTrailAttrs = new ArrayList();
            ArrayList alInits = new ArrayList();
            if (!splitUp(tokens, alAttrs, alName, alParms, alTrailAttrs,
                    alInits)) {
                failed = true;
                return;
            }

            parseAttributes(alAttrs);
            parseNameAndRetType(alName);
            parseParameters(alParms);
            parseTrailingAttributes(alTrailAttrs);

            // Ignore any tokens after the ) since these are (hopefully)
            // constructor initialisers

            traceable = !Configuration.methodExcluded(className, methodName);
        } catch (NullPointerException npe) {
            failed = true;
            traceable = false;
        }
    }

    /**
     * Parse the signature into tokens. This removes whitespace and comments and
     * separates out "*", ",", "(", ")", "&", "[" and "]".
     */
    private static List tokenise(String s) {
        ArrayList tokens = new ArrayList();
        String tok = null;
        boolean space = true;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isWhitespace(c)) {
                space = true;
                continue;
            }
            if (space) {
                if (tok != null)
                    tokens.add(tok);
                tok = "" + c;
            } else
                tok += c;
            space = false;

            if (tok.endsWith("/*")) {
                String sub = s.substring(i);
                int endcomm = sub.indexOf("*/");
                if (endcomm == -1)
                    break;
                i += endcomm + 1;
                if (tok.equals("/*"))
                    tok = "";
                else
                    tok = tok.substring(0, tok.length() - 2);
                continue;
            }

            if (tok.endsWith("//")) {
                String sub = s.substring(i);
                int endcomm = sub.indexOf("\n");
                if (endcomm == -1)
                    break;
                i += endcomm;
                if (tok.equals("//"))
                    tok = "";
                else
                    tok = tok.substring(0, tok.length() - 1);
                continue;
            }

            if (tok.endsWith("::"))
                space = true;

            String sc = "" + c;
            if (specialOperators.contains(sc)) {
                if (!tok.equals(sc)) {
                    tokens.add(tok.substring(0, tok.length() - 1));
                    tok = sc;
                }
                space = true;
            }
        }
        tokens.add(tok);
        return tokens;
    }

    /**
     * Split up a tokenised method signature into a list of attributes, a list
     * of name and return type tokens, a list of parameter tokens and a list of
     * initialiser tokens.
     */
    private static boolean splitUp(List tokens, List attrs, List nameAndRet,
            List parms, List trailAttrs, List inits) {

        // nameStart points to the start of the return type if there is one
        // else the start of the method name
        int nameStart;
        for (nameStart = 0; nameStart < tokens.size(); nameStart++) {
            String tok = (String) (tokens.get(nameStart));
            if (!knownAttrs.contains(tok) && !Configuration.isAttribute(tok))
                break;
        }
        if (nameStart == tokens.size())
            return false;

        // initStart points to the initialisers, or thrown exceptions after
        // the parameter list. throw is a keyword so we can safely search for
        // it.
        int initStart = tokens.size();
        for (int i = nameStart; i < tokens.size(); i++) {
            String tok = (String) tokens.get(i);
            if ((tok.startsWith(":") && !tok.startsWith("::"))
                    || "throw".equals(tok))
                initStart = i;
        }

        int parmEnd;
        for (parmEnd = initStart - 1; parmEnd > nameStart; parmEnd--)
            if (")".equals(tokens.get(parmEnd)))
                break;
        if (parmEnd == nameStart)
            return false;

        int parmStart = parmEnd;
        for (parmStart = parmEnd; parmStart > nameStart; parmStart--)
            if ("(".equals(tokens.get(parmStart)))
                break;

        for (int i = 0; i < tokens.size(); i++) {
            Object tok = tokens.get(i);
            if (i < nameStart || Configuration.isAttribute((String) tok))
                attrs.add(tok);
            else if (i < parmStart)
                nameAndRet.add(tok);
            else if (i <= parmEnd)
                parms.add(tok);
            else if (i < initStart)
                trailAttrs.add(tok);
            else
                inits.add(tok);
        }
        return true;
    }

    private void parseAttributes(List list) {
        attributes = new String();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            if (attributes.length() > 0)
                attributes += " ";
            String next = (String) it.next();
            
            //Tuscancy 
            //the scope is not present in the attributes
            //but is set later in the InputCppSource contructor
            if ("public".equals(next) || "protected".equals(next)
                    || "private".equals(next))
                scope = next;
            attributes += next;
        }
    }

    private void parseNameAndRetType(List list) {
        int size = list.size();
        int idx;
        // "operator" is a key word so if it's present we know we're
        // dealing with operator overloading. The operator that's been
        // overloaded might have been split up into multiple tokens.
        for (idx = 0; idx < size; idx++)
            if ("operator".equals(list.get(idx)))
                break;

        if (idx < size) {
            methodName = "";
            for (int i = idx; i < size; i++)
                methodName += (String) list.get(i);
        } else { // No operator overloading
            methodName = "" + list.get(size - 1);
            idx = size - 1;
        }

        // If it's a destructor, the "~" will be split out into a separate
        // token, so add it onto the methodName here.
        if (idx > 0 && "~".equals(list.get(idx - 1))) {
            methodName = "~" + methodName;
            idx--;
        }

        // The class name comes before the method name
        while (idx > 0 && ((String) list.get(idx - 1)).endsWith("::")) {
            if (null == className)
                className = (String) list.get(idx - 1);
            else
                className = (String) list.get(idx - 1) + className;
            idx--;
        }

        // Whatever's left before the classname/methodname must be the
        // return type
        ArrayList retParm = new ArrayList();
        for (int i = 0; i < idx; i++)
            retParm.add(list.get(i));

        returnType = new Parameter(retParm, true);
    }

    /**
     * Constructs the parameter list
     */
    private void parseParameters(List list) {
        ArrayList alParams = new ArrayList();
        Iterator it = list.iterator();
        String token = (String) it.next(); // step over the (
        while (it.hasNext() && !")".equals(token)) {
            token = (String) it.next();

            int template = 0; // Depth of template scope
            boolean foundEquals = false;
            // Ignore default value for an optional parameter
            ArrayList parm = new ArrayList();
            while (!token.equals(")") && (!token.equals(",") || template > 0)) {
                if (token.equals("="))
                    foundEquals = true;
                if (!foundEquals)
                    parm.add(token);
                if (contains(token, "<"))
                    template++;
                if (contains(token, ">"))
                    template--;
                token = (String) it.next();
            }

            // No parameters so break out
            if (token.equals(")") && 0 == parm.size())
                break;

            Parameter p = new Parameter(parm);
            if (p.failed()) {
                failed = true;
                return;
            }

            // Copes with void func(void)
            if (!p.isVoid())
                alParams.add(p);
        }

        int size = alParams.size();
        if (size > 0) {
            params = new Parameter[size];
            System.arraycopy(alParams.toArray(), 0, params, 0, size);
        }
    }

    private void parseTrailingAttributes(List list) {
        trailingAttributes = new String();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            if (trailingAttributes.length() > 0)
                trailingAttributes += " ";
            trailingAttributes += (String) it.next();
        }
    }

    public String getOriginal() {
        return originalText;
    }

    public int originalLength() {
        return originalText.length();
    }

    public boolean failed() {
        return failed;
    }

    public String getAttributes() {
        return attributes;
    }

    public String getClassName() {
        return className;
    }

    /**
     * @param namespace The namespace to set.
     */
    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    /**
     * @return Returns the namespace.
     */
    public String getNamespace() {
        return namespace;
    }

    public String getTrimClassName() {
        return trimClassName(className);
    }

    public String getMethodName() {
        return methodName;
    }

    public Parameter getReturnType() {
        return returnType;
    }

    public Parameter[] getParameters() {
        return params;
    }

    public boolean isConstructor() {
        return className != null && methodName != null
                && trimClassName(className).equals(methodName);
    }

    public boolean isDestructor() {
        return className != null && methodName != null
                && methodName.startsWith("~")
                && methodName.endsWith(trimClassName(className));
    }

    private static String trimClassName(String name) {
        if (name.endsWith("::"))
            return name.substring(0, name.length() - 2);
        return name;
    }

    void setClassName(String className) {
        if (null == className)
            return;
        if (!className.endsWith("::"))
            className += "::";
        this.className = className;
    }

    public String getScope() {
        return scope;
    }

    /**
     * Sets the scope, but only if the scope is not set by an explicit attribute
     * in the signature.
     */
    public void setScope(String scope) {
        if (-1 == attributes.indexOf(this.scope))
            this.scope = scope;
    }

    /**
     * Should this method be traced?
     */
    public boolean traceable() {
        return traceable;
    }

    private static boolean contains(String src, String tgt) {
        if (src == null || tgt == null)
            return false;
        if (-1 == src.indexOf(tgt))
            return false;
        return true;
    }

    public boolean equals(Object obj) {
        if (null == obj || !(obj instanceof Signature))
            return false;
        Signature that = (Signature) obj;
        if (!Utils.safeEquals(className, that.className))
            return false;
        if (!Utils.safeEquals(methodName, that.methodName))
            return false;
        if (!Utils.safeEquals(returnType, that.returnType))
            return false;
        if (null == params && null == that.params)
            return true;
        if (null != params && null == that.params)
            return false;
        if (null == params && null != that.params)
            return false;
        if (params.length != that.params.length)
            return false;
        for (int i = 0; i < params.length; i++)
            if (!Utils.safeEquals(params[i], that.params[i]))
                return false;
        return true;
    }

    public String toStringWithoutAttrs() {
        String s = new String();
        if (returnType != null)
            s += returnType + " ";
        if (className != null)
            s += className;
        s += methodName + "(";
        for (int i = 0; params != null && i < params.length; i++) {
            if (i > 0)
                s += ", ";
            s += params[i].toString();
        }
        s += ")";
        return s;
    }

    public String toString() {
        String s = attributes;
        if (attributes.length() > 0)
            s += " ";
        s += toStringWithoutAttrs();
        if (trailingAttributes.length() > 0)
            s += " " + trailingAttributes;
        return s;
    }
}