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.h"
      8 #include "src/ast/ast-value-factory.h"
      9 #include "src/list-inl.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 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       zone_(zone) {
     21 }
     22 
     23 
     24 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   if (!name->IsEmpty() && unibrow::Uppercase::Is(name->FirstCharacter())) {
     29     names_stack_.Add(Name(name, kEnclosingConstructorName), zone());
     30   }
     31 }
     32 
     33 
     34 void FuncNameInferrer::PushLiteralName(const AstRawString* name) {
     35   if (IsOpen() && name != ast_value_factory_->prototype_string()) {
     36     names_stack_.Add(Name(name, kLiteralName), zone());
     37   }
     38 }
     39 
     40 
     41 void FuncNameInferrer::PushVariableName(const AstRawString* name) {
     42   if (IsOpen() && name != ast_value_factory_->dot_result_string()) {
     43     names_stack_.Add(Name(name, kVariableName), zone());
     44   }
     45 }
     46 
     47 
     48 const AstString* FuncNameInferrer::MakeNameFromStack() {
     49   return MakeNameFromStackHelper(0, ast_value_factory_->empty_string());
     50 }
     51 
     52 const AstString* FuncNameInferrer::MakeNameFromStackHelper(
     53     int pos, const AstString* prev) {
     54   if (pos >= names_stack_.length()) return prev;
     55   if (pos < names_stack_.length() - 1 &&
     56       names_stack_.at(pos).type == kVariableName &&
     57       names_stack_.at(pos + 1).type == kVariableName) {
     58     // Skip consecutive variable declarations.
     59     return MakeNameFromStackHelper(pos + 1, prev);
     60   } else {
     61     if (prev->length() > 0) {
     62       const AstRawString* name = names_stack_.at(pos).name;
     63       if (prev->length() + name->length() + 1 > String::kMaxLength) return prev;
     64       const AstConsString* curr = ast_value_factory_->NewConsString(
     65           ast_value_factory_->dot_string(), name);
     66       curr = ast_value_factory_->NewConsString(prev, curr);
     67       return MakeNameFromStackHelper(pos + 1, curr);
     68     } else {
     69       return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name);
     70     }
     71   }
     72 }
     73 
     74 
     75 void FuncNameInferrer::InferFunctionsNames() {
     76   const AstString* func_name = MakeNameFromStack();
     77   for (int i = 0; i < funcs_to_infer_.length(); ++i) {
     78     funcs_to_infer_[i]->set_raw_inferred_name(func_name);
     79   }
     80   funcs_to_infer_.Rewind(0);
     81 }
     82 
     83 
     84 }  // namespace internal
     85 }  // namespace v8
     86