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 "clang/Basic/LLVM.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 builtin_lang;
55 
56   bool operator==(const Info &RHS) const {
57     return !strcmp(Name, RHS.Name) &&
58            !strcmp(Type, RHS.Type) &&
59            !strcmp(Attributes, RHS.Attributes);
60   }
61   bool operator!=(const Info &RHS) const { return !(*this == RHS); }
62 };
63 
64 /// \brief Holds information about both target-independent and
65 /// target-specific builtins, allowing easy queries by clients.
66 class Context {
67   const Info *TSRecords;
68   unsigned NumTSRecords;
69 public:
70   Context();
71 
72   /// \brief Perform target-specific initialization
73   void InitializeTarget(const TargetInfo &Target);
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 Populate the vector with the names of all of the builtins.
81   void GetBuiltinNames(SmallVectorImpl<const char *> &Names);
82 
83   /// \brief Return the identifier name for the specified builtin,
84   /// e.g. "__builtin_abs".
GetName(unsigned ID)85   const char *GetName(unsigned ID) const {
86     return GetRecord(ID).Name;
87   }
88 
89   /// \brief Get the type descriptor string for the specified builtin.
GetTypeString(unsigned ID)90   const char *GetTypeString(unsigned ID) const {
91     return GetRecord(ID).Type;
92   }
93 
94   /// \brief Return true if this function has no side effects and doesn't
95   /// read memory.
isConst(unsigned ID)96   bool isConst(unsigned ID) const {
97     return strchr(GetRecord(ID).Attributes, 'c') != nullptr;
98   }
99 
100   /// \brief Return true if we know this builtin never throws an exception.
isNoThrow(unsigned ID)101   bool isNoThrow(unsigned ID) const {
102     return strchr(GetRecord(ID).Attributes, 'n') != nullptr;
103   }
104 
105   /// \brief Return true if we know this builtin never returns.
isNoReturn(unsigned ID)106   bool isNoReturn(unsigned ID) const {
107     return strchr(GetRecord(ID).Attributes, 'r') != nullptr;
108   }
109 
110   /// \brief Return true if we know this builtin can return twice.
isReturnsTwice(unsigned ID)111   bool isReturnsTwice(unsigned ID) const {
112     return strchr(GetRecord(ID).Attributes, 'j') != nullptr;
113   }
114 
115   /// \brief Returns true if this builtin does not perform the side-effects
116   /// of its arguments.
isUnevaluated(unsigned ID)117   bool isUnevaluated(unsigned ID) const {
118     return strchr(GetRecord(ID).Attributes, 'u') != nullptr;
119   }
120 
121   /// \brief Return true if this is a builtin for a libc/libm function,
122   /// with a "__builtin_" prefix (e.g. __builtin_abs).
isLibFunction(unsigned ID)123   bool isLibFunction(unsigned ID) const {
124     return strchr(GetRecord(ID).Attributes, 'F') != nullptr;
125   }
126 
127   /// \brief Determines whether this builtin is a predefined libc/libm
128   /// function, such as "malloc", where we know the signature a
129   /// priori.
isPredefinedLibFunction(unsigned ID)130   bool isPredefinedLibFunction(unsigned ID) const {
131     return strchr(GetRecord(ID).Attributes, 'f') != nullptr;
132   }
133 
134   /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
135   /// function, such as "__clear_cache", where we know the signature a
136   /// priori.
isPredefinedRuntimeFunction(unsigned ID)137   bool isPredefinedRuntimeFunction(unsigned ID) const {
138     return strchr(GetRecord(ID).Attributes, 'i') != nullptr;
139   }
140 
141   /// \brief Determines whether this builtin has custom typechecking.
hasCustomTypechecking(unsigned ID)142   bool hasCustomTypechecking(unsigned ID) const {
143     return strchr(GetRecord(ID).Attributes, 't') != nullptr;
144   }
145 
146   /// \brief Determines whether this builtin has a result or any arguments which
147   /// are pointer types.
hasPtrArgsOrResult(unsigned ID)148   bool hasPtrArgsOrResult(unsigned ID) const {
149     return strchr(GetRecord(ID).Type, '*') != nullptr;
150   }
151 
152   /// \brief Completely forget that the given ID was ever considered a builtin,
153   /// e.g., because the user provided a conflicting signature.
154   void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
155 
156   /// \brief If this is a library function that comes from a specific
157   /// header, retrieve that header name.
getHeaderName(unsigned ID)158   const char *getHeaderName(unsigned ID) const {
159     return GetRecord(ID).HeaderName;
160   }
161 
162   /// \brief Determine whether this builtin is like printf in its
163   /// formatting rules and, if so, set the index to the format string
164   /// argument and whether this function as a va_list argument.
165   bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
166 
167   /// \brief Determine whether this builtin is like scanf in its
168   /// formatting rules and, if so, set the index to the format string
169   /// argument and whether this function as a va_list argument.
170   bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
171 
172   /// \brief Return true if this function has no side effects and doesn't
173   /// read memory, except for possibly errno.
174   ///
175   /// Such functions can be const when the MathErrno lang option is disabled.
isConstWithoutErrno(unsigned ID)176   bool isConstWithoutErrno(unsigned ID) const {
177     return strchr(GetRecord(ID).Attributes, 'e') != nullptr;
178   }
179 
180 private:
181   const Info &GetRecord(unsigned ID) const;
182 
183   /// \brief Is this builtin supported according to the given language options?
184   bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
185                           const LangOptions &LangOpts);
186 
187   /// \brief Helper function for isPrintfLike and isScanfLike.
188   bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
189               const char *Fmt) const;
190 };
191 
192 }
193 } // end namespace clang
194 #endif
195