1//===--- Builtins.h - Builtin function header -------------------*- 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 enum values for all the target-independent builtin
11/// functions.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_BASIC_BUILTINS_H
16#define LLVM_CLANG_BASIC_BUILTINS_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include <cstring>
20
21// VC++ defines 'alloca' as an object-like macro, which interferes with our
22// builtins.
23#undef alloca
24
25namespace clang {
26class TargetInfo;
27class IdentifierTable;
28class LangOptions;
29
30enum LanguageID {
31  GNU_LANG = 0x1,     // builtin requires GNU mode.
32  C_LANG = 0x2,       // builtin for c only.
33  CXX_LANG = 0x4,     // builtin for cplusplus only.
34  OBJC_LANG = 0x8,    // builtin for objective-c and objective-c++
35  MS_LANG = 0x10,     // builtin requires MS mode.
36  OCLC20_LANG = 0x20, // builtin for OpenCL C 2.0 only.
37  OCLC1X_LANG = 0x40, // builtin for OpenCL C 1.x only.
38  OMP_LANG = 0x80,    // builtin requires OpenMP.
39  CUDA_LANG = 0x100,  // builtin requires CUDA.
40  ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
41  ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG,  // builtin requires GNU mode.
42  ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG,    // builtin requires MS mode.
43  ALL_OCLC_LANGUAGES = OCLC1X_LANG | OCLC20_LANG // builtin for OCLC languages.
44};
45
46namespace Builtin {
47enum ID {
48  NotBuiltin  = 0,      // This is not a builtin function.
49#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
50#include "clang/Basic/Builtins.def"
51  FirstTSBuiltin
52};
53
54struct Info {
55  const char *Name, *Type, *Attributes, *HeaderName;
56  LanguageID Langs;
57  const char *Features;
58};
59
60/// Holds information about both target-independent and
61/// target-specific builtins, allowing easy queries by clients.
62///
63/// Builtins from an optional auxiliary target are stored in
64/// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
65/// be translated back with getAuxBuiltinID() before use.
66class Context {
67  llvm::ArrayRef<Info> TSRecords;
68  llvm::ArrayRef<Info> AuxTSRecords;
69
70public:
71  Context() {}
72
73  /// Perform target-specific initialization
74  /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
75  void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
76
77  /// Mark the identifiers for all the builtins with their
78  /// appropriate builtin ID # and mark any non-portable builtin identifiers as
79  /// such.
80  void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
81
82  /// Return the identifier name for the specified builtin,
83  /// e.g. "__builtin_abs".
84  const char *getName(unsigned ID) const {
85    return getRecord(ID).Name;
86  }
87
88  /// Get the type descriptor string for the specified builtin.
89  const char *getTypeString(unsigned ID) const {
90    return getRecord(ID).Type;
91  }
92
93  /// Return true if this function is a target-specific builtin.
94  bool isTSBuiltin(unsigned ID) const {
95    return ID >= Builtin::FirstTSBuiltin;
96  }
97
98  /// Return true if this function has no side effects.
99  bool isPure(unsigned ID) const {
100    return strchr(getRecord(ID).Attributes, 'U') != nullptr;
101  }
102
103  /// Return true if this function has no side effects and doesn't
104  /// read memory.
105  bool isConst(unsigned ID) const {
106    return strchr(getRecord(ID).Attributes, 'c') != nullptr;
107  }
108
109  /// Return true if we know this builtin never throws an exception.
110  bool isNoThrow(unsigned ID) const {
111    return strchr(getRecord(ID).Attributes, 'n') != nullptr;
112  }
113
114  /// Return true if we know this builtin never returns.
115  bool isNoReturn(unsigned ID) const {
116    return strchr(getRecord(ID).Attributes, 'r') != nullptr;
117  }
118
119  /// Return true if we know this builtin can return twice.
120  bool isReturnsTwice(unsigned ID) const {
121    return strchr(getRecord(ID).Attributes, 'j') != nullptr;
122  }
123
124  /// Returns true if this builtin does not perform the side-effects
125  /// of its arguments.
126  bool isUnevaluated(unsigned ID) const {
127    return strchr(getRecord(ID).Attributes, 'u') != nullptr;
128  }
129
130  /// Return true if this is a builtin for a libc/libm function,
131  /// with a "__builtin_" prefix (e.g. __builtin_abs).
132  bool isLibFunction(unsigned ID) const {
133    return strchr(getRecord(ID).Attributes, 'F') != nullptr;
134  }
135
136  /// Determines whether this builtin is a predefined libc/libm
137  /// function, such as "malloc", where we know the signature a
138  /// priori.
139  bool isPredefinedLibFunction(unsigned ID) const {
140    return strchr(getRecord(ID).Attributes, 'f') != nullptr;
141  }
142
143  /// Returns true if this builtin requires appropriate header in other
144  /// compilers. In Clang it will work even without including it, but we can emit
145  /// a warning about missing header.
146  bool isHeaderDependentFunction(unsigned ID) const {
147    return strchr(getRecord(ID).Attributes, 'h') != nullptr;
148  }
149
150  /// Determines whether this builtin is a predefined compiler-rt/libgcc
151  /// function, such as "__clear_cache", where we know the signature a
152  /// priori.
153  bool isPredefinedRuntimeFunction(unsigned ID) const {
154    return strchr(getRecord(ID).Attributes, 'i') != nullptr;
155  }
156
157  /// Determines whether this builtin has custom typechecking.
158  bool hasCustomTypechecking(unsigned ID) const {
159    return strchr(getRecord(ID).Attributes, 't') != nullptr;
160  }
161
162  /// Determines whether a declaration of this builtin should be recognized
163  /// even if the type doesn't match the specified signature.
164  bool allowTypeMismatch(unsigned ID) const {
165    return strchr(getRecord(ID).Attributes, 'T') != nullptr ||
166           hasCustomTypechecking(ID);
167  }
168
169  /// Determines whether this builtin has a result or any arguments which
170  /// are pointer types.
171  bool hasPtrArgsOrResult(unsigned ID) const {
172    return strchr(getRecord(ID).Type, '*') != nullptr;
173  }
174
175  /// Return true if this builtin has a result or any arguments which are
176  /// reference types.
177  bool hasReferenceArgsOrResult(unsigned ID) const {
178    return strchr(getRecord(ID).Type, '&') != nullptr ||
179           strchr(getRecord(ID).Type, 'A') != nullptr;
180  }
181
182  /// If this is a library function that comes from a specific
183  /// header, retrieve that header name.
184  const char *getHeaderName(unsigned ID) const {
185    return getRecord(ID).HeaderName;
186  }
187
188  /// Determine whether this builtin is like printf in its
189  /// formatting rules and, if so, set the index to the format string
190  /// argument and whether this function as a va_list argument.
191  bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
192
193  /// Determine whether this builtin is like scanf in its
194  /// formatting rules and, if so, set the index to the format string
195  /// argument and whether this function as a va_list argument.
196  bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
197
198  /// Determine whether this builtin has callback behavior (see
199  /// llvm::AbstractCallSites for details). If so, add the index to the
200  /// callback callee argument and the callback payload arguments.
201  bool performsCallback(unsigned ID,
202                        llvm::SmallVectorImpl<int> &Encoding) const;
203
204  /// Return true if this function has no side effects and doesn't
205  /// read memory, except for possibly errno.
206  ///
207  /// Such functions can be const when the MathErrno lang option is disabled.
208  bool isConstWithoutErrno(unsigned ID) const {
209    return strchr(getRecord(ID).Attributes, 'e') != nullptr;
210  }
211
212  const char *getRequiredFeatures(unsigned ID) const {
213    return getRecord(ID).Features;
214  }
215
216  unsigned getRequiredVectorWidth(unsigned ID) const;
217
218  /// Return true if builtin ID belongs to AuxTarget.
219  bool isAuxBuiltinID(unsigned ID) const {
220    return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
221  }
222
223  /// Return real builtin ID (i.e. ID it would have during compilation
224  /// for AuxTarget).
225  unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
226
227  /// Returns true if this is a libc/libm function without the '__builtin_'
228  /// prefix.
229  static bool isBuiltinFunc(llvm::StringRef Name);
230
231  /// Returns true if this is a builtin that can be redeclared.  Returns true
232  /// for non-builtins.
233  bool canBeRedeclared(unsigned ID) const;
234
235private:
236  const Info &getRecord(unsigned ID) const;
237
238  /// Is this builtin supported according to the given language options?
239  bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
240                          const LangOptions &LangOpts);
241
242  /// Helper function for isPrintfLike and isScanfLike.
243  bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
244              const char *Fmt) const;
245};
246
247}
248
249/// Kinds of BuiltinTemplateDecl.
250enum BuiltinTemplateKind : int {
251  /// This names the __make_integer_seq BuiltinTemplateDecl.
252  BTK__make_integer_seq,
253
254  /// This names the __type_pack_element BuiltinTemplateDecl.
255  BTK__type_pack_element
256};
257
258} // end namespace clang
259#endif
260