| 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 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef V8_FUNC_NAME_INFERRER_H_ | 28 #ifndef V8_FUNC_NAME_INFERRER_H_ |
| 29 #define V8_FUNC_NAME_INFERRER_H_ | 29 #define V8_FUNC_NAME_INFERRER_H_ |
| 30 | 30 |
| 31 namespace v8 { | 31 namespace v8 { |
| 32 namespace internal { | 32 namespace internal { |
| 33 | 33 |
| 34 class Isolate; |
| 35 |
| 34 // FuncNameInferrer is a stateful class that is used to perform name | 36 // FuncNameInferrer is a stateful class that is used to perform name |
| 35 // inference for anonymous functions during static analysis of source code. | 37 // inference for anonymous functions during static analysis of source code. |
| 36 // Inference is performed in cases when an anonymous function is assigned | 38 // Inference is performed in cases when an anonymous function is assigned |
| 37 // to a variable or a property (see test-func-name-inference.cc for examples.) | 39 // to a variable or a property (see test-func-name-inference.cc for examples.) |
| 38 // | 40 // |
| 39 // The basic idea is that during parsing of LHSs of certain expressions | 41 // The basic idea is that during parsing of LHSs of certain expressions |
| 40 // (assignments, declarations, object literals) we collect name strings, | 42 // (assignments, declarations, object literals) we collect name strings, |
| 41 // and during parsing of the RHS, a function literal can be collected. After | 43 // and during parsing of the RHS, a function literal can be collected. After |
| 42 // parsing the RHS we can infer a name for function literals that do not have | 44 // parsing the RHS we can infer a name for function literals that do not have |
| 43 // a name. | 45 // a name. |
| 44 class FuncNameInferrer : public ZoneObject { | 46 class FuncNameInferrer : public ZoneObject { |
| 45 public: | 47 public: |
| 46 FuncNameInferrer() | 48 explicit FuncNameInferrer(Isolate* isolate); |
| 47 : entries_stack_(10), | |
| 48 names_stack_(5), | |
| 49 funcs_to_infer_(4), | |
| 50 dot_(FACTORY->NewStringFromAscii(CStrVector("."))) { | |
| 51 } | |
| 52 | 49 |
| 53 // Returns whether we have entered name collection state. | 50 // Returns whether we have entered name collection state. |
| 54 bool IsOpen() const { return !entries_stack_.is_empty(); } | 51 bool IsOpen() const { return !entries_stack_.is_empty(); } |
| 55 | 52 |
| 56 // Pushes an enclosing the name of enclosing function onto names stack. | 53 // Pushes an enclosing the name of enclosing function onto names stack. |
| 57 void PushEnclosingName(Handle<String> name); | 54 void PushEnclosingName(Handle<String> name); |
| 58 | 55 |
| 59 // Enters name collection state. | 56 // Enters name collection state. |
| 60 void Enter() { | 57 void Enter() { |
| 61 entries_stack_.Add(names_stack_.length()); | 58 entries_stack_.Add(names_stack_.length()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 74 } | 71 } |
| 75 | 72 |
| 76 // Infers a function name and leaves names collection state. | 73 // Infers a function name and leaves names collection state. |
| 77 void Infer() { | 74 void Infer() { |
| 78 ASSERT(IsOpen()); | 75 ASSERT(IsOpen()); |
| 79 if (!funcs_to_infer_.is_empty()) { | 76 if (!funcs_to_infer_.is_empty()) { |
| 80 InferFunctionsNames(); | 77 InferFunctionsNames(); |
| 81 } | 78 } |
| 82 } | 79 } |
| 83 | 80 |
| 84 // Infers a function name and leaves names collection state. | 81 // Leaves names collection state. |
| 85 void Leave() { | 82 void Leave() { |
| 86 ASSERT(IsOpen()); | 83 ASSERT(IsOpen()); |
| 87 names_stack_.Rewind(entries_stack_.RemoveLast()); | 84 names_stack_.Rewind(entries_stack_.RemoveLast()); |
| 88 } | 85 } |
| 89 | 86 |
| 90 private: | 87 private: |
| 88 enum NameType { |
| 89 kEnclosingConstructorName, |
| 90 kLiteralName, |
| 91 kVariableName |
| 92 }; |
| 93 struct Name { |
| 94 Name(Handle<String> name, NameType type) : name(name), type(type) { } |
| 95 Handle<String> name; |
| 96 NameType type; |
| 97 }; |
| 98 |
| 99 Isolate* isolate() { return isolate_; } |
| 100 |
| 91 // Constructs a full name in dotted notation from gathered names. | 101 // Constructs a full name in dotted notation from gathered names. |
| 92 Handle<String> MakeNameFromStack(); | 102 Handle<String> MakeNameFromStack(); |
| 93 | 103 |
| 94 // A helper function for MakeNameFromStack. | 104 // A helper function for MakeNameFromStack. |
| 95 Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev); | 105 Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev); |
| 96 | 106 |
| 97 // Performs name inferring for added functions. | 107 // Performs name inferring for added functions. |
| 98 void InferFunctionsNames(); | 108 void InferFunctionsNames(); |
| 99 | 109 |
| 110 Isolate* isolate_; |
| 100 ZoneList<int> entries_stack_; | 111 ZoneList<int> entries_stack_; |
| 101 ZoneList<Handle<String> > names_stack_; | 112 ZoneList<Name> names_stack_; |
| 102 ZoneList<FunctionLiteral*> funcs_to_infer_; | 113 ZoneList<FunctionLiteral*> funcs_to_infer_; |
| 103 Handle<String> dot_; | |
| 104 | 114 |
| 105 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); | 115 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); |
| 106 }; | 116 }; |
| 107 | 117 |
| 108 | 118 |
| 109 } } // namespace v8::internal | 119 } } // namespace v8::internal |
| 110 | 120 |
| 111 #endif // V8_FUNC_NAME_INFERRER_H_ | 121 #endif // V8_FUNC_NAME_INFERRER_H_ |
| OLD | NEW |