Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 95b4623 | 2016-09-20 14:46:55 -0700 | [diff] [blame] | 21 | #include <hidl-util/StringHelper.h> |
| 22 | |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 23 | namespace android { |
| 24 | |
| 25 | FunctionDeclaration::FunctionDeclaration(Type* type, |
| 26 | const std::string &name, |
| 27 | std::vector<Declaration *> *params) |
Steven Moreland | 95b4623 | 2016-09-20 14:46:55 -0700 | [diff] [blame] | 28 | : Declaration(""), |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 29 | mType(type), |
| 30 | mParams(params) |
Steven Moreland | 95b4623 | 2016-09-20 14:46:55 -0700 | [diff] [blame] | 31 | { |
| 32 | setName(name); |
| 33 | } |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 34 | |
| 35 | FunctionDeclaration::~FunctionDeclaration() { |
| 36 | delete mType; |
| 37 | |
| 38 | if(mParams != NULL) { |
| 39 | for(auto* param : *mParams) { |
| 40 | delete param; |
| 41 | } |
| 42 | } |
| 43 | delete mParams; |
| 44 | } |
| 45 | |
Steven Moreland | 95b4623 | 2016-09-20 14:46:55 -0700 | [diff] [blame] | 46 | void FunctionDeclaration::setName(const std::string &name) { |
| 47 | Declaration::setName(name); |
| 48 | forceCamelCase(); |
| 49 | } |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 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 (" |
Steven Moreland | 95b4623 | 2016-09-20 14:46:55 -0700 | [diff] [blame] | 62 | << getType()->decorateName(getName() + "Ret") |
Steven Moreland | f1a35f7 | 2016-08-17 08:41:49 -0700 | [diff] [blame] | 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 |