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