• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
49   FirstTSBuiltin
50 };
51 
52 struct Info {
53   const char *Name, *Type, *Attributes, *HeaderName;
54   LanguageID Langs;
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:
Context()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".
getName(unsigned ID)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.
getTypeString(unsigned ID)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
isTSBuiltin(unsigned ID)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.
isConst(unsigned ID)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.
isNoThrow(unsigned ID)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.
isNoReturn(unsigned ID)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.
isReturnsTwice(unsigned ID)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.
isUnevaluated(unsigned ID)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).
isLibFunction(unsigned ID)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.
isPredefinedLibFunction(unsigned ID)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.
isPredefinedRuntimeFunction(unsigned ID)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.
hasCustomTypechecking(unsigned ID)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.
hasPtrArgsOrResult(unsigned ID)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.
getHeaderName(unsigned ID)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.
isConstWithoutErrno(unsigned ID)178   bool isConstWithoutErrno(unsigned ID) const {
179     return strchr(getRecord(ID).Attributes, 'e') != nullptr;
180   }
181 
getRequiredFeatures(unsigned ID)182   const char *getRequiredFeatures(unsigned ID) const {
183     return getRecord(ID).Features;
184   }
185 
186   /// \brief Return true if builtin ID belongs to AuxTarget.
isAuxBuiltinID(unsigned ID)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).
getAuxBuiltinID(unsigned ID)193   unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
194 
195 private:
196   const Info &getRecord(unsigned ID) const;
197 
198   /// \brief Is this builtin supported according to the given language options?
199   bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
200                           const LangOptions &LangOpts);
201 
202   /// \brief Helper function for isPrintfLike and isScanfLike.
203   bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
204               const char *Fmt) const;
205 };
206 
207 }
208 
209 /// \brief Kinds of BuiltinTemplateDecl.
210 enum BuiltinTemplateKind : int {
211   /// \brief This names the __make_integer_seq BuiltinTemplateDecl.
212   BTK__make_integer_seq
213 };
214 
215 } // end namespace clang
216 #endif
217