Builtins.h revision 263508
121923Sdavidn//===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
221923Sdavidn//
321923Sdavidn//                     The LLVM Compiler Infrastructure
421923Sdavidn//
521923Sdavidn// This file is distributed under the University of Illinois Open Source
621923Sdavidn// License. See LICENSE.TXT for details.
721923Sdavidn//
821923Sdavidn//===----------------------------------------------------------------------===//
921923Sdavidn///
1021923Sdavidn/// \file
1121923Sdavidn/// \brief Defines enum values for all the target-independent builtin
1221923Sdavidn/// functions.
1321923Sdavidn///
1421923Sdavidn//===----------------------------------------------------------------------===//
1521923Sdavidn
1621923Sdavidn#ifndef LLVM_CLANG_BASIC_BUILTINS_H
1721923Sdavidn#define LLVM_CLANG_BASIC_BUILTINS_H
1821923Sdavidn
1921923Sdavidn#include "clang/Basic/LLVM.h"
2050477Speter#include <cstring>
2121923Sdavidn
22235211Sgjb// VC++ defines 'alloca' as an object-like macro, which interferes with our
2321923Sdavidn// builtins.
2479535Sru#undef alloca
2521923Sdavidn
2621923Sdavidnnamespace clang {
2727573Scharnier  class TargetInfo;
2821923Sdavidn  class IdentifierTable;
2968963Sru  class ASTContext;
30230549Strociny  class QualType;
3121923Sdavidn  class LangOptions;
3227573Scharnier
33220358Spluknet  enum LanguageID {
3468963Sru    GNU_LANG = 0x1,  // builtin requires GNU mode.
35107267Sru    C_LANG = 0x2,    // builtin for c only.
3621923Sdavidn    CXX_LANG = 0x4,  // builtin for cplusplus only.
37220358Spluknet    OBJC_LANG = 0x8, // builtin for objective-c and objective-c++
3821923Sdavidn    MS_LANG = 0x10,  // builtin requires MS mode.
39107267Sru    ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
4071895Sru    ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG,  // builtin requires GNU mode.
41107267Sru    ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG     // builtin requires MS mode.
42107267Sru  };
4321923Sdavidn
4495124Scharniernamespace Builtin {
4595124Scharnierenum ID {
4695124Scharnier  NotBuiltin  = 0,      // This is not a builtin function.
4721923Sdavidn#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
4821923Sdavidn#include "clang/Basic/Builtins.def"
4921923Sdavidn  FirstTSBuiltin
5021923Sdavidn};
5158609Scharnier
52107267Srustruct Info {
5372477Sru  const char *Name, *Type, *Attributes, *HeaderName;
5472477Sru  LanguageID builtin_lang;
5572477Sru
5668963Sru  bool operator==(const Info &RHS) const {
57107267Sru    return !strcmp(Name, RHS.Name) &&
5821923Sdavidn           !strcmp(Type, RHS.Type) &&
5972477Sru           !strcmp(Attributes, RHS.Attributes);
6021923Sdavidn  }
6121923Sdavidn  bool operator!=(const Info &RHS) const { return !(*this == RHS); }
6221923Sdavidn};
6371895Sru
64107267Sru/// \brief Holds information about both target-independent and
65107267Sru/// target-specific builtins, allowing easy queries by clients.
66107267Sruclass Context {
6721923Sdavidn  const Info *TSRecords;
6821923Sdavidn  unsigned NumTSRecords;
6921923Sdavidnpublic:
7021923Sdavidn  Context();
7121923Sdavidn
7221923Sdavidn  /// \brief Perform target-specific initialization
7321923Sdavidn  void InitializeTarget(const TargetInfo &Target);
7421923Sdavidn
7521923Sdavidn  /// \brief Mark the identifiers for all the builtins with their
7621923Sdavidn  /// appropriate builtin ID # and mark any non-portable builtin identifiers as
77107267Sru  /// such.
7878686Sdd  void InitializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
7921923Sdavidn
8021923Sdavidn  /// \brief Populate the vector with the names of all of the builtins.
8121923Sdavidn  void GetBuiltinNames(SmallVectorImpl<const char *> &Names);
82107267Sru
8321923Sdavidn  /// \brief Return the identifier name for the specified builtin,
8421923Sdavidn  /// e.g. "__builtin_abs".
8521923Sdavidn  const char *GetName(unsigned ID) const {
86107267Sru    return GetRecord(ID).Name;
8721923Sdavidn  }
88107267Sru
8921923Sdavidn  /// \brief Get the type descriptor string for the specified builtin.
9021923Sdavidn  const char *GetTypeString(unsigned ID) const {
9121923Sdavidn    return GetRecord(ID).Type;
92107788Sru  }
93131491Sru
94107267Sru  /// \brief Return true if this function has no side effects and doesn't
95107267Sru  /// read memory.
96107267Sru  bool isConst(unsigned ID) const {
9758609Scharnier    return strchr(GetRecord(ID).Attributes, 'c') != 0;
98107267Sru  }
99107267Sru
100107267Sru  /// \brief Return true if we know this builtin never throws an exception.
101107267Sru  bool isNoThrow(unsigned ID) const {
102107267Sru    return strchr(GetRecord(ID).Attributes, 'n') != 0;
10321923Sdavidn  }
104107267Sru
105107267Sru  /// \brief Return true if we know this builtin never returns.
10621923Sdavidn  bool isNoReturn(unsigned ID) const {
10771895Sru    return strchr(GetRecord(ID).Attributes, 'r') != 0;
10821923Sdavidn  }
10921923Sdavidn
11021923Sdavidn  /// \brief Return true if we know this builtin can return twice.
11121923Sdavidn  bool isReturnsTwice(unsigned ID) const {
11221923Sdavidn    return strchr(GetRecord(ID).Attributes, 'j') != 0;
11321923Sdavidn  }
11421923Sdavidn
11521923Sdavidn  /// \brief Returns true if this builtin does not perform the side-effects
11621923Sdavidn  /// of its arguments.
11758609Scharnier  bool isUnevaluated(unsigned ID) const {
11821923Sdavidn    return strchr(GetRecord(ID).Attributes, 'u') != 0;
11921923Sdavidn  }
120107267Sru
12121923Sdavidn  /// \brief Return true if this is a builtin for a libc/libm function,
12221923Sdavidn  /// with a "__builtin_" prefix (e.g. __builtin_abs).
12358609Scharnier  bool isLibFunction(unsigned ID) const {
12421923Sdavidn    return strchr(GetRecord(ID).Attributes, 'F') != 0;
12521923Sdavidn  }
12621923Sdavidn
12798840Ssheldonh  /// \brief Determines whether this builtin is a predefined libc/libm
12898840Ssheldonh  /// function, such as "malloc", where we know the signature a
12998840Ssheldonh  /// priori.
130107267Sru  bool isPredefinedLibFunction(unsigned ID) const {
13121923Sdavidn    return strchr(GetRecord(ID).Attributes, 'f') != 0;
13221923Sdavidn  }
133107267Sru
134107267Sru  /// \brief Determines whether this builtin is a predefined compiler-rt/libgcc
13521923Sdavidn  /// function, such as "__clear_cache", where we know the signature a
13621923Sdavidn  /// priori.
137107267Sru  bool isPredefinedRuntimeFunction(unsigned ID) const {
138107267Sru    return strchr(GetRecord(ID).Attributes, 'i') != 0;
139107267Sru  }
140107267Sru
141107267Sru  /// \brief Determines whether this builtin has custom typechecking.
142107267Sru  bool hasCustomTypechecking(unsigned ID) const {
143107267Sru    return strchr(GetRecord(ID).Attributes, 't') != 0;
144107267Sru  }
145107267Sru
146230549Strociny  /// \brief Completely forget that the given ID was ever considered a builtin,
147230549Strociny  /// e.g., because the user provided a conflicting signature.
148230549Strociny  void ForgetBuiltin(unsigned ID, IdentifierTable &Table);
14921923Sdavidn
150107267Sru  /// \brief If this is a library function that comes from a specific
151107267Sru  /// header, retrieve that header name.
152107267Sru  const char *getHeaderName(unsigned ID) const {
15321923Sdavidn    return GetRecord(ID).HeaderName;
15421923Sdavidn  }
15521923Sdavidn
15621923Sdavidn  /// \brief Determine whether this builtin is like printf in its
15721923Sdavidn  /// formatting rules and, if so, set the index to the format string
158107267Sru  /// argument and whether this function as a va_list argument.
15921923Sdavidn  bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
160107267Sru
161107267Sru  /// \brief Determine whether this builtin is like scanf in its
162107267Sru  /// formatting rules and, if so, set the index to the format string
16321923Sdavidn  /// argument and whether this function as a va_list argument.
16421923Sdavidn  bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
16521923Sdavidn
16621923Sdavidn  /// \brief Return true if this function has no side effects and doesn't
16721923Sdavidn  /// read memory, except for possibly errno.
168107267Sru  ///
16921923Sdavidn  /// Such functions can be const when the MathErrno lang option is disabled.
170107267Sru  bool isConstWithoutErrno(unsigned ID) const {
171107267Sru    return strchr(GetRecord(ID).Attributes, 'e') != 0;
172107267Sru  }
173107267Sru
174107267Sruprivate:
17521923Sdavidn  const Info &GetRecord(unsigned ID) const;
17621923Sdavidn
17721923Sdavidn  /// \brief Is this builtin supported according to the given language options?
17821923Sdavidn  bool BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
17979755Sdd                          const LangOptions &LangOpts);
18021923Sdavidn};
181107267Sru
182235211Sgjb}
183107267Sru} // end namespace clang
184107267Sru#endif
185107267Sru