1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/gn/parse_tree.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "tools/gn/functions.h"
12 #include "tools/gn/operators.h"
13 #include "tools/gn/scope.h"
14 #include "tools/gn/string_utils.h"
18 std::string
IndentFor(int value
) {
20 for (int i
= 0; i
< value
; i
++)
27 ParseNode::ParseNode() {
30 ParseNode::~ParseNode() {
33 const AccessorNode
* ParseNode::AsAccessor() const { return NULL
; }
34 const BinaryOpNode
* ParseNode::AsBinaryOp() const { return NULL
; }
35 const BlockNode
* ParseNode::AsBlock() const { return NULL
; }
36 const ConditionNode
* ParseNode::AsConditionNode() const { return NULL
; }
37 const FunctionCallNode
* ParseNode::AsFunctionCall() const { return NULL
; }
38 const IdentifierNode
* ParseNode::AsIdentifier() const { return NULL
; }
39 const ListNode
* ParseNode::AsList() const { return NULL
; }
40 const LiteralNode
* ParseNode::AsLiteral() const { return NULL
; }
41 const UnaryOpNode
* ParseNode::AsUnaryOp() const { return NULL
; }
43 // AccessorNode ---------------------------------------------------------------
45 AccessorNode::AccessorNode() {
48 AccessorNode::~AccessorNode() {
51 const AccessorNode
* AccessorNode::AsAccessor() const {
55 Value
AccessorNode::Execute(Scope
* scope
, Err
* err
) const {
57 return ExecuteArrayAccess(scope
, err
);
59 return ExecuteScopeAccess(scope
, err
);
64 LocationRange
AccessorNode::GetRange() const {
66 return LocationRange(base_
.location(), index_
->GetRange().end());
68 return LocationRange(base_
.location(), member_
->GetRange().end());
70 return LocationRange();
73 Err
AccessorNode::MakeErrorDescribing(const std::string
& msg
,
74 const std::string
& help
) const {
75 return Err(GetRange(), msg
, help
);
78 void AccessorNode::Print(std::ostream
& out
, int indent
) const {
79 out
<< IndentFor(indent
) << "ACCESSOR\n";
80 out
<< IndentFor(indent
+ 1) << base_
.value() << "\n";
82 index_
->Print(out
, indent
+ 1);
84 member_
->Print(out
, indent
+ 1);
87 Value
AccessorNode::ExecuteArrayAccess(Scope
* scope
, Err
* err
) const {
88 Value index_value
= index_
->Execute(scope
, err
);
91 if (!index_value
.VerifyTypeIs(Value::INTEGER
, err
))
94 const Value
* base_value
= scope
->GetValue(base_
.value(), true);
96 *err
= MakeErrorDescribing("Undefined identifier.");
99 if (!base_value
->VerifyTypeIs(Value::LIST
, err
))
102 int64 index_int
= index_value
.int_value();
104 *err
= Err(index_
->GetRange(), "Negative array subscript.",
105 "You gave me " + base::Int64ToString(index_int
) + ".");
108 size_t index_sizet
= static_cast<size_t>(index_int
);
109 if (index_sizet
>= base_value
->list_value().size()) {
110 *err
= Err(index_
->GetRange(), "Array subscript out of range.",
111 "You gave me " + base::Int64ToString(index_int
) +
112 " but I was expecting something from 0 to " +
114 static_cast<int64
>(base_value
->list_value().size()) - 1) +
119 // Doing this assumes that there's no way in the language to do anything
120 // between the time the reference is created and the time that the reference
121 // is used. If there is, this will crash! Currently, this is just used for
122 // array accesses where this "shouldn't" happen.
123 return base_value
->list_value()[index_sizet
];
126 Value
AccessorNode::ExecuteScopeAccess(Scope
* scope
, Err
* err
) const {
127 // We jump through some hoops here since ideally a.b will count "b" as
128 // accessed in the given scope. The value "a" might be in some normal nested
129 // scope and we can modify it, but it might also be inherited from the
130 // readonly root scope and we can't do used variable tracking on it. (It's
131 // not legal to const cast it away since the root scope will be in readonly
132 // mode and being accessed from multiple threads without locking.) So this
133 // code handles both cases.
134 const Value
* result
= NULL
;
136 // Look up the value in the scope named by "base_".
137 Value
* mutable_base_value
= scope
->GetMutableValue(base_
.value(), true);
138 if (mutable_base_value
) {
139 // Common case: base value is mutable so we can track variable accesses
140 // for unused value warnings.
141 if (!mutable_base_value
->VerifyTypeIs(Value::SCOPE
, err
))
143 result
= mutable_base_value
->scope_value()->GetValue(
144 member_
->value().value(), true);
146 // Fall back to see if the value is on a read-only scope.
147 const Value
* const_base_value
= scope
->GetValue(base_
.value(), true);
148 if (const_base_value
) {
149 // Read only value, don't try to mark the value access as a "used" one.
150 if (!const_base_value
->VerifyTypeIs(Value::SCOPE
, err
))
153 const_base_value
->scope_value()->GetValue(member_
->value().value());
155 *err
= Err(base_
, "Undefined identifier.");
161 *err
= Err(member_
.get(), "No value named \"" +
162 member_
->value().value() + "\" in scope \"" + base_
.value() + "\"");
168 // BinaryOpNode ---------------------------------------------------------------
170 BinaryOpNode::BinaryOpNode() {
173 BinaryOpNode::~BinaryOpNode() {
176 const BinaryOpNode
* BinaryOpNode::AsBinaryOp() const {
180 Value
BinaryOpNode::Execute(Scope
* scope
, Err
* err
) const {
181 return ExecuteBinaryOperator(scope
, this, left_
.get(), right_
.get(), err
);
184 LocationRange
BinaryOpNode::GetRange() const {
185 return left_
->GetRange().Union(right_
->GetRange());
188 Err
BinaryOpNode::MakeErrorDescribing(const std::string
& msg
,
189 const std::string
& help
) const {
190 return Err(op_
, msg
, help
);
193 void BinaryOpNode::Print(std::ostream
& out
, int indent
) const {
194 out
<< IndentFor(indent
) << "BINARY(" << op_
.value() << ")\n";
195 left_
->Print(out
, indent
+ 1);
196 right_
->Print(out
, indent
+ 1);
199 // BlockNode ------------------------------------------------------------------
201 BlockNode::BlockNode(bool has_scope
) : has_scope_(has_scope
) {
204 BlockNode::~BlockNode() {
205 STLDeleteContainerPointers(statements_
.begin(), statements_
.end());
208 const BlockNode
* BlockNode::AsBlock() const {
212 Value
BlockNode::Execute(Scope
* containing_scope
, Err
* err
) const {
214 Scope
our_scope(containing_scope
);
215 Value ret
= ExecuteBlockInScope(&our_scope
, err
);
216 if (err
->has_error())
219 // Check for unused vars in the scope.
220 our_scope
.CheckForUnusedVars(err
);
223 return ExecuteBlockInScope(containing_scope
, err
);
226 LocationRange
BlockNode::GetRange() const {
227 if (begin_token_
.type() != Token::INVALID
&&
228 end_token_
.type() != Token::INVALID
) {
229 return begin_token_
.range().Union(end_token_
.range());
230 } else if (!statements_
.empty()) {
231 return statements_
[0]->GetRange().Union(
232 statements_
[statements_
.size() - 1]->GetRange());
234 return LocationRange();
237 Err
BlockNode::MakeErrorDescribing(const std::string
& msg
,
238 const std::string
& help
) const {
239 return Err(GetRange(), msg
, help
);
242 void BlockNode::Print(std::ostream
& out
, int indent
) const {
243 out
<< IndentFor(indent
) << "BLOCK\n";
244 for (size_t i
= 0; i
< statements_
.size(); i
++)
245 statements_
[i
]->Print(out
, indent
+ 1);
248 Value
BlockNode::ExecuteBlockInScope(Scope
* our_scope
, Err
* err
) const {
249 for (size_t i
= 0; i
< statements_
.size() && !err
->has_error(); i
++) {
250 // Check for trying to execute things with no side effects in a block.
251 const ParseNode
* cur
= statements_
[i
];
252 if (cur
->AsList() || cur
->AsLiteral() || cur
->AsUnaryOp() ||
253 cur
->AsIdentifier()) {
254 *err
= cur
->MakeErrorDescribing(
255 "This statement has no effect.",
256 "Either delete it or do something with the result.");
259 cur
->Execute(our_scope
, err
);
264 // ConditionNode --------------------------------------------------------------
266 ConditionNode::ConditionNode() {
269 ConditionNode::~ConditionNode() {
272 const ConditionNode
* ConditionNode::AsConditionNode() const {
276 Value
ConditionNode::Execute(Scope
* scope
, Err
* err
) const {
277 Value condition_result
= condition_
->Execute(scope
, err
);
278 if (err
->has_error())
280 if (condition_result
.type() != Value::BOOLEAN
) {
281 *err
= condition_
->MakeErrorDescribing(
282 "Condition does not evaluate to a boolean value.",
283 std::string("This is a value of type \"") +
284 Value::DescribeType(condition_result
.type()) +
286 err
->AppendRange(if_token_
.range());
290 if (condition_result
.boolean_value()) {
291 if_true_
->ExecuteBlockInScope(scope
, err
);
292 } else if (if_false_
) {
293 // The else block is optional. It's either another condition (for an
294 // "else if" and we can just Execute it and the condition will handle
295 // the scoping) or it's a block indicating an "else" in which ase we
296 // need to be sure it inherits our scope.
297 const BlockNode
* if_false_block
= if_false_
->AsBlock();
299 if_false_block
->ExecuteBlockInScope(scope
, err
);
301 if_false_
->Execute(scope
, err
);
307 LocationRange
ConditionNode::GetRange() const {
309 return if_token_
.range().Union(if_false_
->GetRange());
310 return if_token_
.range().Union(if_true_
->GetRange());
313 Err
ConditionNode::MakeErrorDescribing(const std::string
& msg
,
314 const std::string
& help
) const {
315 return Err(if_token_
, msg
, help
);
318 void ConditionNode::Print(std::ostream
& out
, int indent
) const {
319 out
<< IndentFor(indent
) << "CONDITION\n";
320 condition_
->Print(out
, indent
+ 1);
321 if_true_
->Print(out
, indent
+ 1);
323 if_false_
->Print(out
, indent
+ 1);
326 // FunctionCallNode -----------------------------------------------------------
328 FunctionCallNode::FunctionCallNode() {
331 FunctionCallNode::~FunctionCallNode() {
334 const FunctionCallNode
* FunctionCallNode::AsFunctionCall() const {
338 Value
FunctionCallNode::Execute(Scope
* scope
, Err
* err
) const {
339 return functions::RunFunction(scope
, this, args_
.get(), block_
.get(), err
);
342 LocationRange
FunctionCallNode::GetRange() const {
344 return function_
.range().Union(block_
->GetRange());
345 return function_
.range().Union(args_
->GetRange());
348 Err
FunctionCallNode::MakeErrorDescribing(const std::string
& msg
,
349 const std::string
& help
) const {
350 return Err(function_
, msg
, help
);
353 void FunctionCallNode::Print(std::ostream
& out
, int indent
) const {
354 out
<< IndentFor(indent
) << "FUNCTION(" << function_
.value() << ")\n";
355 args_
->Print(out
, indent
+ 1);
357 block_
->Print(out
, indent
+ 1);
360 // IdentifierNode --------------------------------------------------------------
362 IdentifierNode::IdentifierNode() {
365 IdentifierNode::IdentifierNode(const Token
& token
) : value_(token
) {
368 IdentifierNode::~IdentifierNode() {
371 const IdentifierNode
* IdentifierNode::AsIdentifier() const {
375 Value
IdentifierNode::Execute(Scope
* scope
, Err
* err
) const {
376 const Value
* result
= scope
->GetValue(value_
.value(), true);
378 *err
= MakeErrorDescribing("Undefined identifier");
384 LocationRange
IdentifierNode::GetRange() const {
385 return value_
.range();
388 Err
IdentifierNode::MakeErrorDescribing(const std::string
& msg
,
389 const std::string
& help
) const {
390 return Err(value_
, msg
, help
);
393 void IdentifierNode::Print(std::ostream
& out
, int indent
) const {
394 out
<< IndentFor(indent
) << "IDENTIFIER(" << value_
.value() << ")\n";
397 // ListNode -------------------------------------------------------------------
399 ListNode::ListNode() {
402 ListNode::~ListNode() {
403 STLDeleteContainerPointers(contents_
.begin(), contents_
.end());
406 const ListNode
* ListNode::AsList() const {
410 Value
ListNode::Execute(Scope
* scope
, Err
* err
) const {
411 Value
result_value(this, Value::LIST
);
412 std::vector
<Value
>& results
= result_value
.list_value();
413 results
.resize(contents_
.size());
415 for (size_t i
= 0; i
< contents_
.size(); i
++) {
416 const ParseNode
* cur
= contents_
[i
];
417 results
[i
] = cur
->Execute(scope
, err
);
418 if (err
->has_error())
420 if (results
[i
].type() == Value::NONE
) {
421 *err
= cur
->MakeErrorDescribing(
422 "This does not evaluate to a value.",
423 "I can't do something with nothing.");
430 LocationRange
ListNode::GetRange() const {
431 return LocationRange(begin_token_
.location(), end_token_
.location());
434 Err
ListNode::MakeErrorDescribing(const std::string
& msg
,
435 const std::string
& help
) const {
436 return Err(begin_token_
, msg
, help
);
439 void ListNode::Print(std::ostream
& out
, int indent
) const {
440 out
<< IndentFor(indent
) << "LIST\n";
441 for (size_t i
= 0; i
< contents_
.size(); i
++)
442 contents_
[i
]->Print(out
, indent
+ 1);
445 // LiteralNode -----------------------------------------------------------------
447 LiteralNode::LiteralNode() {
450 LiteralNode::LiteralNode(const Token
& token
) : value_(token
) {
453 LiteralNode::~LiteralNode() {
456 const LiteralNode
* LiteralNode::AsLiteral() const {
460 Value
LiteralNode::Execute(Scope
* scope
, Err
* err
) const {
461 switch (value_
.type()) {
462 case Token::TRUE_TOKEN
:
463 return Value(this, true);
464 case Token::FALSE_TOKEN
:
465 return Value(this, false);
466 case Token::INTEGER
: {
468 if (!base::StringToInt64(value_
.value(), &result_int
)) {
469 *err
= MakeErrorDescribing("This does not look like an integer");
472 return Value(this, result_int
);
474 case Token::STRING
: {
475 Value
v(this, Value::STRING
);
476 ExpandStringLiteral(scope
, value_
, &v
, err
);
485 LocationRange
LiteralNode::GetRange() const {
486 return value_
.range();
489 Err
LiteralNode::MakeErrorDescribing(const std::string
& msg
,
490 const std::string
& help
) const {
491 return Err(value_
, msg
, help
);
494 void LiteralNode::Print(std::ostream
& out
, int indent
) const {
495 out
<< IndentFor(indent
) << "LITERAL(" << value_
.value() << ")\n";
498 // UnaryOpNode ----------------------------------------------------------------
500 UnaryOpNode::UnaryOpNode() {
503 UnaryOpNode::~UnaryOpNode() {
506 const UnaryOpNode
* UnaryOpNode::AsUnaryOp() const {
510 Value
UnaryOpNode::Execute(Scope
* scope
, Err
* err
) const {
511 Value operand_value
= operand_
->Execute(scope
, err
);
512 if (err
->has_error())
514 return ExecuteUnaryOperator(scope
, this, operand_value
, err
);
517 LocationRange
UnaryOpNode::GetRange() const {
518 return op_
.range().Union(operand_
->GetRange());
521 Err
UnaryOpNode::MakeErrorDescribing(const std::string
& msg
,
522 const std::string
& help
) const {
523 return Err(op_
, msg
, help
);
526 void UnaryOpNode::Print(std::ostream
& out
, int indent
) const {
527 out
<< IndentFor(indent
) << "UNARY(" << op_
.value() << ")\n";
528 operand_
->Print(out
, indent
+ 1);