1 // Copyright 2016 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 "gn/parse_node_value_adapter.h"
6
7 #include "gn/parse_tree.h"
8 #include "gn/scope.h"
9
ParseNodeValueAdapter()10 ParseNodeValueAdapter::ParseNodeValueAdapter() : ref_(nullptr) {}
11
12 ParseNodeValueAdapter::~ParseNodeValueAdapter() = default;
13
Init(Scope * scope,const ParseNode * node,Err * err)14 bool ParseNodeValueAdapter::Init(Scope* scope,
15 const ParseNode* node,
16 Err* err) {
17 const IdentifierNode* identifier = node->AsIdentifier();
18 if (identifier) {
19 ref_ = scope->GetValue(identifier->value().value(), true);
20 if (!ref_) {
21 identifier->MakeErrorDescribing("Undefined identifier");
22 return false;
23 }
24 return true;
25 }
26
27 temporary_ = node->Execute(scope, err);
28 return !err->has_error();
29 }
30
InitForType(Scope * scope,const ParseNode * node,Value::Type type,Err * err)31 bool ParseNodeValueAdapter::InitForType(Scope* scope,
32 const ParseNode* node,
33 Value::Type type,
34 Err* err) {
35 if (!Init(scope, node, err))
36 return false;
37 if (get().VerifyTypeIs(type, err))
38 return true;
39
40 // Fix up the error range (see class comment in the header file) to be the
41 // identifier node rather than the original value.
42 *err = Err(node, err->message(), err->help_text());
43 return false;
44 }
45