blob: 12b7dc06b3cade46e9a161f101ab8080f5116cbd [file] [log] [blame]
Steven Morelandf1a35f72016-08-17 08:41:49 -07001/*
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 Moreland95b46232016-09-20 14:46:55 -070021#include <hidl-util/StringHelper.h>
22
Steven Morelandf1a35f72016-08-17 08:41:49 -070023namespace android {
24
25FunctionDeclaration::FunctionDeclaration(Type* type,
26 const std::string &name,
27 std::vector<Declaration *> *params)
Steven Moreland95b46232016-09-20 14:46:55 -070028 : Declaration(""),
Steven Morelandf1a35f72016-08-17 08:41:49 -070029 mType(type),
30 mParams(params)
Steven Moreland95b46232016-09-20 14:46:55 -070031 {
32 setName(name);
33 }
Steven Morelandf1a35f72016-08-17 08:41:49 -070034
35FunctionDeclaration::~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 Moreland95b46232016-09-20 14:46:55 -070046void FunctionDeclaration::setName(const std::string &name) {
47 Declaration::setName(name);
48 forceCamelCase();
49}
Steven Morelandf1a35f72016-08-17 08:41:49 -070050
51const Type* FunctionDeclaration::getType() const {
52 return mType;
53}
54
55void FunctionDeclaration::generateSource(Formatter &out) const {
56 out << getName();
57
58 generateParams(out);
59
60 if (!getType()->isVoid()) {
61 out << " generates ("
Steven Moreland95b46232016-09-20 14:46:55 -070062 << getType()->decorateName(getName() + "Ret")
Steven Morelandf1a35f72016-08-17 08:41:49 -070063 << ")";
64 }
65
66 out << ";\n";
67}
68
69void FunctionDeclaration::generateParameterSource(Formatter &out) const {
70 out << getType()->decorateName("(*" + getName() + ")");
71
72 generateParams(out);
73}
74
75void 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
86void 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