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