clang  3.7.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 "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"
50 };
51 
52 struct Info {
53  const char *Name, *Type, *Attributes, *HeaderName;
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.
82 
83  /// \brief Return the identifier name for the specified builtin,
84  /// e.g. "__builtin_abs".
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
Definition: Builtins.h:66
const char * Attributes
Definition: Builtins.h:53
bool isNoReturn(unsigned ID) const
Return true if we know this builtin never returns.
Definition: Builtins.h:106
bool operator==(const Info &RHS) const
Definition: Builtins.h:56
static const Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:22
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:130
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:123
const char * Name
Definition: Builtins.h:53
void GetBuiltinNames(SmallVectorImpl< const char * > &Names)
Populate the vector with the names of all of the builtins.
Definition: Builtins.cpp:84
LanguageID builtin_lang
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:148
bool isUnevaluated(unsigned ID) const
Returns true if this builtin does not perform the side-effects of its arguments.
Definition: Builtins.h:117
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
bool operator!=(const Info &RHS) const
Definition: Builtins.h:61
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:158
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:96
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:111
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:101
Implements an efficient mapping from strings to IdentifierInfo nodes.
void InitializeTarget(const TargetInfo &Target)
Perform target-specific initialization.
Definition: Builtins.cpp:44
bool hasCustomTypechecking(unsigned ID) const
Determines whether this builtin has custom typechecking.
Definition: Builtins.h:142
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:96
bool isPredefinedRuntimeFunction(unsigned ID) const
Determines whether this builtin is a predefined compiler-rt/libgcc function, such as "__clear_cache"...
Definition: Builtins.h:137
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:176
const char * GetName(unsigned ID) const
Return the identifier name for the specified builtin, e.g. "__builtin_abs".
Definition: Builtins.h:85
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:128
const char * GetTypeString(unsigned ID) const
Get the type descriptor string for the specified builtin.
Definition: Builtins.h:90
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:123
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:69
LanguageID
Definition: Builtins.h:33