Home | History | Annotate | Download | only in Basic
      1 //===--- Builtins.cpp - Builtin function implementation -------------------===//
      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 //  This file implements various things for builtin functions.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/Basic/Builtins.h"
     15 #include "clang/Basic/IdentifierTable.h"
     16 #include "clang/Basic/LangOptions.h"
     17 #include "clang/Basic/TargetInfo.h"
     18 #include "llvm/ADT/SmallVector.h"
     19 #include "llvm/ADT/StringRef.h"
     20 using namespace clang;
     21 
     22 static const Builtin::Info BuiltinInfo[] = {
     23   { "not a builtin function", 0, 0, 0, ALL_LANGUAGES },
     24 #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
     25 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
     26                                                             BUILTIN_LANG },
     27 #include "clang/Basic/Builtins.def"
     28 };
     29 
     30 const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
     31   if (ID < Builtin::FirstTSBuiltin)
     32     return BuiltinInfo[ID];
     33   assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
     34   return TSRecords[ID - Builtin::FirstTSBuiltin];
     35 }
     36 
     37 Builtin::Context::Context() {
     38   // Get the target specific builtins from the target.
     39   TSRecords = 0;
     40   NumTSRecords = 0;
     41 }
     42 
     43 void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
     44   assert(NumTSRecords == 0 && "Already initialized target?");
     45   Target.getTargetBuiltins(TSRecords, NumTSRecords);
     46 }
     47 
     48 bool Builtin::Context::BuiltinIsSupported(const Builtin::Info &BuiltinInfo,
     49                                           const LangOptions &LangOpts) {
     50   bool BuiltinsUnsupported = LangOpts.NoBuiltin &&
     51                              strchr(BuiltinInfo.Attributes, 'f');
     52   bool MathBuiltinsUnsupported =
     53     LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
     54     llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
     55   bool GnuModeUnsupported = !LangOpts.GNUMode &&
     56                             (BuiltinInfo.builtin_lang & GNU_LANG);
     57   bool ObjCUnsupported = !LangOpts.ObjC1 &&
     58                          BuiltinInfo.builtin_lang == OBJC_LANG;
     59   return !BuiltinsUnsupported && !MathBuiltinsUnsupported &&
     60          !GnuModeUnsupported && !ObjCUnsupported;
     61 }
     62 
     63 /// InitializeBuiltins - Mark the identifiers for all the builtins with their
     64 /// appropriate builtin ID # and mark any non-portable builtin identifiers as
     65 /// such.
     66 void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
     67                                           const LangOptions& LangOpts) {
     68   // Step #1: mark all target-independent builtins with their ID's.
     69   for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
     70     if (BuiltinIsSupported(BuiltinInfo[i], LangOpts)) {
     71       Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
     72     }
     73 
     74   // Step #2: Register target-specific builtins.
     75   for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
     76     if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
     77       Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
     78 }
     79 
     80 void
     81 Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names) {
     82   // Final all target-independent names
     83   for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
     84     if (!strchr(BuiltinInfo[i].Attributes, 'f'))
     85       Names.push_back(BuiltinInfo[i].Name);
     86 
     87   // Find target-specific names.
     88   for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
     89     if (!strchr(TSRecords[i].Attributes, 'f'))
     90       Names.push_back(TSRecords[i].Name);
     91 }
     92 
     93 void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
     94   Table.get(GetRecord(ID).Name).setBuiltinID(0);
     95 }
     96 
     97 bool
     98 Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
     99                                bool &HasVAListArg) {
    100   const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
    101   if (!Printf)
    102     return false;
    103 
    104   HasVAListArg = (*Printf == 'P');
    105 
    106   ++Printf;
    107   assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
    108   ++Printf;
    109 
    110   assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
    111   FormatIdx = strtol(Printf, 0, 10);
    112   return true;
    113 }
    114 
    115 // FIXME: Refactor with isPrintfLike.
    116 bool
    117 Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
    118                               bool &HasVAListArg) {
    119   const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
    120   if (!Scanf)
    121     return false;
    122 
    123   HasVAListArg = (*Scanf == 'S');
    124 
    125   ++Scanf;
    126   assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
    127   ++Scanf;
    128 
    129   assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
    130   FormatIdx = strtol(Scanf, 0, 10);
    131   return true;
    132 }
    133 
    134