blob: f0a763e4ecfde2455e2daa7844ee7e707af6da5b [file] [log] [blame]
Chris Lattner289ab7b2006-11-08 06:54:53 +00001//===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===//
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner289ab7b2006-11-08 06:54:53 +000010// This file implements semantic analysis for declaration specifiers.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000011//
12//===----------------------------------------------------------------------===//
13
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency!
15#include "clang/Sema/DeclSpec.h"
Douglas Gregorc15b0cf2011-06-25 00:56:27 +000016#include "clang/Sema/LocInfoType.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/ParsedTemplate.h"
Peter Collingbourne485b80f2011-10-06 03:01:00 +000018#include "clang/Sema/Sema.h"
Douglas Gregor869ad452011-02-24 17:54:50 +000019#include "clang/AST/ASTContext.h"
Douglas Gregorc15b0cf2011-06-25 00:56:27 +000020#include "clang/AST/Expr.h"
Douglas Gregor90c99722011-02-24 00:17:56 +000021#include "clang/AST/NestedNameSpecifier.h"
22#include "clang/AST/TypeLoc.h"
Douglas Gregore3e01a22009-04-01 22:41:11 +000023#include "clang/Lex/Preprocessor.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000024#include "clang/Basic/LangOptions.h"
Chris Lattner1ce41ed2009-01-20 19:11:22 +000025#include "llvm/ADT/STLExtras.h"
John McCall898cd0f2009-08-03 18:47:27 +000026#include "llvm/Support/ErrorHandling.h"
Douglas Gregor52537682009-03-19 00:18:19 +000027#include <cstring>
Chris Lattnerb9093cd2006-08-04 04:39:53 +000028using namespace clang;
29
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000030
David Blaikie9c902b52011-09-25 23:23:43 +000031static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc,
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000032 unsigned DiagID) {
33 return D.Report(Loc, DiagID);
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000034}
35
Douglas Gregorb53edfb2009-11-10 19:49:08 +000036
37void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
38 assert(TemplateId && "NULL template-id annotation?");
39 Kind = IK_TemplateId;
40 this->TemplateId = TemplateId;
41 StartLocation = TemplateId->TemplateNameLoc;
42 EndLocation = TemplateId->RAngleLoc;
43}
44
Douglas Gregor9de54ea2010-01-13 17:31:36 +000045void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
46 assert(TemplateId && "NULL template-id annotation?");
47 Kind = IK_ConstructorTemplateId;
48 this->TemplateId = TemplateId;
49 StartLocation = TemplateId->TemplateNameLoc;
50 EndLocation = TemplateId->RAngleLoc;
51}
52
Douglas Gregor90c99722011-02-24 00:17:56 +000053void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
54 TypeLoc TL, SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000055 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +000056 if (Range.getBegin().isInvalid())
57 Range.setBegin(TL.getBeginLoc());
58 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000059
Douglas Gregor9b272512011-02-28 23:58:31 +000060 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000061 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000062}
63
64void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
65 SourceLocation IdentifierLoc,
66 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000067 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
68
Douglas Gregor90c99722011-02-24 00:17:56 +000069 if (Range.getBegin().isInvalid())
70 Range.setBegin(IdentifierLoc);
71 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000072
Douglas Gregor9b272512011-02-28 23:58:31 +000073 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000074 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000075}
76
77void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
78 SourceLocation NamespaceLoc,
79 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000080 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
81
Douglas Gregor90c99722011-02-24 00:17:56 +000082 if (Range.getBegin().isInvalid())
83 Range.setBegin(NamespaceLoc);
84 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000085
Douglas Gregor9b272512011-02-28 23:58:31 +000086 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000087 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000088}
89
Douglas Gregor7b26ff92011-02-24 02:36:08 +000090void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
91 SourceLocation AliasLoc,
92 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000093 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
94
Douglas Gregor7b26ff92011-02-24 02:36:08 +000095 if (Range.getBegin().isInvalid())
96 Range.setBegin(AliasLoc);
97 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000098
Douglas Gregor9b272512011-02-28 23:58:31 +000099 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +0000100 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000101}
102
Douglas Gregor90c99722011-02-24 00:17:56 +0000103void CXXScopeSpec::MakeGlobal(ASTContext &Context,
104 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +0000105 Builder.MakeGlobal(Context, ColonColonLoc);
106
Douglas Gregor90c99722011-02-24 00:17:56 +0000107 Range = SourceRange(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +0000108
Douglas Gregor9b272512011-02-28 23:58:31 +0000109 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +0000110 "NestedNameSpecifierLoc range computation incorrect");
111}
112
113void CXXScopeSpec::MakeTrivial(ASTContext &Context,
114 NestedNameSpecifier *Qualifier, SourceRange R) {
Douglas Gregor9b272512011-02-28 23:58:31 +0000115 Builder.MakeTrivial(Context, Qualifier, R);
Douglas Gregor869ad452011-02-24 17:54:50 +0000116 Range = R;
Douglas Gregor869ad452011-02-24 17:54:50 +0000117}
118
119void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
120 if (!Other) {
121 Range = SourceRange();
Douglas Gregor9b272512011-02-28 23:58:31 +0000122 Builder.Clear();
Douglas Gregor869ad452011-02-24 17:54:50 +0000123 return;
124 }
Douglas Gregor9b272512011-02-28 23:58:31 +0000125
Douglas Gregor869ad452011-02-24 17:54:50 +0000126 Range = Other.getSourceRange();
Douglas Gregor9b272512011-02-28 23:58:31 +0000127 Builder.Adopt(Other);
Douglas Gregor869ad452011-02-24 17:54:50 +0000128}
129
John McCall21878762011-07-06 06:57:57 +0000130SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
131 if (!Builder.getRepresentation())
132 return SourceLocation();
133 return Builder.getTemporary().getLocalBeginLoc();
134}
135
Douglas Gregor14454802011-02-25 02:25:35 +0000136NestedNameSpecifierLoc
137CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregor2b1ca9e2011-03-03 21:48:55 +0000138 if (!Builder.getRepresentation())
Douglas Gregor869ad452011-02-24 17:54:50 +0000139 return NestedNameSpecifierLoc();
140
Douglas Gregor9b272512011-02-28 23:58:31 +0000141 return Builder.getWithLocInContext(Context);
Douglas Gregor90c99722011-02-24 00:17:56 +0000142}
143
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000144/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
145/// "TheDeclarator" is the declarator that this will be added to.
John McCall084e83d2011-03-24 11:26:52 +0000146DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +0000147 SourceLocation EllipsisLoc,
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000148 ParamInfo *ArgInfo,
149 unsigned NumArgs,
150 unsigned TypeQuals,
Douglas Gregor54992352011-01-26 03:43:54 +0000151 bool RefQualifierIsLvalueRef,
152 SourceLocation RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +0000153 SourceLocation MutableLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +0000154 ExceptionSpecificationType
155 ESpecType,
156 SourceLocation ESpecLoc,
John McCallba7bf592010-08-24 05:47:05 +0000157 ParsedType *Exceptions,
Sebastian Redld6434562009-05-29 18:02:33 +0000158 SourceRange *ExceptionRanges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000159 unsigned NumExceptions,
Sebastian Redl802a4532011-03-05 22:42:13 +0000160 Expr *NoexceptExpr,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000161 SourceLocation LocalRangeBegin,
162 SourceLocation LocalRangeEnd,
Douglas Gregor7fb25412010-10-01 18:44:50 +0000163 Declarator &TheDeclarator,
164 ParsedType TrailingReturnType) {
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000165 DeclaratorChunk I;
Sebastian Redl802a4532011-03-05 22:42:13 +0000166 I.Kind = Function;
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000167 I.Loc = LocalRangeBegin;
168 I.EndLoc = LocalRangeEnd;
John McCall084e83d2011-03-24 11:26:52 +0000169 I.Fun.AttrList = 0;
Sebastian Redl802a4532011-03-05 22:42:13 +0000170 I.Fun.hasPrototype = hasProto;
171 I.Fun.isVariadic = isVariadic;
172 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
173 I.Fun.DeleteArgInfo = false;
174 I.Fun.TypeQuals = TypeQuals;
175 I.Fun.NumArgs = NumArgs;
176 I.Fun.ArgInfo = 0;
Douglas Gregor54992352011-01-26 03:43:54 +0000177 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl802a4532011-03-05 22:42:13 +0000178 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
Douglas Gregorad69e652011-07-13 21:47:47 +0000179 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
Sebastian Redl802a4532011-03-05 22:42:13 +0000180 I.Fun.ExceptionSpecType = ESpecType;
181 I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding();
182 I.Fun.NumExceptions = 0;
183 I.Fun.Exceptions = 0;
184 I.Fun.NoexceptExpr = 0;
Douglas Gregor7fb25412010-10-01 18:44:50 +0000185 I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000186
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000187 // new[] an argument array if needed.
188 if (NumArgs) {
189 // If the 'InlineParams' in Declarator is unused and big enough, put our
190 // parameter list there (in an effort to avoid new/delete traffic). If it
191 // is already used (consider a function returning a function pointer) or too
192 // small (function taking too many arguments), go to the heap.
Mike Stump11289f42009-09-09 15:08:12 +0000193 if (!TheDeclarator.InlineParamsUsed &&
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000194 NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
195 I.Fun.ArgInfo = TheDeclarator.InlineParams;
196 I.Fun.DeleteArgInfo = false;
197 TheDeclarator.InlineParamsUsed = true;
198 } else {
199 I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs];
200 I.Fun.DeleteArgInfo = true;
201 }
202 memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs);
203 }
Sebastian Redl802a4532011-03-05 22:42:13 +0000204
205 // Check what exception specification information we should actually store.
206 switch (ESpecType) {
207 default: break; // By default, save nothing.
208 case EST_Dynamic:
209 // new[] an exception array if needed
210 if (NumExceptions) {
211 I.Fun.NumExceptions = NumExceptions;
212 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
213 for (unsigned i = 0; i != NumExceptions; ++i) {
214 I.Fun.Exceptions[i].Ty = Exceptions[i];
215 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
216 }
Sebastian Redld6434562009-05-29 18:02:33 +0000217 }
Sebastian Redl802a4532011-03-05 22:42:13 +0000218 break;
219
220 case EST_ComputedNoexcept:
221 I.Fun.NoexceptExpr = NoexceptExpr;
222 break;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000223 }
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000224 return I;
225}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000226
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000227bool Declarator::isDeclarationOfFunction() const {
Richard Smithcfcdf3a2011-06-25 02:28:38 +0000228 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
229 switch (DeclTypeInfo[i].Kind) {
230 case DeclaratorChunk::Function:
231 return true;
232 case DeclaratorChunk::Paren:
233 continue;
234 case DeclaratorChunk::Pointer:
235 case DeclaratorChunk::Reference:
236 case DeclaratorChunk::Array:
237 case DeclaratorChunk::BlockPointer:
238 case DeclaratorChunk::MemberPointer:
239 return false;
240 }
241 llvm_unreachable("Invalid type chunk");
242 return false;
243 }
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000244
245 switch (DS.getTypeSpecType()) {
Eli Friedman0dfb8892011-10-06 23:00:33 +0000246 case TST_atomic:
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000247 case TST_auto:
248 case TST_bool:
249 case TST_char:
250 case TST_char16:
251 case TST_char32:
252 case TST_class:
253 case TST_decimal128:
254 case TST_decimal32:
255 case TST_decimal64:
256 case TST_double:
257 case TST_enum:
258 case TST_error:
259 case TST_float:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000260 case TST_half:
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000261 case TST_int:
262 case TST_struct:
263 case TST_union:
264 case TST_unknown_anytype:
265 case TST_unspecified:
266 case TST_void:
267 case TST_wchar:
268 return false;
269
270 case TST_decltype:
271 case TST_typeofExpr:
272 if (Expr *E = DS.getRepAsExpr())
273 return E->getType()->isFunctionType();
274 return false;
275
276 case TST_underlyingType:
277 case TST_typename:
278 case TST_typeofType: {
279 QualType QT = DS.getRepAsType().get();
280 if (QT.isNull())
281 return false;
282
283 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
284 QT = LIT->getType();
285
286 if (QT.isNull())
287 return false;
288
289 return QT->isFunctionType();
290 }
291 }
292
293 return false;
294}
295
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000296/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +0000297/// declaration specifier includes.
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000298///
299unsigned DeclSpec::getParsedSpecifiers() const {
300 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000301 if (StorageClassSpec != SCS_unspecified ||
302 SCS_thread_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000303 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +0000304
Chris Lattner4d8f8732006-11-28 05:05:08 +0000305 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000306 Res |= PQ_TypeQualifier;
Mike Stump11289f42009-09-09 15:08:12 +0000307
Chris Lattnerf055d432006-11-28 04:28:12 +0000308 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000309 Res |= PQ_TypeSpecifier;
Mike Stump11289f42009-09-09 15:08:12 +0000310
Douglas Gregor61956c42008-10-31 09:07:45 +0000311 if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000312 Res |= PQ_FunctionSpecifier;
313 return Res;
314}
315
John McCall49bfce42009-08-03 20:12:06 +0000316template <class T> static bool BadSpecifier(T TNew, T TPrev,
317 const char *&PrevSpec,
318 unsigned &DiagID) {
John McCall898cd0f2009-08-03 18:47:27 +0000319 PrevSpec = DeclSpec::getSpecifierName(TPrev);
John McCall49bfce42009-08-03 20:12:06 +0000320 DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec
321 : diag::err_invalid_decl_spec_combination);
John McCall898cd0f2009-08-03 18:47:27 +0000322 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000323}
John McCall898cd0f2009-08-03 18:47:27 +0000324
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000325const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000326 switch (S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000327 case DeclSpec::SCS_unspecified: return "unspecified";
328 case DeclSpec::SCS_typedef: return "typedef";
329 case DeclSpec::SCS_extern: return "extern";
330 case DeclSpec::SCS_static: return "static";
331 case DeclSpec::SCS_auto: return "auto";
332 case DeclSpec::SCS_register: return "register";
Eli Friedmand5c0eed2009-04-19 20:27:55 +0000333 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000334 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000335 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000336 llvm_unreachable("Unknown typespec!");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000337}
338
John McCall898cd0f2009-08-03 18:47:27 +0000339const char *DeclSpec::getSpecifierName(TSW W) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000340 switch (W) {
John McCall898cd0f2009-08-03 18:47:27 +0000341 case TSW_unspecified: return "unspecified";
342 case TSW_short: return "short";
343 case TSW_long: return "long";
344 case TSW_longlong: return "long long";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000345 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000346 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000347}
348
John McCall898cd0f2009-08-03 18:47:27 +0000349const char *DeclSpec::getSpecifierName(TSC C) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000350 switch (C) {
John McCall898cd0f2009-08-03 18:47:27 +0000351 case TSC_unspecified: return "unspecified";
352 case TSC_imaginary: return "imaginary";
353 case TSC_complex: return "complex";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000354 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000355 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000356}
357
358
John McCall898cd0f2009-08-03 18:47:27 +0000359const char *DeclSpec::getSpecifierName(TSS S) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000360 switch (S) {
John McCall898cd0f2009-08-03 18:47:27 +0000361 case TSS_unspecified: return "unspecified";
362 case TSS_signed: return "signed";
363 case TSS_unsigned: return "unsigned";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000364 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000365 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000366}
367
Chris Lattner69680ea2007-01-23 04:34:43 +0000368const char *DeclSpec::getSpecifierName(DeclSpec::TST T) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000369 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000370 case DeclSpec::TST_unspecified: return "unspecified";
371 case DeclSpec::TST_void: return "void";
372 case DeclSpec::TST_char: return "char";
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000373 case DeclSpec::TST_wchar: return "wchar_t";
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000374 case DeclSpec::TST_char16: return "char16_t";
375 case DeclSpec::TST_char32: return "char32_t";
Chris Lattner839713c2006-08-04 06:15:52 +0000376 case DeclSpec::TST_int: return "int";
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000377 case DeclSpec::TST_half: return "half";
Chris Lattner839713c2006-08-04 06:15:52 +0000378 case DeclSpec::TST_float: return "float";
379 case DeclSpec::TST_double: return "double";
380 case DeclSpec::TST_bool: return "_Bool";
381 case DeclSpec::TST_decimal32: return "_Decimal32";
382 case DeclSpec::TST_decimal64: return "_Decimal64";
383 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +0000384 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000385 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000386 case DeclSpec::TST_union: return "union";
387 case DeclSpec::TST_struct: return "struct";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000388 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000389 case DeclSpec::TST_typeofType:
390 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall898cd0f2009-08-03 18:47:27 +0000391 case DeclSpec::TST_auto: return "auto";
392 case DeclSpec::TST_decltype: return "(decltype)";
Alexis Hunte852b102011-05-24 22:41:36 +0000393 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCall39439732011-04-09 22:50:59 +0000394 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
Eli Friedman0dfb8892011-10-06 23:00:33 +0000395 case DeclSpec::TST_atomic: return "_Atomic";
John McCall898cd0f2009-08-03 18:47:27 +0000396 case DeclSpec::TST_error: return "(error)";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000397 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000398 llvm_unreachable("Unknown typespec!");
Chris Lattner839713c2006-08-04 06:15:52 +0000399}
400
John McCall898cd0f2009-08-03 18:47:27 +0000401const char *DeclSpec::getSpecifierName(TQ T) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000402 switch (T) {
John McCall898cd0f2009-08-03 18:47:27 +0000403 case DeclSpec::TQ_unspecified: return "unspecified";
404 case DeclSpec::TQ_const: return "const";
405 case DeclSpec::TQ_restrict: return "restrict";
406 case DeclSpec::TQ_volatile: return "volatile";
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000407 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000408 llvm_unreachable("Unknown typespec!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000409}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000410
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000411bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000412 const char *&PrevSpec,
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000413 unsigned &DiagID) {
414 // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class
415 // specifiers are not supported."
Peter Collingbournede32b202011-02-11 19:59:54 +0000416 // It seems sensible to prohibit private_extern too
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000417 // The cl_clang_storage_class_specifiers extension enables support for
418 // these storage-class specifiers.
419 if (S.getLangOptions().OpenCL &&
420 !S.getOpenCLOptions().cl_clang_storage_class_specifiers) {
421 switch (SC) {
Peter Collingbournede32b202011-02-11 19:59:54 +0000422 case SCS_extern:
423 case SCS_private_extern:
424 case SCS_auto:
425 case SCS_register:
426 case SCS_static:
427 DiagID = diag::err_not_opencl_storage_class_specifier;
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000428 PrevSpec = getSpecifierName(SC);
Peter Collingbournede32b202011-02-11 19:59:54 +0000429 return true;
430 default:
431 break;
432 }
433 }
434
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000435 if (StorageClassSpec != SCS_unspecified) {
Richard Smith58c74332011-09-04 19:54:14 +0000436 // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode.
437 bool isInvalid = true;
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000438 if (TypeSpecType == TST_unspecified && S.getLangOptions().CPlusPlus) {
439 if (SC == SCS_auto)
Richard Smith58c74332011-09-04 19:54:14 +0000440 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID);
441 if (StorageClassSpec == SCS_auto) {
442 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
443 PrevSpec, DiagID);
444 assert(!isInvalid && "auto SCS -> TST recovery failed");
445 }
446 }
447
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000448 // Changing storage class is allowed only if the previous one
449 // was the 'extern' that is part of a linkage specification and
450 // the new storage class is 'typedef'.
Richard Smith58c74332011-09-04 19:54:14 +0000451 if (isInvalid &&
452 !(SCS_extern_in_linkage_spec &&
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000453 StorageClassSpec == SCS_extern &&
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000454 SC == SCS_typedef))
455 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000456 }
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000457 StorageClassSpec = SC;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000458 StorageClassSpecLoc = Loc;
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000459 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000460 return false;
461}
462
Mike Stump11289f42009-09-09 15:08:12 +0000463bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000464 const char *&PrevSpec,
465 unsigned &DiagID) {
Chris Lattner353f5742006-11-28 04:50:12 +0000466 if (SCS_thread_specified) {
467 PrevSpec = "__thread";
John McCall49bfce42009-08-03 20:12:06 +0000468 DiagID = diag::ext_duplicate_declspec;
Chris Lattner353f5742006-11-28 04:50:12 +0000469 return true;
470 }
471 SCS_thread_specified = true;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000472 SCS_threadLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000473 return false;
474}
475
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000476/// These methods set the specified attribute of the DeclSpec, but return true
477/// and ignore the request if invalid (e.g. "extern" then "auto" is
478/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000479bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000480 const char *&PrevSpec,
481 unsigned &DiagID) {
Abramo Bagnaraead67d92011-03-06 22:21:56 +0000482 // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that
483 // for 'long long' we will keep the source location of the first 'long'.
484 if (TypeSpecWidth == TSW_unspecified)
485 TSWLoc = Loc;
486 // Allow turning long -> long long.
487 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCall49bfce42009-08-03 20:12:06 +0000488 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000489 TypeSpecWidth = W;
Chris Lattner37141f42010-06-23 06:00:24 +0000490 if (TypeAltiVecVector && !TypeAltiVecBool &&
491 ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) {
John Thompson22334602010-02-05 00:12:22 +0000492 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
493 DiagID = diag::warn_vector_long_decl_spec_combination;
494 return true;
495 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000496 return false;
497}
498
Mike Stump11289f42009-09-09 15:08:12 +0000499bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000500 const char *&PrevSpec,
501 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000502 if (TypeSpecComplex != TSC_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000503 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000504 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000505 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000506 return false;
507}
508
Mike Stump11289f42009-09-09 15:08:12 +0000509bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000510 const char *&PrevSpec,
511 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000512 if (TypeSpecSign != TSS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000513 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000514 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000515 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000516 return false;
517}
518
Chris Lattnerb20e8942006-11-28 05:30:29 +0000519bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000520 const char *&PrevSpec,
521 unsigned &DiagID,
John McCallba7bf592010-08-24 05:47:05 +0000522 ParsedType Rep) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000523 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep);
524}
525
526bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
527 SourceLocation TagNameLoc,
528 const char *&PrevSpec,
529 unsigned &DiagID,
530 ParsedType Rep) {
John McCallba7bf592010-08-24 05:47:05 +0000531 assert(isTypeRep(T) && "T does not store a type");
532 assert(Rep && "no type provided!");
533 if (TypeSpecType != TST_unspecified) {
534 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
535 DiagID = diag::err_invalid_decl_spec_combination;
536 return true;
537 }
538 TypeSpecType = T;
539 TypeRep = Rep;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000540 TSTLoc = TagKwLoc;
541 TSTNameLoc = TagNameLoc;
John McCallba7bf592010-08-24 05:47:05 +0000542 TypeSpecOwned = false;
543 return false;
544}
545
546bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
547 const char *&PrevSpec,
548 unsigned &DiagID,
549 Expr *Rep) {
550 assert(isExprRep(T) && "T does not store an expr");
551 assert(Rep && "no expression provided!");
552 if (TypeSpecType != TST_unspecified) {
553 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
554 DiagID = diag::err_invalid_decl_spec_combination;
555 return true;
556 }
557 TypeSpecType = T;
558 ExprRep = Rep;
559 TSTLoc = Loc;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000560 TSTNameLoc = Loc;
John McCallba7bf592010-08-24 05:47:05 +0000561 TypeSpecOwned = false;
562 return false;
563}
564
565bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
566 const char *&PrevSpec,
567 unsigned &DiagID,
568 Decl *Rep, bool Owned) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000569 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned);
570}
571
572bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
573 SourceLocation TagNameLoc,
574 const char *&PrevSpec,
575 unsigned &DiagID,
576 Decl *Rep, bool Owned) {
John McCallba7bf592010-08-24 05:47:05 +0000577 assert(isDeclRep(T) && "T does not store a decl");
578 // Unlike the other cases, we don't assert that we actually get a decl.
579
580 if (TypeSpecType != TST_unspecified) {
581 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
582 DiagID = diag::err_invalid_decl_spec_combination;
583 return true;
584 }
585 TypeSpecType = T;
586 DeclRep = Rep;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000587 TSTLoc = TagKwLoc;
588 TSTNameLoc = TagNameLoc;
John McCallba7bf592010-08-24 05:47:05 +0000589 TypeSpecOwned = Owned;
590 return false;
591}
592
593bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
594 const char *&PrevSpec,
595 unsigned &DiagID) {
596 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
597 "rep required for these type-spec kinds!");
John McCall49bfce42009-08-03 20:12:06 +0000598 if (TypeSpecType != TST_unspecified) {
599 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
600 DiagID = diag::err_invalid_decl_spec_combination;
601 return true;
602 }
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000603 TSTLoc = Loc;
604 TSTNameLoc = Loc;
Chris Lattner37141f42010-06-23 06:00:24 +0000605 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
606 TypeAltiVecBool = true;
Chris Lattner37141f42010-06-23 06:00:24 +0000607 return false;
608 }
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000609 TypeSpecType = T;
John McCallba7bf592010-08-24 05:47:05 +0000610 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000611 if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) {
John Thompson22334602010-02-05 00:12:22 +0000612 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
Chris Lattner37141f42010-06-23 06:00:24 +0000613 DiagID = diag::err_invalid_vector_decl_spec;
John Thompson22334602010-02-05 00:12:22 +0000614 return true;
615 }
616 return false;
617}
618
619bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
620 const char *&PrevSpec, unsigned &DiagID) {
621 if (TypeSpecType != TST_unspecified) {
622 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
623 DiagID = diag::err_invalid_vector_decl_spec_combination;
624 return true;
625 }
626 TypeAltiVecVector = isAltiVecVector;
627 AltiVecLoc = Loc;
628 return false;
629}
630
631bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
632 const char *&PrevSpec, unsigned &DiagID) {
Chris Lattner37141f42010-06-23 06:00:24 +0000633 if (!TypeAltiVecVector || TypeAltiVecPixel ||
634 (TypeSpecType != TST_unspecified)) {
John Thompson22334602010-02-05 00:12:22 +0000635 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType);
636 DiagID = diag::err_invalid_pixel_decl_spec_combination;
637 return true;
638 }
John Thompson22334602010-02-05 00:12:22 +0000639 TypeAltiVecPixel = isAltiVecPixel;
640 TSTLoc = Loc;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000641 TSTNameLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000642 return false;
643}
644
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000645bool DeclSpec::SetTypeSpecError() {
646 TypeSpecType = TST_error;
John McCalla3707cc2010-08-26 17:22:34 +0000647 TypeSpecOwned = false;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000648 TSTLoc = SourceLocation();
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000649 TSTNameLoc = SourceLocation();
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000650 return false;
651}
652
Chris Lattner60809f52006-11-28 05:18:46 +0000653bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000654 unsigned &DiagID, const LangOptions &Lang) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000655 // Duplicates turn into warnings pre-C99.
656 if ((TypeQualifiers & T) && !Lang.C99)
John McCall49bfce42009-08-03 20:12:06 +0000657 return BadSpecifier(T, T, PrevSpec, DiagID);
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000658 TypeQualifiers |= T;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Chris Lattner60809f52006-11-28 05:18:46 +0000660 switch (T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000661 default: llvm_unreachable("Unknown type qualifier!");
Chris Lattner60809f52006-11-28 05:18:46 +0000662 case TQ_const: TQ_constLoc = Loc; break;
663 case TQ_restrict: TQ_restrictLoc = Loc; break;
664 case TQ_volatile: TQ_volatileLoc = Loc; break;
665 }
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000666 return false;
667}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000668
John McCall49bfce42009-08-03 20:12:06 +0000669bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
670 unsigned &DiagID) {
Chris Lattnera925dc62006-11-28 04:33:46 +0000671 // 'inline inline' is ok.
672 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000673 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000674 return false;
675}
676
John McCall49bfce42009-08-03 20:12:06 +0000677bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
678 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000679 // 'virtual virtual' is ok.
680 FS_virtual_specified = true;
681 FS_virtualLoc = Loc;
682 return false;
683}
684
John McCall49bfce42009-08-03 20:12:06 +0000685bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
686 unsigned &DiagID) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000687 // 'explicit explicit' is ok.
688 FS_explicit_specified = true;
689 FS_explicitLoc = Loc;
690 return false;
691}
692
John McCall49bfce42009-08-03 20:12:06 +0000693bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
694 unsigned &DiagID) {
Anders Carlssoncd8db412009-05-06 04:46:28 +0000695 if (Friend_specified) {
696 PrevSpec = "friend";
John McCall49bfce42009-08-03 20:12:06 +0000697 DiagID = diag::ext_duplicate_declspec;
Anders Carlssoncd8db412009-05-06 04:46:28 +0000698 return true;
699 }
John McCall49bfce42009-08-03 20:12:06 +0000700
Anders Carlssoncd8db412009-05-06 04:46:28 +0000701 Friend_specified = true;
702 FriendLoc = Loc;
703 return false;
704}
Chris Lattnera925dc62006-11-28 04:33:46 +0000705
Douglas Gregor26701a42011-09-09 02:06:17 +0000706bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
707 unsigned &DiagID) {
708 if (isModulePrivateSpecified()) {
709 PrevSpec = "__module_private__";
710 DiagID = diag::ext_duplicate_declspec;
711 return true;
712 }
713
714 ModulePrivateLoc = Loc;
715 return false;
716}
717
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000718bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
719 unsigned &DiagID) {
720 // 'constexpr constexpr' is ok.
721 Constexpr_specified = true;
722 ConstexprLoc = Loc;
723 return false;
724}
725
John McCall48871652010-08-21 09:40:31 +0000726void DeclSpec::setProtocolQualifiers(Decl * const *Protos,
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000727 unsigned NP,
728 SourceLocation *ProtoLocs,
729 SourceLocation LAngleLoc) {
730 if (NP == 0) return;
John McCall48871652010-08-21 09:40:31 +0000731 ProtocolQualifiers = new Decl*[NP];
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000732 ProtocolLocs = new SourceLocation[NP];
John McCall48871652010-08-21 09:40:31 +0000733 memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP);
Argyrios Kyrtzidis5ec645b2009-09-29 19:42:11 +0000734 memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP);
735 NumProtocolQualifiers = NP;
736 ProtocolLAngleLoc = LAngleLoc;
737}
738
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000739void DeclSpec::SaveWrittenBuiltinSpecs() {
740 writtenBS.Sign = getTypeSpecSign();
741 writtenBS.Width = getTypeSpecWidth();
742 writtenBS.Type = getTypeSpecType();
743 // Search the list of attributes for the presence of a mode attribute.
744 writtenBS.ModeAttr = false;
John McCall53fa7142010-12-24 02:08:15 +0000745 AttributeList* attrs = getAttributes().getList();
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000746 while (attrs) {
747 if (attrs->getKind() == AttributeList::AT_mode) {
748 writtenBS.ModeAttr = true;
749 break;
750 }
751 attrs = attrs->getNext();
752 }
753}
754
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000755void DeclSpec::SaveStorageSpecifierAsWritten() {
756 if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern)
757 // If 'extern' is part of a linkage specification,
758 // then it is not a storage class "as written".
759 StorageClassSpecAsWritten = SCS_unspecified;
760 else
761 StorageClassSpecAsWritten = StorageClassSpec;
762}
763
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000764/// Finish - This does final analysis of the declspec, rejecting things like
765/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
766/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
767/// DeclSpec is guaranteed self-consistent, even if an error occurred.
David Blaikie9c902b52011-09-25 23:23:43 +0000768void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000769 // Before possibly changing their values, save specs as written.
770 SaveWrittenBuiltinSpecs();
Douglas Gregorc4df4072010-04-19 22:54:31 +0000771 SaveStorageSpecifierAsWritten();
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000772
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000773 // Check the type specifier components first.
774
Chris Lattner37141f42010-06-23 06:00:24 +0000775 // Validate and finalize AltiVec vector declspec.
776 if (TypeAltiVecVector) {
777 if (TypeAltiVecBool) {
778 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
779 if (TypeSpecSign != TSS_unspecified) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000780 Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +0000781 << getSpecifierName((TSS)TypeSpecSign);
782 }
783
784 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sandsd3e231e2010-06-23 19:34:52 +0000785 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
786 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000787 Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +0000788 << (TypeAltiVecPixel ? "__pixel" :
789 getSpecifierName((TST)TypeSpecType));
790 }
791
792 // Only 'short' is valid with vector bool. (PIM 2.1)
793 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short))
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000794 Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +0000795 << getSpecifierName((TSW)TypeSpecWidth);
796
797 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
798 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
799 (TypeSpecWidth != TSW_unspecified))
800 TypeSpecSign = TSS_unsigned;
801 }
802
803 if (TypeAltiVecPixel) {
804 //TODO: perform validation
805 TypeSpecType = TST_int;
806 TypeSpecSign = TSS_unsigned;
807 TypeSpecWidth = TSW_short;
John McCalla3707cc2010-08-26 17:22:34 +0000808 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +0000809 }
810 }
811
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000812 // signed/unsigned are only valid with int/char/wchar_t.
Chris Lattner839713c2006-08-04 06:15:52 +0000813 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000814 if (TypeSpecType == TST_unspecified)
815 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000816 else if (TypeSpecType != TST_int &&
817 TypeSpecType != TST_char && TypeSpecType != TST_wchar) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000818 Diag(D, TSSLoc, diag::err_invalid_sign_spec)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000819 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000820 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +0000821 TypeSpecSign = TSS_unspecified;
822 }
823 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000824
Chris Lattner839713c2006-08-04 06:15:52 +0000825 // Validate the width of the type.
826 switch (TypeSpecWidth) {
827 case TSW_unspecified: break;
828 case TSW_short: // short int
829 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000830 if (TypeSpecType == TST_unspecified)
831 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
832 else if (TypeSpecType != TST_int) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000833 Diag(D, TSWLoc,
Chris Lattner36982e42007-05-16 17:49:37 +0000834 TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000835 : diag::err_invalid_longlong_spec)
836 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000837 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000838 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000839 }
840 break;
841 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000842 if (TypeSpecType == TST_unspecified)
843 TypeSpecType = TST_int; // long -> long int.
844 else if (TypeSpecType != TST_int && TypeSpecType != TST_double) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000845 Diag(D, TSWLoc, diag::err_invalid_long_spec)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000846 << getSpecifierName((TST)TypeSpecType);
Chris Lattner839713c2006-08-04 06:15:52 +0000847 TypeSpecType = TST_int;
John McCalla3707cc2010-08-26 17:22:34 +0000848 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +0000849 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000850 break;
Chris Lattner839713c2006-08-04 06:15:52 +0000851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner0e894622006-08-13 19:58:17 +0000853 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000854 // disallow their use. Need information about the backend.
855 if (TypeSpecComplex != TSC_unspecified) {
856 if (TypeSpecType == TST_unspecified) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000857 Diag(D, TSCLoc, diag::ext_plain_complex)
Douglas Gregora771f462010-03-31 17:46:05 +0000858 << FixItHint::CreateInsertion(
Douglas Gregore3e01a22009-04-01 22:41:11 +0000859 PP.getLocForEndOfToken(getTypeSpecComplexLoc()),
860 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000861 TypeSpecType = TST_double; // _Complex -> _Complex double.
862 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000863 // Note that this intentionally doesn't include _Complex _Bool.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000864 Diag(D, TSTLoc, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000865 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000866 Diag(D, TSCLoc, diag::err_invalid_complex_spec)
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000867 << getSpecifierName((TST)TypeSpecType);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +0000868 TypeSpecComplex = TSC_unspecified;
869 }
870 }
Chris Lattner8e90ef62006-08-05 03:30:45 +0000871
Richard Smith58c74332011-09-04 19:54:14 +0000872 // If no type specifier was provided and we're parsing a language where
873 // the type specifier is not optional, but we got 'auto' as a storage
874 // class specifier, then assume this is an attempt to use C++0x's 'auto'
875 // type specifier.
876 // FIXME: Does Microsoft really support implicit int in C++?
Francois Pichet0706d202011-09-17 17:15:52 +0000877 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().MicrosoftExt &&
Richard Smith58c74332011-09-04 19:54:14 +0000878 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
879 TypeSpecType = TST_auto;
880 StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified;
881 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
882 StorageClassSpecLoc = SourceLocation();
883 }
884 // Diagnose if we've recovered from an ill-formed 'auto' storage class
885 // specifier in a pre-C++0x dialect of C++.
886 if (!PP.getLangOptions().CPlusPlus0x && TypeSpecType == TST_auto)
887 Diag(D, TSTLoc, diag::ext_auto_type_specifier);
888 if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().CPlusPlus0x &&
889 StorageClassSpec == SCS_auto)
890 Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class)
891 << FixItHint::CreateRemoval(StorageClassSpecLoc);
892
John McCall07e91c02009-08-06 02:15:43 +0000893 // C++ [class.friend]p6:
894 // No storage-class-specifier shall appear in the decl-specifier-seq
895 // of a friend declaration.
896 if (isFriendSpecified() && getStorageClassSpec()) {
897 DeclSpec::SCS SC = getStorageClassSpec();
898 const char *SpecName = getSpecifierName(SC);
899
900 SourceLocation SCLoc = getStorageClassSpecLoc();
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000901 SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName));
John McCall07e91c02009-08-06 02:15:43 +0000902
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000903 Diag(D, SCLoc, diag::err_friend_storage_spec)
John McCall07e91c02009-08-06 02:15:43 +0000904 << SpecName
Douglas Gregora771f462010-03-31 17:46:05 +0000905 << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc));
John McCall07e91c02009-08-06 02:15:43 +0000906
907 ClearStorageClassSpecs();
908 }
909
Douglas Gregor64226422011-09-09 21:14:29 +0000910 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
911
Chris Lattner8e90ef62006-08-05 03:30:45 +0000912 // Okay, now we can infer the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000913
Chris Lattner0e894622006-08-13 19:58:17 +0000914 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner1ae23292006-08-04 06:42:31 +0000916 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000917}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +0000918
Sebastian Redla2b5e312008-12-28 15:28:59 +0000919bool DeclSpec::isMissingDeclaratorOk() {
920 TST tst = getTypeSpecType();
John McCallba7bf592010-08-24 05:47:05 +0000921 return isDeclRep(tst) && getRepAsDecl() != 0 &&
922 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +0000923}
Douglas Gregor7861a802009-11-03 01:35:08 +0000924
925void UnqualifiedId::clear() {
Douglas Gregor7861a802009-11-03 01:35:08 +0000926 Kind = IK_Identifier;
927 Identifier = 0;
928 StartLocation = SourceLocation();
929 EndLocation = SourceLocation();
930}
931
932void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
933 OverloadedOperatorKind Op,
934 SourceLocation SymbolLocations[3]) {
935 Kind = IK_OperatorFunctionId;
936 StartLocation = OperatorLoc;
937 EndLocation = OperatorLoc;
938 OperatorFunctionId.Operator = Op;
939 for (unsigned I = 0; I != 3; ++I) {
940 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
941
942 if (SymbolLocations[I].isValid())
943 EndLocation = SymbolLocations[I];
944 }
945}
Anders Carlsson56104902011-01-17 03:05:47 +0000946
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000947bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlssonf2ca3892011-01-22 15:58:16 +0000948 const char *&PrevSpec) {
Douglas Gregorf2f08062011-03-08 17:10:18 +0000949 LastLocation = Loc;
950
Anders Carlsson56104902011-01-17 03:05:47 +0000951 if (Specifiers & VS) {
952 PrevSpec = getSpecifierName(VS);
953 return true;
954 }
955
956 Specifiers |= VS;
957
958 switch (VS) {
David Blaikie83d382b2011-09-23 05:06:16 +0000959 default: llvm_unreachable("Unknown specifier!");
Anders Carlsson56104902011-01-17 03:05:47 +0000960 case VS_Override: VS_overrideLoc = Loc; break;
961 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlsson56104902011-01-17 03:05:47 +0000962 }
Anders Carlssonf2ca3892011-01-22 15:58:16 +0000963
Anders Carlsson56104902011-01-17 03:05:47 +0000964 return false;
965}
966
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000967const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssona6d35012011-01-22 15:11:37 +0000968 switch (VS) {
David Blaikie83d382b2011-09-23 05:06:16 +0000969 default: llvm_unreachable("Unknown specifier");
Anders Carlssona6d35012011-01-22 15:11:37 +0000970 case VS_Override: return "override";
971 case VS_Final: return "final";
Anders Carlssona6d35012011-01-22 15:11:37 +0000972 }
973}