OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project 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 V8_FUNC_NAME_INFERRER_H_ | 5 #ifndef V8_FUNC_NAME_INFERRER_H_ |
6 #define V8_FUNC_NAME_INFERRER_H_ | 6 #define V8_FUNC_NAME_INFERRER_H_ |
7 | 7 |
8 #include "src/handles.h" | 8 #include "src/handles.h" |
9 #include "src/zone.h" | 9 #include "src/zone.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 class AstRawString; | 14 class AstRawString; |
15 class AstString; | 15 class AstString; |
| 16 class AstValue; |
16 class AstValueFactory; | 17 class AstValueFactory; |
17 class FunctionLiteral; | 18 class FunctionLiteral; |
| 19 class Expression; |
18 | 20 |
19 // FuncNameInferrer is a stateful class that is used to perform name | 21 // FuncNameInferrer is a stateful class that is used to perform name |
20 // inference for anonymous functions during static analysis of source code. | 22 // inference for anonymous functions during static analysis of source code. |
21 // Inference is performed in cases when an anonymous function is assigned | 23 // Inference is performed in cases when an anonymous function is assigned |
22 // to a variable or a property (see test-func-name-inference.cc for examples.) | 24 // to a variable or a property (see test-func-name-inference.cc for examples.) |
23 // | 25 // |
24 // The basic idea is that during parsing of LHSs of certain expressions | 26 // The basic idea is that during parsing of LHSs of certain expressions |
25 // (assignments, declarations, object literals) we collect name strings, | 27 // (assignments, declarations, object literals) we collect name strings, |
26 // and during parsing of the RHS, a function literal can be collected. After | 28 // and during parsing of the RHS, a function literal can be collected. After |
27 // parsing the RHS we can infer a name for function literals that do not have | 29 // parsing the RHS we can infer a name for function literals that do not have |
28 // a name. | 30 // a name. |
29 class FuncNameInferrer : public ZoneObject { | 31 class FuncNameInferrer : public ZoneObject { |
30 public: | 32 public: |
31 FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone); | 33 FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone); |
32 | 34 |
33 // Returns whether we have entered name collection state. | 35 // Returns whether we have entered name collection state. |
34 bool IsOpen() const { return !entries_stack_.is_empty(); } | 36 bool IsOpen() const { return !entries_stack_.is_empty(); } |
35 | 37 |
36 // Pushes an enclosing the name of enclosing function onto names stack. | 38 // Pushes an enclosing the name of enclosing function onto names stack. |
37 void PushEnclosingName(const AstRawString* name); | 39 void PushEnclosingName(const AstRawString* name); |
38 | 40 |
39 // Enters name collection state. | 41 // Enters name collection state. |
40 void Enter() { | 42 void Enter() { |
41 entries_stack_.Add(names_stack_.length(), zone()); | 43 entries_stack_.Add(names_stack_.length(), zone()); |
42 } | 44 } |
43 | 45 |
44 // Pushes an encountered name onto names stack when in collection state. | 46 // Pushes an encountered name onto names stack when in collection state. |
| 47 void PushLiteralName(const AstValue* name); |
45 void PushLiteralName(const AstRawString* name); | 48 void PushLiteralName(const AstRawString* name); |
46 | 49 void PushLiteralName(Expression* expression); |
47 void PushVariableName(const AstRawString* name); | 50 void PushVariableName(const AstRawString* name); |
48 | 51 |
49 // Adds a function to infer name for. | 52 // Adds a function to infer name for. |
50 void AddFunction(FunctionLiteral* func_to_infer) { | 53 void AddFunction(FunctionLiteral* func_to_infer) { |
51 if (IsOpen()) { | 54 if (IsOpen()) { |
52 funcs_to_infer_.Add(func_to_infer, zone()); | 55 funcs_to_infer_.Add(func_to_infer, zone()); |
53 } | 56 } |
54 } | 57 } |
55 | 58 |
56 void RemoveLastFunction() { | 59 void RemoveLastFunction() { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 ZoneList<FunctionLiteral*> funcs_to_infer_; | 108 ZoneList<FunctionLiteral*> funcs_to_infer_; |
106 Zone* zone_; | 109 Zone* zone_; |
107 | 110 |
108 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); | 111 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); |
109 }; | 112 }; |
110 | 113 |
111 | 114 |
112 } } // namespace v8::internal | 115 } } // namespace v8::internal |
113 | 116 |
114 #endif // V8_FUNC_NAME_INFERRER_H_ | 117 #endif // V8_FUNC_NAME_INFERRER_H_ |
OLD | NEW |