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 27 matching lines...) Expand all Loading... |
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 | 42 |
43 class FuncNameInferrer BASE_EMBEDDED { | 43 class FuncNameInferrer BASE_EMBEDDED { |
44 public: | 44 public: |
45 FuncNameInferrer() : | 45 FuncNameInferrer() : |
46 entries_stack_(10), | 46 entries_stack_(10), |
47 names_stack_(5), | 47 names_stack_(5), |
48 func_to_infer_(NULL), | 48 funcs_to_infer_(4), |
49 dot_(Factory::NewStringFromAscii(CStrVector("."))) { | 49 dot_(Factory::NewStringFromAscii(CStrVector("."))) { |
50 } | 50 } |
51 | 51 |
52 bool IsOpen() const { return !entries_stack_.is_empty(); } | 52 bool IsOpen() const { return !entries_stack_.is_empty(); } |
53 | 53 |
54 void PushEnclosingName(Handle<String> name); | 54 void PushEnclosingName(Handle<String> name); |
55 | 55 |
56 void Enter() { | 56 void Enter() { |
57 entries_stack_.Add(names_stack_.length()); | 57 entries_stack_.Add(names_stack_.length()); |
58 } | 58 } |
59 | 59 |
60 void Leave() { | |
61 ASSERT(IsOpen()); | |
62 names_stack_.Rewind(entries_stack_.RemoveLast()); | |
63 } | |
64 | |
65 void PushName(Handle<String> name) { | 60 void PushName(Handle<String> name) { |
66 if (IsOpen()) { | 61 if (IsOpen()) { |
67 names_stack_.Add(name); | 62 names_stack_.Add(name); |
68 } | 63 } |
69 } | 64 } |
70 | 65 |
71 void SetFuncToInfer(FunctionLiteral* func_to_infer) { | 66 void AddFunction(FunctionLiteral* func_to_infer) { |
72 if (IsOpen()) { | 67 if (IsOpen()) { |
73 // If we encounter another function literal after already having | 68 funcs_to_infer_.Add(func_to_infer); |
74 // encountered one, the second one replaces the first. | |
75 func_to_infer_ = func_to_infer; | |
76 } | 69 } |
77 } | 70 } |
78 | 71 |
79 void InferAndLeave() { | 72 void InferAndLeave() { |
80 ASSERT(IsOpen()); | 73 ASSERT(IsOpen()); |
81 MaybeInferFunctionName(); | 74 if (!funcs_to_infer_.is_empty()) { |
82 Leave(); | 75 InferFunctionsNames(); |
| 76 } |
| 77 names_stack_.Rewind(entries_stack_.RemoveLast()); |
83 } | 78 } |
84 | 79 |
85 private: | 80 private: |
86 Handle<String> MakeNameFromStack(); | 81 Handle<String> MakeNameFromStack(); |
87 Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev); | 82 Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev); |
88 void MaybeInferFunctionName(); | 83 void InferFunctionsNames(); |
89 | 84 |
90 List<int> entries_stack_; | 85 List<int> entries_stack_; |
91 List<Handle<String> > names_stack_; | 86 List<Handle<String> > names_stack_; |
92 FunctionLiteral* func_to_infer_; | 87 List<FunctionLiteral*> funcs_to_infer_; |
93 Handle<String> dot_; | 88 Handle<String> dot_; |
94 | 89 |
95 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); | 90 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer); |
96 }; | 91 }; |
97 | 92 |
98 | 93 |
99 // A wrapper class that automatically calls InferAndLeave when | 94 // A wrapper class that automatically calls InferAndLeave when |
100 // leaving scope. | 95 // leaving scope. |
101 class ScopedFuncNameInferrer BASE_EMBEDDED { | 96 class ScopedFuncNameInferrer BASE_EMBEDDED { |
102 public: | 97 public: |
(...skipping 15 matching lines...) Expand all Loading... |
118 FuncNameInferrer* inferrer_; | 113 FuncNameInferrer* inferrer_; |
119 bool is_entered_; | 114 bool is_entered_; |
120 | 115 |
121 DISALLOW_COPY_AND_ASSIGN(ScopedFuncNameInferrer); | 116 DISALLOW_COPY_AND_ASSIGN(ScopedFuncNameInferrer); |
122 }; | 117 }; |
123 | 118 |
124 | 119 |
125 } } // namespace v8::internal | 120 } } // namespace v8::internal |
126 | 121 |
127 #endif // V8_FUNC_NAME_INFERRER_H_ | 122 #endif // V8_FUNC_NAME_INFERRER_H_ |
OLD | NEW |