blob: 9ecb0c0c8b75e4d67f8825f749c32e184586a4be [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Basic/TargetInfo.h"
Fariborz Jahanian67aba812010-11-30 17:35:24 +000017#include "clang/Basic/LangOptions.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
20static const Builtin::Info BuiltinInfo[] = {
Fariborz Jahanian67aba812010-11-30 17:35:24 +000021 { "not a builtin function", 0, 0, 0, ALL_LANGUAGES, false },
22#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES, false },
23#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\
24 BUILTIN_LANG, false },
Chris Lattner6b15cdc2009-06-14 01:05:48 +000025#include "clang/Basic/Builtins.def"
Reid Spencer5f016e22007-07-11 17:01:13 +000026};
27
28const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const {
29 if (ID < Builtin::FirstTSBuiltin)
30 return BuiltinInfo[ID];
31 assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!");
32 return TSRecords[ID - Builtin::FirstTSBuiltin];
33}
34
Chris Lattner030e8fe2009-06-16 16:18:48 +000035Builtin::Context::Context(const TargetInfo &Target) {
36 // Get the target specific builtins from the target.
Chris Lattnerff1d4d92009-06-16 17:27:50 +000037 TSRecords = 0;
38 NumTSRecords = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000039 Target.getTargetBuiltins(TSRecords, NumTSRecords);
Chris Lattner030e8fe2009-06-16 16:18:48 +000040}
41
Reid Spencer5f016e22007-07-11 17:01:13 +000042/// InitializeBuiltins - Mark the identifiers for all the builtins with their
43/// appropriate builtin ID # and mark any non-portable builtin identifiers as
44/// such.
45void Builtin::Context::InitializeBuiltins(IdentifierTable &Table,
Fariborz Jahanian67aba812010-11-30 17:35:24 +000046 const LangOptions& LangOpts) {
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Step #1: mark all target-independent builtins with their ID's.
48 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
Douglas Gregor3573c0c2009-02-14 20:49:29 +000049 if (!BuiltinInfo[i].Suppressed &&
Fariborz Jahanian67aba812010-11-30 17:35:24 +000050 (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f'))) {
51 if (LangOpts.ObjC1 ||
52 BuiltinInfo[i].builtin_lang != clang::OBJC_LANG)
53 Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
54 }
Chris Lattner1b63e4f2009-06-14 01:54:56 +000055
Douglas Gregor71dfdb92009-04-22 04:56:28 +000056 // Step #2: Register target-specific builtins.
Reid Spencer5f016e22007-07-11 17:01:13 +000057 for (unsigned i = 0, e = NumTSRecords; i != e; ++i)
Douglas Gregor3573c0c2009-02-14 20:49:29 +000058 if (!TSRecords[i].Suppressed &&
Fariborz Jahanian67aba812010-11-30 17:35:24 +000059 (!LangOpts.NoBuiltin ||
Mike Stump1eb44332009-09-09 15:08:12 +000060 (TSRecords[i].Attributes &&
Daniel Dunbare8699902009-02-15 18:23:07 +000061 !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
Douglas Gregor2deaea32009-04-22 18:49:13 +000066Builtin::Context::GetBuiltinNames(llvm::SmallVectorImpl<const char *> &Names,
67 bool NoBuiltins) {
68 // Final all target-independent names
69 for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
70 if (!BuiltinInfo[i].Suppressed &&
71 (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')))
72 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)
76 if (!TSRecords[i].Suppressed &&
Mike Stump1eb44332009-09-09 15:08:12 +000077 (!NoBuiltins ||
78 (TSRecords[i].Attributes &&
Douglas Gregor2deaea32009-04-22 18:49:13 +000079 !strchr(TSRecords[i].Attributes, 'f'))))
80 Names.push_back(TSRecords[i].Name);
81}
82
Douglas Gregorb68e3992010-12-21 19:47:46 +000083void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) {
84 Table.get(GetRecord(ID).Name).setBuiltinID(0);
85}
86
Mike Stump1eb44332009-09-09 15:08:12 +000087bool
88Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
Douglas Gregora316e7b2009-02-14 00:32:47 +000089 bool &HasVAListArg) {
Cedric Venetea684e62009-02-14 16:15:20 +000090 const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP");
Douglas Gregora316e7b2009-02-14 00:32:47 +000091 if (!Printf)
92 return false;
93
94 HasVAListArg = (*Printf == 'P');
95
96 ++Printf;
97 assert(*Printf == ':' && "p or P specifier must have be followed by a ':'");
98 ++Printf;
99
Chris Lattner8a778d62009-02-19 06:41:13 +0000100 assert(strchr(Printf, ':') && "printf specifier must end with a ':'");
Douglas Gregora316e7b2009-02-14 00:32:47 +0000101 FormatIdx = strtol(Printf, 0, 10);
102 return true;
103}
104
Ted Kremenekbee05c12010-07-16 02:11:15 +0000105// FIXME: Refactor with isPrintfLike.
106bool
107Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
108 bool &HasVAListArg) {
109 const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS");
110 if (!Scanf)
111 return false;
112
113 HasVAListArg = (*Scanf == 'S');
114
115 ++Scanf;
116 assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'");
117 ++Scanf;
118
119 assert(strchr(Scanf, ':') && "printf specifier must end with a ':'");
120 FormatIdx = strtol(Scanf, 0, 10);
121 return true;
122}
123
124