| 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 18 matching lines...) Expand all Loading... |
| 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 // FuncNameInferrer is a stateful class that is used to perform name | 34 // FuncNameInferrer is a stateful class that is used to perform name |
| 35 // inference for anonymous functions during static analysis of source code. | 35 // inference for anonymous functions during static analysis of source code. |
| 36 // Inference is performed in cases when an anonymous function is assigned | 36 // 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.) | 37 // to a variable or a property (see test-func-name-inference.cc for examples.) |
| 38 // | 38 // |
| 39 // The basic idea is that during AST traversal LHSs of expressions are | 39 // The basic idea is that during parsing of LHSs of certain expressions |
| 40 // always visited before RHSs. Thus, during visiting the LHS, a name can be | 40 // (assignments, declarations, object literals) we collect name strings, |
| 41 // collected, and during visiting the RHS, a function literal can be collected. | 41 // and during parsing of the RHS, a function literal can be collected. After |
| 42 // Inference is performed while leaving the assignment node. | 42 // parsing the RHS we can infer a name for function literals that do not have |
| 43 class FuncNameInferrer BASE_EMBEDDED { | 43 // a name. |
| 44 class FuncNameInferrer : public ZoneObject { |
| 44 public: | 45 public: |
| 45 FuncNameInferrer() | 46 FuncNameInferrer() |
| 46 : entries_stack_(10), | 47 : entries_stack_(10), |
| 47 names_stack_(5), | 48 names_stack_(5), |
| 48 funcs_to_infer_(4), | 49 funcs_to_infer_(4), |
| 49 dot_(Factory::NewStringFromAscii(CStrVector("."))) { | 50 dot_(Factory::NewStringFromAscii(CStrVector("."))) { |
| 50 } | 51 } |
| 51 | 52 |
| 52 // Returns whether we have entered name collection state. | 53 // Returns whether we have entered name collection state. |
| 53 bool IsOpen() const { return !entries_stack_.is_empty(); } | 54 bool IsOpen() const { return !entries_stack_.is_empty(); } |
| 54 | 55 |
| 55 // Pushes an enclosing the name of enclosing function onto names stack. | 56 // Pushes an enclosing the name of enclosing function onto names stack. |
| 56 void PushEnclosingName(Handle<String> name); | 57 void PushEnclosingName(Handle<String> name); |
| 57 | 58 |
| 58 // Enters name collection state. | 59 // Enters name collection state. |
| 59 void Enter() { | 60 void Enter() { |
| 60 entries_stack_.Add(names_stack_.length()); | 61 entries_stack_.Add(names_stack_.length()); |
| 61 } | 62 } |
| 62 | 63 |
| 63 // Pushes an encountered name onto names stack when in collection state. | 64 // Pushes an encountered name onto names stack when in collection state. |
| 64 void PushName(Handle<String> name) { | 65 void PushLiteralName(Handle<String> name); |
| 65 if (IsOpen()) { | 66 |
| 66 names_stack_.Add(name); | 67 void PushVariableName(Handle<String> name); |
| 67 } | |
| 68 } | |
| 69 | 68 |
| 70 // Adds a function to infer name for. | 69 // Adds a function to infer name for. |
| 71 void AddFunction(FunctionLiteral* func_to_infer) { | 70 void AddFunction(FunctionLiteral* func_to_infer) { |
| 72 if (IsOpen()) { | 71 if (IsOpen()) { |
| 73 funcs_to_infer_.Add(func_to_infer); | 72 funcs_to_infer_.Add(func_to_infer); |
| 74 } | 73 } |
| 75 } | 74 } |
| 76 | 75 |
| 77 // Infers a function name and leaves names collection state. | 76 // Infers a function name and leaves names collection state. |
| 78 void InferAndLeave() { | 77 void Infer() { |
| 79 ASSERT(IsOpen()); | 78 ASSERT(IsOpen()); |
| 80 if (!funcs_to_infer_.is_empty()) { | 79 if (!funcs_to_infer_.is_empty()) { |
| 81 InferFunctionsNames(); | 80 InferFunctionsNames(); |
| 82 } | 81 } |
| 82 } |
| 83 |
| 84 // Infers a function name and leaves names collection state. |
| 85 void Leave() { |
| 86 ASSERT(IsOpen()); |
| 83 names_stack_.Rewind(entries_stack_.RemoveLast()); | 87 names_stack_.Rewind(entries_stack_.RemoveLast()); |
| 84 } | 88 } |
| 85 | 89 |
| 86 private: | 90 private: |
| 87 // Constructs a full name in dotted notation from gathered names. | 91 // Constructs a full name in dotted notation from gathered names. |
| 88 Handle<String> MakeNameFromStack(); | 92 Handle<String> MakeNameFromStack(); |
| 89 | 93 |
| 90 // A helper function for MakeNameFromStack. | 94 // A helper function for MakeNameFromStack. |
| 91 Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev); | 95 Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev); |
| 92 | 96 |
| 93 // Performs name inferring for added functions. | 97 // Performs name inferring for added functions. |
| 94 void InferFunctionsNames(); | 98 void InferFunctionsNames(); |
| 95 | 99 |
| 96 ZoneList<int> entries_stack_; | 100 ZoneList<int> entries_stack_; |
| 97 ZoneList<Handle<String> > names_stack_; | 101 ZoneList<Handle<String> > names_stack_; |
| 98 ZoneList<FunctionLiteral*> funcs_to_infer_; | 102 ZoneList<FunctionLiteral*> funcs_to_infer_; |
| 99 Handle<String> dot_; | 103 Handle<String> dot_; |
| 100 | 104 |
| 101 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); | 105 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); |
| 102 }; | 106 }; |
| 103 | 107 |
| 104 | 108 |
| 105 // A wrapper class that automatically calls InferAndLeave when | |
| 106 // leaving scope. | |
| 107 class ScopedFuncNameInferrer BASE_EMBEDDED { | |
| 108 public: | |
| 109 explicit ScopedFuncNameInferrer(FuncNameInferrer* inferrer) | |
| 110 : inferrer_(inferrer), | |
| 111 is_entered_(false) {} | |
| 112 | |
| 113 ~ScopedFuncNameInferrer() { | |
| 114 if (is_entered_) { | |
| 115 inferrer_->InferAndLeave(); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // Triggers the wrapped inferrer into name collection state. | |
| 120 void Enter() { | |
| 121 inferrer_->Enter(); | |
| 122 is_entered_ = true; | |
| 123 } | |
| 124 | |
| 125 private: | |
| 126 FuncNameInferrer* inferrer_; | |
| 127 bool is_entered_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(ScopedFuncNameInferrer); | |
| 130 }; | |
| 131 | |
| 132 | |
| 133 } } // namespace v8::internal | 109 } } // namespace v8::internal |
| 134 | 110 |
| 135 #endif // V8_FUNC_NAME_INFERRER_H_ | 111 #endif // V8_FUNC_NAME_INFERRER_H_ |
| OLD | NEW |