clang  3.8.0
Builtins.h
Go to the documentation of this file.
1 //===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Defines enum values for all the target-independent builtin
12 /// functions.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_BASIC_BUILTINS_H
17 #define LLVM_CLANG_BASIC_BUILTINS_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include <cstring>
21 
22 // VC++ defines 'alloca' as an object-like macro, which interferes with our
23 // builtins.
24 #undef alloca
25 
26 namespace clang {
27 class TargetInfo;
28 class IdentifierTable;
29 class ASTContext;
30 class QualType;
31 class LangOptions;
32 
33 enum LanguageID {
34  GNU_LANG = 0x1, // builtin requires GNU mode.
35  C_LANG = 0x2, // builtin for c only.
36  CXX_LANG = 0x4, // builtin for cplusplus only.
37  OBJC_LANG = 0x8, // builtin for objective-c and objective-c++
38  MS_LANG = 0x10, // builtin requires MS mode.
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 };
43 
44 namespace Builtin {
45 enum ID {
46  NotBuiltin = 0, // This is not a builtin function.
47 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
48 #include "clang/Basic/Builtins.def"
50 };
51 
52 struct Info {
53  const char *Name, *Type, *Attributes, *HeaderName;
55  const char *Features;
56 };
57 
58 /// \brief Holds information about both target-independent and
59 /// target-specific builtins, allowing easy queries by clients.
60 ///
61 /// Builtins from an optional auxiliary target are stored in
62 /// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
63 /// be translated back with getAuxBuiltinID() before use.
64 class Context {
65  llvm::ArrayRef<Info> TSRecords;
66  llvm::ArrayRef<Info> AuxTSRecords;
67 
68 public:
69  Context() {}
70 
71  /// \brief Perform target-specific initialization
72  /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
73  void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
74 
75  /// \brief Mark the identifiers for all the builtins with their
76  /// appropriate builtin ID # and mark any non-portable builtin identifiers as
77  /// such.
78  void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
79 
80  /// \brief Return the identifier name for the specified builtin,
81  /// e.g. "__builtin_abs".
82  const char *getName(unsigned ID) const {
83  return getRecord(ID).Name;
84  }
85 
86  /// \brief Get the type descriptor string for the specified builtin.
87  const char *getTypeString(unsigned ID) const {
88  return getRecord(ID).Type;
89  }
90 
91  /// \brief Return true if this function is a target-specific builtin
92  bool isTSBuiltin(unsigned ID) const {
93  return ID >= Builtin::FirstTSBuiltin;
94  }
95 
96  /// \brief Return true if this function has no side effects and doesn't
97  /// read memory.
98  bool isConst(unsigned ID) const {
99  return strchr(getRecord(ID).Attributes, 'c') != nullptr;
100  }
101 
102  /// \brief Return true if we know this builtin never throws an exception.
103  bool isNoThrow(unsigned ID) const {
104  return strchr(getRecord(ID).Attributes, 'n') != nullptr;
105  }
106 
107  /// \brief Return true if we know this builtin never returns.
108  bool isNoReturn(unsigned ID) const {
109  return strchr(getRecord(ID).Attributes, 'r') != nullptr;
110  }
111 
112  /// \brief Return true if we know this builtin can return twice.
113  bool isReturnsTwice(unsigned ID) const {
114  return strchr(getRecord(ID).Attributes, 'j') != nullptr;
115  }
116 
117  /// \brief Returns true if this builtin does not perform the side-effects
118  /// of its arguments.
119  bool isUnevaluated(unsigned ID) const {
120  return strchr(getRecord(ID).Attributes, 'u') != nullptr;
121  }
122 
123  /// \brief Return true if this is a builtin for a libc/libm function,
124  /// with a "__builtin_" prefix (e.g. __builtin_abs).
125  bool isLibFunction(unsigned ID) const {
126  return strchr(getRecord(ID).Attributes, 'F') != nullptr;
127  }
128 
129  /// \brief Determines whether this builtin is a predefined libc/libm
130  /// function, such as "malloc", where we know the signature a
131  /// priori.
132  bool isPredefinedLibFunction(unsigned ID) const {
133  return strchr(getRecord(ID).Attributes, 'f') != nullptr;
134  }
135 
136  /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
137  /// function, such as "__clear_cache", where we know the signature a
138  /// priori.
139  bool isPredefinedRuntimeFunction(unsigned ID) const {
140  return strchr(getRecord(ID).Attributes, 'i') != nullptr;
141  }
142 
143  /// \brief Determines whether this builtin has custom typechecking.
144  bool hasCustomTypechecking(unsigned ID) const {
145  return strchr(getRecord(ID).Attributes, 't') != nullptr;
146  }
147 
148  /// \brief Determines whether this builtin has a result or any arguments which
149  /// are pointer types.
150  bool hasPtrArgsOrResult(unsigned ID) const {
151  return strchr(getRecord(ID).Type, '*') != nullptr;
152  }
153 
154  /// \brief Completely forget that the given ID was ever considered a builtin,
155  /// e.g., because the user provided a conflicting signature.
156  void forgetBuiltin(unsigned ID, IdentifierTable &Table);
157 
158  /// \brief If this is a library function that comes from a specific
159  /// header, retrieve that header name.
160  const char *getHeaderName(unsigned ID) const {
161  return getRecord(ID).HeaderName;
162  }
163 
164  /// \brief Determine whether this builtin is like printf in its
165  /// formatting rules and, if so, set the index to the format string
166  /// argument and whether this function as a va_list argument.
167  bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
168 
169  /// \brief Determine whether this builtin is like scanf in its
170  /// formatting rules and, if so, set the index to the format string
171  /// argument and whether this function as a va_list argument.
172  bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
173 
174  /// \brief Return true if this function has no side effects and doesn't
175  /// read memory, except for possibly errno.
176  ///
177  /// Such functions can be const when the MathErrno lang option is disabled.
178  bool isConstWithoutErrno(unsigned ID) const {
179  return strchr(getRecord(ID).Attributes, 'e') != nullptr;
180  }
181 
182  const char *getRequiredFeatures(unsigned ID) const {
183  return getRecord(ID).Features;
184  }
185 
186  /// \brief Return true if builtin ID belongs to AuxTarget.
187  bool isAuxBuiltinID(unsigned ID) const {
188  return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
189  }
190 
191  /// Return real buitin ID (i.e. ID it would have furing compilation
192  /// for AuxTarget).
193  unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
194 
195  /// Returns true if this is a libc/libm function without the '__builtin_'
196  /// prefix.
197  static bool isBuiltinFunc(const char *Name);
198 
199 private:
200  const Info &getRecord(unsigned ID) const;
201 
202  /// \brief Is this builtin supported according to the given language options?
203  bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
204  const LangOptions &LangOpts);
205 
206  /// \brief Helper function for isPrintfLike and isScanfLike.
207  bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
208  const char *Fmt) const;
209 };
210 
211 }
212 
213 /// \brief Kinds of BuiltinTemplateDecl.
215  /// \brief This names the __make_integer_seq BuiltinTemplateDecl.
217 };
218 
219 } // end namespace clang
220 #endif
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Definition: Builtins.h:64
const char * Attributes
Definition: Builtins.h:53
bool isNoReturn(unsigned ID) const
Return true if we know this builtin never returns.
Definition: Builtins.h:108
static const Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:21
static bool isBuiltinFunc(const char *Name)
Returns true if this is a libc/libm function without the '__builtin_' prefix.
Definition: Builtins.cpp:51
void forgetBuiltin(unsigned ID, IdentifierTable &Table)
Completely forget that the given ID was ever considered a builtin, e.g., because the user provided a ...
Definition: Builtins.cpp:98
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:132
bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like printf in its formatting rules and, if so, set the index to th...
Definition: Builtins.cpp:125
The base class of the type hierarchy.
Definition: Type.h:1249
const char * Name
Definition: Builtins.h:53
bool isAuxBuiltinID(unsigned ID) const
Return true if builtin ID belongs to AuxTarget.
Definition: Builtins.h:187
bool hasPtrArgsOrResult(unsigned ID) const
Determines whether this builtin has a result or any arguments which are pointer types.
Definition: Builtins.h:150
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4381
bool isUnevaluated(unsigned ID) const
Returns true if this builtin does not perform the side-effects of its arguments.
Definition: Builtins.h:119
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
const char * getTypeString(unsigned ID) const
Get the type descriptor string for the specified builtin.
Definition: Builtins.h:87
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:160
LanguageID Langs
Definition: Builtins.h:54
const char * Type
Definition: Builtins.h:53
bool isReturnsTwice(unsigned ID) const
Return true if we know this builtin can return twice.
Definition: Builtins.h:113
Exposes information about the current target.
bool isNoThrow(unsigned ID) const
Return true if we know this builtin never throws an exception.
Definition: Builtins.h:103
friend class ASTContext
Definition: Type.h:4012
const char * getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Definition: Builtins.h:82
Implements an efficient mapping from strings to IdentifierInfo nodes.
bool hasCustomTypechecking(unsigned ID) const
Determines whether this builtin has custom typechecking.
Definition: Builtins.h:144
const char * HeaderName
Definition: Builtins.h:53
bool isConst(unsigned ID) const
Return true if this function has no side effects and doesn't read memory.
Definition: Builtins.h:98
bool isPredefinedRuntimeFunction(unsigned ID) const
Determines whether this builtin is a predefined compiler-rt/libgcc function, such as "__clear_cache"...
Definition: Builtins.h:139
This names the __make_integer_seq BuiltinTemplateDecl.
Definition: Builtins.h:216
const char * getRequiredFeatures(unsigned ID) const
Definition: Builtins.h:182
bool isConstWithoutErrno(unsigned ID) const
Return true if this function has no side effects and doesn't read memory, except for possibly errno...
Definition: Builtins.h:178
void initializeBuiltins(IdentifierTable &Table, const LangOptions &LangOpts)
Mark the identifiers for all the builtins with their appropriate builtin ID # and mark any non-portab...
Definition: Builtins.cpp:79
unsigned getAuxBuiltinID(unsigned ID) const
Return real buitin ID (i.e.
Definition: Builtins.h:193
BuiltinTemplateKind
Kinds of BuiltinTemplateDecl.
Definition: Builtins.h:214
bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg)
Determine whether this builtin is like scanf in its formatting rules and, if so, set the index to the...
Definition: Builtins.cpp:130
bool isTSBuiltin(unsigned ID) const
Return true if this function is a target-specific builtin.
Definition: Builtins.h:92
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:125
const char * Features
Definition: Builtins.h:55
void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget)
Perform target-specific initialization.
Definition: Builtins.cpp:43
LanguageID
Definition: Builtins.h:33