1 /* 2 * Copyright (C) 2016 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 #include "FunctionDeclaration.h" 18 #include "VarDeclaration.h" 19 #include "Type.h" 20 21 #include <hidl-util/StringHelper.h> 22 23 namespace android { 24 25 FunctionDeclaration::FunctionDeclaration(Type* type, 26 const std::string &name, 27 std::vector<Declaration *> *params) 28 : Declaration(""), 29 mType(type), 30 mParams(params) 31 { 32 setName(name); 33 } 34 35 FunctionDeclaration::~FunctionDeclaration() { 36 delete mType; 37 38 if(mParams != nullptr) { 39 for(auto* param : *mParams) { 40 delete param; 41 } 42 } 43 delete mParams; 44 } 45 46 void FunctionDeclaration::setName(const std::string &name) { 47 Declaration::setName(name); 48 forceCamelCase(); 49 } 50 51 const Type* FunctionDeclaration::getType() const { 52 return mType; 53 } 54 55 void FunctionDeclaration::generateSource(Formatter &out) const { 56 out << getName(); 57 58 generateParams(out); 59 60 if (!getType()->isVoid()) { 61 out << " generates (" 62 << getType()->decorateName(getName() + "Ret") 63 << ")"; 64 } 65 66 out << ";\n"; 67 } 68 69 void FunctionDeclaration::generateParameterSource(Formatter &out) const { 70 out << getType()->decorateName("(*" + getName() + ")"); 71 72 generateParams(out); 73 } 74 75 void FunctionDeclaration::processContents(AST &) { 76 if (mParams->size() == 1 && 77 (*mParams)[0]->decType() == VarDeclaration::type()) { 78 79 VarDeclaration* var = (VarDeclaration *)(*mParams)[0]; 80 if (var->getType()->isVoid()) { 81 mParams->clear(); 82 } 83 } 84 } 85 86 void FunctionDeclaration::generateParams(Formatter &out) const { 87 out << "("; 88 89 for (auto it = mParams->begin(); it != mParams->end(); ++it) { 90 if (it != mParams->begin()) { 91 out << ", "; 92 } 93 94 (*it)->generateParameterSource(out); 95 } 96 97 out << ")"; 98 } 99 100 } //namespace android