Line data Source code
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 :
14 7582309 : FuncNameInferrer::FuncNameInferrer(AstValueFactory* ast_value_factory,
15 : Zone* zone)
16 : : ast_value_factory_(ast_value_factory),
17 : entries_stack_(10, zone),
18 : names_stack_(5, zone),
19 : funcs_to_infer_(4, zone),
20 7582309 : zone_(zone) {
21 7582311 : }
22 :
23 :
24 2531727 : void FuncNameInferrer::PushEnclosingName(const AstRawString* name) {
25 : // Enclosing name is a name of a constructor function. To check
26 : // that it is really a constructor, we check that it is not empty
27 : // and starts with a capital letter.
28 1950852 : if (!name->IsEmpty() && unibrow::Uppercase::Is(name->FirstCharacter())) {
29 580875 : names_stack_.Add(Name(name, kEnclosingConstructorName), zone());
30 : }
31 1950852 : }
32 :
33 :
34 44508300 : void FuncNameInferrer::PushLiteralName(const AstRawString* name) {
35 45929326 : if (IsOpen() && name != ast_value_factory_->prototype_string()) {
36 21543612 : names_stack_.Add(Name(name, kLiteralName), zone());
37 : }
38 22964687 : }
39 :
40 :
41 100895955 : void FuncNameInferrer::PushVariableName(const AstRawString* name) {
42 100895954 : if (IsOpen() && name != ast_value_factory_->dot_result_string()) {
43 50090064 : names_stack_.Add(Name(name, kVariableName), zone());
44 : }
45 50805889 : }
46 :
47 5208 : void FuncNameInferrer::RemoveAsyncKeywordFromEnd() {
48 5208 : if (IsOpen()) {
49 5198 : CHECK_GT(names_stack_.length(), 0);
50 5198 : CHECK(names_stack_.last().name->IsOneByteEqualTo("async"));
51 5198 : names_stack_.RemoveLast();
52 : }
53 5208 : }
54 :
55 7697308 : const AstConsString* FuncNameInferrer::MakeNameFromStack() {
56 7697308 : AstConsString* result = ast_value_factory_->NewConsString();
57 10923777 : for (int pos = 0; pos < names_stack_.length(); pos++) {
58 : // Skip consecutive variable declarations.
59 27562527 : if (pos + 1 < names_stack_.length() &&
60 16410091 : names_stack_.at(pos).type == kVariableName &&
61 4631951 : names_stack_.at(pos + 1).type == kVariableName) {
62 : continue;
63 : }
64 : // Add name. Separate names with ".".
65 3830728 : if (!result->IsEmpty()) {
66 3691050 : result->AddString(zone(), ast_value_factory_->dot_string());
67 : }
68 7661456 : result->AddString(zone(), names_stack_.at(pos).name);
69 : }
70 2021055 : return result;
71 : }
72 :
73 2021055 : void FuncNameInferrer::InferFunctionsNames() {
74 2021055 : const AstConsString* func_name = MakeNameFromStack();
75 8134628 : for (int i = 0; i < funcs_to_infer_.length(); ++i) {
76 8159832 : funcs_to_infer_[i]->set_raw_inferred_name(func_name);
77 : }
78 : funcs_to_infer_.Rewind(0);
79 2021055 : }
80 :
81 :
82 : } // namespace internal
83 : } // namespace v8
|