• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/transform/renamer.h"
16 
17 #include <memory>
18 #include <unordered_set>
19 #include <utility>
20 
21 #include "src/program_builder.h"
22 #include "src/sem/call.h"
23 #include "src/sem/member_accessor_expression.h"
24 
25 TINT_INSTANTIATE_TYPEINFO(tint::transform::Renamer);
26 TINT_INSTANTIATE_TYPEINFO(tint::transform::Renamer::Data);
27 TINT_INSTANTIATE_TYPEINFO(tint::transform::Renamer::Config);
28 
29 namespace tint {
30 namespace transform {
31 
32 namespace {
33 
34 // This list is used for a binary search and must be kept in sorted order.
35 const char* kReservedKeywordsGLSL[] = {
36     "abs",
37     "acos",
38     "acosh",
39     "active",
40     "all",
41     "any",
42     "asin",
43     "asinh",
44     "asm",
45     "atan",
46     "atanh",
47     "atomicAdd",
48     "atomicAnd",
49     "atomicCompSwap",
50     "atomicCounter",
51     "atomicCounterDecrement",
52     "atomicCounterIncrement",
53     "atomicExchange",
54     "atomicMax",
55     "atomicMin",
56     "atomicOr",
57     "atomicXor",
58     "atomic_uint",
59     "attribute",
60     "barrier",
61     "bitCount",
62     "bitfieldExtract",
63     "bitfieldInsert",
64     "bitfieldReverse",
65     "bool",
66     "break",
67     "buffer",
68     "bvec2",
69     "bvec3",
70     "bvec4",
71     "case",
72     "cast",
73     "ceil",
74     "centroid",
75     "clamp",
76     "class",
77     "coherent",
78     "common",
79     "const",
80     "continue",
81     "cos",
82     "cosh",
83     "cross",
84     "dFdx",
85     "dFdy",
86     "default",
87     "degrees",
88     "determinant",
89     "discard",
90     "distance",
91     "dmat2",
92     "dmat2x2",
93     "dmat2x3",
94     "dmat2x4",
95     "dmat3",
96     "dmat3x2",
97     "dmat3x3",
98     "dmat3x4",
99     "dmat4",
100     "dmat4x2",
101     "dmat4x3",
102     "dmat4x4",
103     "do",
104     "dot",
105     "double",
106     "dvec2",
107     "dvec3",
108     "dvec4",
109     "else",
110     "enum",
111     "equal",
112     "exp",
113     "exp2",
114     "extern",
115     "external",
116     "faceforward",
117     "false",
118     "filter",
119     "findLSB",
120     "findMSB",
121     "fixed",
122     "flat",
123     "float",
124     "floatBitsToInt",
125     "floatBitsToUint",
126     "floor",
127     "for",
128     "fract",
129     "frexp",
130     "fvec2",
131     "fvec3",
132     "fvec4",
133     "fwidth",
134     "gl_BaseInstance",
135     "gl_BaseVertex",
136     "gl_ClipDistance",
137     "gl_DepthRangeParameters",
138     "gl_DrawID",
139     "gl_FragCoord",
140     "gl_FragDepth",
141     "gl_FrontFacing",
142     "gl_GlobalInvocationID",
143     "gl_InstanceID",
144     "gl_LocalInvocationID",
145     "gl_LocalInvocationIndex",
146     "gl_NumSamples",
147     "gl_NumWorkGroups",
148     "gl_PerVertex",
149     "gl_PointCoord",
150     "gl_PointSize",
151     "gl_Position",
152     "gl_PrimitiveID",
153     "gl_SampleID",
154     "gl_SampleMask",
155     "gl_SampleMaskIn",
156     "gl_SamplePosition",
157     "gl_VertexID",
158     "gl_WorkGroupID",
159     "gl_WorkGroupSize",
160     "goto",
161     "greaterThan",
162     "greaterThanEqual",
163     "groupMemoryBarrier",
164     "half",
165     "highp",
166     "hvec2",
167     "hvec3",
168     "hvec4",
169     "if",
170     "iimage1D",
171     "iimage1DArray",
172     "iimage2D",
173     "iimage2DArray",
174     "iimage2DMS",
175     "iimage2DMSArray",
176     "iimage2DRect",
177     "iimage3D",
178     "iimageBuffer",
179     "iimageCube",
180     "iimageCubeArray",
181     "image1D",
182     "image1DArray",
183     "image2D",
184     "image2DArray",
185     "image2DMS",
186     "image2DMSArray",
187     "image2DRect",
188     "image3D",
189     "imageBuffer",
190     "imageCube",
191     "imageCubeArray",
192     "imageLoad",
193     "imageSize",
194     "imageStore",
195     "imulExtended",
196     "in",
197     "inline",
198     "inout",
199     "input",
200     "int",
201     "intBitsToFloat",
202     "interface",
203     "invariant",
204     "inverse",
205     "inversesqrt",
206     "isampler1D",
207     "isampler1DArray",
208     "isampler2D",
209     "isampler2DArray",
210     "isampler2DMS",
211     "isampler2DMSArray",
212     "isampler2DRect",
213     "isampler3D",
214     "isamplerBuffer",
215     "isamplerCube",
216     "isamplerCubeArray",
217     "isinf",
218     "isnan",
219     "ivec2",
220     "ivec3",
221     "ivec4",
222     "layout",
223     "ldexp",
224     "length",
225     "lessThan",
226     "lessThanEqual",
227     "log",
228     "log2",
229     "long",
230     "lowp",
231     "main",
232     "mat2",
233     "mat2x2",
234     "mat2x3",
235     "mat2x4",
236     "mat3",
237     "mat3x2",
238     "mat3x3",
239     "mat3x4",
240     "mat4",
241     "mat4x2",
242     "mat4x3",
243     "mat4x4",
244     "matrixCompMult",
245     "max",
246     "mediump",
247     "memoryBarrier",
248     "memoryBarrierAtomicCounter",
249     "memoryBarrierBuffer",
250     "memoryBarrierImage",
251     "memoryBarrierShared",
252     "min",
253     "mix",
254     "mod",
255     "modf",
256     "namespace",
257     "noinline",
258     "noperspective",
259     "normalize",
260     "not",
261     "notEqual",
262     "out",
263     "outerProduct",
264     "output",
265     "packHalf2x16",
266     "packSnorm2x16",
267     "packSnorm4x8",
268     "packUnorm2x16",
269     "packUnorm4x8",
270     "partition",
271     "patch",
272     "pow",
273     "precise",
274     "precision",
275     "public",
276     "radians",
277     "readonly",
278     "reflect",
279     "refract",
280     "resource",
281     "restrict",
282     "return",
283     "round",
284     "roundEven",
285     "sample",
286     "sampler1D",
287     "sampler1DArray",
288     "sampler1DArrayShadow",
289     "sampler1DShadow",
290     "sampler2D",
291     "sampler2DArray",
292     "sampler2DArrayShadow",
293     "sampler2DMS",
294     "sampler2DMSArray",
295     "sampler2DRect",
296     "sampler2DRectShadow",
297     "sampler2DShadow",
298     "sampler3D",
299     "sampler3DRect",
300     "samplerBuffer",
301     "samplerCube",
302     "samplerCubeArray",
303     "samplerCubeArrayShadow",
304     "samplerCubeShadow",
305     "shared",
306     "short",
307     "sign",
308     "sin",
309     "sinh",
310     "sizeof",
311     "smooth",
312     "smoothstep",
313     "sqrt",
314     "static",
315     "step",
316     "struct",
317     "subroutine",
318     "superp",
319     "switch",
320     "tan",
321     "tanh",
322     "template",
323     "texelFetch",
324     "texelFetchOffset",
325     "texture",
326     "textureGather",
327     "textureGatherOffset",
328     "textureGrad",
329     "textureGradOffset",
330     "textureLod",
331     "textureLodOffset",
332     "textureOffset",
333     "textureProj",
334     "textureProjGrad",
335     "textureProjGradOffset",
336     "textureProjLod",
337     "textureProjLodOffset",
338     "textureProjOffset",
339     "textureSize",
340     "this",
341     "transpose",
342     "true",
343     "trunc",
344     "typedef",
345     "uaddCarry",
346     "uimage1D",
347     "uimage1DArray",
348     "uimage2D",
349     "uimage2DArray",
350     "uimage2DMS",
351     "uimage2DMSArray",
352     "uimage2DRect",
353     "uimage3D",
354     "uimageBuffer",
355     "uimageCube",
356     "uimageCubeArray",
357     "uint",
358     "uintBitsToFloat",
359     "umulExtended",
360     "uniform",
361     "union",
362     "unpackHalf2x16",
363     "unpackSnorm2x16",
364     "unpackSnorm4x8",
365     "unpackUnorm2x16",
366     "unpackUnorm4x8",
367     "unsigned",
368     "usampler1D",
369     "usampler1DArray",
370     "usampler2D",
371     "usampler2DArray",
372     "usampler2DMS",
373     "usampler2DMSArray",
374     "usampler2DRect",
375     "usampler3D",
376     "usamplerBuffer",
377     "usamplerCube",
378     "usamplerCubeArray",
379     "using",
380     "usubBorrow",
381     "uvec2",
382     "uvec3",
383     "uvec4",
384     "varying",
385     "vec2",
386     "vec3",
387     "vec4",
388     "void",
389     "volatile",
390     "while",
391     "writeonly",
392 };
393 
394 // This list is used for a binary search and must be kept in sorted order.
395 const char* kReservedKeywordsHLSL[] = {
396     "AddressU",
397     "AddressV",
398     "AddressW",
399     "AllMemoryBarrier",
400     "AllMemoryBarrierWithGroupSync",
401     "AppendStructuredBuffer",
402     "BINORMAL",
403     "BLENDINDICES",
404     "BLENDWEIGHT",
405     "BlendState",
406     "BorderColor",
407     "Buffer",
408     "ByteAddressBuffer",
409     "COLOR",
410     "CheckAccessFullyMapped",
411     "ComparisonFunc",
412     "CompileShader",
413     "ComputeShader",
414     "ConsumeStructuredBuffer",
415     "D3DCOLORtoUBYTE4",
416     "DEPTH",
417     "DepthStencilState",
418     "DepthStencilView",
419     "DeviceMemoryBarrier",
420     "DeviceMemroyBarrierWithGroupSync",
421     "DomainShader",
422     "EvaluateAttributeAtCentroid",
423     "EvaluateAttributeAtSample",
424     "EvaluateAttributeSnapped",
425     "FOG",
426     "Filter",
427     "GeometryShader",
428     "GetRenderTargetSampleCount",
429     "GetRenderTargetSamplePosition",
430     "GroupMemoryBarrier",
431     "GroupMemroyBarrierWithGroupSync",
432     "Hullshader",
433     "InputPatch",
434     "InterlockedAdd",
435     "InterlockedAnd",
436     "InterlockedCompareExchange",
437     "InterlockedCompareStore",
438     "InterlockedExchange",
439     "InterlockedMax",
440     "InterlockedMin",
441     "InterlockedOr",
442     "InterlockedXor",
443     "LineStream",
444     "MaxAnisotropy",
445     "MaxLOD",
446     "MinLOD",
447     "MipLODBias",
448     "NORMAL",
449     "NULL",
450     "Normal",
451     "OutputPatch",
452     "POSITION",
453     "POSITIONT",
454     "PSIZE",
455     "PixelShader",
456     "PointStream",
457     "Process2DQuadTessFactorsAvg",
458     "Process2DQuadTessFactorsMax",
459     "Process2DQuadTessFactorsMin",
460     "ProcessIsolineTessFactors",
461     "ProcessQuadTessFactorsAvg",
462     "ProcessQuadTessFactorsMax",
463     "ProcessQuadTessFactorsMin",
464     "ProcessTriTessFactorsAvg",
465     "ProcessTriTessFactorsMax",
466     "ProcessTriTessFactorsMin",
467     "RWBuffer",
468     "RWByteAddressBuffer",
469     "RWStructuredBuffer",
470     "RWTexture1D",
471     "RWTexture1DArray",
472     "RWTexture2D",
473     "RWTexture2DArray",
474     "RWTexture3D",
475     "RasterizerState",
476     "RenderTargetView",
477     "SV_ClipDistance",
478     "SV_Coverage",
479     "SV_CullDistance",
480     "SV_Depth",
481     "SV_DepthGreaterEqual",
482     "SV_DepthLessEqual",
483     "SV_DispatchThreadID",
484     "SV_DomainLocation",
485     "SV_GSInstanceID",
486     "SV_GroupID",
487     "SV_GroupIndex",
488     "SV_GroupThreadID",
489     "SV_InnerCoverage",
490     "SV_InsideTessFactor",
491     "SV_InstanceID",
492     "SV_IsFrontFace",
493     "SV_OutputControlPointID",
494     "SV_Position",
495     "SV_PrimitiveID",
496     "SV_RenderTargetArrayIndex",
497     "SV_SampleIndex",
498     "SV_StencilRef",
499     "SV_Target",
500     "SV_TessFactor",
501     "SV_VertexArrayIndex",
502     "SV_VertexID",
503     "Sampler",
504     "Sampler1D",
505     "Sampler2D",
506     "Sampler3D",
507     "SamplerCUBE",
508     "SamplerComparisonState",
509     "SamplerState",
510     "StructuredBuffer",
511     "TANGENT",
512     "TESSFACTOR",
513     "TEXCOORD",
514     "Texcoord",
515     "Texture",
516     "Texture1D",
517     "Texture1DArray",
518     "Texture2D",
519     "Texture2DArray",
520     "Texture2DMS",
521     "Texture2DMSArray",
522     "Texture3D",
523     "TextureCube",
524     "TextureCubeArray",
525     "TriangleStream",
526     "VFACE",
527     "VPOS",
528     "VertexShader",
529     "abort",
530     "allow_uav_condition",
531     "asdouble",
532     "asfloat",
533     "asint",
534     "asm",
535     "asm_fragment",
536     "asuint",
537     "auto",
538     "bool",
539     "bool1",
540     "bool1x1",
541     "bool1x2",
542     "bool1x3",
543     "bool1x4",
544     "bool2",
545     "bool2x1",
546     "bool2x2",
547     "bool2x3",
548     "bool2x4",
549     "bool3",
550     "bool3x1",
551     "bool3x2",
552     "bool3x3",
553     "bool3x4",
554     "bool4",
555     "bool4x1",
556     "bool4x2",
557     "bool4x3",
558     "bool4x4",
559     "branch",
560     "break",
561     "call",
562     "case",
563     "catch",
564     "cbuffer",
565     "centroid",
566     "char",
567     "class",
568     "clip",
569     "column_major",
570     "compile",
571     "compile_fragment",
572     "const",
573     "const_cast",
574     "continue",
575     "countbits",
576     "ddx",
577     "ddx_coarse",
578     "ddx_fine",
579     "ddy",
580     "ddy_coarse",
581     "ddy_fine",
582     "default",
583     "degrees",
584     "delete",
585     "discard",
586     "do",
587     "double",
588     "double1",
589     "double1x1",
590     "double1x2",
591     "double1x3",
592     "double1x4",
593     "double2",
594     "double2x1",
595     "double2x2",
596     "double2x3",
597     "double2x4",
598     "double3",
599     "double3x1",
600     "double3x2",
601     "double3x3",
602     "double3x4",
603     "double4",
604     "double4x1",
605     "double4x2",
606     "double4x3",
607     "double4x4",
608     "dst",
609     "dword",
610     "dword1",
611     "dword1x1",
612     "dword1x2",
613     "dword1x3",
614     "dword1x4",
615     "dword2",
616     "dword2x1",
617     "dword2x2",
618     "dword2x3",
619     "dword2x4",
620     "dword3",
621     "dword3x1",
622     "dword3x2",
623     "dword3x3",
624     "dword3x4",
625     "dword4",
626     "dword4x1",
627     "dword4x2",
628     "dword4x3",
629     "dword4x4",
630     "dynamic_cast",
631     "else",
632     "enum",
633     "errorf",
634     "explicit",
635     "export",
636     "extern",
637     "f16to32",
638     "f32tof16",
639     "false",
640     "fastopt",
641     "firstbithigh",
642     "firstbitlow",
643     "flatten",
644     "float",
645     "float1",
646     "float1x1",
647     "float1x2",
648     "float1x3",
649     "float1x4",
650     "float2",
651     "float2x1",
652     "float2x2",
653     "float2x3",
654     "float2x4",
655     "float3",
656     "float3x1",
657     "float3x2",
658     "float3x3",
659     "float3x4",
660     "float4",
661     "float4x1",
662     "float4x2",
663     "float4x3",
664     "float4x4",
665     "fmod",
666     "for",
667     "forcecase",
668     "frac",
669     "friend",
670     "fxgroup",
671     "goto",
672     "groupshared",
673     "half",
674     "half1",
675     "half1x1",
676     "half1x2",
677     "half1x3",
678     "half1x4",
679     "half2",
680     "half2x1",
681     "half2x2",
682     "half2x3",
683     "half2x4",
684     "half3",
685     "half3x1",
686     "half3x2",
687     "half3x3",
688     "half3x4",
689     "half4",
690     "half4x1",
691     "half4x2",
692     "half4x3",
693     "half4x4",
694     "if",
695     "in",
696     "inline",
697     "inout",
698     "int",
699     "int1",
700     "int1x1",
701     "int1x2",
702     "int1x3",
703     "int1x4",
704     "int2",
705     "int2x1",
706     "int2x2",
707     "int2x3",
708     "int2x4",
709     "int3",
710     "int3x1",
711     "int3x2",
712     "int3x3",
713     "int3x4",
714     "int4",
715     "int4x1",
716     "int4x2",
717     "int4x3",
718     "int4x4",
719     "interface",
720     "isfinite",
721     "isinf",
722     "isnan",
723     "lerp",
724     "line",
725     "lineadj",
726     "linear",
727     "lit",
728     "log10",
729     "long",
730     "loop",
731     "mad",
732     "matrix",
733     "min10float",
734     "min10float1",
735     "min10float1x1",
736     "min10float1x2",
737     "min10float1x3",
738     "min10float1x4",
739     "min10float2",
740     "min10float2x1",
741     "min10float2x2",
742     "min10float2x3",
743     "min10float2x4",
744     "min10float3",
745     "min10float3x1",
746     "min10float3x2",
747     "min10float3x3",
748     "min10float3x4",
749     "min10float4",
750     "min10float4x1",
751     "min10float4x2",
752     "min10float4x3",
753     "min10float4x4",
754     "min12int",
755     "min12int1",
756     "min12int1x1",
757     "min12int1x2",
758     "min12int1x3",
759     "min12int1x4",
760     "min12int2",
761     "min12int2x1",
762     "min12int2x2",
763     "min12int2x3",
764     "min12int2x4",
765     "min12int3",
766     "min12int3x1",
767     "min12int3x2",
768     "min12int3x3",
769     "min12int3x4",
770     "min12int4",
771     "min12int4x1",
772     "min12int4x2",
773     "min12int4x3",
774     "min12int4x4",
775     "min16float",
776     "min16float1",
777     "min16float1x1",
778     "min16float1x2",
779     "min16float1x3",
780     "min16float1x4",
781     "min16float2",
782     "min16float2x1",
783     "min16float2x2",
784     "min16float2x3",
785     "min16float2x4",
786     "min16float3",
787     "min16float3x1",
788     "min16float3x2",
789     "min16float3x3",
790     "min16float3x4",
791     "min16float4",
792     "min16float4x1",
793     "min16float4x2",
794     "min16float4x3",
795     "min16float4x4",
796     "min16int",
797     "min16int1",
798     "min16int1x1",
799     "min16int1x2",
800     "min16int1x3",
801     "min16int1x4",
802     "min16int2",
803     "min16int2x1",
804     "min16int2x2",
805     "min16int2x3",
806     "min16int2x4",
807     "min16int3",
808     "min16int3x1",
809     "min16int3x2",
810     "min16int3x3",
811     "min16int3x4",
812     "min16int4",
813     "min16int4x1",
814     "min16int4x2",
815     "min16int4x3",
816     "min16int4x4",
817     "min16uint",
818     "min16uint1",
819     "min16uint1x1",
820     "min16uint1x2",
821     "min16uint1x3",
822     "min16uint1x4",
823     "min16uint2",
824     "min16uint2x1",
825     "min16uint2x2",
826     "min16uint2x3",
827     "min16uint2x4",
828     "min16uint3",
829     "min16uint3x1",
830     "min16uint3x2",
831     "min16uint3x3",
832     "min16uint3x4",
833     "min16uint4",
834     "min16uint4x1",
835     "min16uint4x2",
836     "min16uint4x3",
837     "min16uint4x4",
838     "msad4",
839     "mul",
840     "mutable",
841     "namespace",
842     "new",
843     "nointerpolation",
844     "noise",
845     "noperspective",
846     "numthreads",
847     "operator",
848     "out",
849     "packoffset",
850     "pass",
851     "pixelfragment",
852     "pixelshader",
853     "point",
854     "precise",
855     "printf",
856     "private",
857     "protected",
858     "public",
859     "radians",
860     "rcp",
861     "refract",
862     "register",
863     "reinterpret_cast",
864     "return",
865     "row_major",
866     "rsqrt",
867     "sample",
868     "sampler",
869     "sampler1D",
870     "sampler2D",
871     "sampler3D",
872     "samplerCUBE",
873     "sampler_state",
874     "saturate",
875     "shared",
876     "short",
877     "signed",
878     "sincos",
879     "sizeof",
880     "snorm",
881     "stateblock",
882     "stateblock_state",
883     "static",
884     "static_cast",
885     "string",
886     "struct",
887     "switch",
888     "tbuffer",
889     "technique",
890     "technique10",
891     "technique11",
892     "template",
893     "tex1D",
894     "tex1Dbias",
895     "tex1Dgrad",
896     "tex1Dlod",
897     "tex1Dproj",
898     "tex2D",
899     "tex2Dbias",
900     "tex2Dgrad",
901     "tex2Dlod",
902     "tex2Dproj",
903     "tex3D",
904     "tex3Dbias",
905     "tex3Dgrad",
906     "tex3Dlod",
907     "tex3Dproj",
908     "texCUBE",
909     "texCUBEbias",
910     "texCUBEgrad",
911     "texCUBElod",
912     "texCUBEproj",
913     "texture",
914     "texture1D",
915     "texture1DArray",
916     "texture2D",
917     "texture2DArray",
918     "texture2DMS",
919     "texture2DMSArray",
920     "texture3D",
921     "textureCube",
922     "textureCubeArray",
923     "this",
924     "throw",
925     "transpose",
926     "triangle",
927     "triangleadj",
928     "true",
929     "try",
930     "typedef",
931     "typename",
932     "uint",
933     "uint1",
934     "uint1x1",
935     "uint1x2",
936     "uint1x3",
937     "uint1x4",
938     "uint2",
939     "uint2x1",
940     "uint2x2",
941     "uint2x3",
942     "uint2x4",
943     "uint3",
944     "uint3x1",
945     "uint3x2",
946     "uint3x3",
947     "uint3x4",
948     "uint4",
949     "uint4x1",
950     "uint4x2",
951     "uint4x3",
952     "uint4x4",
953     "uniform",
954     "union",
955     "unorm",
956     "unroll",
957     "unsigned",
958     "using",
959     "vector",
960     "vertexfragment",
961     "vertexshader",
962     "virtual",
963     "void",
964     "volatile",
965     "while",
966 };
967 
968 // This list is used for a binary search and must be kept in sorted order.
969 const char* kReservedKeywordsMSL[] = {
970     "HUGE_VALF",
971     "HUGE_VALH",
972     "INFINITY",
973     "MAXFLOAT",
974     "MAXHALF",
975     "M_1_PI_F",
976     "M_1_PI_H",
977     "M_2_PI_F",
978     "M_2_PI_H",
979     "M_2_SQRTPI_F",
980     "M_2_SQRTPI_H",
981     "M_E_F",
982     "M_E_H",
983     "M_LN10_F",
984     "M_LN10_H",
985     "M_LN2_F",
986     "M_LN2_H",
987     "M_LOG10E_F",
988     "M_LOG10E_H",
989     "M_LOG2E_F",
990     "M_LOG2E_H",
991     "M_PI_2_F",
992     "M_PI_2_H",
993     "M_PI_4_F",
994     "M_PI_4_H",
995     "M_PI_F",
996     "M_PI_H",
997     "M_SQRT1_2_F",
998     "M_SQRT1_2_H",
999     "M_SQRT2_F",
1000     "M_SQRT2_H",
1001     "NAN",
1002     "access",
1003     "alignas",
1004     "alignof",
1005     "and",
1006     "and_eq",
1007     "array",
1008     "array_ref",
1009     "as_type",
1010     "asm",
1011     "atomic",
1012     "atomic_bool",
1013     "atomic_int",
1014     "atomic_uint",
1015     "auto",
1016     "bitand",
1017     "bitor",
1018     "bool",
1019     "bool2",
1020     "bool3",
1021     "bool4",
1022     "break",
1023     "buffer",
1024     "case",
1025     "catch",
1026     "char",
1027     "char16_t",
1028     "char2",
1029     "char3",
1030     "char32_t",
1031     "char4",
1032     "class",
1033     "compl",
1034     "const",
1035     "const_cast",
1036     "const_reference",
1037     "constant",
1038     "constexpr",
1039     "continue",
1040     "decltype",
1041     "default",
1042     "delete",
1043     "depth2d",
1044     "depth2d_array",
1045     "depth2d_ms",
1046     "depth2d_ms_array",
1047     "depthcube",
1048     "depthcube_array",
1049     "device",
1050     "discard_fragment",
1051     "do",
1052     "double",
1053     "dynamic_cast",
1054     "else",
1055     "enum",
1056     "explicit",
1057     "extern",
1058     "false",
1059     "final",
1060     "float",
1061     "float2",
1062     "float2x2",
1063     "float2x3",
1064     "float2x4",
1065     "float3",
1066     "float3x2",
1067     "float3x3",
1068     "float3x4",
1069     "float4",
1070     "float4x2",
1071     "float4x3",
1072     "float4x4",
1073     "for",
1074     "fragment",
1075     "friend",
1076     "goto",
1077     "half",
1078     "half2",
1079     "half2x2",
1080     "half2x3",
1081     "half2x4",
1082     "half3",
1083     "half3x2",
1084     "half3x3",
1085     "half3x4",
1086     "half4",
1087     "half4x2",
1088     "half4x3",
1089     "half4x4",
1090     "if",
1091     "imageblock",
1092     "infinity",
1093     "inline",
1094     "int",
1095     "int16_t",
1096     "int2",
1097     "int3",
1098     "int32_t",
1099     "int4",
1100     "int64_t",
1101     "int8_t",
1102     "kernel",
1103     "long",
1104     "long2",
1105     "long3",
1106     "long4",
1107     "main",
1108     "matrix",
1109     "metal",
1110     "mutable",
1111     "namespace",
1112     "new",
1113     "noexcept",
1114     "not",
1115     "not_eq",
1116     "nullptr",
1117     "operator",
1118     "or",
1119     "or_eq",
1120     "override",
1121     "packed_bool2",
1122     "packed_bool3",
1123     "packed_bool4",
1124     "packed_char2",
1125     "packed_char3",
1126     "packed_char4",
1127     "packed_float2",
1128     "packed_float3",
1129     "packed_float4",
1130     "packed_half2",
1131     "packed_half3",
1132     "packed_half4",
1133     "packed_int2",
1134     "packed_int3",
1135     "packed_int4",
1136     "packed_short2",
1137     "packed_short3",
1138     "packed_short4",
1139     "packed_uchar2",
1140     "packed_uchar3",
1141     "packed_uchar4",
1142     "packed_uint2",
1143     "packed_uint3",
1144     "packed_uint4",
1145     "packed_ushort2",
1146     "packed_ushort3",
1147     "packed_ushort4",
1148     "patch_control_point",
1149     "private",
1150     "protected",
1151     "ptrdiff_t",
1152     "public",
1153     "r16snorm",
1154     "r16unorm",
1155     "r8unorm",
1156     "reference",
1157     "register",
1158     "reinterpret_cast",
1159     "return",
1160     "rg11b10f",
1161     "rg16snorm",
1162     "rg16unorm",
1163     "rg8snorm",
1164     "rg8unorm",
1165     "rgb10a2",
1166     "rgb9e5",
1167     "rgba16snorm",
1168     "rgba16unorm",
1169     "rgba8snorm",
1170     "rgba8unorm",
1171     "sampler",
1172     "short",
1173     "short2",
1174     "short3",
1175     "short4",
1176     "signed",
1177     "size_t",
1178     "sizeof",
1179     "srgba8unorm",
1180     "static",
1181     "static_assert",
1182     "static_cast",
1183     "struct",
1184     "switch",
1185     "template",
1186     "texture",
1187     "texture1d",
1188     "texture1d_array",
1189     "texture2d",
1190     "texture2d_array",
1191     "texture2d_ms",
1192     "texture2d_ms_array",
1193     "texture3d",
1194     "texture_buffer",
1195     "texturecube",
1196     "texturecube_array",
1197     "this",
1198     "thread",
1199     "thread_local",
1200     "threadgroup",
1201     "threadgroup_imageblock",
1202     "throw",
1203     "true",
1204     "try",
1205     "typedef",
1206     "typeid",
1207     "typename",
1208     "uchar",
1209     "uchar2",
1210     "uchar3",
1211     "uchar4",
1212     "uint",
1213     "uint16_t",
1214     "uint2",
1215     "uint3",
1216     "uint32_t",
1217     "uint4",
1218     "uint64_t",
1219     "uint8_t",
1220     "ulong2",
1221     "ulong3",
1222     "ulong4",
1223     "uniform",
1224     "union",
1225     "unsigned",
1226     "ushort",
1227     "ushort2",
1228     "ushort3",
1229     "ushort4",
1230     "using",
1231     "vec",
1232     "vertex",
1233     "virtual",
1234     "void",
1235     "volatile",
1236     "wchar_t",
1237     "while",
1238     "xor",
1239     "xor_eq",
1240 };
1241 
1242 }  // namespace
1243 
Data(Remappings && r)1244 Renamer::Data::Data(Remappings&& r) : remappings(std::move(r)) {}
1245 Renamer::Data::Data(const Data&) = default;
1246 Renamer::Data::~Data() = default;
1247 
Config(Target t)1248 Renamer::Config::Config(Target t) : target(t) {}
1249 Renamer::Config::Config(const Config&) = default;
1250 Renamer::Config::~Config() = default;
1251 
1252 Renamer::Renamer() = default;
1253 Renamer::~Renamer() = default;
1254 
Run(const Program * in,const DataMap & inputs)1255 Output Renamer::Run(const Program* in, const DataMap& inputs) {
1256   ProgramBuilder out;
1257   // Disable auto-cloning of symbols, since we want to rename them.
1258   CloneContext ctx(&out, in, false);
1259 
1260   // Swizzles, intrinsic calls and builtin structure members need to keep their
1261   // symbols preserved.
1262   std::unordered_set<const ast::IdentifierExpression*> preserve;
1263   for (auto* node : in->ASTNodes().Objects()) {
1264     if (auto* member = node->As<ast::MemberAccessorExpression>()) {
1265       auto* sem = in->Sem().Get(member);
1266       if (!sem) {
1267         TINT_ICE(Transform, out.Diagnostics())
1268             << "MemberAccessorExpression has no semantic info";
1269         continue;
1270       }
1271       if (sem->Is<sem::Swizzle>()) {
1272         preserve.emplace(member->member);
1273       } else if (auto* str_expr = in->Sem().Get(member->structure)) {
1274         if (auto* ty = str_expr->Type()->UnwrapRef()->As<sem::Struct>()) {
1275           if (ty->Declaration() == nullptr) {  // Builtin structure
1276             preserve.emplace(member->member);
1277           }
1278         }
1279       }
1280     } else if (auto* call = node->As<ast::CallExpression>()) {
1281       auto* sem = in->Sem().Get(call);
1282       if (!sem) {
1283         TINT_ICE(Transform, out.Diagnostics())
1284             << "CallExpression has no semantic info";
1285         continue;
1286       }
1287       if (sem->Target()->Is<sem::Intrinsic>()) {
1288         preserve.emplace(call->target.name);
1289       }
1290     }
1291   }
1292 
1293   Data::Remappings remappings;
1294 
1295   Target target = Target::kAll;
1296 
1297   if (auto* cfg = inputs.Get<Config>()) {
1298     target = cfg->target;
1299   }
1300 
1301   ctx.ReplaceAll([&](Symbol sym_in) {
1302     auto name_in = ctx.src->Symbols().NameFor(sym_in);
1303     switch (target) {
1304       case Target::kAll:
1305         // Always rename.
1306         break;
1307       case Target::kGlslKeywords:
1308         if (!std::binary_search(
1309                 kReservedKeywordsGLSL,
1310                 kReservedKeywordsGLSL +
1311                     sizeof(kReservedKeywordsGLSL) / sizeof(const char*),
1312                 name_in) &&
1313             name_in.compare(0, 3, "gl_")) {
1314           // No match, just reuse the original name.
1315           return ctx.dst->Symbols().New(name_in);
1316         }
1317         break;
1318       case Target::kHlslKeywords:
1319         if (!std::binary_search(
1320                 kReservedKeywordsHLSL,
1321                 kReservedKeywordsHLSL +
1322                     sizeof(kReservedKeywordsHLSL) / sizeof(const char*),
1323                 name_in)) {
1324           // No match, just reuse the original name.
1325           return ctx.dst->Symbols().New(name_in);
1326         }
1327         break;
1328       case Target::kMslKeywords:
1329         if (!std::binary_search(
1330                 kReservedKeywordsMSL,
1331                 kReservedKeywordsMSL +
1332                     sizeof(kReservedKeywordsMSL) / sizeof(const char*),
1333                 name_in)) {
1334           // No match, just reuse the original name.
1335           return ctx.dst->Symbols().New(name_in);
1336         }
1337         break;
1338     }
1339 
1340     auto sym_out = ctx.dst->Sym();
1341     remappings.emplace(name_in, ctx.dst->Symbols().NameFor(sym_out));
1342     return sym_out;
1343   });
1344 
1345   ctx.ReplaceAll([&](const ast::IdentifierExpression* ident)
1346                      -> const ast::IdentifierExpression* {
1347     if (preserve.count(ident)) {
1348       auto sym_in = ident->symbol;
1349       auto str = in->Symbols().NameFor(sym_in);
1350       auto sym_out = out.Symbols().Register(str);
1351       return ctx.dst->create<ast::IdentifierExpression>(
1352           ctx.Clone(ident->source), sym_out);
1353     }
1354     return nullptr;  // Clone ident. Uses the symbol remapping above.
1355   });
1356   ctx.Clone();
1357 
1358   return Output(Program(std::move(out)),
1359                 std::make_unique<Data>(std::move(remappings)));
1360 }
1361 
1362 }  // namespace transform
1363 }  // namespace tint
1364