func-name-inferrer.h 3.43 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
#ifndef V8_PARSING_FUNC_NAME_INFERRER_H_
#define V8_PARSING_FUNC_NAME_INFERRER_H_
7

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

10 11
namespace v8 {
namespace internal {
12

13
class AstConsString;
14 15
class AstRawString;
class AstValueFactory;
16
class FunctionLiteral;
17

18
enum class InferName { kYes, kNo };
19

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

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  // To enter function name inference state, put a FuncNameInferrer::State
  // on the stack.
  class State {
   public:
    explicit State(FuncNameInferrer* fni) : fni_(fni) {
      if (fni_ != nullptr) fni_->Enter();
    }
    ~State() {
      if (fni_ != nullptr) fni_->Leave();
    }

   private:
    FuncNameInferrer* fni_;

    DISALLOW_COPY_AND_ASSIGN(State);
  };

51
  // Returns whether we have entered name collection state.
52 53
  bool IsOpen() const { return !entries_stack_.is_empty(); }

54
  // Pushes an enclosing the name of enclosing function onto names stack.
55
  void PushEnclosingName(const AstRawString* name);
56

57
  // Pushes an encountered name onto names stack when in collection state.
58
  void PushLiteralName(const AstRawString* name);
59

60
  void PushVariableName(const AstRawString* name);
61

62
  // Adds a function to infer name for.
63
  void AddFunction(FunctionLiteral* func_to_infer) {
64
    if (IsOpen()) {
65
      funcs_to_infer_.Add(func_to_infer, zone());
66 67 68
    }
  }

69 70 71 72 73 74
  void RemoveLastFunction() {
    if (IsOpen() && !funcs_to_infer_.is_empty()) {
      funcs_to_infer_.RemoveLast();
    }
  }

75 76
  void RemoveAsyncKeywordFromEnd();

77
  // Infers a function name and leaves names collection state.
78
  void Infer() {
79
    DCHECK(IsOpen());
80 81 82
    if (!funcs_to_infer_.is_empty()) {
      InferFunctionsNames();
    }
83 84
  }

85
 private:
86 87 88 89 90 91
  enum NameType {
    kEnclosingConstructorName,
    kLiteralName,
    kVariableName
  };
  struct Name {
92 93
    Name(const AstRawString* name, NameType type) : name(name), type(type) {}
    const AstRawString* name;
94 95 96
    NameType type;
  };

97 98 99 100 101 102 103 104
  void Enter() { entries_stack_.Add(names_stack_.length(), zone()); }

  void Leave() {
    DCHECK(IsOpen());
    names_stack_.Rewind(entries_stack_.RemoveLast());
    if (entries_stack_.is_empty()) funcs_to_infer_.Clear();
  }

105
  Zone* zone() const { return zone_; }
106

107
  // Constructs a full name in dotted notation from gathered names.
108
  const AstConsString* MakeNameFromStack();
109 110

  // Performs name inferring for added functions.
111
  void InferFunctionsNames();
112

113
  AstValueFactory* ast_value_factory_;
114
  ZoneList<int> entries_stack_;
115
  ZoneList<Name> names_stack_;
116
  ZoneList<FunctionLiteral*> funcs_to_infer_;
117
  Zone* zone_;
118 119 120 121 122

  DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer);
};


123 124
}  // namespace internal
}  // namespace v8
125

126
#endif  // V8_PARSING_FUNC_NAME_INFERRER_H_