• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2002 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 
7 #ifndef COMPILER_TRANSLATOR_QUALIFIER_TYPES_H_
8 #define COMPILER_TRANSLATOR_QUALIFIER_TYPES_H_
9 
10 #include "common/angleutils.h"
11 #include "compiler/translator/BaseTypes.h"
12 #include "compiler/translator/ImmutableString.h"
13 #include "compiler/translator/Types.h"
14 
15 namespace sh
16 {
17 class TDiagnostics;
18 
19 TLayoutQualifier JoinLayoutQualifiers(TLayoutQualifier leftQualifier,
20                                       TLayoutQualifier rightQualifier,
21                                       const TSourceLoc &rightQualifierLocation,
22                                       TDiagnostics *diagnostics);
23 
24 enum TQualifierType
25 {
26     QtInvariant,
27     QtPrecise,
28     QtInterpolation,
29     QtLayout,
30     QtStorage,
31     QtPrecision,
32     QtMemory
33 };
34 
35 class TQualifierWrapperBase : angle::NonCopyable
36 {
37   public:
38     POOL_ALLOCATOR_NEW_DELETE
TQualifierWrapperBase(const TSourceLoc & line)39     TQualifierWrapperBase(const TSourceLoc &line) : mLine(line) {}
~TQualifierWrapperBase()40     virtual ~TQualifierWrapperBase() {}
41     virtual TQualifierType getType() const             = 0;
42     virtual ImmutableString getQualifierString() const = 0;
43     virtual unsigned int getRank() const               = 0;
getLine()44     const TSourceLoc &getLine() const { return mLine; }
45 
46   private:
47     TSourceLoc mLine;
48 };
49 
50 class TInvariantQualifierWrapper final : public TQualifierWrapperBase
51 {
52   public:
TInvariantQualifierWrapper(const TSourceLoc & line)53     TInvariantQualifierWrapper(const TSourceLoc &line) : TQualifierWrapperBase(line) {}
~TInvariantQualifierWrapper()54     ~TInvariantQualifierWrapper() override {}
55 
getType()56     TQualifierType getType() const override { return QtInvariant; }
getQualifierString()57     ImmutableString getQualifierString() const override { return ImmutableString("invariant"); }
58     unsigned int getRank() const override;
59 };
60 
61 class TPreciseQualifierWrapper final : public TQualifierWrapperBase
62 {
63   public:
TPreciseQualifierWrapper(const TSourceLoc & line)64     TPreciseQualifierWrapper(const TSourceLoc &line) : TQualifierWrapperBase(line) {}
~TPreciseQualifierWrapper()65     ~TPreciseQualifierWrapper() override {}
66 
getType()67     TQualifierType getType() const override { return QtPrecise; }
getQualifierString()68     ImmutableString getQualifierString() const override { return ImmutableString("precise"); }
69     unsigned int getRank() const override;
70 };
71 
72 class TInterpolationQualifierWrapper final : public TQualifierWrapperBase
73 {
74   public:
TInterpolationQualifierWrapper(TQualifier interpolationQualifier,const TSourceLoc & line)75     TInterpolationQualifierWrapper(TQualifier interpolationQualifier, const TSourceLoc &line)
76         : TQualifierWrapperBase(line), mInterpolationQualifier(interpolationQualifier)
77     {}
~TInterpolationQualifierWrapper()78     ~TInterpolationQualifierWrapper() override {}
79 
getType()80     TQualifierType getType() const override { return QtInterpolation; }
getQualifierString()81     ImmutableString getQualifierString() const override
82     {
83         return ImmutableString(sh::getQualifierString(mInterpolationQualifier));
84     }
getQualifier()85     TQualifier getQualifier() const { return mInterpolationQualifier; }
86     unsigned int getRank() const override;
87 
88   private:
89     TQualifier mInterpolationQualifier;
90 };
91 
92 class TLayoutQualifierWrapper final : public TQualifierWrapperBase
93 {
94   public:
TLayoutQualifierWrapper(TLayoutQualifier layoutQualifier,const TSourceLoc & line)95     TLayoutQualifierWrapper(TLayoutQualifier layoutQualifier, const TSourceLoc &line)
96         : TQualifierWrapperBase(line), mLayoutQualifier(layoutQualifier)
97     {}
~TLayoutQualifierWrapper()98     ~TLayoutQualifierWrapper() override {}
99 
getType()100     TQualifierType getType() const override { return QtLayout; }
getQualifierString()101     ImmutableString getQualifierString() const override { return ImmutableString("layout"); }
getQualifier()102     const TLayoutQualifier &getQualifier() const { return mLayoutQualifier; }
103     unsigned int getRank() const override;
104 
105   private:
106     TLayoutQualifier mLayoutQualifier;
107 };
108 
109 class TStorageQualifierWrapper final : public TQualifierWrapperBase
110 {
111   public:
TStorageQualifierWrapper(TQualifier storageQualifier,const TSourceLoc & line)112     TStorageQualifierWrapper(TQualifier storageQualifier, const TSourceLoc &line)
113         : TQualifierWrapperBase(line), mStorageQualifier(storageQualifier)
114     {}
~TStorageQualifierWrapper()115     ~TStorageQualifierWrapper() override {}
116 
getType()117     TQualifierType getType() const override { return QtStorage; }
getQualifierString()118     ImmutableString getQualifierString() const override
119     {
120         return ImmutableString(sh::getQualifierString(mStorageQualifier));
121     }
getQualifier()122     TQualifier getQualifier() const { return mStorageQualifier; }
123     unsigned int getRank() const override;
124 
125   private:
126     TQualifier mStorageQualifier;
127 };
128 
129 class TPrecisionQualifierWrapper final : public TQualifierWrapperBase
130 {
131   public:
TPrecisionQualifierWrapper(TPrecision precisionQualifier,const TSourceLoc & line)132     TPrecisionQualifierWrapper(TPrecision precisionQualifier, const TSourceLoc &line)
133         : TQualifierWrapperBase(line), mPrecisionQualifier(precisionQualifier)
134     {}
~TPrecisionQualifierWrapper()135     ~TPrecisionQualifierWrapper() override {}
136 
getType()137     TQualifierType getType() const override { return QtPrecision; }
getQualifierString()138     ImmutableString getQualifierString() const override
139     {
140         return ImmutableString(sh::getPrecisionString(mPrecisionQualifier));
141     }
getQualifier()142     TPrecision getQualifier() const { return mPrecisionQualifier; }
143     unsigned int getRank() const override;
144 
145   private:
146     TPrecision mPrecisionQualifier;
147 };
148 
149 class TMemoryQualifierWrapper final : public TQualifierWrapperBase
150 {
151   public:
TMemoryQualifierWrapper(TQualifier memoryQualifier,const TSourceLoc & line)152     TMemoryQualifierWrapper(TQualifier memoryQualifier, const TSourceLoc &line)
153         : TQualifierWrapperBase(line), mMemoryQualifier(memoryQualifier)
154     {}
~TMemoryQualifierWrapper()155     ~TMemoryQualifierWrapper() override {}
156 
getType()157     TQualifierType getType() const override { return QtMemory; }
getQualifierString()158     ImmutableString getQualifierString() const override
159     {
160         return ImmutableString(sh::getQualifierString(mMemoryQualifier));
161     }
getQualifier()162     TQualifier getQualifier() const { return mMemoryQualifier; }
163     unsigned int getRank() const override;
164 
165   private:
166     TQualifier mMemoryQualifier;
167 };
168 
169 // TTypeQualifier tightly covers type_qualifier from the grammar
170 struct TTypeQualifier
171 {
172     // initializes all of the qualifiers and sets the scope
173     TTypeQualifier(TQualifier scope, const TSourceLoc &loc);
174 
175     TLayoutQualifier layoutQualifier;
176     TMemoryQualifier memoryQualifier;
177     TPrecision precision;
178     TQualifier qualifier;
179     bool invariant;
180     bool precise;
181     TSourceLoc line;
182 };
183 
184 // TTypeQualifierBuilder contains all of the qualifiers when type_qualifier gets parsed.
185 // It is to be used to validate the qualifier sequence and build a TTypeQualifier from it.
186 class TTypeQualifierBuilder : angle::NonCopyable
187 {
188   public:
189     using QualifierSequence = TVector<const TQualifierWrapperBase *>;
190 
191   public:
192     POOL_ALLOCATOR_NEW_DELETE
193     TTypeQualifierBuilder(const TStorageQualifierWrapper *scope, int shaderVersion);
194     // Adds the passed qualifier to the end of the sequence.
195     void appendQualifier(const TQualifierWrapperBase *qualifier);
196     // Checks for the order of qualification and repeating qualifiers.
197     bool checkSequenceIsValid(TDiagnostics *diagnostics) const;
198     // Goes over the qualifier sequence and parses it to form a type qualifier for a function
199     // parameter.
200     // The returned object is initialized even if the parsing fails.
201     TTypeQualifier getParameterTypeQualifier(TDiagnostics *diagnostics) const;
202     // Goes over the qualifier sequence and parses it to form a type qualifier for a variable.
203     // The returned object is initialized even if the parsing fails.
204     TTypeQualifier getVariableTypeQualifier(TDiagnostics *diagnostics) const;
205 
206   private:
207     QualifierSequence mQualifiers;
208     int mShaderVersion;
209 };
210 
211 }  // namespace sh
212 
213 #endif  // COMPILER_TRANSLATOR_QUALIFIER_TYPES_H_
214