blob: d8aaa6e8c9e163d7325de2964e436ec9dd2ac335 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Builtins.cpp - Builtin function implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements various things for builtin functions.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6b15cdc2009-06-14 01:05:48 +000014#include "clang/Basic/Builtins.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000015#include "clang/Basic/IdentifierTable.h"
Fariborz Jahanian67aba812010-11-30 17:35:24 +000016#include "clang/Basic/LangOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/TargetInfo.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000018#include "llvm/ADT/SmallVector.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
21static const Builtin::Info BuiltinInfo[] = {
Eli Friedmane7e66f72011-07-05 21:53:01 +000022 { "not a builtin function", 0, 0, 0, ALL_LANGUAGES },
23#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES },
Fariborz Jahanian67aba812010-11-30 17:35:24 +000024#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
Eli Friedmane7e66f72011-07-05 21:53:01 +000025 BUILTIN_LANG },
Chris Lattner6b15cdc2009-06-14 01:05:48 +000026#include "clang/Basic/Builtins.def"
Reid Spencer5f016e22007-07-11 17:01:13 +000027};
28
29const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
30 if (ID < Builtin::FirstTSBuiltin)
31 return BuiltinInfo[ID];
32 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
33 return TSRecords[ID - Builtin::FirstTSBuiltin];
34}
35
Douglas Gregor998b3d32011-09-01 23:39:15 +000036Builtin::Context::Context() {
Chris Lattner030e8fe2009-06-16 16:18:48 +000037 // Get the target specific builtins from the target.
Chris Lattnerff1d4d92009-06-16 17:27:50 +000038 TSRecords = 0;
39 NumTSRecords = 0;
Douglas Gregor998b3d32011-09-01 23:39:15 +000040}
41
42void Builtin::Context::InitializeTarget(const TargetInfo &Target) {
43 assert(NumTSRecords == 0 && "Already initialized target?");
44 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Chris Lattner030e8fe2009-06-16 16:18:48 +000045}
46
Reid Spencer5f016e22007-07-11 17:01:13 +000047/// InitializeBuiltins - Mark the identifiers for all the builtins with their
48/// appropriate builtin ID # and mark any non-portable builtin identifiers as
49/// such.
50void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
Fariborz Jahanian67aba812010-11-30 17:35:24 +000051 const LangOptions& LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +000052 // Step #1: mark all target-independent builtins with their ID's.
53 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Benjamin Kramerc89f02a2013-05-31 16:29:28 +000054 if ((!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) &&
55 (LangOpts.GNUMode || !(BuiltinInfo[i].builtin_lang & GNU_LANG)) &&
56 (LangOpts.ObjC1 || BuiltinInfo[i].builtin_lang != OBJC_LANG))
57 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
Chris Lattner1b63e4f2009-06-14 01:54:56 +000058
Douglas Gregor71dfdb92009-04-22 04:56:28 +000059 // Step #2: Register target-specific builtins.
Reid Spencer5f016e22007-07-11 17:01:13 +000060 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Bill Wendlingad017fa2012-12-20 19:22:21 +000061 if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor3573c0c2009-02-14 20:49:29 +000062 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Reid Spencer5f016e22007-07-11 17:01:13 +000063}
64
Mike Stump1eb44332009-09-09 15:08:12 +000065void
Eli Bendersky97a03cf2013-07-11 16:53:04 +000066Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000067 // Final all target-independent names
68 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Eli Bendersky97a03cf2013-07-11 16:53:04 +000069 if (!strchr(BuiltinInfo[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000070 Names.push_back(BuiltinInfo[i].Name);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Douglas Gregor2deaea32009-04-22 18:49:13 +000072 // Find target-specific names.
73 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Eli Bendersky97a03cf2013-07-11 16:53:04 +000074 if (!strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000075 Names.push_back(TSRecords[i].Name);
76}
77
Douglas Gregorb68e3992010-12-21 19:47:46 +000078void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
79 Table.get(GetRecord(ID).Name).setBuiltinID(0);
80}
81
Mike Stump1eb44332009-09-09 15:08:12 +000082bool
83Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
Douglas Gregora316e7b2009-02-14 00:32:47 +000084 bool &HasVAListArg) {
Bill Wendlingad017fa2012-12-20 19:22:21 +000085 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-02-14 00:32:47 +000086 if (!Printf)
87 return false;
88
89 HasVAListArg = (*Printf == 'P');
90
91 ++Printf;
92 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
93 ++Printf;
94
Chris Lattner8a778d62009-02-19 06:41:13 +000095 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregora316e7b2009-02-14 00:32:47 +000096 FormatIdx = strtol(Printf, 0, 10);
97 return true;
98}
99
Ted Kremenekbee05c12010-07-16 02:11:15 +0000100// FIXME: Refactor with isPrintfLike.
101bool
102Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
103 bool &HasVAListArg) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000104 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
Ted Kremenekbee05c12010-07-16 02:11:15 +0000105 if (!Scanf)
106 return false;
107
108 HasVAListArg = (*Scanf == 'S');
109
110 ++Scanf;
111 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
112 ++Scanf;
113
114 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
115 FormatIdx = strtol(Scanf, 0, 10);
116 return true;
117}
118