blob: e71e053bcdd3a2cd5958a794af86ad1359cfb1b1 [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
Chris Lattner5f9e2722011-07-23 10:55:15 +000066Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names,
Douglas Gregor2deaea32009-04-22 18:49:13 +000067 bool NoBuiltins) {
68 // Final all target-independent names
69 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Bill Wendlingad017fa2012-12-20 19:22:21 +000070 if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000071 Names.push_back(BuiltinInfo[i].Name);
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2deaea32009-04-22 18:49:13 +000073 // Find target-specific names.
74 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Bill Wendlingad017fa2012-12-20 19:22:21 +000075 if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000076 Names.push_back(TSRecords[i].Name);
77}
78
Douglas Gregorb68e3992010-12-21 19:47:46 +000079void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
80 Table.get(GetRecord(ID).Name).setBuiltinID(0);
81}
82
Mike Stump1eb44332009-09-09 15:08:12 +000083bool
84Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
Douglas Gregora316e7b2009-02-14 00:32:47 +000085 bool &HasVAListArg) {
Bill Wendlingad017fa2012-12-20 19:22:21 +000086 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-02-14 00:32:47 +000087 if (!Printf)
88 return false;
89
90 HasVAListArg = (*Printf == 'P');
91
92 ++Printf;
93 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
94 ++Printf;
95
Chris Lattner8a778d62009-02-19 06:41:13 +000096 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregora316e7b2009-02-14 00:32:47 +000097 FormatIdx = strtol(Printf, 0, 10);
98 return true;
99}
100
Ted Kremenekbee05c12010-07-16 02:11:15 +0000101// FIXME: Refactor with isPrintfLike.
102bool
103Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
104 bool &HasVAListArg) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000105 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
Ted Kremenekbee05c12010-07-16 02:11:15 +0000106 if (!Scanf)
107 return false;
108
109 HasVAListArg = (*Scanf == 'S');
110
111 ++Scanf;
112 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
113 ++Scanf;
114
115 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
116 FormatIdx = strtol(Scanf, 0, 10);
117 return true;
118}
119