OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef TOOLS_GN_PARSE_TREE_H_ | 5 #ifndef TOOLS_GN_PARSE_TREE_H_ |
6 #define TOOLS_GN_PARSE_TREE_H_ | 6 #define TOOLS_GN_PARSE_TREE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 |
| 10 #include <memory> |
9 #include <utility> | 11 #include <utility> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "tools/gn/err.h" | 15 #include "tools/gn/err.h" |
15 #include "tools/gn/token.h" | 16 #include "tools/gn/token.h" |
16 #include "tools/gn/value.h" | 17 #include "tools/gn/value.h" |
17 | 18 |
18 class AccessorNode; | 19 class AccessorNode; |
19 class BinaryOpNode; | 20 class BinaryOpNode; |
20 class BlockCommentNode; | 21 class BlockCommentNode; |
21 class BlockNode; | 22 class BlockNode; |
22 class ConditionNode; | 23 class ConditionNode; |
23 class EndNode; | 24 class EndNode; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 94 |
94 // Prints a representation of this node to the given string, indenting | 95 // Prints a representation of this node to the given string, indenting |
95 // by the given number of spaces. | 96 // by the given number of spaces. |
96 virtual void Print(std::ostream& out, int indent) const = 0; | 97 virtual void Print(std::ostream& out, int indent) const = 0; |
97 | 98 |
98 const Comments* comments() const { return comments_.get(); } | 99 const Comments* comments() const { return comments_.get(); } |
99 Comments* comments_mutable(); | 100 Comments* comments_mutable(); |
100 void PrintComments(std::ostream& out, int indent) const; | 101 void PrintComments(std::ostream& out, int indent) const; |
101 | 102 |
102 private: | 103 private: |
103 scoped_ptr<Comments> comments_; | 104 std::unique_ptr<Comments> comments_; |
104 | 105 |
105 DISALLOW_COPY_AND_ASSIGN(ParseNode); | 106 DISALLOW_COPY_AND_ASSIGN(ParseNode); |
106 }; | 107 }; |
107 | 108 |
108 // AccessorNode ---------------------------------------------------------------- | 109 // AccessorNode ---------------------------------------------------------------- |
109 | 110 |
110 // Access an array or scope element. | 111 // Access an array or scope element. |
111 // | 112 // |
112 // Currently, such values are only read-only. In that you can do: | 113 // Currently, such values are only read-only. In that you can do: |
113 // a = obj1.a | 114 // a = obj1.a |
(...skipping 29 matching lines...) Expand all Loading... |
143 const std::string& help = std::string()) const override; | 144 const std::string& help = std::string()) const override; |
144 void Print(std::ostream& out, int indent) const override; | 145 void Print(std::ostream& out, int indent) const override; |
145 | 146 |
146 // Base is the thing on the left of the [] or dot, currently always required | 147 // Base is the thing on the left of the [] or dot, currently always required |
147 // to be an identifier token. | 148 // to be an identifier token. |
148 const Token& base() const { return base_; } | 149 const Token& base() const { return base_; } |
149 void set_base(const Token& b) { base_ = b; } | 150 void set_base(const Token& b) { base_ = b; } |
150 | 151 |
151 // Index is the expression inside the []. Will be null if member is set. | 152 // Index is the expression inside the []. Will be null if member is set. |
152 const ParseNode* index() const { return index_.get(); } | 153 const ParseNode* index() const { return index_.get(); } |
153 void set_index(scoped_ptr<ParseNode> i) { index_ = std::move(i); } | 154 void set_index(std::unique_ptr<ParseNode> i) { index_ = std::move(i); } |
154 | 155 |
155 // The member is the identifier on the right hand side of the dot. Will be | 156 // The member is the identifier on the right hand side of the dot. Will be |
156 // null if the index is set. | 157 // null if the index is set. |
157 const IdentifierNode* member() const { return member_.get(); } | 158 const IdentifierNode* member() const { return member_.get(); } |
158 void set_member(scoped_ptr<IdentifierNode> i) { member_ = std::move(i); } | 159 void set_member(std::unique_ptr<IdentifierNode> i) { member_ = std::move(i); } |
159 | 160 |
160 void SetNewLocation(int line_number); | 161 void SetNewLocation(int line_number); |
161 | 162 |
162 private: | 163 private: |
163 Value ExecuteArrayAccess(Scope* scope, Err* err) const; | 164 Value ExecuteArrayAccess(Scope* scope, Err* err) const; |
164 Value ExecuteScopeAccess(Scope* scope, Err* err) const; | 165 Value ExecuteScopeAccess(Scope* scope, Err* err) const; |
165 | 166 |
166 Token base_; | 167 Token base_; |
167 | 168 |
168 // Either index or member will be set according to what type of access this | 169 // Either index or member will be set according to what type of access this |
169 // is. | 170 // is. |
170 scoped_ptr<ParseNode> index_; | 171 std::unique_ptr<ParseNode> index_; |
171 scoped_ptr<IdentifierNode> member_; | 172 std::unique_ptr<IdentifierNode> member_; |
172 | 173 |
173 DISALLOW_COPY_AND_ASSIGN(AccessorNode); | 174 DISALLOW_COPY_AND_ASSIGN(AccessorNode); |
174 }; | 175 }; |
175 | 176 |
176 // BinaryOpNode ---------------------------------------------------------------- | 177 // BinaryOpNode ---------------------------------------------------------------- |
177 | 178 |
178 class BinaryOpNode : public ParseNode { | 179 class BinaryOpNode : public ParseNode { |
179 public: | 180 public: |
180 BinaryOpNode(); | 181 BinaryOpNode(); |
181 ~BinaryOpNode() override; | 182 ~BinaryOpNode() override; |
182 | 183 |
183 const BinaryOpNode* AsBinaryOp() const override; | 184 const BinaryOpNode* AsBinaryOp() const override; |
184 Value Execute(Scope* scope, Err* err) const override; | 185 Value Execute(Scope* scope, Err* err) const override; |
185 LocationRange GetRange() const override; | 186 LocationRange GetRange() const override; |
186 Err MakeErrorDescribing( | 187 Err MakeErrorDescribing( |
187 const std::string& msg, | 188 const std::string& msg, |
188 const std::string& help = std::string()) const override; | 189 const std::string& help = std::string()) const override; |
189 void Print(std::ostream& out, int indent) const override; | 190 void Print(std::ostream& out, int indent) const override; |
190 | 191 |
191 const Token& op() const { return op_; } | 192 const Token& op() const { return op_; } |
192 void set_op(const Token& t) { op_ = t; } | 193 void set_op(const Token& t) { op_ = t; } |
193 | 194 |
194 const ParseNode* left() const { return left_.get(); } | 195 const ParseNode* left() const { return left_.get(); } |
195 void set_left(scoped_ptr<ParseNode> left) { left_ = std::move(left); } | 196 void set_left(std::unique_ptr<ParseNode> left) { left_ = std::move(left); } |
196 | 197 |
197 const ParseNode* right() const { return right_.get(); } | 198 const ParseNode* right() const { return right_.get(); } |
198 void set_right(scoped_ptr<ParseNode> right) { right_ = std::move(right); } | 199 void set_right(std::unique_ptr<ParseNode> right) { |
| 200 right_ = std::move(right); |
| 201 } |
199 | 202 |
200 private: | 203 private: |
201 scoped_ptr<ParseNode> left_; | 204 std::unique_ptr<ParseNode> left_; |
202 Token op_; | 205 Token op_; |
203 scoped_ptr<ParseNode> right_; | 206 std::unique_ptr<ParseNode> right_; |
204 | 207 |
205 DISALLOW_COPY_AND_ASSIGN(BinaryOpNode); | 208 DISALLOW_COPY_AND_ASSIGN(BinaryOpNode); |
206 }; | 209 }; |
207 | 210 |
208 // BlockNode ------------------------------------------------------------------- | 211 // BlockNode ------------------------------------------------------------------- |
209 | 212 |
210 class BlockNode : public ParseNode { | 213 class BlockNode : public ParseNode { |
211 public: | 214 public: |
212 BlockNode(); | 215 BlockNode(); |
213 ~BlockNode() override; | 216 ~BlockNode() override; |
214 | 217 |
215 const BlockNode* AsBlock() const override; | 218 const BlockNode* AsBlock() const override; |
216 Value Execute(Scope* scope, Err* err) const override; | 219 Value Execute(Scope* scope, Err* err) const override; |
217 LocationRange GetRange() const override; | 220 LocationRange GetRange() const override; |
218 Err MakeErrorDescribing( | 221 Err MakeErrorDescribing( |
219 const std::string& msg, | 222 const std::string& msg, |
220 const std::string& help = std::string()) const override; | 223 const std::string& help = std::string()) const override; |
221 void Print(std::ostream& out, int indent) const override; | 224 void Print(std::ostream& out, int indent) const override; |
222 | 225 |
223 void set_begin_token(const Token& t) { begin_token_ = t; } | 226 void set_begin_token(const Token& t) { begin_token_ = t; } |
224 void set_end(scoped_ptr<EndNode> e) { end_ = std::move(e); } | 227 void set_end(std::unique_ptr<EndNode> e) { end_ = std::move(e); } |
225 const EndNode* End() const { return end_.get(); } | 228 const EndNode* End() const { return end_.get(); } |
226 | 229 |
227 const std::vector<ParseNode*>& statements() const { return statements_; } | 230 const std::vector<ParseNode*>& statements() const { return statements_; } |
228 void append_statement(scoped_ptr<ParseNode> s) { | 231 void append_statement(std::unique_ptr<ParseNode> s) { |
229 statements_.push_back(s.release()); | 232 statements_.push_back(s.release()); |
230 } | 233 } |
231 | 234 |
232 private: | 235 private: |
233 // Tokens corresponding to { and }, if any (may be NULL). The end is stored | 236 // Tokens corresponding to { and }, if any (may be NULL). The end is stored |
234 // in a custom parse node so that it can have comments hung off of it. | 237 // in a custom parse node so that it can have comments hung off of it. |
235 Token begin_token_; | 238 Token begin_token_; |
236 scoped_ptr<EndNode> end_; | 239 std::unique_ptr<EndNode> end_; |
237 | 240 |
238 // Owning pointers, use unique_ptr when we can use C++11. | 241 // Owning pointers, use unique_ptr when we can use C++11. |
239 std::vector<ParseNode*> statements_; | 242 std::vector<ParseNode*> statements_; |
240 | 243 |
241 DISALLOW_COPY_AND_ASSIGN(BlockNode); | 244 DISALLOW_COPY_AND_ASSIGN(BlockNode); |
242 }; | 245 }; |
243 | 246 |
244 // ConditionNode --------------------------------------------------------------- | 247 // ConditionNode --------------------------------------------------------------- |
245 | 248 |
246 class ConditionNode : public ParseNode { | 249 class ConditionNode : public ParseNode { |
247 public: | 250 public: |
248 ConditionNode(); | 251 ConditionNode(); |
249 ~ConditionNode() override; | 252 ~ConditionNode() override; |
250 | 253 |
251 const ConditionNode* AsConditionNode() const override; | 254 const ConditionNode* AsConditionNode() const override; |
252 Value Execute(Scope* scope, Err* err) const override; | 255 Value Execute(Scope* scope, Err* err) const override; |
253 LocationRange GetRange() const override; | 256 LocationRange GetRange() const override; |
254 Err MakeErrorDescribing( | 257 Err MakeErrorDescribing( |
255 const std::string& msg, | 258 const std::string& msg, |
256 const std::string& help = std::string()) const override; | 259 const std::string& help = std::string()) const override; |
257 void Print(std::ostream& out, int indent) const override; | 260 void Print(std::ostream& out, int indent) const override; |
258 | 261 |
259 void set_if_token(const Token& token) { if_token_ = token; } | 262 void set_if_token(const Token& token) { if_token_ = token; } |
260 | 263 |
261 const ParseNode* condition() const { return condition_.get(); } | 264 const ParseNode* condition() const { return condition_.get(); } |
262 void set_condition(scoped_ptr<ParseNode> c) { condition_ = std::move(c); } | 265 void set_condition(std::unique_ptr<ParseNode> c) { |
| 266 condition_ = std::move(c); |
| 267 } |
263 | 268 |
264 const BlockNode* if_true() const { return if_true_.get(); } | 269 const BlockNode* if_true() const { return if_true_.get(); } |
265 void set_if_true(scoped_ptr<BlockNode> t) { if_true_ = std::move(t); } | 270 void set_if_true(std::unique_ptr<BlockNode> t) { if_true_ = std::move(t); } |
266 | 271 |
267 // This is either empty, a block (for the else clause), or another | 272 // This is either empty, a block (for the else clause), or another |
268 // condition. | 273 // condition. |
269 const ParseNode* if_false() const { return if_false_.get(); } | 274 const ParseNode* if_false() const { return if_false_.get(); } |
270 void set_if_false(scoped_ptr<ParseNode> f) { if_false_ = std::move(f); } | 275 void set_if_false(std::unique_ptr<ParseNode> f) { if_false_ = std::move(f); } |
271 | 276 |
272 private: | 277 private: |
273 // Token corresponding to the "if" string. | 278 // Token corresponding to the "if" string. |
274 Token if_token_; | 279 Token if_token_; |
275 | 280 |
276 scoped_ptr<ParseNode> condition_; // Always non-null. | 281 std::unique_ptr<ParseNode> condition_; // Always non-null. |
277 scoped_ptr<BlockNode> if_true_; // Always non-null. | 282 std::unique_ptr<BlockNode> if_true_; // Always non-null. |
278 scoped_ptr<ParseNode> if_false_; // May be null. | 283 std::unique_ptr<ParseNode> if_false_; // May be null. |
279 | 284 |
280 DISALLOW_COPY_AND_ASSIGN(ConditionNode); | 285 DISALLOW_COPY_AND_ASSIGN(ConditionNode); |
281 }; | 286 }; |
282 | 287 |
283 // FunctionCallNode ------------------------------------------------------------ | 288 // FunctionCallNode ------------------------------------------------------------ |
284 | 289 |
285 class FunctionCallNode : public ParseNode { | 290 class FunctionCallNode : public ParseNode { |
286 public: | 291 public: |
287 FunctionCallNode(); | 292 FunctionCallNode(); |
288 ~FunctionCallNode() override; | 293 ~FunctionCallNode() override; |
289 | 294 |
290 const FunctionCallNode* AsFunctionCall() const override; | 295 const FunctionCallNode* AsFunctionCall() const override; |
291 Value Execute(Scope* scope, Err* err) const override; | 296 Value Execute(Scope* scope, Err* err) const override; |
292 LocationRange GetRange() const override; | 297 LocationRange GetRange() const override; |
293 Err MakeErrorDescribing( | 298 Err MakeErrorDescribing( |
294 const std::string& msg, | 299 const std::string& msg, |
295 const std::string& help = std::string()) const override; | 300 const std::string& help = std::string()) const override; |
296 void Print(std::ostream& out, int indent) const override; | 301 void Print(std::ostream& out, int indent) const override; |
297 | 302 |
298 const Token& function() const { return function_; } | 303 const Token& function() const { return function_; } |
299 void set_function(Token t) { function_ = t; } | 304 void set_function(Token t) { function_ = t; } |
300 | 305 |
301 const ListNode* args() const { return args_.get(); } | 306 const ListNode* args() const { return args_.get(); } |
302 void set_args(scoped_ptr<ListNode> a) { args_ = std::move(a); } | 307 void set_args(std::unique_ptr<ListNode> a) { args_ = std::move(a); } |
303 | 308 |
304 const BlockNode* block() const { return block_.get(); } | 309 const BlockNode* block() const { return block_.get(); } |
305 void set_block(scoped_ptr<BlockNode> b) { block_ = std::move(b); } | 310 void set_block(std::unique_ptr<BlockNode> b) { block_ = std::move(b); } |
306 | 311 |
307 private: | 312 private: |
308 Token function_; | 313 Token function_; |
309 scoped_ptr<ListNode> args_; | 314 std::unique_ptr<ListNode> args_; |
310 scoped_ptr<BlockNode> block_; // May be null. | 315 std::unique_ptr<BlockNode> block_; // May be null. |
311 | 316 |
312 DISALLOW_COPY_AND_ASSIGN(FunctionCallNode); | 317 DISALLOW_COPY_AND_ASSIGN(FunctionCallNode); |
313 }; | 318 }; |
314 | 319 |
315 // IdentifierNode -------------------------------------------------------------- | 320 // IdentifierNode -------------------------------------------------------------- |
316 | 321 |
317 class IdentifierNode : public ParseNode { | 322 class IdentifierNode : public ParseNode { |
318 public: | 323 public: |
319 IdentifierNode(); | 324 IdentifierNode(); |
320 explicit IdentifierNode(const Token& token); | 325 explicit IdentifierNode(const Token& token); |
(...skipping 27 matching lines...) Expand all Loading... |
348 | 353 |
349 const ListNode* AsList() const override; | 354 const ListNode* AsList() const override; |
350 Value Execute(Scope* scope, Err* err) const override; | 355 Value Execute(Scope* scope, Err* err) const override; |
351 LocationRange GetRange() const override; | 356 LocationRange GetRange() const override; |
352 Err MakeErrorDescribing( | 357 Err MakeErrorDescribing( |
353 const std::string& msg, | 358 const std::string& msg, |
354 const std::string& help = std::string()) const override; | 359 const std::string& help = std::string()) const override; |
355 void Print(std::ostream& out, int indent) const override; | 360 void Print(std::ostream& out, int indent) const override; |
356 | 361 |
357 void set_begin_token(const Token& t) { begin_token_ = t; } | 362 void set_begin_token(const Token& t) { begin_token_ = t; } |
358 void set_end(scoped_ptr<EndNode> e) { end_ = std::move(e); } | 363 void set_end(std::unique_ptr<EndNode> e) { end_ = std::move(e); } |
359 const EndNode* End() const { return end_.get(); } | 364 const EndNode* End() const { return end_.get(); } |
360 | 365 |
361 void append_item(scoped_ptr<ParseNode> s) { | 366 void append_item(std::unique_ptr<ParseNode> s) { |
362 contents_.push_back(s.release()); | 367 contents_.push_back(s.release()); |
363 } | 368 } |
364 const std::vector<const ParseNode*>& contents() const { return contents_; } | 369 const std::vector<const ParseNode*>& contents() const { return contents_; } |
365 | 370 |
366 void SortAsStringsList(); | 371 void SortAsStringsList(); |
367 void SortAsDepsList(); | 372 void SortAsDepsList(); |
368 | 373 |
369 // During formatting, do we want this list to always be multliline? This is | 374 // During formatting, do we want this list to always be multliline? This is |
370 // used to make assignments to deps, sources, etc. always be multiline lists, | 375 // used to make assignments to deps, sources, etc. always be multiline lists, |
371 // rather than collapsed to a single line when they're one element. | 376 // rather than collapsed to a single line when they're one element. |
(...skipping 10 matching lines...) Expand all Loading... |
382 // Only public for testing. | 387 // Only public for testing. |
383 std::vector<SortRange> GetSortRanges() const; | 388 std::vector<SortRange> GetSortRanges() const; |
384 | 389 |
385 private: | 390 private: |
386 template <typename Comparator> | 391 template <typename Comparator> |
387 void SortList(Comparator comparator); | 392 void SortList(Comparator comparator); |
388 | 393 |
389 // Tokens corresponding to the [ and ]. The end token is stored in inside an | 394 // Tokens corresponding to the [ and ]. The end token is stored in inside an |
390 // custom parse node so that it can have comments hung off of it. | 395 // custom parse node so that it can have comments hung off of it. |
391 Token begin_token_; | 396 Token begin_token_; |
392 scoped_ptr<EndNode> end_; | 397 std::unique_ptr<EndNode> end_; |
393 bool prefer_multiline_; | 398 bool prefer_multiline_; |
394 | 399 |
395 // Owning pointers, use unique_ptr when we can use C++11. | 400 // Owning pointers, use unique_ptr when we can use C++11. |
396 std::vector<const ParseNode*> contents_; | 401 std::vector<const ParseNode*> contents_; |
397 | 402 |
398 DISALLOW_COPY_AND_ASSIGN(ListNode); | 403 DISALLOW_COPY_AND_ASSIGN(ListNode); |
399 }; | 404 }; |
400 | 405 |
401 // LiteralNode ----------------------------------------------------------------- | 406 // LiteralNode ----------------------------------------------------------------- |
402 | 407 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 LocationRange GetRange() const override; | 442 LocationRange GetRange() const override; |
438 Err MakeErrorDescribing( | 443 Err MakeErrorDescribing( |
439 const std::string& msg, | 444 const std::string& msg, |
440 const std::string& help = std::string()) const override; | 445 const std::string& help = std::string()) const override; |
441 void Print(std::ostream& out, int indent) const override; | 446 void Print(std::ostream& out, int indent) const override; |
442 | 447 |
443 const Token& op() const { return op_; } | 448 const Token& op() const { return op_; } |
444 void set_op(const Token& t) { op_ = t; } | 449 void set_op(const Token& t) { op_ = t; } |
445 | 450 |
446 const ParseNode* operand() const { return operand_.get(); } | 451 const ParseNode* operand() const { return operand_.get(); } |
447 void set_operand(scoped_ptr<ParseNode> operand) { | 452 void set_operand(std::unique_ptr<ParseNode> operand) { |
448 operand_ = std::move(operand); | 453 operand_ = std::move(operand); |
449 } | 454 } |
450 | 455 |
451 private: | 456 private: |
452 Token op_; | 457 Token op_; |
453 scoped_ptr<ParseNode> operand_; | 458 std::unique_ptr<ParseNode> operand_; |
454 | 459 |
455 DISALLOW_COPY_AND_ASSIGN(UnaryOpNode); | 460 DISALLOW_COPY_AND_ASSIGN(UnaryOpNode); |
456 }; | 461 }; |
457 | 462 |
458 // BlockCommentNode ------------------------------------------------------------ | 463 // BlockCommentNode ------------------------------------------------------------ |
459 | 464 |
460 // This node type is only used for standalone comments (that is, those not | 465 // This node type is only used for standalone comments (that is, those not |
461 // specifically attached to another syntax element. The most common of these | 466 // specifically attached to another syntax element. The most common of these |
462 // is a standard header block. This node contains only the last line of such | 467 // is a standard header block. This node contains only the last line of such |
463 // a comment block as the anchor, and other lines of the block comment are | 468 // a comment block as the anchor, and other lines of the block comment are |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 const Token& value() const { return value_; } | 511 const Token& value() const { return value_; } |
507 void set_value(const Token& t) { value_ = t; } | 512 void set_value(const Token& t) { value_ = t; } |
508 | 513 |
509 private: | 514 private: |
510 Token value_; | 515 Token value_; |
511 | 516 |
512 DISALLOW_COPY_AND_ASSIGN(EndNode); | 517 DISALLOW_COPY_AND_ASSIGN(EndNode); |
513 }; | 518 }; |
514 | 519 |
515 #endif // TOOLS_GN_PARSE_TREE_H_ | 520 #endif // TOOLS_GN_PARSE_TREE_H_ |
OLD | NEW |