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