• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/parsing/func-name-inferrer.h"
6 
7 #include "src/ast/ast-value-factory.h"
8 #include "src/ast/ast.h"
9 #include "src/objects-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
FuncNameInferrer(AstValueFactory * ast_value_factory,Zone * zone)14 FuncNameInferrer::FuncNameInferrer(AstValueFactory* ast_value_factory,
15                                    Zone* zone)
16     : ast_value_factory_(ast_value_factory),
17       entries_stack_(zone),
18       names_stack_(zone),
19       funcs_to_infer_(zone),
20       zone_(zone) {}
21 
PushEnclosingName(const AstRawString * name)22 void FuncNameInferrer::PushEnclosingName(const AstRawString* name) {
23   // Enclosing name is a name of a constructor function. To check
24   // that it is really a constructor, we check that it is not empty
25   // and starts with a capital letter.
26   if (!name->IsEmpty() && unibrow::Uppercase::Is(name->FirstCharacter())) {
27     names_stack_.push_back(Name(name, kEnclosingConstructorName));
28   }
29 }
30 
31 
PushLiteralName(const AstRawString * name)32 void FuncNameInferrer::PushLiteralName(const AstRawString* name) {
33   if (IsOpen() && name != ast_value_factory_->prototype_string()) {
34     names_stack_.push_back(Name(name, kLiteralName));
35   }
36 }
37 
38 
PushVariableName(const AstRawString * name)39 void FuncNameInferrer::PushVariableName(const AstRawString* name) {
40   if (IsOpen() && name != ast_value_factory_->dot_result_string()) {
41     names_stack_.push_back(Name(name, kVariableName));
42   }
43 }
44 
RemoveAsyncKeywordFromEnd()45 void FuncNameInferrer::RemoveAsyncKeywordFromEnd() {
46   if (IsOpen()) {
47     CHECK_GT(names_stack_.size(), 0);
48     CHECK(names_stack_.back().name->IsOneByteEqualTo("async"));
49     names_stack_.pop_back();
50   }
51 }
52 
Leave()53 void FuncNameInferrer::Leave() {
54   DCHECK(IsOpen());
55   size_t last_entry = entries_stack_.back();
56   entries_stack_.pop_back();
57   names_stack_.Rewind(last_entry);
58   if (entries_stack_.is_empty()) funcs_to_infer_.Rewind();
59 }
60 
MakeNameFromStack()61 const AstConsString* FuncNameInferrer::MakeNameFromStack() {
62   AstConsString* result = ast_value_factory_->NewConsString();
63   auto it = names_stack_.begin();
64   while (it != names_stack_.end()) {
65     // Advance the iterator to be able to peek the next value.
66     auto current = it++;
67     // Skip consecutive variable declarations.
68     if (it != names_stack_.end() && current->type == kVariableName &&
69         it->type == kVariableName) {
70       continue;
71     }
72     // Add name. Separate names with ".".
73     if (!result->IsEmpty()) {
74       result->AddString(zone(), ast_value_factory_->dot_string());
75     }
76     result->AddString(zone(), current->name);
77   }
78   return result;
79 }
80 
InferFunctionsNames()81 void FuncNameInferrer::InferFunctionsNames() {
82   const AstConsString* func_name = MakeNameFromStack();
83   for (FunctionLiteral* func : funcs_to_infer_) {
84     func->set_raw_inferred_name(func_name);
85   }
86   funcs_to_infer_.Rewind(0);
87 }
88 
89 
90 }  // namespace internal
91 }  // namespace v8
92