2009-04-14 00:51:59 +00:00
|
|
|
// Copyright 2006-2009 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
#ifndef V8_FUNC_NAME_INFERRER_H_
|
|
|
|
#define V8_FUNC_NAME_INFERRER_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/handles.h"
|
|
|
|
#include "src/zone.h"
|
2014-03-11 15:40:41 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
class AstRawString;
|
|
|
|
class AstString;
|
|
|
|
class AstValueFactory;
|
2014-03-11 15:40:41 +00:00
|
|
|
class FunctionLiteral;
|
2011-06-22 20:23:48 +00:00
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
// FuncNameInferrer is a stateful class that is used to perform name
|
|
|
|
// inference for anonymous functions during static analysis of source code.
|
|
|
|
// Inference is performed in cases when an anonymous function is assigned
|
|
|
|
// to a variable or a property (see test-func-name-inference.cc for examples.)
|
2009-05-25 08:39:22 +00:00
|
|
|
//
|
2010-08-23 13:26:03 +00:00
|
|
|
// The basic idea is that during parsing of LHSs of certain expressions
|
|
|
|
// (assignments, declarations, object literals) we collect name strings,
|
|
|
|
// and during parsing of the RHS, a function literal can be collected. After
|
|
|
|
// parsing the RHS we can infer a name for function literals that do not have
|
|
|
|
// a name.
|
|
|
|
class FuncNameInferrer : public ZoneObject {
|
2009-04-14 00:51:59 +00:00
|
|
|
public:
|
2014-06-24 14:03:24 +00:00
|
|
|
FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone);
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
// Returns whether we have entered name collection state.
|
2009-04-14 00:51:59 +00:00
|
|
|
bool IsOpen() const { return !entries_stack_.is_empty(); }
|
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
// Pushes an enclosing the name of enclosing function onto names stack.
|
2014-06-24 14:03:24 +00:00
|
|
|
void PushEnclosingName(const AstRawString* name);
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
// Enters name collection state.
|
2009-04-14 00:51:59 +00:00
|
|
|
void Enter() {
|
2012-06-11 12:42:31 +00:00
|
|
|
entries_stack_.Add(names_stack_.length(), zone());
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
// Pushes an encountered name onto names stack when in collection state.
|
2014-06-24 14:03:24 +00:00
|
|
|
void PushLiteralName(const AstRawString* name);
|
2010-08-23 13:26:03 +00:00
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
void PushVariableName(const AstRawString* name);
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
// Adds a function to infer name for.
|
2009-04-16 16:34:24 +00:00
|
|
|
void AddFunction(FunctionLiteral* func_to_infer) {
|
2009-04-14 00:51:59 +00:00
|
|
|
if (IsOpen()) {
|
2012-06-11 12:42:31 +00:00
|
|
|
funcs_to_infer_.Add(func_to_infer, zone());
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-03 19:18:05 +00:00
|
|
|
void RemoveLastFunction() {
|
|
|
|
if (IsOpen() && !funcs_to_infer_.is_empty()) {
|
|
|
|
funcs_to_infer_.RemoveLast();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
// Infers a function name and leaves names collection state.
|
2010-08-23 13:26:03 +00:00
|
|
|
void Infer() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsOpen());
|
2009-04-16 16:34:24 +00:00
|
|
|
if (!funcs_to_infer_.is_empty()) {
|
|
|
|
InferFunctionsNames();
|
|
|
|
}
|
2010-08-23 13:26:03 +00:00
|
|
|
}
|
|
|
|
|
2011-06-22 20:23:48 +00:00
|
|
|
// Leaves names collection state.
|
2010-08-23 13:26:03 +00:00
|
|
|
void Leave() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsOpen());
|
2009-04-16 16:34:24 +00:00
|
|
|
names_stack_.Rewind(entries_stack_.RemoveLast());
|
2012-05-24 12:41:55 +00:00
|
|
|
if (entries_stack_.is_empty())
|
|
|
|
funcs_to_infer_.Clear();
|
2009-04-14 00:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-06-22 20:23:48 +00:00
|
|
|
enum NameType {
|
|
|
|
kEnclosingConstructorName,
|
|
|
|
kLiteralName,
|
|
|
|
kVariableName
|
|
|
|
};
|
|
|
|
struct Name {
|
2014-06-24 14:03:24 +00:00
|
|
|
Name(const AstRawString* name, NameType type) : name(name), type(type) {}
|
|
|
|
const AstRawString* name;
|
2011-06-22 20:23:48 +00:00
|
|
|
NameType type;
|
|
|
|
};
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
Zone* zone() const { return zone_; }
|
2011-06-22 20:23:48 +00:00
|
|
|
|
2009-05-25 08:39:22 +00:00
|
|
|
// Constructs a full name in dotted notation from gathered names.
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstString* MakeNameFromStack();
|
2014-06-18 07:30:56 +00:00
|
|
|
|
|
|
|
// A helper function for MakeNameFromStack.
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstString* MakeNameFromStackHelper(int pos,
|
|
|
|
const AstString* prev);
|
2009-05-25 08:39:22 +00:00
|
|
|
|
|
|
|
// Performs name inferring for added functions.
|
2009-04-16 16:34:24 +00:00
|
|
|
void InferFunctionsNames();
|
2009-04-14 00:51:59 +00:00
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
AstValueFactory* ast_value_factory_;
|
2009-05-18 09:41:16 +00:00
|
|
|
ZoneList<int> entries_stack_;
|
2011-06-22 20:23:48 +00:00
|
|
|
ZoneList<Name> names_stack_;
|
2009-05-18 09:41:16 +00:00
|
|
|
ZoneList<FunctionLiteral*> funcs_to_infer_;
|
2012-06-11 12:42:31 +00:00
|
|
|
Zone* zone_;
|
2009-04-14 00:51:59 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_FUNC_NAME_INFERRER_H_
|