func-name-inferrer.h 3.22 KB
Newer Older
1
// Copyright 2006-2009 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_FUNC_NAME_INFERRER_H_
#define V8_FUNC_NAME_INFERRER_H_

8 9
#include "src/handles.h"
#include "src/zone.h"
10

11 12
namespace v8 {
namespace internal {
13

14 15 16
class AstRawString;
class AstString;
class AstValueFactory;
17
class FunctionLiteral;
18

19 20 21 22
// 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.)
23
//
24 25 26 27 28 29
// 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 {
30
 public:
31
  FuncNameInferrer(AstValueFactory* ast_value_factory, Zone* zone);
32

33
  // Returns whether we have entered name collection state.
34 35
  bool IsOpen() const { return !entries_stack_.is_empty(); }

36
  // Pushes an enclosing the name of enclosing function onto names stack.
37
  void PushEnclosingName(const AstRawString* name);
38

39
  // Enters name collection state.
40
  void Enter() {
41
    entries_stack_.Add(names_stack_.length(), zone());
42 43
  }

44
  // Pushes an encountered name onto names stack when in collection state.
45
  void PushLiteralName(const AstRawString* name);
46

47
  void PushVariableName(const AstRawString* name);
48

49
  // Adds a function to infer name for.
50
  void AddFunction(FunctionLiteral* func_to_infer) {
51
    if (IsOpen()) {
52
      funcs_to_infer_.Add(func_to_infer, zone());
53 54 55
    }
  }

56 57 58 59 60 61
  void RemoveLastFunction() {
    if (IsOpen() && !funcs_to_infer_.is_empty()) {
      funcs_to_infer_.RemoveLast();
    }
  }

62
  // Infers a function name and leaves names collection state.
63
  void Infer() {
64
    DCHECK(IsOpen());
65 66 67
    if (!funcs_to_infer_.is_empty()) {
      InferFunctionsNames();
    }
68 69
  }

70
  // Leaves names collection state.
71
  void Leave() {
72
    DCHECK(IsOpen());
73
    names_stack_.Rewind(entries_stack_.RemoveLast());
74 75
    if (entries_stack_.is_empty())
      funcs_to_infer_.Clear();
76 77 78
  }

 private:
79 80 81 82 83 84
  enum NameType {
    kEnclosingConstructorName,
    kLiteralName,
    kVariableName
  };
  struct Name {
85 86
    Name(const AstRawString* name, NameType type) : name(name), type(type) {}
    const AstRawString* name;
87 88 89
    NameType type;
  };

90
  Zone* zone() const { return zone_; }
91

92
  // Constructs a full name in dotted notation from gathered names.
93
  const AstString* MakeNameFromStack();
94 95

  // A helper function for MakeNameFromStack.
96 97
  const AstString* MakeNameFromStackHelper(int pos,
                                               const AstString* prev);
98 99

  // Performs name inferring for added functions.
100
  void InferFunctionsNames();
101

102
  AstValueFactory* ast_value_factory_;
103
  ZoneList<int> entries_stack_;
104
  ZoneList<Name> names_stack_;
105
  ZoneList<FunctionLiteral*> funcs_to_infer_;
106
  Zone* zone_;
107 108 109 110 111 112 113 114

  DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer);
};


} }  // namespace v8::internal

#endif  // V8_FUNC_NAME_INFERRER_H_