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