| 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_PARSING_FUNC_NAME_INFERRER_H_ | 5 #ifndef V8_PARSING_FUNC_NAME_INFERRER_H_ |
| 6 #define V8_PARSING_FUNC_NAME_INFERRER_H_ | 6 #define V8_PARSING_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 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // | 23 // |
| 24 // 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 |
| 25 // (assignments, declarations, object literals) we collect name strings, | 25 // (assignments, declarations, object literals) we collect name strings, |
| 26 // 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 |
| 27 // 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 |
| 28 // a name. | 28 // a name. |
| 29 class FuncNameInferrer : public ZoneObject { | 29 class FuncNameInferrer : public ZoneObject { |
| 30 public: | 30 public: |
| 31 FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone); | 31 FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone); |
| 32 | 32 |
| 33 // To enter function name inference state, put a FuncNameInferrer::State |
| 34 // on the stack. |
| 35 class State { |
| 36 public: |
| 37 explicit State(FuncNameInferrer* fni) : fni_(fni) { |
| 38 if (fni_ != nullptr) fni_->Enter(); |
| 39 } |
| 40 ~State() { |
| 41 if (fni_ != nullptr) fni_->Leave(); |
| 42 } |
| 43 |
| 44 private: |
| 45 FuncNameInferrer* fni_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(State); |
| 48 }; |
| 49 |
| 33 // Returns whether we have entered name collection state. | 50 // Returns whether we have entered name collection state. |
| 34 bool IsOpen() const { return !entries_stack_.is_empty(); } | 51 bool IsOpen() const { return !entries_stack_.is_empty(); } |
| 35 | 52 |
| 36 // Pushes an enclosing the name of enclosing function onto names stack. | 53 // Pushes an enclosing the name of enclosing function onto names stack. |
| 37 void PushEnclosingName(const AstRawString* name); | 54 void PushEnclosingName(const AstRawString* name); |
| 38 | 55 |
| 39 // Enters name collection state. | |
| 40 void Enter() { | |
| 41 entries_stack_.Add(names_stack_.length(), zone()); | |
| 42 } | |
| 43 | |
| 44 // Pushes an encountered name onto names stack when in collection state. | 56 // Pushes an encountered name onto names stack when in collection state. |
| 45 void PushLiteralName(const AstRawString* name); | 57 void PushLiteralName(const AstRawString* name); |
| 46 | 58 |
| 47 void PushVariableName(const AstRawString* name); | 59 void PushVariableName(const AstRawString* name); |
| 48 | 60 |
| 49 // Adds a function to infer name for. | 61 // Adds a function to infer name for. |
| 50 void AddFunction(FunctionLiteral* func_to_infer) { | 62 void AddFunction(FunctionLiteral* func_to_infer) { |
| 51 if (IsOpen()) { | 63 if (IsOpen()) { |
| 52 funcs_to_infer_.Add(func_to_infer, zone()); | 64 funcs_to_infer_.Add(func_to_infer, zone()); |
| 53 } | 65 } |
| 54 } | 66 } |
| 55 | 67 |
| 56 void RemoveLastFunction() { | 68 void RemoveLastFunction() { |
| 57 if (IsOpen() && !funcs_to_infer_.is_empty()) { | 69 if (IsOpen() && !funcs_to_infer_.is_empty()) { |
| 58 funcs_to_infer_.RemoveLast(); | 70 funcs_to_infer_.RemoveLast(); |
| 59 } | 71 } |
| 60 } | 72 } |
| 61 | 73 |
| 62 // Infers a function name and leaves names collection state. | 74 // Infers a function name and leaves names collection state. |
| 63 void Infer() { | 75 void Infer() { |
| 64 DCHECK(IsOpen()); | 76 DCHECK(IsOpen()); |
| 65 if (!funcs_to_infer_.is_empty()) { | 77 if (!funcs_to_infer_.is_empty()) { |
| 66 InferFunctionsNames(); | 78 InferFunctionsNames(); |
| 67 } | 79 } |
| 68 } | 80 } |
| 69 | 81 |
| 70 // Leaves names collection state. | |
| 71 void Leave() { | |
| 72 DCHECK(IsOpen()); | |
| 73 names_stack_.Rewind(entries_stack_.RemoveLast()); | |
| 74 if (entries_stack_.is_empty()) | |
| 75 funcs_to_infer_.Clear(); | |
| 76 } | |
| 77 | |
| 78 private: | 82 private: |
| 79 enum NameType { | 83 enum NameType { |
| 80 kEnclosingConstructorName, | 84 kEnclosingConstructorName, |
| 81 kLiteralName, | 85 kLiteralName, |
| 82 kVariableName | 86 kVariableName |
| 83 }; | 87 }; |
| 84 struct Name { | 88 struct Name { |
| 85 Name(const AstRawString* name, NameType type) : name(name), type(type) {} | 89 Name(const AstRawString* name, NameType type) : name(name), type(type) {} |
| 86 const AstRawString* name; | 90 const AstRawString* name; |
| 87 NameType type; | 91 NameType type; |
| 88 }; | 92 }; |
| 89 | 93 |
| 94 void Enter() { entries_stack_.Add(names_stack_.length(), zone()); } |
| 95 |
| 96 void Leave() { |
| 97 DCHECK(IsOpen()); |
| 98 names_stack_.Rewind(entries_stack_.RemoveLast()); |
| 99 if (entries_stack_.is_empty()) funcs_to_infer_.Clear(); |
| 100 } |
| 101 |
| 90 Zone* zone() const { return zone_; } | 102 Zone* zone() const { return zone_; } |
| 91 | 103 |
| 92 // Constructs a full name in dotted notation from gathered names. | 104 // Constructs a full name in dotted notation from gathered names. |
| 93 const AstString* MakeNameFromStack(); | 105 const AstString* MakeNameFromStack(); |
| 94 | 106 |
| 95 // A helper function for MakeNameFromStack. | 107 // A helper function for MakeNameFromStack. |
| 96 const AstString* MakeNameFromStackHelper(int pos, | 108 const AstString* MakeNameFromStackHelper(int pos, |
| 97 const AstString* prev); | 109 const AstString* prev); |
| 98 | 110 |
| 99 // Performs name inferring for added functions. | 111 // Performs name inferring for added functions. |
| 100 void InferFunctionsNames(); | 112 void InferFunctionsNames(); |
| 101 | 113 |
| 102 AstValueFactory* ast_value_factory_; | 114 AstValueFactory* ast_value_factory_; |
| 103 ZoneList<int> entries_stack_; | 115 ZoneList<int> entries_stack_; |
| 104 ZoneList<Name> names_stack_; | 116 ZoneList<Name> names_stack_; |
| 105 ZoneList<FunctionLiteral*> funcs_to_infer_; | 117 ZoneList<FunctionLiteral*> funcs_to_infer_; |
| 106 Zone* zone_; | 118 Zone* zone_; |
| 107 | 119 |
| 108 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); | 120 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); |
| 109 }; | 121 }; |
| 110 | 122 |
| 111 | 123 |
| 112 } // namespace internal | 124 } // namespace internal |
| 113 } // namespace v8 | 125 } // namespace v8 |
| 114 | 126 |
| 115 #endif // V8_PARSING_FUNC_NAME_INFERRER_H_ | 127 #endif // V8_PARSING_FUNC_NAME_INFERRER_H_ |
| OLD | NEW |