1 //
2 // Copyright (c) 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // FunctionLookup.cpp: Used for storing function calls that have not yet been resolved during
7 // parsing.
8 //
9 
10 #include "compiler/translator/FunctionLookup.h"
11 
12 namespace sh
13 {
14 
15 namespace
16 {
17 
18 const char kFunctionMangledNameSeparator = '(';
19 
20 constexpr const ImmutableString kEmptyName("");
21 
22 }  // anonymous namespace
23 
TFunctionLookup(const ImmutableString & name,const TType * constructorType,const TSymbol * symbol)24 TFunctionLookup::TFunctionLookup(const ImmutableString &name,
25                                  const TType *constructorType,
26                                  const TSymbol *symbol)
27     : mName(name), mConstructorType(constructorType), mThisNode(nullptr), mSymbol(symbol)
28 {
29 }
30 
31 // static
CreateConstructor(const TType * type)32 TFunctionLookup *TFunctionLookup::CreateConstructor(const TType *type)
33 {
34     ASSERT(type != nullptr);
35     return new TFunctionLookup(kEmptyName, type, nullptr);
36 }
37 
38 // static
CreateFunctionCall(const ImmutableString & name,const TSymbol * symbol)39 TFunctionLookup *TFunctionLookup::CreateFunctionCall(const ImmutableString &name,
40                                                      const TSymbol *symbol)
41 {
42     ASSERT(name != "");
43     return new TFunctionLookup(name, nullptr, symbol);
44 }
45 
name() const46 const ImmutableString &TFunctionLookup::name() const
47 {
48     return mName;
49 }
50 
getMangledName() const51 ImmutableString TFunctionLookup::getMangledName() const
52 {
53     return GetMangledName(mName.data(), mArguments);
54 }
55 
GetMangledName(const char * functionName,const TIntermSequence & arguments)56 ImmutableString TFunctionLookup::GetMangledName(const char *functionName,
57                                                 const TIntermSequence &arguments)
58 {
59     std::string newName(functionName);
60     newName += kFunctionMangledNameSeparator;
61 
62     for (TIntermNode *argument : arguments)
63     {
64         newName += argument->getAsTyped()->getType().getMangledName();
65     }
66     return ImmutableString(newName);
67 }
68 
isConstructor() const69 bool TFunctionLookup::isConstructor() const
70 {
71     return mConstructorType != nullptr;
72 }
73 
constructorType() const74 const TType &TFunctionLookup::constructorType() const
75 {
76     return *mConstructorType;
77 }
78 
setThisNode(TIntermTyped * thisNode)79 void TFunctionLookup::setThisNode(TIntermTyped *thisNode)
80 {
81     mThisNode = thisNode;
82 }
83 
thisNode() const84 TIntermTyped *TFunctionLookup::thisNode() const
85 {
86     return mThisNode;
87 }
88 
addArgument(TIntermTyped * argument)89 void TFunctionLookup::addArgument(TIntermTyped *argument)
90 {
91     mArguments.push_back(argument);
92 }
93 
arguments()94 TIntermSequence &TFunctionLookup::arguments()
95 {
96     return mArguments;
97 }
98 
symbol() const99 const TSymbol *TFunctionLookup::symbol() const
100 {
101     return mSymbol;
102 }
103 
104 }  // namespace sh
105