OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/value.h" | 5 #include "tools/gn/value.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 | 8 |
9 Value::Value() | 9 Value::Value() |
10 : type_(NONE), | 10 : type_(NONE), |
11 boolean_value_(false), | 11 boolean_value_(false), |
12 int_value_(0), | 12 int_value_(0), |
| 13 scope_value_(NULL), |
13 origin_(NULL) { | 14 origin_(NULL) { |
14 } | 15 } |
15 | 16 |
16 Value::Value(const ParseNode* origin, Type t) | 17 Value::Value(const ParseNode* origin, Type t) |
17 : type_(t), | 18 : type_(t), |
18 boolean_value_(false), | 19 boolean_value_(false), |
19 int_value_(0), | 20 int_value_(0), |
| 21 scope_value_(NULL), |
20 origin_(origin) { | 22 origin_(origin) { |
21 } | 23 } |
22 | 24 |
23 Value::Value(const ParseNode* origin, bool bool_val) | 25 Value::Value(const ParseNode* origin, bool bool_val) |
24 : type_(BOOLEAN), | 26 : type_(BOOLEAN), |
25 boolean_value_(bool_val), | 27 boolean_value_(bool_val), |
26 int_value_(0), | 28 int_value_(0), |
| 29 scope_value_(NULL), |
27 origin_(origin) { | 30 origin_(origin) { |
28 } | 31 } |
29 | 32 |
30 Value::Value(const ParseNode* origin, int64 int_val) | 33 Value::Value(const ParseNode* origin, int64 int_val) |
31 : type_(INTEGER), | 34 : type_(INTEGER), |
32 boolean_value_(false), | 35 boolean_value_(false), |
33 int_value_(int_val), | 36 int_value_(int_val), |
| 37 scope_value_(NULL), |
34 origin_(origin) { | 38 origin_(origin) { |
35 } | 39 } |
36 | 40 |
37 Value::Value(const ParseNode* origin, std::string str_val) | 41 Value::Value(const ParseNode* origin, std::string str_val) |
38 : type_(STRING), | 42 : type_(STRING), |
39 string_value_(), | 43 string_value_(), |
40 boolean_value_(false), | 44 boolean_value_(false), |
41 int_value_(0), | 45 int_value_(0), |
| 46 scope_value_(NULL), |
42 origin_(origin) { | 47 origin_(origin) { |
43 string_value_.swap(str_val); | 48 string_value_.swap(str_val); |
44 } | 49 } |
45 | 50 |
46 Value::Value(const ParseNode* origin, const char* str_val) | 51 Value::Value(const ParseNode* origin, const char* str_val) |
47 : type_(STRING), | 52 : type_(STRING), |
48 string_value_(str_val), | 53 string_value_(str_val), |
49 boolean_value_(false), | 54 boolean_value_(false), |
50 int_value_(0), | 55 int_value_(0), |
| 56 scope_value_(NULL), |
51 origin_(origin) { | 57 origin_(origin) { |
52 } | 58 } |
53 | 59 |
| 60 Value::Value(const ParseNode* origin, Scope* scope) |
| 61 : type_(SCOPE), |
| 62 string_value_(), |
| 63 boolean_value_(false), |
| 64 int_value_(0), |
| 65 scope_value_(scope), |
| 66 origin_(origin) { |
| 67 } |
| 68 |
54 Value::~Value() { | 69 Value::~Value() { |
55 } | 70 } |
56 | 71 |
57 void Value::RecursivelySetOrigin(const ParseNode* origin) { | 72 void Value::RecursivelySetOrigin(const ParseNode* origin) { |
58 set_origin(origin); | 73 set_origin(origin); |
59 if (type_ == Value::LIST) { | 74 if (type_ == Value::LIST) { |
60 for (size_t i = 0; i < list_value_.size(); i++) | 75 for (size_t i = 0; i < list_value_.size(); i++) |
61 list_value_[i].RecursivelySetOrigin(origin); | 76 list_value_[i].RecursivelySetOrigin(origin); |
62 } | 77 } |
63 } | 78 } |
64 | 79 |
65 // static | 80 // static |
66 const char* Value::DescribeType(Type t) { | 81 const char* Value::DescribeType(Type t) { |
67 switch (t) { | 82 switch (t) { |
68 case NONE: | 83 case NONE: |
69 return "none"; | 84 return "none"; |
70 case BOOLEAN: | 85 case BOOLEAN: |
71 return "boolean"; | 86 return "boolean"; |
72 case INTEGER: | 87 case INTEGER: |
73 return "integer"; | 88 return "integer"; |
74 case STRING: | 89 case STRING: |
75 return "string"; | 90 return "string"; |
76 case LIST: | 91 case LIST: |
77 return "list"; | 92 return "list"; |
| 93 case SCOPE: |
| 94 return "scope"; |
78 default: | 95 default: |
79 NOTREACHED(); | 96 NOTREACHED(); |
80 return "UNKNOWN"; | 97 return "UNKNOWN"; |
81 } | 98 } |
82 } | 99 } |
83 | 100 |
84 std::string Value::ToString(bool quote_string) const { | 101 std::string Value::ToString(bool quote_string) const { |
85 switch (type_) { | 102 switch (type_) { |
86 case NONE: | 103 case NONE: |
87 return "<void>"; | 104 return "<void>"; |
88 case BOOLEAN: | 105 case BOOLEAN: |
89 return boolean_value_ ? "true" : "false"; | 106 return boolean_value_ ? "true" : "false"; |
90 case INTEGER: | 107 case INTEGER: |
91 return base::Int64ToString(int_value_); | 108 return base::Int64ToString(int_value_); |
92 case STRING: | 109 case STRING: |
93 if (quote_string) | 110 if (quote_string) |
94 return "\"" + string_value_ + "\""; | 111 return "\"" + string_value_ + "\""; |
95 return string_value_; | 112 return string_value_; |
96 case LIST: { | 113 case LIST: { |
97 std::string result = "["; | 114 std::string result = "["; |
98 for (size_t i = 0; i < list_value_.size(); i++) { | 115 for (size_t i = 0; i < list_value_.size(); i++) { |
99 if (i > 0) | 116 if (i > 0) |
100 result += ", "; | 117 result += ", "; |
101 result += list_value_[i].ToString(true); | 118 result += list_value_[i].ToString(true); |
102 } | 119 } |
103 result.push_back(']'); | 120 result.push_back(']'); |
104 return result; | 121 return result; |
105 } | 122 } |
| 123 case SCOPE: |
| 124 return std::string("<scope>"); |
106 } | 125 } |
107 return std::string(); | 126 return std::string(); |
108 } | 127 } |
109 | 128 |
110 bool Value::VerifyTypeIs(Type t, Err* err) const { | 129 bool Value::VerifyTypeIs(Type t, Err* err) const { |
111 if (type_ == t) | 130 if (type_ == t) |
112 return true; | 131 return true; |
113 | 132 |
114 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + "."); | 133 *err = Err(origin(), std::string("This is not a ") + DescribeType(t) + "."); |
115 return false; | 134 return false; |
(...skipping 11 matching lines...) Expand all Loading... |
127 case Value::STRING: | 146 case Value::STRING: |
128 return string_value() == other.string_value(); | 147 return string_value() == other.string_value(); |
129 case Value::LIST: | 148 case Value::LIST: |
130 if (list_value().size() != other.list_value().size()) | 149 if (list_value().size() != other.list_value().size()) |
131 return false; | 150 return false; |
132 for (size_t i = 0; i < list_value().size(); i++) { | 151 for (size_t i = 0; i < list_value().size(); i++) { |
133 if (list_value()[i] != other.list_value()[i]) | 152 if (list_value()[i] != other.list_value()[i]) |
134 return false; | 153 return false; |
135 } | 154 } |
136 return true; | 155 return true; |
| 156 case Value::SCOPE: |
| 157 // Its not clear what people mean when comparing scope values, so we test |
| 158 // for scope identity and not contents equality. |
| 159 return scope_value() == other.scope_value(); |
137 default: | 160 default: |
138 return false; | 161 return false; |
139 } | 162 } |
140 } | 163 } |
141 | 164 |
142 bool Value::operator!=(const Value& other) const { | 165 bool Value::operator!=(const Value& other) const { |
143 return !operator==(other); | 166 return !operator==(other); |
144 } | 167 } |
OLD | NEW |