Path: blob/main_old/src/compiler/translator/FunctionLookup.cpp
1693 views
//1// Copyright 2018 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// FunctionLookup.cpp: Used for storing function calls that have not yet been resolved during6// parsing.7//89#include "compiler/translator/FunctionLookup.h"10#include "compiler/translator/ImmutableStringBuilder.h"1112namespace sh13{1415namespace16{1718const char kFunctionMangledNameSeparator = '(';1920constexpr const ImmutableString kEmptyName("");2122// Helper function for GetMangledNames23// Gets all ordered combinations of elements in list[currentIndex, end]24std::vector<std::vector<int>> GetImplicitConversionCombinations(const std::vector<int> &list)25{26std::vector<std::vector<int>> target;27target.push_back(std::vector<int>());2829for (size_t currentIndex = 0; currentIndex < list.size(); currentIndex++)30{31size_t prevIterSize = target.size();32for (size_t copyIndex = 0; copyIndex < prevIterSize; copyIndex++)33{34std::vector<int> combination = target[copyIndex];35combination.push_back(list[currentIndex]);36target.push_back(combination);37}38}3940return target;41}4243} // anonymous namespace4445TFunctionLookup::TFunctionLookup(const ImmutableString &name,46const TType *constructorType,47const TSymbol *symbol)48: mName(name), mConstructorType(constructorType), mThisNode(nullptr), mSymbol(symbol)49{}5051// static52TFunctionLookup *TFunctionLookup::CreateConstructor(const TType *type)53{54ASSERT(type != nullptr);55return new TFunctionLookup(kEmptyName, type, nullptr);56}5758// static59TFunctionLookup *TFunctionLookup::CreateFunctionCall(const ImmutableString &name,60const TSymbol *symbol)61{62ASSERT(name != "");63return new TFunctionLookup(name, nullptr, symbol);64}6566const ImmutableString &TFunctionLookup::name() const67{68return mName;69}7071ImmutableString TFunctionLookup::getMangledName() const72{73return GetMangledName(mName.data(), mArguments);74}7576ImmutableString TFunctionLookup::GetMangledName(const char *functionName,77const TIntermSequence &arguments)78{79std::string newName(functionName);80newName += kFunctionMangledNameSeparator;8182for (TIntermNode *argument : arguments)83{84newName += argument->getAsTyped()->getType().getMangledName();85}86return ImmutableString(newName);87}8889std::vector<ImmutableString> GetMangledNames(const char *functionName,90const TIntermSequence &arguments)91{92std::vector<ImmutableString> target;9394std::vector<int> indexes;95for (int i = 0; i < static_cast<int>(arguments.size()); i++)96{97TIntermNode *argument = arguments[i];98TBasicType argType = argument->getAsTyped()->getType().getBasicType();99if (argType == EbtInt || argType == EbtUInt)100{101indexes.push_back(i);102}103}104105std::vector<std::vector<int>> combinations = GetImplicitConversionCombinations(indexes);106for (const std::vector<int> &combination : combinations)107{108// combination: ordered list of indexes for arguments that should be converted to float109std::string newName(functionName);110newName += kFunctionMangledNameSeparator;111// combination[currentIndex] represents index of next argument to be converted112int currentIndex = 0;113for (int i = 0; i < (int)arguments.size(); i++)114{115TIntermNode *argument = arguments[i];116117if (currentIndex != static_cast<int>(combination.size()) &&118combination[currentIndex] == i)119{120// Convert121TType type = argument->getAsTyped()->getType();122type.setBasicType(EbtFloat);123newName += type.getMangledName();124currentIndex++;125}126else127{128// Don't convert129newName += argument->getAsTyped()->getType().getMangledName();130}131}132target.push_back(ImmutableString(newName));133}134135return target;136}137138std::vector<ImmutableString> TFunctionLookup::getMangledNamesForImplicitConversions() const139{140return GetMangledNames(mName.data(), mArguments);141}142143bool TFunctionLookup::isConstructor() const144{145return mConstructorType != nullptr;146}147148const TType &TFunctionLookup::constructorType() const149{150return *mConstructorType;151}152153void TFunctionLookup::setThisNode(TIntermTyped *thisNode)154{155mThisNode = thisNode;156}157158TIntermTyped *TFunctionLookup::thisNode() const159{160return mThisNode;161}162163void TFunctionLookup::addArgument(TIntermTyped *argument)164{165mArguments.push_back(argument);166}167168TIntermSequence &TFunctionLookup::arguments()169{170return mArguments;171}172173const TSymbol *TFunctionLookup::symbol() const174{175return mSymbol;176}177178} // namespace sh179180181