parseVersions.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Copyright (C) 2015-2018 Google, Inc.
  3. // Copyright (C) 2017 ARM Limited.
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions
  9. // are met:
  10. //
  11. // Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. //
  14. // Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following
  16. // disclaimer in the documentation and/or other materials provided
  17. // with the distribution.
  18. //
  19. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  20. // contributors may be used to endorse or promote products derived
  21. // from this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is implemented in Versions.cpp
  37. #ifndef _PARSE_VERSIONS_INCLUDED_
  38. #define _PARSE_VERSIONS_INCLUDED_
  39. #include "../Public/ShaderLang.h"
  40. #include "../Include/InfoSink.h"
  41. #include "Scan.h"
  42. #include <map>
  43. namespace glslang {
  44. //
  45. // Base class for parse helpers.
  46. // This just has version-related information and checking.
  47. // This class should be sufficient for preprocessing.
  48. //
  49. class TParseVersions {
  50. public:
  51. TParseVersions(TIntermediate& interm, int version, EProfile profile,
  52. const SpvVersion& spvVersion, EShLanguage language, TInfoSink& infoSink,
  53. bool forwardCompatible, EShMessages messages)
  54. :
  55. #if !defined(GLSLANG_WEB) && !defined(GLSLANG_ANGLE)
  56. forwardCompatible(forwardCompatible),
  57. profile(profile),
  58. #endif
  59. infoSink(infoSink), version(version),
  60. language(language),
  61. spvVersion(spvVersion),
  62. intermediate(interm), messages(messages), numErrors(0), currentScanner(0) { }
  63. virtual ~TParseVersions() { }
  64. void requireStage(const TSourceLoc&, EShLanguageMask, const char* featureDesc);
  65. void requireStage(const TSourceLoc&, EShLanguage, const char* featureDesc);
  66. #ifdef GLSLANG_WEB
  67. const EProfile profile = EEsProfile;
  68. bool isEsProfile() const { return true; }
  69. void requireProfile(const TSourceLoc& loc, int profileMask, const char* featureDesc)
  70. {
  71. if (! (EEsProfile & profileMask))
  72. error(loc, "not supported with this profile:", featureDesc, ProfileName(profile));
  73. }
  74. void profileRequires(const TSourceLoc& loc, int profileMask, int minVersion, int numExtensions,
  75. const char* const extensions[], const char* featureDesc)
  76. {
  77. if ((EEsProfile & profileMask) && (minVersion == 0 || version < minVersion))
  78. error(loc, "not supported for this version or the enabled extensions", featureDesc, "");
  79. }
  80. void profileRequires(const TSourceLoc& loc, int profileMask, int minVersion, const char* extension,
  81. const char* featureDesc)
  82. {
  83. profileRequires(loc, profileMask, minVersion, extension ? 1 : 0, &extension, featureDesc);
  84. }
  85. void initializeExtensionBehavior() { }
  86. void checkDeprecated(const TSourceLoc&, int queryProfiles, int depVersion, const char* featureDesc) { }
  87. void requireNotRemoved(const TSourceLoc&, int queryProfiles, int removedVersion, const char* featureDesc) { }
  88. void requireExtensions(const TSourceLoc&, int numExtensions, const char* const extensions[],
  89. const char* featureDesc) { }
  90. void ppRequireExtensions(const TSourceLoc&, int numExtensions, const char* const extensions[],
  91. const char* featureDesc) { }
  92. TExtensionBehavior getExtensionBehavior(const char*) { return EBhMissing; }
  93. bool extensionTurnedOn(const char* const extension) { return false; }
  94. bool extensionsTurnedOn(int numExtensions, const char* const extensions[]) { return false; }
  95. void updateExtensionBehavior(int line, const char* const extension, const char* behavior) { }
  96. void updateExtensionBehavior(const char* const extension, TExtensionBehavior) { }
  97. void checkExtensionStage(const TSourceLoc&, const char* const extension) { }
  98. void extensionRequires(const TSourceLoc&, const char* const extension, const char* behavior) { }
  99. void fullIntegerCheck(const TSourceLoc&, const char* op) { }
  100. void doubleCheck(const TSourceLoc&, const char* op) { }
  101. bool float16Arithmetic() { return false; }
  102. void requireFloat16Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc) { }
  103. bool int16Arithmetic() { return false; }
  104. void requireInt16Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc) { }
  105. bool int8Arithmetic() { return false; }
  106. void requireInt8Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc) { }
  107. void int64Check(const TSourceLoc&, const char* op, bool builtIn = false) { }
  108. void explicitFloat32Check(const TSourceLoc&, const char* op, bool builtIn = false) { }
  109. void explicitFloat64Check(const TSourceLoc&, const char* op, bool builtIn = false) { }
  110. bool relaxedErrors() const { return false; }
  111. bool suppressWarnings() const { return true; }
  112. bool isForwardCompatible() const { return false; }
  113. #else
  114. #ifdef GLSLANG_ANGLE
  115. const bool forwardCompatible = true;
  116. const EProfile profile = ECoreProfile;
  117. #else
  118. bool forwardCompatible; // true if errors are to be given for use of deprecated features
  119. EProfile profile; // the declared profile in the shader (core by default)
  120. #endif
  121. bool isEsProfile() const { return profile == EEsProfile; }
  122. void requireProfile(const TSourceLoc& loc, int profileMask, const char* featureDesc);
  123. void profileRequires(const TSourceLoc& loc, int profileMask, int minVersion, int numExtensions,
  124. const char* const extensions[], const char* featureDesc);
  125. void profileRequires(const TSourceLoc& loc, int profileMask, int minVersion, const char* extension,
  126. const char* featureDesc);
  127. virtual void initializeExtensionBehavior();
  128. virtual void checkDeprecated(const TSourceLoc&, int queryProfiles, int depVersion, const char* featureDesc);
  129. virtual void requireNotRemoved(const TSourceLoc&, int queryProfiles, int removedVersion, const char* featureDesc);
  130. virtual void requireExtensions(const TSourceLoc&, int numExtensions, const char* const extensions[],
  131. const char* featureDesc);
  132. virtual void ppRequireExtensions(const TSourceLoc&, int numExtensions, const char* const extensions[],
  133. const char* featureDesc);
  134. virtual TExtensionBehavior getExtensionBehavior(const char*);
  135. virtual bool extensionTurnedOn(const char* const extension);
  136. virtual bool extensionsTurnedOn(int numExtensions, const char* const extensions[]);
  137. virtual void updateExtensionBehavior(int line, const char* const extension, const char* behavior);
  138. virtual void updateExtensionBehavior(const char* const extension, TExtensionBehavior);
  139. virtual bool checkExtensionsRequested(const TSourceLoc&, int numExtensions, const char* const extensions[],
  140. const char* featureDesc);
  141. virtual void checkExtensionStage(const TSourceLoc&, const char* const extension);
  142. virtual void extensionRequires(const TSourceLoc&, const char* const extension, const char* behavior);
  143. virtual void fullIntegerCheck(const TSourceLoc&, const char* op);
  144. virtual void unimplemented(const TSourceLoc&, const char* featureDesc);
  145. virtual void doubleCheck(const TSourceLoc&, const char* op);
  146. virtual void float16Check(const TSourceLoc&, const char* op, bool builtIn = false);
  147. virtual void float16ScalarVectorCheck(const TSourceLoc&, const char* op, bool builtIn = false);
  148. virtual bool float16Arithmetic();
  149. virtual void requireFloat16Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc);
  150. virtual void int16ScalarVectorCheck(const TSourceLoc&, const char* op, bool builtIn = false);
  151. virtual bool int16Arithmetic();
  152. virtual void requireInt16Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc);
  153. virtual void int8ScalarVectorCheck(const TSourceLoc&, const char* op, bool builtIn = false);
  154. virtual bool int8Arithmetic();
  155. virtual void requireInt8Arithmetic(const TSourceLoc& loc, const char* op, const char* featureDesc);
  156. virtual void float16OpaqueCheck(const TSourceLoc&, const char* op, bool builtIn = false);
  157. virtual void int64Check(const TSourceLoc&, const char* op, bool builtIn = false);
  158. virtual void explicitInt8Check(const TSourceLoc&, const char* op, bool builtIn = false);
  159. virtual void explicitInt16Check(const TSourceLoc&, const char* op, bool builtIn = false);
  160. virtual void explicitInt32Check(const TSourceLoc&, const char* op, bool builtIn = false);
  161. virtual void explicitFloat32Check(const TSourceLoc&, const char* op, bool builtIn = false);
  162. virtual void explicitFloat64Check(const TSourceLoc&, const char* op, bool builtIn = false);
  163. virtual void fcoopmatCheck(const TSourceLoc&, const char* op, bool builtIn = false);
  164. virtual void intcoopmatCheck(const TSourceLoc&, const char *op, bool builtIn = false);
  165. bool relaxedErrors() const { return (messages & EShMsgRelaxedErrors) != 0; }
  166. bool suppressWarnings() const { return (messages & EShMsgSuppressWarnings) != 0; }
  167. bool isForwardCompatible() const { return forwardCompatible; }
  168. #endif // GLSLANG_WEB
  169. virtual void spvRemoved(const TSourceLoc&, const char* op);
  170. virtual void vulkanRemoved(const TSourceLoc&, const char* op);
  171. virtual void requireVulkan(const TSourceLoc&, const char* op);
  172. virtual void requireSpv(const TSourceLoc&, const char* op);
  173. virtual void requireSpv(const TSourceLoc&, const char *op, unsigned int version);
  174. #if defined(GLSLANG_WEB) && !defined(GLSLANG_WEB_DEVEL)
  175. void C_DECL error(const TSourceLoc&, const char* szReason, const char* szToken,
  176. const char* szExtraInfoFormat, ...) { addError(); }
  177. void C_DECL warn(const TSourceLoc&, const char* szReason, const char* szToken,
  178. const char* szExtraInfoFormat, ...) { }
  179. void C_DECL ppError(const TSourceLoc&, const char* szReason, const char* szToken,
  180. const char* szExtraInfoFormat, ...) { addError(); }
  181. void C_DECL ppWarn(const TSourceLoc&, const char* szReason, const char* szToken,
  182. const char* szExtraInfoFormat, ...) { }
  183. #else
  184. virtual void C_DECL error(const TSourceLoc&, const char* szReason, const char* szToken,
  185. const char* szExtraInfoFormat, ...) = 0;
  186. virtual void C_DECL warn(const TSourceLoc&, const char* szReason, const char* szToken,
  187. const char* szExtraInfoFormat, ...) = 0;
  188. virtual void C_DECL ppError(const TSourceLoc&, const char* szReason, const char* szToken,
  189. const char* szExtraInfoFormat, ...) = 0;
  190. virtual void C_DECL ppWarn(const TSourceLoc&, const char* szReason, const char* szToken,
  191. const char* szExtraInfoFormat, ...) = 0;
  192. #endif
  193. void addError() { ++numErrors; }
  194. int getNumErrors() const { return numErrors; }
  195. void setScanner(TInputScanner* scanner) { currentScanner = scanner; }
  196. TInputScanner* getScanner() const { return currentScanner; }
  197. const TSourceLoc& getCurrentLoc() const { return currentScanner->getSourceLoc(); }
  198. void setCurrentLine(int line) { currentScanner->setLine(line); }
  199. void setCurrentColumn(int col) { currentScanner->setColumn(col); }
  200. void setCurrentSourceName(const char* name) { currentScanner->setFile(name); }
  201. void setCurrentString(int string) { currentScanner->setString(string); }
  202. void getPreamble(std::string&);
  203. #ifdef ENABLE_HLSL
  204. bool isReadingHLSL() const { return (messages & EShMsgReadHlsl) == EShMsgReadHlsl; }
  205. bool hlslEnable16BitTypes() const { return (messages & EShMsgHlslEnable16BitTypes) != 0; }
  206. bool hlslDX9Compatible() const { return (messages & EShMsgHlslDX9Compatible) != 0; }
  207. #else
  208. bool isReadingHLSL() const { return false; }
  209. #endif
  210. TInfoSink& infoSink;
  211. // compilation mode
  212. int version; // version, updated by #version in the shader
  213. EShLanguage language; // really the stage
  214. SpvVersion spvVersion;
  215. TIntermediate& intermediate; // helper for making and hooking up pieces of the parse tree
  216. protected:
  217. TMap<TString, TExtensionBehavior> extensionBehavior; // for each extension string, what its current behavior is
  218. TMap<TString, unsigned int> extensionMinSpv; // for each extension string, store minimum spirv required
  219. EShMessages messages; // errors/warnings/rule-sets
  220. int numErrors; // number of compile-time errors encountered
  221. TInputScanner* currentScanner;
  222. private:
  223. explicit TParseVersions(const TParseVersions&);
  224. TParseVersions& operator=(const TParseVersions&);
  225. };
  226. } // end namespace glslang
  227. #endif // _PARSE_VERSIONS_INCLUDED_