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 AstString; | |
15 class AstValueFactory; | |
16 class FunctionLiteral; | 14 class FunctionLiteral; |
| 15 class Isolate; |
17 | 16 |
18 // FuncNameInferrer is a stateful class that is used to perform name | 17 // FuncNameInferrer is a stateful class that is used to perform name |
19 // inference for anonymous functions during static analysis of source code. | 18 // inference for anonymous functions during static analysis of source code. |
20 // Inference is performed in cases when an anonymous function is assigned | 19 // Inference is performed in cases when an anonymous function is assigned |
21 // to a variable or a property (see test-func-name-inference.cc for examples.) | 20 // to a variable or a property (see test-func-name-inference.cc for examples.) |
22 // | 21 // |
23 // The basic idea is that during parsing of LHSs of certain expressions | 22 // The basic idea is that during parsing of LHSs of certain expressions |
24 // (assignments, declarations, object literals) we collect name strings, | 23 // (assignments, declarations, object literals) we collect name strings, |
25 // and during parsing of the RHS, a function literal can be collected. After | 24 // and during parsing of the RHS, a function literal can be collected. After |
26 // parsing the RHS we can infer a name for function literals that do not have | 25 // parsing the RHS we can infer a name for function literals that do not have |
27 // a name. | 26 // a name. |
28 class FuncNameInferrer : public ZoneObject { | 27 class FuncNameInferrer : public ZoneObject { |
29 public: | 28 public: |
30 FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone); | 29 FuncNameInferrer(Isolate* isolate, Zone* zone); |
31 | 30 |
32 // Returns whether we have entered name collection state. | 31 // Returns whether we have entered name collection state. |
33 bool IsOpen() const { return !entries_stack_.is_empty(); } | 32 bool IsOpen() const { return !entries_stack_.is_empty(); } |
34 | 33 |
35 // Pushes an enclosing the name of enclosing function onto names stack. | 34 // Pushes an enclosing the name of enclosing function onto names stack. |
36 void PushEnclosingName(const AstString* name); | 35 void PushEnclosingName(Handle<String> name); |
37 | 36 |
38 // Enters name collection state. | 37 // Enters name collection state. |
39 void Enter() { | 38 void Enter() { |
40 entries_stack_.Add(names_stack_.length(), zone()); | 39 entries_stack_.Add(names_stack_.length(), zone()); |
41 } | 40 } |
42 | 41 |
43 // Pushes an encountered name onto names stack when in collection state. | 42 // Pushes an encountered name onto names stack when in collection state. |
44 void PushLiteralName(const AstString* name); | 43 void PushLiteralName(Handle<String> name); |
45 | 44 |
46 void PushVariableName(const AstString* name); | 45 void PushVariableName(Handle<String> name); |
47 | 46 |
48 // Adds a function to infer name for. | 47 // Adds a function to infer name for. |
49 void AddFunction(FunctionLiteral* func_to_infer) { | 48 void AddFunction(FunctionLiteral* func_to_infer) { |
50 if (IsOpen()) { | 49 if (IsOpen()) { |
51 funcs_to_infer_.Add(func_to_infer, zone()); | 50 funcs_to_infer_.Add(func_to_infer, zone()); |
52 } | 51 } |
53 } | 52 } |
54 | 53 |
55 void RemoveLastFunction() { | 54 void RemoveLastFunction() { |
56 if (IsOpen() && !funcs_to_infer_.is_empty()) { | 55 if (IsOpen() && !funcs_to_infer_.is_empty()) { |
(...skipping 17 matching lines...) Expand all Loading... |
74 funcs_to_infer_.Clear(); | 73 funcs_to_infer_.Clear(); |
75 } | 74 } |
76 | 75 |
77 private: | 76 private: |
78 enum NameType { | 77 enum NameType { |
79 kEnclosingConstructorName, | 78 kEnclosingConstructorName, |
80 kLiteralName, | 79 kLiteralName, |
81 kVariableName | 80 kVariableName |
82 }; | 81 }; |
83 struct Name { | 82 struct Name { |
84 Name(const AstString* name, NameType type) : name(name), type(type) {} | 83 Name(Handle<String> name, NameType type) : name(name), type(type) { } |
85 const AstString* name; | 84 Handle<String> name; |
86 NameType type; | 85 NameType type; |
87 }; | 86 }; |
88 | 87 |
| 88 Isolate* isolate() { return isolate_; } |
89 Zone* zone() const { return zone_; } | 89 Zone* zone() const { return zone_; } |
90 | 90 |
91 // Constructs a full name in dotted notation from gathered names. | 91 // Constructs a full name in dotted notation from gathered names. |
92 const AstString* MakeNameFromStack(); | 92 Handle<String> MakeNameFromStack(); |
| 93 |
| 94 // A helper function for MakeNameFromStack. |
| 95 Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev); |
93 | 96 |
94 // Performs name inferring for added functions. | 97 // Performs name inferring for added functions. |
95 void InferFunctionsNames(); | 98 void InferFunctionsNames(); |
96 | 99 |
97 AstValueFactory* ast_value_factory_; | 100 Isolate* isolate_; |
98 ZoneList<int> entries_stack_; | 101 ZoneList<int> entries_stack_; |
99 ZoneList<Name> names_stack_; | 102 ZoneList<Name> names_stack_; |
100 ZoneList<FunctionLiteral*> funcs_to_infer_; | 103 ZoneList<FunctionLiteral*> funcs_to_infer_; |
101 Zone* zone_; | 104 Zone* zone_; |
102 | 105 |
103 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); | 106 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); |
104 }; | 107 }; |
105 | 108 |
106 | 109 |
107 } } // namespace v8::internal | 110 } } // namespace v8::internal |
108 | 111 |
109 #endif // V8_FUNC_NAME_INFERRER_H_ | 112 #endif // V8_FUNC_NAME_INFERRER_H_ |
OLD | NEW |