Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import java.util.*;
     18 
     19 public class CFunc {
     20 
     21     String original;
     22 
     23     CType ftype;
     24     String fname;
     25 
     26     List<String> argNames = new ArrayList<String>();
     27     List<CType> argTypes = new ArrayList<CType>();
     28 
     29     boolean hasPointerArg = false;
     30     boolean hasTypedPointerArg = false;
     31     boolean hasEGLHandleArg = false;
     32 
     33     public CFunc(String original) {
     34         this.original = original;
     35     }
     36 
     37     public String getOriginal() {
     38         return original;
     39     }
     40 
     41     public void setName(String fname) {
     42         this.fname = fname;
     43     }
     44 
     45     public String getName() {
     46         return fname;
     47     }
     48 
     49     public void setType(CType ftype) {
     50         this.ftype = ftype;
     51     }
     52 
     53     public CType getType() {
     54         return ftype;
     55     }
     56 
     57     public void addArgument(String argName, CType argType) {
     58         argNames.add(argName);
     59         argTypes.add(argType);
     60 
     61         if (argType.isPointer()) {
     62             hasPointerArg = true;
     63         }
     64         if (argType.isTypedPointer()) {
     65             hasTypedPointerArg = true;
     66         }
     67         if (argType.isEGLHandle()) {
     68             hasEGLHandleArg = true;
     69         }
     70     }
     71 
     72     public int getNumArgs() {
     73         return argNames.size();
     74     }
     75 
     76     public int getArgIndex(String name) {
     77         int len = argNames.size();
     78         for (int i = 0; i < len; i++) {
     79             if (name.equals(argNames.get(i))) {
     80                 return i;
     81             }
     82         }
     83         return -1;
     84     }
     85 
     86     public String getArgName(int index) {
     87         return argNames.get(index);
     88     }
     89 
     90     public CType getArgType(int index) {
     91         return argTypes.get(index);
     92     }
     93 
     94     public boolean hasPointerArg() {
     95         return hasPointerArg;
     96     }
     97 
     98     public boolean hasTypedPointerArg() {
     99         return hasTypedPointerArg;
    100     }
    101 
    102     public boolean hasEGLHandleArg() {
    103         return hasEGLHandleArg;
    104     }
    105 
    106     @Override
    107     public String toString() {
    108         String s =  "Function " + fname + " returns " + ftype + ": ";
    109         for (int i = 0; i < argNames.size(); i++) {
    110             if (i > 0) {
    111                 s += ", ";
    112             }
    113             s += argTypes.get(i) + " " + argNames.get(i);
    114         }
    115         return s;
    116     }
    117 
    118     public static CFunc parseCFunc(String s) {
    119         CFunc cfunc = new CFunc(s);
    120         String[] tokens = s.split("\\s");
    121 
    122         int i = 0;
    123         CType ftype = new CType();
    124         String ftypeName = tokens[i++];
    125         if (ftypeName.equals("const")) {
    126             ftype.setIsConst(true);
    127             ftypeName = tokens[i++];
    128         }
    129         ftype.setBaseType(ftypeName);
    130 
    131         String fname = tokens[i++];
    132         if (fname.equals("*")) {
    133             ftype.setIsPointer(true);
    134             fname = tokens[i++];
    135         }
    136 
    137         cfunc.setName(fname);
    138         cfunc.setType(ftype);
    139 
    140         while (i < tokens.length) {
    141             String tok = tokens[i++];
    142 
    143             if (tok.equals("(")) {
    144                 continue;
    145             }
    146             if (tok.equals(")")) {
    147                 break;
    148             }
    149 
    150             CType argType = new CType();
    151 
    152             String argTypeName = tok;
    153             String argName = "";
    154 
    155             if (argTypeName.equals("const")) {
    156                 argType.setIsConst(true);
    157                 argTypeName = tokens[i++];
    158             }
    159             argType.setBaseType(argTypeName);
    160 
    161             if (argTypeName.equals("void")) {
    162                 break;
    163             }
    164 
    165             argName = tokens[i++];
    166             if (argName.startsWith("*")) {
    167                 argType.setIsPointer(true);
    168                 argName = argName.substring(1, argName.length());
    169             }
    170             if (argName.endsWith(",")) {
    171                 argName = argName.substring(0, argName.length() - 1);
    172             }
    173 
    174             cfunc.addArgument(argName, argType);
    175         }
    176 
    177         return cfunc;
    178     }
    179 }
    180