Landing Recent QUIC changes until 8/19/2015 17:00 UTC.
[chromium-blink-merge.git] / tools / gn / operators.cc
blobb78e9350df2a9b1db36441df5e39b1e7cafcbb09
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/operators.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "tools/gn/err.h"
9 #include "tools/gn/parse_tree.h"
10 #include "tools/gn/scope.h"
11 #include "tools/gn/token.h"
12 #include "tools/gn/value.h"
14 namespace {
16 const char kSourcesName[] = "sources";
18 // Applies the sources assignment filter from the given scope to each element
19 // of source (can be a list or a string), appending it to dest if it doesn't
20 // match.
21 void AppendFilteredSourcesToValue(const Scope* scope,
22 const Value& source,
23 Value* dest) {
24 const PatternList* filter = scope->GetSourcesAssignmentFilter();
26 if (source.type() == Value::STRING) {
27 if (!filter || filter->is_empty() ||
28 !filter->MatchesValue(source))
29 dest->list_value().push_back(source);
30 return;
32 if (source.type() != Value::LIST) {
33 // Any non-list and non-string being added to a list can just get appended,
34 // we're not going to filter it.
35 dest->list_value().push_back(source);
36 return;
39 if (!filter || filter->is_empty()) {
40 // No filter, append everything.
41 for (const auto& source_entry : source.list_value())
42 dest->list_value().push_back(source_entry);
43 return;
46 // Note: don't reserve() the dest vector here since that actually hurts
47 // the allocation pattern when the build script is doing multiple small
48 // additions.
49 for (const auto& source_entry : source.list_value()) {
50 if (!filter->MatchesValue(source_entry))
51 dest->list_value().push_back(source_entry);
55 Value GetValueOrFillError(const BinaryOpNode* op_node,
56 const ParseNode* node,
57 const char* name,
58 Scope* scope,
59 Err* err) {
60 Value value = node->Execute(scope, err);
61 if (err->has_error())
62 return Value();
63 if (value.type() == Value::NONE) {
64 *err = Err(op_node->op(),
65 "Operator requires a value.",
66 "This thing on the " + std::string(name) +
67 " does not evaluate to a value.");
68 err->AppendRange(node->GetRange());
69 return Value();
71 return value;
74 void RemoveMatchesFromList(const BinaryOpNode* op_node,
75 Value* list,
76 const Value& to_remove,
77 Err* err) {
78 std::vector<Value>& v = list->list_value();
79 switch (to_remove.type()) {
80 case Value::BOOLEAN:
81 case Value::INTEGER: // Filter out the individual int/string.
82 case Value::STRING: {
83 bool found_match = false;
84 for (size_t i = 0; i < v.size(); /* nothing */) {
85 if (v[i] == to_remove) {
86 found_match = true;
87 v.erase(v.begin() + i);
88 } else {
89 i++;
92 if (!found_match) {
93 *err = Err(to_remove.origin()->GetRange(), "Item not found",
94 "You were trying to remove " + to_remove.ToString(true) +
95 "\nfrom the list but it wasn't there.");
97 break;
100 case Value::LIST: // Filter out each individual thing.
101 for (const auto& elem : to_remove.list_value()) {
102 // TODO(brettw) if the nested item is a list, we may want to search
103 // for the literal list rather than remote the items in it.
104 RemoveMatchesFromList(op_node, list, elem, err);
105 if (err->has_error())
106 return;
108 break;
110 default:
111 break;
115 // Assignment -----------------------------------------------------------------
117 // We return a null value from this rather than the result of doing the append.
118 // See ValuePlusEquals for rationale.
119 Value ExecuteEquals(Scope* scope,
120 const BinaryOpNode* op_node,
121 const Token& left,
122 const Value& right,
123 Err* err) {
124 const Value* old_value = scope->GetValue(left.value(), false);
125 if (old_value) {
126 // Throw an error when overwriting a nonempty list with another nonempty
127 // list item. This is to detect the case where you write
128 // defines = ["FOO"]
129 // and you overwrote inherited ones, when instead you mean to append:
130 // defines += ["FOO"]
131 if (old_value->type() == Value::LIST &&
132 !old_value->list_value().empty() &&
133 right.type() == Value::LIST &&
134 !right.list_value().empty()) {
135 *err = Err(op_node->left()->GetRange(), "Replacing nonempty list.",
136 std::string("This overwrites a previously-defined nonempty list ") +
137 "(length " +
138 base::IntToString(static_cast<int>(old_value->list_value().size()))
139 + ").");
140 err->AppendSubErr(Err(*old_value, "for previous definition",
141 "with another one (length " +
142 base::IntToString(static_cast<int>(right.list_value().size())) +
143 "). Did you mean " +
144 "\"+=\" to append instead? If you\nreally want to do this, do\n " +
145 left.value().as_string() + " = []\nbefore reassigning."));
146 return Value();
149 if (err->has_error())
150 return Value();
152 if (right.type() == Value::LIST && left.value() == kSourcesName) {
153 // Assigning to sources, filter the list. Here we do the filtering and
154 // copying in one step to save an extra list copy (the lists may be
155 // long).
156 Value* set_value = scope->SetValue(left.value(),
157 Value(op_node, Value::LIST), op_node);
158 set_value->list_value().reserve(right.list_value().size());
159 AppendFilteredSourcesToValue(scope, right, set_value);
160 } else {
161 // Normal value set, just copy it.
162 scope->SetValue(left.value(), right, op_node->right());
164 return Value();
167 // allow_type_conversion indicates if we're allowed to change the type of the
168 // left value. This is set to true when doing +, and false when doing +=.
170 // Note that we return Value() from here, which is different than C++. This
171 // means you can't do clever things like foo = [ bar += baz ] to simultaneously
172 // append to and use a value. This is basically never needed in out build
173 // scripts and is just as likely an error as the intended behavior, and it also
174 // involves a copy of the value when it's returned. Many times we're appending
175 // to large lists, and copying the value to discard it for the next statement
176 // is very wasteful.
177 void ValuePlusEquals(const Scope* scope,
178 const BinaryOpNode* op_node,
179 const Token& left_token,
180 Value* left,
181 const Value& right,
182 bool allow_type_conversion,
183 Err* err) {
184 switch (left->type()) {
185 // Left-hand-side int.
186 case Value::INTEGER:
187 switch (right.type()) {
188 case Value::INTEGER: // int + int -> addition.
189 left->int_value() += right.int_value();
190 return;
192 case Value::STRING: // int + string -> string concat.
193 if (allow_type_conversion) {
194 *left = Value(op_node,
195 base::Int64ToString(left->int_value()) + right.string_value());
196 return;
198 break;
200 default:
201 break;
203 break;
205 // Left-hand-side string.
206 case Value::STRING:
207 switch (right.type()) {
208 case Value::INTEGER: // string + int -> string concat.
209 left->string_value().append(base::Int64ToString(right.int_value()));
210 return;
212 case Value::STRING: // string + string -> string contat.
213 left->string_value().append(right.string_value());
214 return;
216 default:
217 break;
219 break;
221 // Left-hand-side list.
222 case Value::LIST:
223 switch (right.type()) {
224 case Value::LIST: // list + list -> list concat.
225 if (left_token.value() == kSourcesName) {
226 // Filter additions through the assignment filter.
227 AppendFilteredSourcesToValue(scope, right, left);
228 } else {
229 // Normal list concat.
230 for (const auto& value : right.list_value())
231 left->list_value().push_back(value);
233 return;
235 default:
236 *err = Err(op_node->op(), "Incompatible types to add.",
237 "To append a single item to a list do \"foo += [ bar ]\".");
238 return;
241 default:
242 break;
245 *err = Err(op_node->op(), "Incompatible types to add.",
246 std::string("I see a ") + Value::DescribeType(left->type()) + " and a " +
247 Value::DescribeType(right.type()) + ".");
250 Value ExecutePlusEquals(Scope* scope,
251 const BinaryOpNode* op_node,
252 const Token& left,
253 const Value& right,
254 Err* err) {
255 // We modify in-place rather than doing read-modify-write to avoid
256 // copying large lists.
257 Value* left_value =
258 scope->GetValueForcedToCurrentScope(left.value(), op_node);
259 if (!left_value) {
260 *err = Err(left, "Undefined variable for +=.",
261 "I don't have something with this name in scope now.");
262 return Value();
264 ValuePlusEquals(scope, op_node, left, left_value, right, false, err);
265 left_value->set_origin(op_node);
266 scope->MarkUnused(left.value());
267 return Value();
270 // We return a null value from this rather than the result of doing the append.
271 // See ValuePlusEquals for rationale.
272 void ValueMinusEquals(const BinaryOpNode* op_node,
273 Value* left,
274 const Value& right,
275 bool allow_type_conversion,
276 Err* err) {
277 switch (left->type()) {
278 // Left-hand-side int.
279 case Value::INTEGER:
280 switch (right.type()) {
281 case Value::INTEGER: // int - int -> subtraction.
282 left->int_value() -= right.int_value();
283 return;
285 default:
286 break;
288 break;
290 // Left-hand-side string.
291 case Value::STRING:
292 break; // All are errors.
294 // Left-hand-side list.
295 case Value::LIST:
296 if (right.type() != Value::LIST) {
297 *err = Err(op_node->op(), "Incompatible types to subtract.",
298 "To remove a single item from a list do \"foo -= [ bar ]\".");
299 } else {
300 RemoveMatchesFromList(op_node, left, right, err);
302 return;
304 default:
305 break;
308 *err = Err(op_node->op(), "Incompatible types to subtract.",
309 std::string("I see a ") + Value::DescribeType(left->type()) + " and a " +
310 Value::DescribeType(right.type()) + ".");
313 Value ExecuteMinusEquals(Scope* scope,
314 const BinaryOpNode* op_node,
315 const Token& left,
316 const Value& right,
317 Err* err) {
318 Value* left_value =
319 scope->GetValueForcedToCurrentScope(left.value(), op_node);
320 if (!left_value) {
321 *err = Err(left, "Undefined variable for -=.",
322 "I don't have something with this name in scope now.");
323 return Value();
325 ValueMinusEquals(op_node, left_value, right, false, err);
326 left_value->set_origin(op_node);
327 scope->MarkUnused(left.value());
328 return Value();
331 // Plus/Minus -----------------------------------------------------------------
333 Value ExecutePlus(Scope* scope,
334 const BinaryOpNode* op_node,
335 const Value& left,
336 const Value& right,
337 Err* err) {
338 Value ret = left;
339 ValuePlusEquals(scope, op_node, Token(), &ret, right, true, err);
340 ret.set_origin(op_node);
341 return ret;
344 Value ExecuteMinus(Scope* scope,
345 const BinaryOpNode* op_node,
346 const Value& left,
347 const Value& right,
348 Err* err) {
349 Value ret = left;
350 ValueMinusEquals(op_node, &ret, right, true, err);
351 ret.set_origin(op_node);
352 return ret;
355 // Comparison -----------------------------------------------------------------
357 Value ExecuteEqualsEquals(Scope* scope,
358 const BinaryOpNode* op_node,
359 const Value& left,
360 const Value& right,
361 Err* err) {
362 if (left == right)
363 return Value(op_node, true);
364 return Value(op_node, false);
367 Value ExecuteNotEquals(Scope* scope,
368 const BinaryOpNode* op_node,
369 const Value& left,
370 const Value& right,
371 Err* err) {
372 // Evaluate in terms of ==.
373 Value result = ExecuteEqualsEquals(scope, op_node, left, right, err);
374 result.boolean_value() = !result.boolean_value();
375 return result;
378 Value FillNeedsTwoIntegersError(const BinaryOpNode* op_node,
379 const Value& left,
380 const Value& right,
381 Err* err) {
382 *err = Err(op_node, "Comparison requires two integers.",
383 "This operator can only compare two integers.");
384 err->AppendRange(left.origin()->GetRange());
385 err->AppendRange(right.origin()->GetRange());
386 return Value();
389 Value ExecuteLessEquals(Scope* scope,
390 const BinaryOpNode* op_node,
391 const Value& left,
392 const Value& right,
393 Err* err) {
394 if (left.type() != Value::INTEGER || right.type() != Value::INTEGER)
395 return FillNeedsTwoIntegersError(op_node, left, right, err);
396 return Value(op_node, left.int_value() <= right.int_value());
399 Value ExecuteGreaterEquals(Scope* scope,
400 const BinaryOpNode* op_node,
401 const Value& left,
402 const Value& right,
403 Err* err) {
404 if (left.type() != Value::INTEGER || right.type() != Value::INTEGER)
405 return FillNeedsTwoIntegersError(op_node, left, right, err);
406 return Value(op_node, left.int_value() >= right.int_value());
409 Value ExecuteGreater(Scope* scope,
410 const BinaryOpNode* op_node,
411 const Value& left,
412 const Value& right,
413 Err* err) {
414 if (left.type() != Value::INTEGER || right.type() != Value::INTEGER)
415 return FillNeedsTwoIntegersError(op_node, left, right, err);
416 return Value(op_node, left.int_value() > right.int_value());
419 Value ExecuteLess(Scope* scope,
420 const BinaryOpNode* op_node,
421 const Value& left,
422 const Value& right,
423 Err* err) {
424 if (left.type() != Value::INTEGER || right.type() != Value::INTEGER)
425 return FillNeedsTwoIntegersError(op_node, left, right, err);
426 return Value(op_node, left.int_value() < right.int_value());
429 // Binary ----------------------------------------------------------------------
431 Value ExecuteOr(Scope* scope,
432 const BinaryOpNode* op_node,
433 const ParseNode* left_node,
434 const ParseNode* right_node,
435 Err* err) {
436 Value left = GetValueOrFillError(op_node, left_node, "left", scope, err);
437 if (err->has_error())
438 return Value();
439 if (left.type() != Value::BOOLEAN) {
440 *err = Err(op_node->left(), "Left side of || operator is not a boolean.",
441 "Type is \"" + std::string(Value::DescribeType(left.type())) +
442 "\" instead.");
443 return Value();
445 if (left.boolean_value())
446 return Value(op_node, left.boolean_value());
448 Value right = GetValueOrFillError(op_node, right_node, "right", scope, err);
449 if (err->has_error())
450 return Value();
451 if (right.type() != Value::BOOLEAN) {
452 *err = Err(op_node->right(), "Right side of || operator is not a boolean.",
453 "Type is \"" + std::string(Value::DescribeType(right.type())) +
454 "\" instead.");
455 return Value();
458 return Value(op_node, left.boolean_value() || right.boolean_value());
461 Value ExecuteAnd(Scope* scope,
462 const BinaryOpNode* op_node,
463 const ParseNode* left_node,
464 const ParseNode* right_node,
465 Err* err) {
466 Value left = GetValueOrFillError(op_node, left_node, "left", scope, err);
467 if (err->has_error())
468 return Value();
469 if (left.type() != Value::BOOLEAN) {
470 *err = Err(op_node->left(), "Left side of && operator is not a boolean.",
471 "Type is \"" + std::string(Value::DescribeType(left.type())) +
472 "\" instead.");
473 return Value();
475 if (!left.boolean_value())
476 return Value(op_node, left.boolean_value());
478 Value right = GetValueOrFillError(op_node, right_node, "right", scope, err);
479 if (err->has_error())
480 return Value();
481 if (right.type() != Value::BOOLEAN) {
482 *err = Err(op_node->right(), "Right side of && operator is not a boolean.",
483 "Type is \"" + std::string(Value::DescribeType(right.type())) +
484 "\" instead.");
485 return Value();
487 return Value(op_node, left.boolean_value() && right.boolean_value());
490 } // namespace
492 // ----------------------------------------------------------------------------
494 Value ExecuteUnaryOperator(Scope* scope,
495 const UnaryOpNode* op_node,
496 const Value& expr,
497 Err* err) {
498 DCHECK(op_node->op().type() == Token::BANG);
500 if (expr.type() != Value::BOOLEAN) {
501 *err = Err(op_node, "Operand of ! operator is not a boolean.",
502 "Type is \"" + std::string(Value::DescribeType(expr.type())) +
503 "\" instead.");
504 return Value();
506 // TODO(scottmg): Why no unary minus?
507 return Value(op_node, !expr.boolean_value());
510 Value ExecuteBinaryOperator(Scope* scope,
511 const BinaryOpNode* op_node,
512 const ParseNode* left,
513 const ParseNode* right,
514 Err* err) {
515 const Token& op = op_node->op();
517 // First handle the ones that take an lvalue.
518 if (op.type() == Token::EQUAL ||
519 op.type() == Token::PLUS_EQUALS ||
520 op.type() == Token::MINUS_EQUALS) {
521 const IdentifierNode* left_id = left->AsIdentifier();
522 if (!left_id) {
523 *err = Err(op, "Operator requires a lvalue.",
524 "This thing on the left is not an identifier.");
525 err->AppendRange(left->GetRange());
526 return Value();
528 const Token& dest = left_id->value();
530 Value right_value = right->Execute(scope, err);
531 if (err->has_error())
532 return Value();
533 if (right_value.type() == Value::NONE) {
534 *err = Err(op, "Operator requires a rvalue.",
535 "This thing on the right does not evaluate to a value.");
536 err->AppendRange(right->GetRange());
537 return Value();
540 if (op.type() == Token::EQUAL)
541 return ExecuteEquals(scope, op_node, dest, right_value, err);
542 if (op.type() == Token::PLUS_EQUALS)
543 return ExecutePlusEquals(scope, op_node, dest, right_value, err);
544 if (op.type() == Token::MINUS_EQUALS)
545 return ExecuteMinusEquals(scope, op_node, dest, right_value, err);
546 NOTREACHED();
547 return Value();
550 // ||, &&. Passed the node instead of the value so that they can avoid
551 // evaluating the RHS on early-out.
552 if (op.type() == Token::BOOLEAN_OR)
553 return ExecuteOr(scope, op_node, left, right, err);
554 if (op.type() == Token::BOOLEAN_AND)
555 return ExecuteAnd(scope, op_node, left, right, err);
557 Value left_value = GetValueOrFillError(op_node, left, "left", scope, err);
558 if (err->has_error())
559 return Value();
560 Value right_value = GetValueOrFillError(op_node, right, "right", scope, err);
561 if (err->has_error())
562 return Value();
564 // +, -.
565 if (op.type() == Token::MINUS)
566 return ExecuteMinus(scope, op_node, left_value, right_value, err);
567 if (op.type() == Token::PLUS)
568 return ExecutePlus(scope, op_node, left_value, right_value, err);
570 // Comparisons.
571 if (op.type() == Token::EQUAL_EQUAL)
572 return ExecuteEqualsEquals(scope, op_node, left_value, right_value, err);
573 if (op.type() == Token::NOT_EQUAL)
574 return ExecuteNotEquals(scope, op_node, left_value, right_value, err);
575 if (op.type() == Token::GREATER_EQUAL)
576 return ExecuteGreaterEquals(scope, op_node, left_value, right_value, err);
577 if (op.type() == Token::LESS_EQUAL)
578 return ExecuteLessEquals(scope, op_node, left_value, right_value, err);
579 if (op.type() == Token::GREATER_THAN)
580 return ExecuteGreater(scope, op_node, left_value, right_value, err);
581 if (op.type() == Token::LESS_THAN)
582 return ExecuteLess(scope, op_node, left_value, right_value, err);
584 return Value();