2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "EntryPoint.h"
19 #include "TypeFactory.h"
27 EntryPoint::EntryPoint()
32 EntryPoint::~EntryPoint()
36 void EntryPoint::reset()
38 m_unsupported = false;
39 m_customDecoder = false;
41 m_flushOnEncode = false;
45 // return true for valid line (need to get into the entry points list)
46 bool EntryPoint::parse(unsigned int lc, const std::string & str)
52 std::string linestr = trim(str);
54 if (linestr.size() == 0) return false;
55 if (linestr.at(0) == '#') return false;
58 field = getNextToken(linestr, 0, &last, "(");
61 field = getNextToken(linestr, pos, &last, ",)");
64 std::string retTypeName;
65 if (!parseTypeDeclaration(field, &retTypeName, &error)) {
67 "line: %d: Parsing error in field <%s>: %s\n",
74 const VarType *theType = TypeFactory::instance()->getVarTypeByName(retTypeName);
75 if (theType->name() == "UNKNOWN") {
76 fprintf(stderr, "UNKNOWN retval: %s\n", linestr.c_str());
79 m_retval.init(std::string(""),
87 m_name = getNextToken(linestr, pos, &last, ",)");
92 while (pos < linestr.size() - 1) {
93 field = getNextToken(linestr, pos, &last, ",)");
94 if (field == "void") {
95 // 'void' is used as a special case for functions that don't take
99 std::string vartype, varname;
100 if (!parseParameterDeclaration(field, &vartype, &varname, &error)) {
102 "line: %d: Parsing error in field <%s>, error: %s\n",
109 const VarType *v = TypeFactory::instance()->getVarTypeByName(vartype);
111 fprintf(stderr, "%d: Unknown type: %s\n", lc, vartype.c_str());
114 !(v->name() == "void" && !v->isPointer())) {
115 std::ostringstream oss;
116 oss << "var" << nvars;
120 m_vars.push_back(Var(varname, v, std::string(""), Var::POINTER_IN, "", ""));
127 void EntryPoint::print(FILE *fp, bool newline,
128 const std::string & name_suffix,
129 const std::string & name_prefix,
130 const std::string & ctx_param ) const
132 fprintf(fp, "%s %s%s%s(",
133 m_retval.type()->name().c_str(),
136 name_suffix.c_str());
138 if (ctx_param != "") fprintf(fp, "%s ", ctx_param.c_str());
140 for (size_t i = 0; i < m_vars.size(); i++) {
141 if (m_vars[i].isVoid()) continue;
142 if (i != 0 || ctx_param != "") fprintf(fp, ", ");
143 fprintf(fp, "%s %s", m_vars[i].type()->name().c_str(),
144 m_vars[i].name().c_str());
146 fprintf(fp, ")%s", newline? "\n" : "");
149 Var * EntryPoint::var(const std::string & name)
152 for (size_t i = 0; i < m_vars.size(); i++) {
153 if (m_vars[i].name() == name) {
161 bool EntryPoint::hasPointers()
163 bool pointers = false;
164 if (m_retval.isPointer()) pointers = true;
166 for (size_t i = 0; i < m_vars.size(); i++) {
167 if (m_vars[i].isPointer()) {
176 int EntryPoint::setAttribute(const std::string &line, size_t lc)
180 std::string token = getNextToken(line, 0, &last, WHITESPACE);
182 if (token == "len") {
184 std::string varname = getNextToken(line, pos, &last, WHITESPACE);
186 if (varname.size() == 0) {
187 fprintf(stderr, "ERROR: %u: Missing variable name in 'len' attribute\n", (unsigned int)lc);
190 Var * v = var(varname);
192 fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
193 (unsigned int)lc, varname.c_str(), name().c_str());
196 // set the size expression into var
198 v->setLenExpression(line.substr(pos));
199 } else if (token == "param_check") {
201 std::string varname = getNextToken(line, pos, &last, WHITESPACE);
203 if (varname.size() == 0) {
204 fprintf(stderr, "ERROR: %u: Missing variable name in 'param_check' attribute\n", (unsigned int)lc);
207 Var * v = var(varname);
209 fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
210 (unsigned int)lc, varname.c_str(), name().c_str());
213 // set the size expression into var
215 v->setParamCheckExpression(line.substr(pos));
217 } else if (token == "dir") {
219 std::string varname = getNextToken(line, pos, &last, WHITESPACE);
220 if (varname.size() == 0) {
221 fprintf(stderr, "ERROR: %u: Missing variable name in 'dir' attribute\n", (unsigned int)lc);
224 Var * v = var(varname);
226 fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
227 (unsigned int)lc, varname.c_str(), name().c_str());
232 std::string pointerDirStr = getNextToken(line, pos, &last, WHITESPACE);
233 if (pointerDirStr.size() == 0) {
234 fprintf(stderr, "ERROR: %u: missing pointer directions\n", (unsigned int)lc);
238 if (pointerDirStr == "out") {
239 v->setPointerDir(Var::POINTER_OUT);
240 } else if (pointerDirStr == "inout") {
241 v->setPointerDir(Var::POINTER_INOUT);
242 } else if (pointerDirStr == "in") {
243 v->setPointerDir(Var::POINTER_IN);
245 fprintf(stderr, "ERROR: %u: unknow pointer direction %s\n", (unsigned int)lc, pointerDirStr.c_str());
247 } else if (token == "var_flag") {
249 std::string varname = getNextToken(line, pos, &last, WHITESPACE);
250 if (varname.size() == 0) {
251 fprintf(stderr, "ERROR: %u: Missing variable name in 'var_flag' attribute\n", (unsigned int)lc);
254 Var * v = var(varname);
256 fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
257 (unsigned int)lc, varname.c_str(), name().c_str());
263 std::string flag = getNextToken(line, pos, &last, WHITESPACE);
264 if (flag.size() == 0) {
266 fprintf(stderr, "ERROR: %u: missing flag\n", (unsigned int) lc);
273 if (flag == "nullAllowed") {
274 if (v->isPointer()) {
275 v->setNullAllowed(true);
277 fprintf(stderr, "WARNING: %u: setting nullAllowed for non-pointer variable %s\n",
278 (unsigned int) lc, v->name().c_str());
280 } else if (flag == "isLarge") {
281 if (v->isPointer()) {
284 fprintf(stderr, "WARNING: %u: setting isLarge flag for a non-pointer variable %s\n",
285 (unsigned int) lc, v->name().c_str());
288 fprintf(stderr, "WARNING: %u: unknow flag %s\n", (unsigned int)lc, flag.c_str());
291 } else if (token == "custom_pack") {
293 std::string varname = getNextToken(line, pos, &last, WHITESPACE);
295 if (varname.size() == 0) {
296 fprintf(stderr, "ERROR: %u: Missing variable name in 'custom_pack' attribute\n", (unsigned int)lc);
299 Var * v = var(varname);
301 fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
302 (unsigned int)lc, varname.c_str(), name().c_str());
305 // set the size expression into var
307 v->setPackExpression(line.substr(pos));
308 } else if (token == "custom_write") {
310 std::string varname = getNextToken(line, pos, &last, WHITESPACE);
312 if (varname.size() == 0) {
313 fprintf(stderr, "ERROR: %u: Missing variable name in 'custom_write' attribute\n", (unsigned int)lc);
316 Var * v = var(varname);
318 fprintf(stderr, "ERROR: %u: variable %s is not a parameter of %s\n",
319 (unsigned int)lc, varname.c_str(), name().c_str());
322 // set the size expression into var
324 v->setWriteExpression(line.substr(pos));
325 } else if (token == "flag") {
327 std::string flag = getNextToken(line, pos, &last, WHITESPACE);
328 if (flag.size() == 0) {
329 fprintf(stderr, "ERROR: %u: missing flag\n", (unsigned int) lc);
333 if (flag == "unsupported") {
334 setUnsupported(true);
335 } else if (flag == "custom_decoder") {
336 setCustomDecoder(true);
337 } else if (flag == "not_api") {
339 } else if (flag == "flushOnEncode") {
340 setFlushOnEncode(true);
342 fprintf(stderr, "WARNING: %u: unknown flag %s\n", (unsigned int)lc, flag.c_str());
345 fprintf(stderr, "WARNING: %u: unknown attribute %s\n", (unsigned int)lc, token.c_str());