blob: 242c204d6d80ea7064281710c590f2df8ded6a02 [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)
Bill Wendlingad017fa2012-12-20 19:22:21 +000054 if (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) {
Fariborz Jahanian67aba812010-11-30 17:35:24 +000055 if (LangOpts.ObjC1 ||
56 BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
57 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
58 }
Chris Lattner1b63e4f2009-06-14 01:54:56 +000059
Douglas Gregor71dfdb92009-04-22 04:56:28 +000060 // Step #2: Register target-specific builtins.
Reid Spencer5f016e22007-07-11 17:01:13 +000061 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Bill Wendlingad017fa2012-12-20 19:22:21 +000062 if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor3573c0c2009-02-14 20:49:29 +000063 Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin);
Reid Spencer5f016e22007-07-11 17:01:13 +000064}
65
Mike Stump1eb44332009-09-09 15:08:12 +000066void
Chris Lattner5f9e2722011-07-23 10:55:15 +000067Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names,
Douglas Gregor2deaea32009-04-22 18:49:13 +000068 bool NoBuiltins) {
69 // Final all target-independent names
70 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Bill Wendlingad017fa2012-12-20 19:22:21 +000071 if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000072 Names.push_back(BuiltinInfo[i].Name);
Mike Stump1eb44332009-09-09 15:08:12 +000073
Douglas Gregor2deaea32009-04-22 18:49:13 +000074 // Find target-specific names.
75 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Bill Wendlingad017fa2012-12-20 19:22:21 +000076 if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f'))
Douglas Gregor2deaea32009-04-22 18:49:13 +000077 Names.push_back(TSRecords[i].Name);
78}
79
Douglas Gregorb68e3992010-12-21 19:47:46 +000080void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
81 Table.get(GetRecord(ID).Name).setBuiltinID(0);
82}
83
Mike Stump1eb44332009-09-09 15:08:12 +000084bool
85Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
Douglas Gregora316e7b2009-02-14 00:32:47 +000086 bool &HasVAListArg) {
Bill Wendlingad017fa2012-12-20 19:22:21 +000087 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-02-14 00:32:47 +000088 if (!Printf)
89 return false;
90
91 HasVAListArg = (*Printf == 'P');
92
93 ++Printf;
94 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
95 ++Printf;
96
Chris Lattner8a778d62009-02-19 06:41:13 +000097 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregora316e7b2009-02-14 00:32:47 +000098 FormatIdx = strtol(Printf, 0, 10);
99 return true;
100}
101
Ted Kremenekbee05c12010-07-16 02:11:15 +0000102// FIXME: Refactor with isPrintfLike.
103bool
104Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
105 bool &HasVAListArg) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000106 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
Ted Kremenekbee05c12010-07-16 02:11:15 +0000107 if (!Scanf)
108 return false;
109
110 HasVAListArg = (*Scanf == 'S');
111
112 ++Scanf;
113 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
114 ++Scanf;
115
116 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
117 FormatIdx = strtol(Scanf, 0, 10);
118 return true;
119}
120