func-name-inferrer.h 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Copyright 2006-2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_FUNC_NAME_INFERRER_H_
#define V8_FUNC_NAME_INFERRER_H_

31 32 33
#include "handles.h"
#include "zone.h"

34 35
namespace v8 {
namespace internal {
36

37
class FunctionLiteral;
38 39
class Isolate;

40 41 42 43
// 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.)
44
//
45 46 47 48 49 50
// 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 {
51
 public:
52
  FuncNameInferrer(Isolate* isolate, Zone* zone);
53

54
  // Returns whether we have entered name collection state.
55 56
  bool IsOpen() const { return !entries_stack_.is_empty(); }

57
  // Pushes an enclosing the name of enclosing function onto names stack.
58 59
  void PushEnclosingName(Handle<String> name);

60
  // Enters name collection state.
61
  void Enter() {
62
    entries_stack_.Add(names_stack_.length(), zone());
63 64
  }

65
  // Pushes an encountered name onto names stack when in collection state.
66 67 68
  void PushLiteralName(Handle<String> name);

  void PushVariableName(Handle<String> name);
69

70
  // Adds a function to infer name for.
71
  void AddFunction(FunctionLiteral* func_to_infer) {
72
    if (IsOpen()) {
73
      funcs_to_infer_.Add(func_to_infer, zone());
74 75 76
    }
  }

77 78 79 80 81 82
  void RemoveLastFunction() {
    if (IsOpen() && !funcs_to_infer_.is_empty()) {
      funcs_to_infer_.RemoveLast();
    }
  }

83
  // Infers a function name and leaves names collection state.
84
  void Infer() {
85
    ASSERT(IsOpen());
86 87 88
    if (!funcs_to_infer_.is_empty()) {
      InferFunctionsNames();
    }
89 90
  }

91
  // Leaves names collection state.
92 93
  void Leave() {
    ASSERT(IsOpen());
94
    names_stack_.Rewind(entries_stack_.RemoveLast());
95 96
    if (entries_stack_.is_empty())
      funcs_to_infer_.Clear();
97 98 99
  }

 private:
100 101 102 103 104 105 106 107 108 109 110 111
  enum NameType {
    kEnclosingConstructorName,
    kLiteralName,
    kVariableName
  };
  struct Name {
    Name(Handle<String> name, NameType type) : name(name), type(type) { }
    Handle<String> name;
    NameType type;
  };

  Isolate* isolate() { return isolate_; }
112
  Zone* zone() const { return zone_; }
113

114
  // Constructs a full name in dotted notation from gathered names.
115
  Handle<String> MakeNameFromStack();
116 117

  // A helper function for MakeNameFromStack.
118
  Handle<String> MakeNameFromStackHelper(int pos, Handle<String> prev);
119 120

  // Performs name inferring for added functions.
121
  void InferFunctionsNames();
122

123
  Isolate* isolate_;
124
  ZoneList<int> entries_stack_;
125
  ZoneList<Name> names_stack_;
126
  ZoneList<FunctionLiteral*> funcs_to_infer_;
127
  Zone* zone_;
128 129 130 131 132 133 134 135

  DISALLOW_COPY_AND_ASSIGN(FuncNameInferrer);
};


} }  // namespace v8::internal

#endif  // V8_FUNC_NAME_INFERRER_H_