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 "Define.h"
18
19 #include "AST.h"
20
21 extern android::status_t parseExpression(android::AST *ast, std::string str);
22
23 namespace android {
24
Define(const std::string & name,const std::string & slurp)25 Define::Define(const std::string &name, const std::string &slurp)
26 : Declaration(name), mSlurp(slurp)
27 {}
28
~Define()29 Define::~Define() {
30 delete mExpression;
31 }
32
getExpressionType() const33 Expression::Type Define::getExpressionType() const {
34 return mExpressionType;
35 }
getExpression() const36 Expression *Define::getExpression() const {
37 return mExpression;
38 }
setExpression(Expression * expression)39 void Define::setExpression(Expression* expression) {
40 mExpression = expression;
41 }
42
generateSource(Formatter & out) const43 void Define::generateSource(Formatter &out) const {
44 out << "/* #define " << getName() << " " << mSlurp << " */\n";
45 }
46
processContents(AST & ast)47 void Define::processContents(AST &ast) {
48 status_t res = parseExpression(&ast, mSlurp);
49
50 if (res != 0) {
51 mExpressionType = Expression::Type::UNKNOWN;
52 return;
53 }
54
55 mExpression = ast.getExpression();
56 ast.setExpression(nullptr);
57
58 mExpressionType = mExpression->getType(ast);
59
60 ast.getDefinesScope().enter(getName(), this);
61 }
62
63 } //namespace android
64