Home | History | Annotate | Download | only in parsing
      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/list-inl.h"
     10 #include "src/objects-inl.h"
     11 
     12 namespace v8 {
     13 namespace internal {
     14 
     15 FuncNameInferrer::FuncNameInferrer(AstValueFactory* ast_value_factory,
     16                                    Zone* zone)
     17     : ast_value_factory_(ast_value_factory),
     18       entries_stack_(10, zone),
     19       names_stack_(5, zone),
     20       funcs_to_infer_(4, zone),
     21       zone_(zone) {
     22 }
     23 
     24 
     25 void FuncNameInferrer::PushEnclosingName(const AstRawString* name) {
     26   // Enclosing name is a name of a constructor function. To check
     27   // that it is really a constructor, we check that it is not empty
     28   // and starts with a capital letter.
     29   if (!name->IsEmpty() && unibrow::Uppercase::Is(name->FirstCharacter())) {
     30     names_stack_.Add(Name(name, kEnclosingConstructorName), zone());
     31   }
     32 }
     33 
     34 
     35 void FuncNameInferrer::PushLiteralName(const AstRawString* name) {
     36   if (IsOpen() && name != ast_value_factory_->prototype_string()) {
     37     names_stack_.Add(Name(name, kLiteralName), zone());
     38   }
     39 }
     40 
     41 
     42 void FuncNameInferrer::PushVariableName(const AstRawString* name) {
     43   if (IsOpen() && name != ast_value_factory_->dot_result_string()) {
     44     names_stack_.Add(Name(name, kVariableName), zone());
     45   }
     46 }
     47 
     48 void FuncNameInferrer::RemoveAsyncKeywordFromEnd() {
     49   if (IsOpen()) {
     50     CHECK(names_stack_.length() > 0);
     51     CHECK(names_stack_.last().name->IsOneByteEqualTo("async"));
     52     names_stack_.RemoveLast();
     53   }
     54 }
     55 
     56 const AstString* FuncNameInferrer::MakeNameFromStack() {
     57   return MakeNameFromStackHelper(0, ast_value_factory_->empty_string());
     58 }
     59 
     60 const AstString* FuncNameInferrer::MakeNameFromStackHelper(
     61     int pos, const AstString* prev) {
     62   if (pos >= names_stack_.length()) return prev;
     63   if (pos < names_stack_.length() - 1 &&
     64       names_stack_.at(pos).type == kVariableName &&
     65       names_stack_.at(pos + 1).type == kVariableName) {
     66     // Skip consecutive variable declarations.
     67     return MakeNameFromStackHelper(pos + 1, prev);
     68   } else {
     69     if (prev->length() > 0) {
     70       const AstRawString* name = names_stack_.at(pos).name;
     71       if (prev->length() + name->length() + 1 > String::kMaxLength) return prev;
     72       const AstConsString* curr = ast_value_factory_->NewConsString(
     73           ast_value_factory_->dot_string(), name);
     74       curr = ast_value_factory_->NewConsString(prev, curr);
     75       return MakeNameFromStackHelper(pos + 1, curr);
     76     } else {
     77       return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name);
     78     }
     79   }
     80 }
     81 
     82 
     83 void FuncNameInferrer::InferFunctionsNames() {
     84   const AstString* func_name = MakeNameFromStack();
     85   for (int i = 0; i < funcs_to_infer_.length(); ++i) {
     86     funcs_to_infer_[i]->set_raw_inferred_name(func_name);
     87   }
     88   funcs_to_infer_.Rewind(0);
     89 }
     90 
     91 
     92 }  // namespace internal
     93 }  // namespace v8
     94