• 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 "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     C_LANG = 0x1,     // builtin for c only.
35     CXX_LANG = 0x2,   // builtin for cplusplus only.
36     OBJC_LANG = 0x4,  // builtin for objective-c and objective-c++
37     ALL_LANGUAGES = (C_LANG|CXX_LANG|OBJC_LANG) //builtin is for all languages.
38   };
39 
40 namespace Builtin {
41 enum ID {
42   NotBuiltin  = 0,      // This is not a builtin function.
43 #define BUILTIN(ID, TYPE, ATTRS) BI##ID,
44 #include "clang/Basic/Builtins.def"
45   FirstTSBuiltin
46 };
47 
48 struct Info {
49   const char *Name, *Type, *Attributes, *HeaderName;
50   LanguageID builtin_lang;
51 
52   bool operator==(const Info &RHS) const {
53     return !strcmp(Name, RHS.Name) &&
54            !strcmp(Type, RHS.Type) &&
55            !strcmp(Attributes, RHS.Attributes);
56   }
57   bool operator!=(const Info &RHS) const { return !(*this == RHS); }
58 };
59 
60 /// \brief Holds information about both target-independent and
61 /// target-specific builtins, allowing easy queries by clients.
62 class Context {
63   const Info *TSRecords;
64   unsigned NumTSRecords;
65 public:
66   Context();
67 
68   /// \brief Perform target-specific initialization
69   void InitializeTarget(const TargetInfo &Target);
70 
71   /// \brief Mark the identifiers for all the builtins with their
72   /// appropriate builtin ID # and mark any non-portable builtin identifiers as
73   /// such.
74   void InitializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
75 
76   /// \brief Popular the vector with the names of all of the builtins.
77   void GetBuiltinNames(SmallVectorImpl<const char *> &Names,
78                        bool NoBuiltins);
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 has no side effects and doesn't
92   /// read memory.
isConst(unsigned ID)93   bool isConst(unsigned ID) const {
94     return strchr(GetRecord(ID).Attributes, 'c') != 0;
95   }
96 
97   /// \brief Return true if we know this builtin never throws an exception.
isNoThrow(unsigned ID)98   bool isNoThrow(unsigned ID) const {
99     return strchr(GetRecord(ID).Attributes, 'n') != 0;
100   }
101 
102   /// \brief Return true if we know this builtin never returns.
isNoReturn(unsigned ID)103   bool isNoReturn(unsigned ID) const {
104     return strchr(GetRecord(ID).Attributes, 'r') != 0;
105   }
106 
107   /// \brief Return true if we know this builtin can return twice.
isReturnsTwice(unsigned ID)108   bool isReturnsTwice(unsigned ID) const {
109     return strchr(GetRecord(ID).Attributes, 'j') != 0;
110   }
111 
112   /// \brief Return true if this is a builtin for a libc/libm function,
113   /// with a "__builtin_" prefix (e.g. __builtin_abs).
isLibFunction(unsigned ID)114   bool isLibFunction(unsigned ID) const {
115     return strchr(GetRecord(ID).Attributes, 'F') != 0;
116   }
117 
118   /// \brief Determines whether this builtin is a predefined libc/libm
119   /// function, such as "malloc", where we know the signature a
120   /// priori.
isPredefinedLibFunction(unsigned ID)121   bool isPredefinedLibFunction(unsigned ID) const {
122     return strchr(GetRecord(ID).Attributes, 'f') != 0;
123   }
124 
125   /// \brief Determines whether this builtin has custom typechecking.
hasCustomTypechecking(unsigned ID)126   bool hasCustomTypechecking(unsigned ID) const {
127     return strchr(GetRecord(ID).Attributes, 't') != 0;
128   }
129 
130   /// \brief Completely forget that the given ID was ever considered a builtin,
131   /// e.g., because the user provided a conflicting signature.
132   void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
133 
134   /// \brief If this is a library function that comes from a specific
135   /// header, retrieve that header name.
getHeaderName(unsigned ID)136   const char *getHeaderName(unsigned ID) const {
137     return GetRecord(ID).HeaderName;
138   }
139 
140   /// \brief Determine whether this builtin is like printf in its
141   /// formatting rules and, if so, set the index to the format string
142   /// argument and whether this function as a va_list argument.
143   bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
144 
145   /// \brief Determine whether this builtin is like scanf in its
146   /// formatting rules and, if so, set the index to the format string
147   /// argument and whether this function as a va_list argument.
148   bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
149 
150   /// \brief Return true if this function has no side effects and doesn't
151   /// read memory, except for possibly errno.
152   ///
153   /// Such functions can be const when the MathErrno lang option is disabled.
isConstWithoutErrno(unsigned ID)154   bool isConstWithoutErrno(unsigned ID) const {
155     return strchr(GetRecord(ID).Attributes, 'e') != 0;
156   }
157 
158 private:
159   const Info &GetRecord(unsigned ID) const;
160 };
161 
162 }
163 } // end namespace clang
164 #endif
165