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)
    143     : has_scope_(has_scope),
    144       begin_token_(NULL),
    145       end_token_(NULL) {
    146 }
    147 
    148 BlockNode::~BlockNode() {
    149   STLDeleteContainerPointers(statements_.begin(), statements_.end());
    150 }
    151 
    152 const BlockNode* BlockNode::AsBlock() const {
    153   return this;
    154 }
    155 
    156 Value BlockNode::Execute(Scope* containing_scope, Err* err) const {
    157   if (has_scope_) {
    158     Scope our_scope(containing_scope);
    159     Value ret = ExecuteBlockInScope(&our_scope, err);
    160     if (err->has_error())
    161       return Value();
    162 
    163     // Check for unused vars in the scope.
    164     //our_scope.CheckForUnusedVars(err);
    165     return ret;
    166   }
    167   return ExecuteBlockInScope(containing_scope, err);
    168 }
    169 
    170 LocationRange BlockNode::GetRange() const {
    171   if (begin_token_ && end_token_) {
    172     return begin_token_->range().Union(end_token_->range());
    173   }
    174   return LocationRange();  // TODO(brettw) indicate the entire file somehow.
    175 }
    176 
    177 Err BlockNode::MakeErrorDescribing(const std::string& msg,
    178                                    const std::string& help) const {
    179   if (begin_token_)
    180     return Err(*begin_token_, msg, help);
    181   // TODO(brettw) this should have the beginning of the file in it or something.
    182   return Err(Location(NULL, 1, 1), msg, help);
    183 }
    184 
    185 void BlockNode::Print(std::ostream& out, int indent) const {
    186   out << IndentFor(indent) << "BLOCK\n";
    187   for (size_t i = 0; i < statements_.size(); i++)
    188     statements_[i]->Print(out, indent + 1);
    189 }
    190 
    191 Value BlockNode::ExecuteBlockInScope(Scope* our_scope, Err* err) const {
    192   for (size_t i = 0; i < statements_.size() && !err->has_error(); i++) {
    193     // Check for trying to execute things with no side effects in a block.
    194     const ParseNode* cur = statements_[i];
    195     if (cur->AsList() || cur->AsLiteral() || cur->AsUnaryOp() ||
    196         cur->AsIdentifier()) {
    197       *err = cur->MakeErrorDescribing(
    198           "This statment has no effect.",
    199           "Either delete it or do something with the result.");
    200       return Value();
    201     }
    202     cur->Execute(our_scope, err);
    203   }
    204   return Value();
    205 }
    206 
    207 // ConditionNode --------------------------------------------------------------
    208 
    209 ConditionNode::ConditionNode() {
    210 }
    211 
    212 ConditionNode::~ConditionNode() {
    213 }
    214 
    215 const ConditionNode* ConditionNode::AsConditionNode() const {
    216   return this;
    217 }
    218 
    219 Value ConditionNode::Execute(Scope* scope, Err* err) const {
    220   Value condition_result = condition_->Execute(scope, err);
    221   if (err->has_error())
    222     return Value();
    223   if (condition_result.type() == Value::NONE) {
    224     *err = condition_->MakeErrorDescribing(
    225         "This does not evaluate to a value.",
    226         "Please give me something to work with for the if statement.");
    227     err->AppendRange(if_token_.range());
    228     return Value();
    229   }
    230 
    231   if (condition_result.InterpretAsInt()) {
    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   Value args = args_->Execute(scope, err);
    281   if (err->has_error())
    282     return Value();
    283   return functions::RunFunction(scope, this, args.list_value(), block_.get(),
    284                                 err);
    285 }
    286 
    287 LocationRange FunctionCallNode::GetRange() const {
    288   if (block_)
    289     return function_.range().Union(block_->GetRange());
    290   return function_.range().Union(args_->GetRange());
    291 }
    292 
    293 Err FunctionCallNode::MakeErrorDescribing(const std::string& msg,
    294                                           const std::string& help) const {
    295   return Err(function_, msg, help);
    296 }
    297 
    298 void FunctionCallNode::Print(std::ostream& out, int indent) const {
    299   out << IndentFor(indent) << "FUNCTION(" << function_.value() << ")\n";
    300   args_->Print(out, indent + 1);
    301   if (block_)
    302     block_->Print(out, indent + 1);
    303 }
    304 
    305 // IdentifierNode --------------------------------------------------------------
    306 
    307 IdentifierNode::IdentifierNode() {
    308 }
    309 
    310 IdentifierNode::IdentifierNode(const Token& token) : value_(token) {
    311 }
    312 
    313 IdentifierNode::~IdentifierNode() {
    314 }
    315 
    316 const IdentifierNode* IdentifierNode::AsIdentifier() const {
    317   return this;
    318 }
    319 
    320 Value IdentifierNode::Execute(Scope* scope, Err* err) const {
    321   const Value* result = scope->GetValue(value_.value(), true);
    322   if (!result) {
    323     *err = MakeErrorDescribing("Undefined identifier");
    324     return Value();
    325   }
    326   return *result;
    327 }
    328 
    329 LocationRange IdentifierNode::GetRange() const {
    330   return value_.range();
    331 }
    332 
    333 Err IdentifierNode::MakeErrorDescribing(const std::string& msg,
    334                                         const std::string& help) const {
    335   return Err(value_, msg, help);
    336 }
    337 
    338 void IdentifierNode::Print(std::ostream& out, int indent) const {
    339   out << IndentFor(indent) << "IDENTIFIER(" << value_.value() << ")\n";
    340 }
    341 
    342 // ListNode -------------------------------------------------------------------
    343 
    344 ListNode::ListNode() {
    345 }
    346 
    347 ListNode::~ListNode() {
    348   STLDeleteContainerPointers(contents_.begin(), contents_.end());
    349 }
    350 
    351 const ListNode* ListNode::AsList() const {
    352   return this;
    353 }
    354 
    355 Value ListNode::Execute(Scope* scope, Err* err) const {
    356   Value result_value(this, Value::LIST);
    357   std::vector<Value>& results = result_value.list_value();
    358   results.resize(contents_.size());
    359 
    360   for (size_t i = 0; i < contents_.size(); i++) {
    361     const ParseNode* cur = contents_[i];
    362     results[i] = cur->Execute(scope, err);
    363     if (err->has_error())
    364       return Value();
    365     if (results[i].type() == Value::NONE) {
    366       *err = cur->MakeErrorDescribing(
    367           "This does not evaluate to a value.",
    368           "I can't do something with nothing.");
    369       return Value();
    370     }
    371   }
    372   return result_value;
    373 }
    374 
    375 LocationRange ListNode::GetRange() const {
    376   return LocationRange(begin_token_.location(), end_token_.location());
    377 }
    378 
    379 Err ListNode::MakeErrorDescribing(const std::string& msg,
    380                                   const std::string& help) const {
    381   return Err(begin_token_, msg, help);
    382 }
    383 
    384 void ListNode::Print(std::ostream& out, int indent) const {
    385   out << IndentFor(indent) << "LIST\n";
    386   for (size_t i = 0; i < contents_.size(); i++)
    387     contents_[i]->Print(out, indent + 1);
    388 }
    389 
    390 // LiteralNode -----------------------------------------------------------------
    391 
    392 LiteralNode::LiteralNode() {
    393 }
    394 
    395 LiteralNode::LiteralNode(const Token& token) : value_(token) {
    396 }
    397 
    398 LiteralNode::~LiteralNode() {
    399 }
    400 
    401 const LiteralNode* LiteralNode::AsLiteral() const {
    402   return this;
    403 }
    404 
    405 Value LiteralNode::Execute(Scope* scope, Err* err) const {
    406   switch (value_.type()) {
    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       // TODO(brettw) Unescaping probably needs to be moved & improved.
    417       // The input value includes the quotes around the string, strip those
    418       // off and unescape.
    419       Value v(this, Value::STRING);
    420       ExpandStringLiteral(scope, value_, &v, err);
    421       return v;
    422     }
    423     default:
    424       NOTREACHED();
    425       return Value();
    426   }
    427 }
    428 
    429 LocationRange LiteralNode::GetRange() const {
    430   return value_.range();
    431 }
    432 
    433 Err LiteralNode::MakeErrorDescribing(const std::string& msg,
    434                                      const std::string& help) const {
    435   return Err(value_, msg, help);
    436 }
    437 
    438 void LiteralNode::Print(std::ostream& out, int indent) const {
    439   out << IndentFor(indent) << "LITERAL(" << value_.value() << ")\n";
    440 }
    441 
    442 // UnaryOpNode ----------------------------------------------------------------
    443 
    444 UnaryOpNode::UnaryOpNode() {
    445 }
    446 
    447 UnaryOpNode::~UnaryOpNode() {
    448 }
    449 
    450 const UnaryOpNode* UnaryOpNode::AsUnaryOp() const {
    451   return this;
    452 }
    453 
    454 Value UnaryOpNode::Execute(Scope* scope, Err* err) const {
    455   Value operand_value = operand_->Execute(scope, err);
    456   if (err->has_error())
    457     return Value();
    458   return ExecuteUnaryOperator(scope, this, operand_value, err);
    459 }
    460 
    461 LocationRange UnaryOpNode::GetRange() const {
    462   return op_.range().Union(operand_->GetRange());
    463 }
    464 
    465 Err UnaryOpNode::MakeErrorDescribing(const std::string& msg,
    466                                      const std::string& help) const {
    467   return Err(op_, msg, help);
    468 }
    469 
    470 void UnaryOpNode::Print(std::ostream& out, int indent) const {
    471   out << IndentFor(indent) << "UNARY(" << op_.value() << ")\n";
    472   operand_->Print(out, indent + 1);
    473 }
    474