Home | History | Annotate | Download | only in gn
      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.
      4 
      5 #include "tools/gn/parse_tree.h"
      6 
      7 #include <string>
      8 
      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"
     15 
     16 namespace {
     17 
     18 std::string IndentFor(int value) {
     19   std::string ret;
     20   for (int i = 0; i < value; i++)
     21     ret.append(" ");
     22   return ret;
     23 }
     24 
     25 }  // namespace
     26 
     27 ParseNode::ParseNode() {
     28 }
     29 
     30 ParseNode::~ParseNode() {
     31 }
     32 
     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; }
     42 
     43 // AccessorNode ---------------------------------------------------------------
     44 
     45 AccessorNode::AccessorNode() {
     46 }
     47 
     48 AccessorNode::~AccessorNode() {
     49 }
     50 
     51 const AccessorNode* AccessorNode::AsAccessor() const {
     52   return this;
     53 }
     54 
     55 Value AccessorNode::Execute(Scope* scope, Err* err) const {
     56   Value index_value = index_->Execute(scope, err);
     57   if (err->has_error())
     58     return Value();
     59   if (!index_value.VerifyTypeIs(Value::INTEGER, err))
     60     return Value();
     61 
     62   const Value* base_value = scope->GetValue(base_.value(), true);
     63   if (!base_value) {
     64     *err = MakeErrorDescribing("Undefined identifier.");
     65     return Value();
     66   }
     67   if (!base_value->VerifyTypeIs(Value::LIST, err))
     68     return Value();
     69 
     70   int64 index_int = index_value.int_value();
     71   if (index_int < 0) {
     72     *err = Err(index_->GetRange(), "Negative array subscript.",
     73         "You gave me " + base::Int64ToString(index_int) + ".");
     74     return Value();
     75   }
     76   size_t index_sizet = static_cast<size_t>(index_int);
     77   if (index_sizet >= base_value->list_value().size()) {
     78     *err = Err(index_->GetRange(), "Array subscript out of range.",
     79         "You gave me " + base::Int64ToString(index_int) +
     80         " but I was expecting something from 0 to " +
     81         base::Int64ToString(
     82             static_cast<int64>(base_value->list_value().size()) - 1) +
     83         ", inclusive.");
     84     return Value();
     85   }
     86 
     87   // Doing this assumes that there's no way in the language to do anything
     88   // between the time the reference is created and the time that the reference
     89   // is used. If there is, this will crash! Currently, this is just used for
     90   // array accesses where this "shouldn't" happen.
     91   return base_value->list_value()[index_sizet];
     92 }
     93 
     94 LocationRange AccessorNode::GetRange() const {
     95   return LocationRange(base_.location(), index_->GetRange().end());
     96 }
     97 
     98 Err AccessorNode::MakeErrorDescribing(const std::string& msg,
     99                                       const std::string& help) const {
    100   return Err(GetRange(), msg, help);
    101 }
    102 
    103 void AccessorNode::Print(std::ostream& out, int indent) const {
    104   out << IndentFor(indent) << "ACCESSOR\n";
    105   out << IndentFor(indent + 1) << base_.value() << "\n";
    106   index_->Print(out, indent + 1);
    107 }
    108 
    109 // BinaryOpNode ---------------------------------------------------------------
    110 
    111 BinaryOpNode::BinaryOpNode() {
    112 }
    113 
    114 BinaryOpNode::~BinaryOpNode() {
    115 }
    116 
    117 const BinaryOpNode* BinaryOpNode::AsBinaryOp() const {
    118   return this;
    119 }
    120 
    121 Value BinaryOpNode::Execute(Scope* scope, Err* err) const {
    122   return ExecuteBinaryOperator(scope, this, left_.get(), right_.get(), err);
    123 }
    124 
    125 LocationRange BinaryOpNode::GetRange() const {
    126   return left_->GetRange().Union(right_->GetRange());
    127 }
    128 
    129 Err BinaryOpNode::MakeErrorDescribing(const std::string& msg,
    130                                       const std::string& help) const {
    131   return Err(op_, msg, help);
    132 }
    133 
    134 void BinaryOpNode::Print(std::ostream& out, int indent) const {
    135   out << IndentFor(indent) << "BINARY(" << op_.value() << ")\n";
    136   left_->Print(out, indent + 1);
    137   right_->Print(out, indent + 1);
    138 }
    139 
    140 // BlockNode ------------------------------------------------------------------
    141 
    142 BlockNode::BlockNode(bool has_scope) : has_scope_(has_scope) {
    143 }
    144 
    145 BlockNode::~BlockNode() {
    146   STLDeleteContainerPointers(statements_.begin(), statements_.end());
    147 }
    148 
    149 const BlockNode* BlockNode::AsBlock() const {
    150   return this;
    151 }
    152 
    153 Value BlockNode::Execute(Scope* containing_scope, Err* err) const {
    154   if (has_scope_) {
    155     Scope our_scope(containing_scope);
    156     Value ret = ExecuteBlockInScope(&our_scope, err);
    157     if (err->has_error())
    158       return Value();
    159 
    160     // Check for unused vars in the scope.
    161     //our_scope.CheckForUnusedVars(err);
    162     return ret;
    163   }
    164   return ExecuteBlockInScope(containing_scope, err);
    165 }
    166 
    167 LocationRange BlockNode::GetRange() const {
    168   if (begin_token_.type() != Token::INVALID &&
    169       end_token_.type() != Token::INVALID) {
    170     return begin_token_.range().Union(end_token_.range());
    171   } else if (!statements_.empty()) {
    172     return statements_[0]->GetRange().Union(
    173         statements_[statements_.size() - 1]->GetRange());
    174   }
    175   return LocationRange();
    176 }
    177 
    178 Err BlockNode::MakeErrorDescribing(const std::string& msg,
    179                                    const std::string& help) const {
    180   return Err(GetRange(), msg, help);
    181 }
    182 
    183 void BlockNode::Print(std::ostream& out, int indent) const {
    184   out << IndentFor(indent) << "BLOCK\n";
    185   for (size_t i = 0; i < statements_.size(); i++)
    186     statements_[i]->Print(out, indent + 1);
    187 }
    188 
    189 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const {
    190   for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) {
    191     // Check for trying to execute things with no side effects in a block.
    192     const ParseNode* cur = statements_[i];
    193     if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() ||
    194         cur->AsIdentifier()) {
    195       *err = cur->MakeErrorDescribing(
    196           "This statment has no effect.",
    197           "Either delete it or do something with the result.");
    198       return Value();
    199     }
    200     cur->Execute(our_scope, err);
    201   }
    202   return Value();
    203 }
    204 
    205 // ConditionNode --------------------------------------------------------------
    206 
    207 ConditionNode::ConditionNode() {
    208 }
    209 
    210 ConditionNode::~ConditionNode() {
    211 }
    212 
    213 const ConditionNode* ConditionNode::AsConditionNode() const {
    214   return this;
    215 }
    216 
    217 Value ConditionNode::Execute(Scope* scope, Err* err) const {
    218   Value condition_result = condition_->Execute(scope, err);
    219   if (err->has_error())
    220     return Value();
    221   if (condition_result.type() != Value::BOOLEAN) {
    222     *err = condition_->MakeErrorDescribing(
    223         "Condition does not evaluate to a boolean value.",
    224         std::string("This is a value of type \"") +
    225             Value::DescribeType(condition_result.type()) +
    226             "\" instead.");
    227     err->AppendRange(if_token_.range());
    228     return Value();
    229   }
    230 
    231   if (condition_result.boolean_value()) {
    232     if_true_->ExecuteBlockInScope(scope, err);
    233   } else if (if_false_) {
    234     // The else block is optional. It's either another condition (for an
    235     // "else if" and we can just Execute it and the condition will handle
    236     // the scoping) or it's a block indicating an "else" in which ase we
    237     // need to be sure it inherits our scope.
    238     const BlockNode* if_false_block = if_false_->AsBlock();
    239     if (if_false_block)
    240       if_false_block->ExecuteBlockInScope(scope, err);
    241     else
    242       if_false_->Execute(scope, err);
    243   }
    244 
    245   return Value();
    246 }
    247 
    248 LocationRange ConditionNode::GetRange() const {
    249   if (if_false_)
    250     return if_token_.range().Union(if_false_->GetRange());
    251   return if_token_.range().Union(if_true_->GetRange());
    252 }
    253 
    254 Err ConditionNode::MakeErrorDescribing(const std::string& msg,
    255                                        const std::string& help) const {
    256   return Err(if_token_, msg, help);
    257 }
    258 
    259 void ConditionNode::Print(std::ostream& out, int indent) const {
    260   out << IndentFor(indent) << "CONDITION\n";
    261   condition_->Print(out, indent + 1);
    262   if_true_->Print(out, indent + 1);
    263   if (if_false_)
    264     if_false_->Print(out, indent + 1);
    265 }
    266 
    267 // FunctionCallNode -----------------------------------------------------------
    268 
    269 FunctionCallNode::FunctionCallNode() {
    270 }
    271 
    272 FunctionCallNode::~FunctionCallNode() {
    273 }
    274 
    275 const FunctionCallNode* FunctionCallNode::AsFunctionCall() const {
    276   return this;
    277 }
    278 
    279 Value FunctionCallNode::Execute(Scope* scope, Err* err) const {
    280   return functions::RunFunction(scope, this, args_.get(), block_.get(), err);
    281 }
    282 
    283 LocationRange FunctionCallNode::GetRange() const {
    284   if (block_)
    285     return function_.range().Union(block_->GetRange());
    286   return function_.range().Union(args_->GetRange());
    287 }
    288 
    289 Err FunctionCallNode::MakeErrorDescribing(const std::string& msg,
    290                                           const std::string& help) const {
    291   return Err(function_, msg, help);
    292 }
    293 
    294 void FunctionCallNode::Print(std::ostream& out, int indent) const {
    295   out << IndentFor(indent) << "FUNCTION(" << function_.value() << ")\n";
    296   args_->Print(out, indent + 1);
    297   if (block_)
    298     block_->Print(out, indent + 1);
    299 }
    300 
    301 // IdentifierNode --------------------------------------------------------------
    302 
    303 IdentifierNode::IdentifierNode() {
    304 }
    305 
    306 IdentifierNode::IdentifierNode(const Token& token) : value_(token) {
    307 }
    308 
    309 IdentifierNode::~IdentifierNode() {
    310 }
    311 
    312 const IdentifierNode* IdentifierNode::AsIdentifier() const {
    313   return this;
    314 }
    315 
    316 Value IdentifierNode::Execute(Scope* scope, Err* err) const {
    317   const Value* result = scope->GetValue(value_.value(), true);
    318   if (!result) {
    319     *err = MakeErrorDescribing("Undefined identifier");
    320     return Value();
    321   }
    322   return *result;
    323 }
    324 
    325 LocationRange IdentifierNode::GetRange() const {
    326   return value_.range();
    327 }
    328 
    329 Err IdentifierNode::MakeErrorDescribing(const std::string& msg,
    330                                         const std::string& help) const {
    331   return Err(value_, msg, help);
    332 }
    333 
    334 void IdentifierNode::Print(std::ostream& out, int indent) const {
    335   out << IndentFor(indent) << "IDENTIFIER(" << value_.value() << ")\n";
    336 }
    337 
    338 // ListNode -------------------------------------------------------------------
    339 
    340 ListNode::ListNode() {
    341 }
    342 
    343 ListNode::~ListNode() {
    344   STLDeleteContainerPointers(contents_.begin(), contents_.end());
    345 }
    346 
    347 const ListNode* ListNode::AsList() const {
    348   return this;
    349 }
    350 
    351 Value ListNode::Execute(Scope* scope, Err* err) const {
    352   Value result_value(this, Value::LIST);
    353   std::vector<Value>& results = result_value.list_value();
    354   results.resize(contents_.size());
    355 
    356   for (size_t i = 0; i < contents_.size(); i++) {
    357     const ParseNode* cur = contents_[i];
    358     results[i] = cur->Execute(scope, err);
    359     if (err->has_error())
    360       return Value();
    361     if (results[i].type() == Value::NONE) {
    362       *err = cur->MakeErrorDescribing(
    363           "This does not evaluate to a value.",
    364           "I can't do something with nothing.");
    365       return Value();
    366     }
    367   }
    368   return result_value;
    369 }
    370 
    371 LocationRange ListNode::GetRange() const {
    372   return LocationRange(begin_token_.location(), end_token_.location());
    373 }
    374 
    375 Err ListNode::MakeErrorDescribing(const std::string& msg,
    376                                   const std::string& help) const {
    377   return Err(begin_token_, msg, help);
    378 }
    379 
    380 void ListNode::Print(std::ostream& out, int indent) const {
    381   out << IndentFor(indent) << "LIST\n";
    382   for (size_t i = 0; i < contents_.size(); i++)
    383     contents_[i]->Print(out, indent + 1);
    384 }
    385 
    386 // LiteralNode -----------------------------------------------------------------
    387 
    388 LiteralNode::LiteralNode() {
    389 }
    390 
    391 LiteralNode::LiteralNode(const Token& token) : value_(token) {
    392 }
    393 
    394 LiteralNode::~LiteralNode() {
    395 }
    396 
    397 const LiteralNode* LiteralNode::AsLiteral() const {
    398   return this;
    399 }
    400 
    401 Value LiteralNode::Execute(Scope* scope, Err* err) const {
    402   switch (value_.type()) {
    403     case Token::TRUE_TOKEN:
    404       return Value(this, true);
    405     case Token::FALSE_TOKEN:
    406       return Value(this, false);
    407     case Token::INTEGER: {
    408       int64 result_int;
    409       if (!base::StringToInt64(value_.value(), &result_int)) {
    410         *err = MakeErrorDescribing("This does not look like an integer");
    411         return Value();
    412       }
    413       return Value(this, result_int);
    414     }
    415     case Token::STRING: {
    416       Value v(this, Value::STRING);
    417       ExpandStringLiteral(scope, value_, &v, err);
    418       return v;
    419     }
    420     default:
    421       NOTREACHED();
    422       return Value();
    423   }
    424 }
    425 
    426 LocationRange LiteralNode::GetRange() const {
    427   return value_.range();
    428 }
    429 
    430 Err LiteralNode::MakeErrorDescribing(const std::string& msg,
    431                                      const std::string& help) const {
    432   return Err(value_, msg, help);
    433 }
    434 
    435 void LiteralNode::Print(std::ostream& out, int indent) const {
    436   out << IndentFor(indent) << "LITERAL(" << value_.value() << ")\n";
    437 }
    438 
    439 // UnaryOpNode ----------------------------------------------------------------
    440 
    441 UnaryOpNode::UnaryOpNode() {
    442 }
    443 
    444 UnaryOpNode::~UnaryOpNode() {
    445 }
    446 
    447 const UnaryOpNode* UnaryOpNode::AsUnaryOp() const {
    448   return this;
    449 }
    450 
    451 Value UnaryOpNode::Execute(Scope* scope, Err* err) const {
    452   Value operand_value = operand_->Execute(scope, err);
    453   if (err->has_error())
    454     return Value();
    455   return ExecuteUnaryOperator(scope, this, operand_value, err);
    456 }
    457 
    458 LocationRange UnaryOpNode::GetRange() const {
    459   return op_.range().Union(operand_->GetRange());
    460 }
    461 
    462 Err UnaryOpNode::MakeErrorDescribing(const std::string& msg,
    463                                      const std::string& help) const {
    464   return Err(op_, msg, help);
    465 }
    466 
    467 void UnaryOpNode::Print(std::ostream& out, int indent) const {
    468   out << IndentFor(indent) << "UNARY(" << op_.value() << ")\n";
    469   operand_->Print(out, indent + 1);
    470 }
    471