blob: 8b077f9d8cf6b4c0ebee72d677378d965bd5c951 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_FUNC_NAME_INFERRER_H_
6#define V8_FUNC_NAME_INFERRER_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/handles.h"
9#include "src/zone.h"
10
Steve Blocka7e24c12009-10-30 11:49:00 +000011namespace v8 {
12namespace internal {
13
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014class AstRawString;
15class AstString;
16class AstValueFactory;
17class FunctionLiteral;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000018
Steve Blocka7e24c12009-10-30 11:49:00 +000019// FuncNameInferrer is a stateful class that is used to perform name
20// inference for anonymous functions during static analysis of source code.
21// Inference is performed in cases when an anonymous function is assigned
22// to a variable or a property (see test-func-name-inference.cc for examples.)
23//
Kristian Monsen80d68ea2010-09-08 11:05:35 +010024// The basic idea is that during parsing of LHSs of certain expressions
25// (assignments, declarations, object literals) we collect name strings,
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
28// a name.
29class FuncNameInferrer : public ZoneObject {
Steve Blocka7e24c12009-10-30 11:49:00 +000030 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone);
Steve Blocka7e24c12009-10-30 11:49:00 +000032
33 // Returns whether we have entered name collection state.
34 bool IsOpen() const { return !entries_stack_.is_empty(); }
35
36 // Pushes an enclosing the name of enclosing function onto names stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 void PushEnclosingName(const AstRawString* name);
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39 // Enters name collection state.
40 void Enter() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 entries_stack_.Add(names_stack_.length(), zone());
Steve Blocka7e24c12009-10-30 11:49:00 +000042 }
43
44 // Pushes an encountered name onto names stack when in collection state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 void PushLiteralName(const AstRawString* name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010046
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 void PushVariableName(const AstRawString* name);
Steve Blocka7e24c12009-10-30 11:49:00 +000048
49 // Adds a function to infer name for.
50 void AddFunction(FunctionLiteral* func_to_infer) {
51 if (IsOpen()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 funcs_to_infer_.Add(func_to_infer, zone());
Steve Blocka7e24c12009-10-30 11:49:00 +000053 }
54 }
55
Ben Murdoch69a99ed2011-11-30 16:03:39 +000056 void RemoveLastFunction() {
57 if (IsOpen() && !funcs_to_infer_.is_empty()) {
58 funcs_to_infer_.RemoveLast();
59 }
60 }
61
Steve Blocka7e24c12009-10-30 11:49:00 +000062 // Infers a function name and leaves names collection state.
Kristian Monsen80d68ea2010-09-08 11:05:35 +010063 void Infer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 DCHECK(IsOpen());
Steve Blocka7e24c12009-10-30 11:49:00 +000065 if (!funcs_to_infer_.is_empty()) {
66 InferFunctionsNames();
67 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +010068 }
69
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000070 // Leaves names collection state.
Kristian Monsen80d68ea2010-09-08 11:05:35 +010071 void Leave() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 DCHECK(IsOpen());
Steve Blocka7e24c12009-10-30 11:49:00 +000073 names_stack_.Rewind(entries_stack_.RemoveLast());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 if (entries_stack_.is_empty())
75 funcs_to_infer_.Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +000076 }
77
78 private:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000079 enum NameType {
80 kEnclosingConstructorName,
81 kLiteralName,
82 kVariableName
83 };
84 struct Name {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 Name(const AstRawString* name, NameType type) : name(name), type(type) {}
86 const AstRawString* name;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000087 NameType type;
88 };
89
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 Zone* zone() const { return zone_; }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000091
Steve Blocka7e24c12009-10-30 11:49:00 +000092 // Constructs a full name in dotted notation from gathered names.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 const AstString* MakeNameFromStack();
Steve Blocka7e24c12009-10-30 11:49:00 +000094
95 // A helper function for MakeNameFromStack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 const AstString* MakeNameFromStackHelper(int pos,
97 const AstString* prev);
Steve Blocka7e24c12009-10-30 11:49:00 +000098
99 // Performs name inferring for added functions.
100 void InferFunctionsNames();
101
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 AstValueFactory* ast_value_factory_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 ZoneList<int> entries_stack_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000104 ZoneList<Name> names_stack_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 ZoneList<FunctionLiteral*> funcs_to_infer_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 Zone* zone_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000107
108 DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer);
109};
110
111
Steve Blocka7e24c12009-10-30 11:49:00 +0000112} } // namespace v8::internal
113
114#endif // V8_FUNC_NAME_INFERRER_H_