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