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
26namespace 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
40namespace Builtin {
41enum 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
48struct 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.
62class Context {
63  const Info *TSRecords;
64  unsigned NumTSRecords;
65public:
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".
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 has no side effects and doesn't
92  /// read memory.
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.
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.
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.
108  bool isReturnsTwice(unsigned ID) const {
109    return strchr(GetRecord(ID).Attributes, 'j') != 0;
110  }
111
112  /// \brief Returns true if this builtin does not perform the side-effects
113  /// of its arguments.
114  bool isUnevaluated(unsigned ID) const {
115    return strchr(GetRecord(ID).Attributes, 'u') != 0;
116  }
117
118  /// \brief Return true if this is a builtin for a libc/libm function,
119  /// with a "__builtin_" prefix (e.g. __builtin_abs).
120  bool isLibFunction(unsigned ID) const {
121    return strchr(GetRecord(ID).Attributes, 'F') != 0;
122  }
123
124  /// \brief Determines whether this builtin is a predefined libc/libm
125  /// function, such as "malloc", where we know the signature a
126  /// priori.
127  bool isPredefinedLibFunction(unsigned ID) const {
128    return strchr(GetRecord(ID).Attributes, 'f') != 0;
129  }
130
131  /// \brief Determines whether this builtin has custom typechecking.
132  bool hasCustomTypechecking(unsigned ID) const {
133    return strchr(GetRecord(ID).Attributes, 't') != 0;
134  }
135
136  /// \brief Completely forget that the given ID was ever considered a builtin,
137  /// e.g., because the user provided a conflicting signature.
138  void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
139
140  /// \brief If this is a library function that comes from a specific
141  /// header, retrieve that header name.
142  const char *getHeaderName(unsigned ID) const {
143    return GetRecord(ID).HeaderName;
144  }
145
146  /// \brief Determine whether this builtin is like printf in its
147  /// formatting rules and, if so, set the index to the format string
148  /// argument and whether this function as a va_list argument.
149  bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
150
151  /// \brief Determine whether this builtin is like scanf in its
152  /// formatting rules and, if so, set the index to the format string
153  /// argument and whether this function as a va_list argument.
154  bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
155
156  /// \brief Return true if this function has no side effects and doesn't
157  /// read memory, except for possibly errno.
158  ///
159  /// Such functions can be const when the MathErrno lang option is disabled.
160  bool isConstWithoutErrno(unsigned ID) const {
161    return strchr(GetRecord(ID).Attributes, 'e') != 0;
162  }
163
164private:
165  const Info &GetRecord(unsigned ID) const;
166};
167
168}
169} // end namespace clang
170#endif
171