1 //===--- Specifiers.h - Declaration and Type Specifiers ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Defines various enumerations that describe declaration and
11 /// type specifiers.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_BASIC_SPECIFIERS_H
16 #define LLVM_CLANG_BASIC_SPECIFIERS_H
17 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/ErrorHandling.h"
21 
22 namespace clang {
23 
24   /// Define the meaning of possible values of the kind in ExplicitSpecifier.
25   enum class ExplicitSpecKind : unsigned {
26     ResolvedFalse,
27     ResolvedTrue,
28     Unresolved,
29   };
30 
31   /// Define the kind of constexpr specifier.
32   enum ConstexprSpecKind {
33     CSK_unspecified,
34     CSK_constexpr,
35     CSK_consteval,
36     CSK_constinit
37   };
38 
39   /// Specifies the width of a type, e.g., short, long, or long long.
40   enum TypeSpecifierWidth {
41     TSW_unspecified,
42     TSW_short,
43     TSW_long,
44     TSW_longlong
45   };
46 
47   /// Specifies the signedness of a type, e.g., signed or unsigned.
48   enum TypeSpecifierSign {
49     TSS_unspecified,
50     TSS_signed,
51     TSS_unsigned
52   };
53 
54   enum TypeSpecifiersPipe {
55     TSP_unspecified,
56     TSP_pipe
57   };
58 
59   /// Specifies the kind of type.
60   enum TypeSpecifierType {
61     TST_unspecified,
62     TST_void,
63     TST_char,
64     TST_wchar,        // C++ wchar_t
65     TST_char8,        // C++20 char8_t (proposed)
66     TST_char16,       // C++11 char16_t
67     TST_char32,       // C++11 char32_t
68     TST_int,
69     TST_int128,
70     TST_extint,       // Extended Int types.
71     TST_half,         // OpenCL half, ARM NEON __fp16
72     TST_Float16,      // C11 extension ISO/IEC TS 18661-3
73     TST_Accum,        // ISO/IEC JTC1 SC22 WG14 N1169 Extension
74     TST_Fract,
75     TST_BFloat16,
76     TST_float,
77     TST_double,
78     TST_float128,
79     TST_bool,         // _Bool
80     TST_decimal32,    // _Decimal32
81     TST_decimal64,    // _Decimal64
82     TST_decimal128,   // _Decimal128
83     TST_enum,
84     TST_union,
85     TST_struct,
86     TST_class,        // C++ class type
87     TST_interface,    // C++ (Microsoft-specific) __interface type
88     TST_typename,     // Typedef, C++ class-name or enum name, etc.
89     TST_typeofType,
90     TST_typeofExpr,
91     TST_decltype,         // C++11 decltype
92     TST_underlyingType,   // __underlying_type for C++11
93     TST_auto,             // C++11 auto
94     TST_decltype_auto,    // C++1y decltype(auto)
95     TST_auto_type,        // __auto_type extension
96     TST_unknown_anytype,  // __unknown_anytype extension
97     TST_atomic,           // C11 _Atomic
98 #define GENERIC_IMAGE_TYPE(ImgType, Id) TST_##ImgType##_t, // OpenCL image types
99 #include "clang/Basic/OpenCLImageTypes.def"
100     TST_error // erroneous type
101   };
102 
103   /// Structure that packs information about the type specifiers that
104   /// were written in a particular type specifier sequence.
105   struct WrittenBuiltinSpecs {
106     static_assert(TST_error < 1 << 6, "Type bitfield not wide enough for TST");
107     /*DeclSpec::TST*/ unsigned Type  : 6;
108     /*DeclSpec::TSS*/ unsigned Sign  : 2;
109     /*DeclSpec::TSW*/ unsigned Width : 2;
110     unsigned ModeAttr : 1;
111   };
112 
113   /// A C++ access specifier (public, private, protected), plus the
114   /// special value "none" which means different things in different contexts.
115   enum AccessSpecifier {
116     AS_public,
117     AS_protected,
118     AS_private,
119     AS_none
120   };
121 
122   /// The categorization of expression values, currently following the
123   /// C++11 scheme.
124   enum ExprValueKind {
125     /// An r-value expression (a pr-value in the C++11 taxonomy)
126     /// produces a temporary value.
127     VK_RValue,
128 
129     /// An l-value expression is a reference to an object with
130     /// independent storage.
131     VK_LValue,
132 
133     /// An x-value expression is a reference to an object with
134     /// independent storage but which can be "moved", i.e.
135     /// efficiently cannibalized for its resources.
136     VK_XValue
137   };
138 
139   /// A further classification of the kind of object referenced by an
140   /// l-value or x-value.
141   enum ExprObjectKind {
142     /// An ordinary object is located at an address in memory.
143     OK_Ordinary,
144 
145     /// A bitfield object is a bitfield on a C or C++ record.
146     OK_BitField,
147 
148     /// A vector component is an element or range of elements on a vector.
149     OK_VectorComponent,
150 
151     /// An Objective-C property is a logical field of an Objective-C
152     /// object which is read and written via Objective-C method calls.
153     OK_ObjCProperty,
154 
155     /// An Objective-C array/dictionary subscripting which reads an
156     /// object or writes at the subscripted array/dictionary element via
157     /// Objective-C method calls.
158     OK_ObjCSubscript,
159 
160     /// A matrix component is a single element of a matrix.
161     OK_MatrixComponent
162   };
163 
164   /// The reason why a DeclRefExpr does not constitute an odr-use.
165   enum NonOdrUseReason {
166     /// This is an odr-use.
167     NOUR_None = 0,
168     /// This name appears in an unevaluated operand.
169     NOUR_Unevaluated,
170     /// This name appears as a potential result of an lvalue-to-rvalue
171     /// conversion that is a constant expression.
172     NOUR_Constant,
173     /// This name appears as a potential result of a discarded value
174     /// expression.
175     NOUR_Discarded,
176   };
177 
178   /// Describes the kind of template specialization that a
179   /// particular template specialization declaration represents.
180   enum TemplateSpecializationKind {
181     /// This template specialization was formed from a template-id but
182     /// has not yet been declared, defined, or instantiated.
183     TSK_Undeclared = 0,
184     /// This template specialization was implicitly instantiated from a
185     /// template. (C++ [temp.inst]).
186     TSK_ImplicitInstantiation,
187     /// This template specialization was declared or defined by an
188     /// explicit specialization (C++ [temp.expl.spec]) or partial
189     /// specialization (C++ [temp.class.spec]).
190     TSK_ExplicitSpecialization,
191     /// This template specialization was instantiated from a template
192     /// due to an explicit instantiation declaration request
193     /// (C++11 [temp.explicit]).
194     TSK_ExplicitInstantiationDeclaration,
195     /// This template specialization was instantiated from a template
196     /// due to an explicit instantiation definition request
197     /// (C++ [temp.explicit]).
198     TSK_ExplicitInstantiationDefinition
199   };
200 
201   /// Determine whether this template specialization kind refers
202   /// to an instantiation of an entity (as opposed to a non-template or
203   /// an explicit specialization).
isTemplateInstantiation(TemplateSpecializationKind Kind)204   inline bool isTemplateInstantiation(TemplateSpecializationKind Kind) {
205     return Kind != TSK_Undeclared && Kind != TSK_ExplicitSpecialization;
206   }
207 
208   /// True if this template specialization kind is an explicit
209   /// specialization, explicit instantiation declaration, or explicit
210   /// instantiation definition.
isTemplateExplicitInstantiationOrSpecialization(TemplateSpecializationKind Kind)211   inline bool isTemplateExplicitInstantiationOrSpecialization(
212       TemplateSpecializationKind Kind) {
213     switch (Kind) {
214     case TSK_ExplicitSpecialization:
215     case TSK_ExplicitInstantiationDeclaration:
216     case TSK_ExplicitInstantiationDefinition:
217       return true;
218 
219     case TSK_Undeclared:
220     case TSK_ImplicitInstantiation:
221       return false;
222     }
223     llvm_unreachable("bad template specialization kind");
224   }
225 
226   /// Thread storage-class-specifier.
227   enum ThreadStorageClassSpecifier {
228     TSCS_unspecified,
229     /// GNU __thread.
230     TSCS___thread,
231     /// C++11 thread_local. Implies 'static' at block scope, but not at
232     /// class scope.
233     TSCS_thread_local,
234     /// C11 _Thread_local. Must be combined with either 'static' or 'extern'
235     /// if used at block scope.
236     TSCS__Thread_local
237   };
238 
239   /// Storage classes.
240   enum StorageClass {
241     // These are legal on both functions and variables.
242     SC_None,
243     SC_Extern,
244     SC_Static,
245     SC_PrivateExtern,
246 
247     // These are only legal on variables.
248     SC_Auto,
249     SC_Register
250   };
251 
252   /// Checks whether the given storage class is legal for functions.
isLegalForFunction(StorageClass SC)253   inline bool isLegalForFunction(StorageClass SC) {
254     return SC <= SC_PrivateExtern;
255   }
256 
257   /// Checks whether the given storage class is legal for variables.
isLegalForVariable(StorageClass SC)258   inline bool isLegalForVariable(StorageClass SC) {
259     return true;
260   }
261 
262   /// In-class initialization styles for non-static data members.
263   enum InClassInitStyle {
264     ICIS_NoInit,   ///< No in-class initializer.
265     ICIS_CopyInit, ///< Copy initialization.
266     ICIS_ListInit  ///< Direct list-initialization.
267   };
268 
269   /// CallingConv - Specifies the calling convention that a function uses.
270   enum CallingConv {
271     CC_C,           // __attribute__((cdecl))
272     CC_X86StdCall,  // __attribute__((stdcall))
273     CC_X86FastCall, // __attribute__((fastcall))
274     CC_X86ThisCall, // __attribute__((thiscall))
275     CC_X86VectorCall, // __attribute__((vectorcall))
276     CC_X86Pascal,   // __attribute__((pascal))
277     CC_Win64,       // __attribute__((ms_abi))
278     CC_X86_64SysV,  // __attribute__((sysv_abi))
279     CC_X86RegCall, // __attribute__((regcall))
280     CC_AAPCS,       // __attribute__((pcs("aapcs")))
281     CC_AAPCS_VFP,   // __attribute__((pcs("aapcs-vfp")))
282     CC_IntelOclBicc, // __attribute__((intel_ocl_bicc))
283     CC_SpirFunction, // default for OpenCL functions on SPIR target
284     CC_OpenCLKernel, // inferred for OpenCL kernels
285     CC_Swift,        // __attribute__((swiftcall))
286     CC_PreserveMost, // __attribute__((preserve_most))
287     CC_PreserveAll,  // __attribute__((preserve_all))
288     CC_AArch64VectorCall, // __attribute__((aarch64_vector_pcs))
289   };
290 
291   /// Checks whether the given calling convention supports variadic
292   /// calls. Unprototyped calls also use the variadic call rules.
supportsVariadicCall(CallingConv CC)293   inline bool supportsVariadicCall(CallingConv CC) {
294     switch (CC) {
295     case CC_X86StdCall:
296     case CC_X86FastCall:
297     case CC_X86ThisCall:
298     case CC_X86RegCall:
299     case CC_X86Pascal:
300     case CC_X86VectorCall:
301     case CC_SpirFunction:
302     case CC_OpenCLKernel:
303     case CC_Swift:
304       return false;
305     default:
306       return true;
307     }
308   }
309 
310   /// The storage duration for an object (per C++ [basic.stc]).
311   enum StorageDuration {
312     SD_FullExpression, ///< Full-expression storage duration (for temporaries).
313     SD_Automatic,      ///< Automatic storage duration (most local variables).
314     SD_Thread,         ///< Thread storage duration.
315     SD_Static,         ///< Static storage duration.
316     SD_Dynamic         ///< Dynamic storage duration.
317   };
318 
319   /// Describes the nullability of a particular type.
320   enum class NullabilityKind : uint8_t {
321     /// Values of this type can never be null.
322     NonNull = 0,
323     /// Values of this type can be null.
324     Nullable,
325     /// Whether values of this type can be null is (explicitly)
326     /// unspecified. This captures a (fairly rare) case where we
327     /// can't conclude anything about the nullability of the type even
328     /// though it has been considered.
329     Unspecified
330   };
331 
332   /// Return true if \p L has a weaker nullability annotation than \p R. The
333   /// ordering is: Unspecified < Nullable < NonNull.
hasWeakerNullability(NullabilityKind L,NullabilityKind R)334   inline bool hasWeakerNullability(NullabilityKind L, NullabilityKind R) {
335     return uint8_t(L) > uint8_t(R);
336   }
337 
338   /// Retrieve the spelling of the given nullability kind.
339   llvm::StringRef getNullabilitySpelling(NullabilityKind kind,
340                                          bool isContextSensitive = false);
341 
342   /// Kinds of parameter ABI.
343   enum class ParameterABI {
344     /// This parameter uses ordinary ABI rules for its type.
345     Ordinary,
346 
347     /// This parameter (which must have pointer type) is a Swift
348     /// indirect result parameter.
349     SwiftIndirectResult,
350 
351     /// This parameter (which must have pointer-to-pointer type) uses
352     /// the special Swift error-result ABI treatment.  There can be at
353     /// most one parameter on a given function that uses this treatment.
354     SwiftErrorResult,
355 
356     /// This parameter (which must have pointer type) uses the special
357     /// Swift context-pointer ABI treatment.  There can be at
358     /// most one parameter on a given function that uses this treatment.
359     SwiftContext
360   };
361 
362   /// Assigned inheritance model for a class in the MS C++ ABI. Must match order
363   /// of spellings in MSInheritanceAttr.
364   enum class MSInheritanceModel {
365     Single = 0,
366     Multiple = 1,
367     Virtual = 2,
368     Unspecified = 3,
369   };
370 
371   llvm::StringRef getParameterABISpelling(ParameterABI kind);
372 
getAccessSpelling(AccessSpecifier AS)373   inline llvm::StringRef getAccessSpelling(AccessSpecifier AS) {
374     switch (AS) {
375     case AccessSpecifier::AS_public:
376       return "public";
377     case AccessSpecifier::AS_protected:
378       return "protected";
379     case AccessSpecifier::AS_private:
380       return "private";
381     case AccessSpecifier::AS_none:
382       return {};
383     }
384     llvm_unreachable("Unknown AccessSpecifier");
385   }
386 } // end namespace clang
387 
388 #endif // LLVM_CLANG_BASIC_SPECIFIERS_H
389