1 /* 2 * Copyright 2016 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SKSL_FUNCTIONDECLARATION 9 #define SKSL_FUNCTIONDECLARATION 10 11 #include "include/core/SkTypes.h" 12 #include "include/private/SkSLIRNode.h" 13 #include "include/private/SkSLSymbol.h" 14 #include "include/private/base/SkTArray.h" 15 #include "src/sksl/SkSLIntrinsicList.h" 16 17 #include <memory> 18 #include <string> 19 #include <string_view> 20 #include <vector> 21 22 namespace SkSL { 23 24 class Context; 25 class ExpressionArray; 26 class FunctionDefinition; 27 class Position; 28 class SymbolTable; 29 class Type; 30 class Variable; 31 32 struct Modifiers; 33 34 /** 35 * A function declaration (not a definition -- does not contain a body). 36 */ 37 class FunctionDeclaration final : public Symbol { 38 public: 39 inline static constexpr Kind kIRNodeKind = Kind::kFunctionDeclaration; 40 41 FunctionDeclaration(Position pos, 42 const Modifiers* modifiers, 43 std::string_view name, 44 std::vector<Variable*> parameters, 45 const Type* returnType, 46 bool builtin); 47 48 static FunctionDeclaration* Convert(const Context& context, 49 SymbolTable& symbols, 50 Position pos, 51 Position modifiersPos, 52 const Modifiers* modifiers, 53 std::string_view name, 54 std::vector<std::unique_ptr<Variable>> parameters, 55 Position returnTypePos, 56 const Type* returnType); 57 modifiers()58 const Modifiers& modifiers() const { 59 return *fModifiers; 60 } 61 setModifiers(const Modifiers * m)62 void setModifiers(const Modifiers* m) { 63 fModifiers = m; 64 } 65 definition()66 const FunctionDefinition* definition() const { 67 return fDefinition; 68 } 69 setDefinition(const FunctionDefinition * definition)70 void setDefinition(const FunctionDefinition* definition) { 71 fDefinition = definition; 72 fIntrinsicKind = kNotIntrinsic; 73 } 74 setNextOverload(FunctionDeclaration * overload)75 void setNextOverload(FunctionDeclaration* overload) { 76 SkASSERT(!overload || overload->name() == this->name()); 77 fNextOverload = overload; 78 } 79 parameters()80 const std::vector<Variable*>& parameters() const { 81 return fParameters; 82 } 83 returnType()84 const Type& returnType() const { 85 return *fReturnType; 86 } 87 isBuiltin()88 bool isBuiltin() const { 89 return fBuiltin; 90 } 91 isMain()92 bool isMain() const { 93 return fIsMain; 94 } 95 intrinsicKind()96 IntrinsicKind intrinsicKind() const { 97 return fIntrinsicKind; 98 } 99 isIntrinsic()100 bool isIntrinsic() const { 101 return this->intrinsicKind() != kNotIntrinsic; 102 } 103 nextOverload()104 const FunctionDeclaration* nextOverload() const { 105 return fNextOverload; 106 } 107 mutableNextOverload()108 FunctionDeclaration* mutableNextOverload() const { 109 return fNextOverload; 110 } 111 112 std::string mangledName() const; 113 114 std::string description() const override; 115 116 bool matches(const FunctionDeclaration& f) const; 117 118 /** 119 * Determine the effective types of this function's parameters and return value when called with 120 * the given arguments. This is relevant for functions with generic parameter types, where this 121 * will collapse the generic types down into specific concrete types. 122 * 123 * Returns true if it was able to select a concrete set of types for the generic function, false 124 * if there is no possible way this can match the argument types. Note that even a true return 125 * does not guarantee that the function can be successfully called with those arguments, merely 126 * indicates that an attempt should be made. If false is returned, the state of 127 * outParameterTypes and outReturnType are undefined. 128 * 129 * This always assumes narrowing conversions are *allowed*. The calling code needs to verify 130 * that each argument can actually be coerced to the final parameter type, respecting the 131 * narrowing-conversions flag. This is handled in callCost(), or in convertCall() (via coerce). 132 */ 133 using ParamTypes = SkSTArray<8, const Type*>; 134 bool determineFinalTypes(const ExpressionArray& arguments, 135 ParamTypes* outParameterTypes, 136 const Type** outReturnType) const; 137 138 private: 139 const FunctionDefinition* fDefinition; 140 FunctionDeclaration* fNextOverload = nullptr; 141 const Modifiers* fModifiers; 142 std::vector<Variable*> fParameters; 143 const Type* fReturnType; 144 bool fBuiltin; 145 bool fIsMain; 146 mutable IntrinsicKind fIntrinsicKind = kNotIntrinsic; 147 148 using INHERITED = Symbol; 149 }; 150 151 } // namespace SkSL 152 153 #endif 154