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/v8.h" 6 7#include "src/ast.h" 8#include "src/ast-value-factory.h" 9#include "src/func-name-inferrer.h" 10#include "src/list-inl.h" 11 12namespace v8 { 13namespace internal { 14 15FuncNameInferrer::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 25void 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 35void 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 42void 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 49const AstString* FuncNameInferrer::MakeNameFromStack() { 50 return MakeNameFromStackHelper(0, ast_value_factory_->empty_string()); 51} 52 53const AstString* FuncNameInferrer::MakeNameFromStackHelper( 54 int pos, const AstString* prev) { 55 if (pos >= names_stack_.length()) return prev; 56 if (pos < names_stack_.length() - 1 && 57 names_stack_.at(pos).type == kVariableName && 58 names_stack_.at(pos + 1).type == kVariableName) { 59 // Skip consecutive variable declarations. 60 return MakeNameFromStackHelper(pos + 1, prev); 61 } else { 62 if (prev->length() > 0) { 63 const AstRawString* name = names_stack_.at(pos).name; 64 if (prev->length() + name->length() + 1 > String::kMaxLength) return prev; 65 const AstConsString* curr = ast_value_factory_->NewConsString( 66 ast_value_factory_->dot_string(), name); 67 curr = ast_value_factory_->NewConsString(prev, curr); 68 return MakeNameFromStackHelper(pos + 1, curr); 69 } else { 70 return MakeNameFromStackHelper(pos + 1, names_stack_.at(pos).name); 71 } 72 } 73} 74 75 76void FuncNameInferrer::InferFunctionsNames() { 77 const AstString* func_name = MakeNameFromStack(); 78 for (int i = 0; i < funcs_to_infer_.length(); ++i) { 79 funcs_to_infer_[i]->set_raw_inferred_name(func_name); 80 } 81 funcs_to_infer_.Rewind(0); 82} 83 84 85} } // namespace v8::internal 86