| Kenny Root | bd393b7 | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 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 |  */ | 
| Jack Palevich | 412f38f2 | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 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; | 
| Thomas Tafertshofer | 66a42db | 2012-06-15 16:22:43 -0700 | [diff] [blame] | 31 |     boolean hasEGLHandleArg = false; | 
| Jack Palevich | 412f38f2 | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 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 |         } | 
| Thomas Tafertshofer | 66a42db | 2012-06-15 16:22:43 -0700 | [diff] [blame] | 67 |         if (argType.isEGLHandle()) { | 
 | 68 |             hasEGLHandleArg = true; | 
 | 69 |         } | 
| Jack Palevich | 412f38f2 | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 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 |  | 
| Thomas Tafertshofer | 66a42db | 2012-06-15 16:22:43 -0700 | [diff] [blame] | 102 |     public boolean hasEGLHandleArg() { | 
 | 103 |         return hasEGLHandleArg; | 
 | 104 |     } | 
 | 105 |  | 
| Jack Palevich | 412f38f2 | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 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("(")) { | 
| Pablo Ceballos | 8a59ca7 | 2015-10-30 10:31:22 -0700 | [diff] [blame] | 144 |                 tok = tokens[i++]; | 
 | 145 |                 if (tok.equals("void")) { | 
 | 146 |                     break; | 
 | 147 |                 } | 
| Jack Palevich | 412f38f2 | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 148 |             } | 
 | 149 |             if (tok.equals(")")) { | 
 | 150 |                 break; | 
 | 151 |             } | 
 | 152 |  | 
 | 153 |             CType argType = new CType(); | 
 | 154 |  | 
 | 155 |             String argTypeName = tok; | 
 | 156 |             String argName = ""; | 
 | 157 |  | 
 | 158 |             if (argTypeName.equals("const")) { | 
 | 159 |                 argType.setIsConst(true); | 
 | 160 |                 argTypeName = tokens[i++]; | 
 | 161 |             } | 
 | 162 |             argType.setBaseType(argTypeName); | 
 | 163 |  | 
| Jack Palevich | 412f38f2 | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 164 |             argName = tokens[i++]; | 
 | 165 |             if (argName.startsWith("*")) { | 
 | 166 |                 argType.setIsPointer(true); | 
 | 167 |                 argName = argName.substring(1, argName.length()); | 
 | 168 |             } | 
 | 169 |             if (argName.endsWith(",")) { | 
 | 170 |                 argName = argName.substring(0, argName.length() - 1); | 
 | 171 |             } | 
 | 172 |  | 
 | 173 |             cfunc.addArgument(argName, argType); | 
 | 174 |         } | 
 | 175 |  | 
 | 176 |         return cfunc; | 
 | 177 |     } | 
 | 178 | } |