 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
87.2% |
2554 / 2928 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
98.6% |
2886 / 2928 |
| |
|
Line Coverage: |
95.1% |
2406 / 2531 |
| |
 |
|
 |
1 : //===--- SemaDecl.cpp - Semantic Analysis for Declarations ----------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file implements semantic analysis for declarations.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "Sema.h"
15 : #include "SemaInit.h"
16 : #include "Lookup.h"
17 : #include "clang/Analysis/AnalysisContext.h"
18 : #include "clang/AST/APValue.h"
19 : #include "clang/AST/ASTConsumer.h"
20 : #include "clang/AST/ASTContext.h"
21 : #include "clang/AST/CXXInheritance.h"
22 : #include "clang/AST/DeclTemplate.h"
23 : #include "clang/AST/ExprCXX.h"
24 : #include "clang/AST/StmtCXX.h"
25 : #include "clang/Parse/DeclSpec.h"
26 : #include "clang/Parse/ParseDiagnostic.h"
27 : #include "clang/Parse/Template.h"
28 : #include "clang/Basic/PartialDiagnostic.h"
29 : #include "clang/Basic/SourceManager.h"
30 : #include "clang/Basic/TargetInfo.h"
31 : // FIXME: layering (ideally, Sema shouldn't be dependent on Lex API's)
32 : #include "clang/Lex/Preprocessor.h"
33 : #include "clang/Lex/HeaderSearch.h"
34 : #include "llvm/ADT/Triple.h"
35 : #include <algorithm>
36 : #include <cstring>
37 : #include <functional>
38 : using namespace clang;
39 :
40 : /// getDeclName - Return a pretty name for the specified decl if possible, or
41 : /// an empty string if not. This is used for pretty crash reporting.
42 0: std::string Sema::getDeclName(DeclPtrTy d) {
43 0: Decl *D = d.getAs<Decl>();
0: branch 1 not taken
0: branch 2 not taken
44 0: if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(D))
45 0: return DN->getQualifiedNameAsString();
46 0: return "";
47 : }
48 :
49 20505: Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(DeclPtrTy Ptr) {
50 20505: return DeclGroupPtrTy::make(DeclGroupRef(Ptr.getAs<Decl>()));
51 : }
52 :
53 : /// \brief If the identifier refers to a type name within this scope,
54 : /// return the declaration of that type.
55 : ///
56 : /// This routine performs ordinary name lookup of the identifier II
57 : /// within the given scope, with optional C++ scope specifier SS, to
58 : /// determine whether the name refers to a type. If so, returns an
59 : /// opaque pointer (actually a QualType) corresponding to that
60 : /// type. Otherwise, returns NULL.
61 : ///
62 : /// If name lookup results in an ambiguity, this routine will complain
63 : /// and then return NULL.
64 : Sema::TypeTy *Sema::getTypeName(IdentifierInfo &II, SourceLocation NameLoc,
65 : Scope *S, const CXXScopeSpec *SS,
66 : bool isClassName,
67 33416: TypeTy *ObjectTypePtr) {
68 : // Determine where we will perform name lookup.
69 33416: DeclContext *LookupCtx = 0;
13: branch 0 taken
33403: branch 1 taken
70 33416: if (ObjectTypePtr) {
71 13: QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr);
7: branch 2 taken
6: branch 3 taken
72 13: if (ObjectType->isRecordType())
73 7: LookupCtx = computeDeclContext(ObjectType);
22739: branch 0 taken
10664: branch 1 taken
1015: branch 3 taken
21724: branch 4 taken
1015: branch 5 taken
32388: branch 6 taken
74 33403: } else if (SS && SS->isSet()) {
75 1015: LookupCtx = computeDeclContext(*SS, false);
76 :
77: branch 0 taken
938: branch 1 taken
77 1015: if (!LookupCtx) {
77: branch 1 taken
0: branch 2 not taken
78 77: if (isDependentScopeSpecifier(*SS)) {
79 : // C++ [temp.res]p3:
80 : // A qualified-id that refers to a type and in which the
81 : // nested-name-specifier depends on a template-parameter (14.6.2)
82 : // shall be prefixed by the keyword typename to indicate that the
83 : // qualified-id denotes a type, forming an
84 : // elaborated-type-specifier (7.1.5.3).
85 : //
86 : // We therefore do not perform any name lookup if the result would
87 : // refer to a member of an unknown specialization.
74: branch 0 taken
3: branch 1 taken
88 77: if (!isClassName)
89 74: return 0;
90 :
91 : // We know from the grammar that this name refers to a type, so build a
92 : // TypenameType node to describe the type.
93 : // FIXME: Record somewhere that this TypenameType node has no "typename"
94 : // keyword associated with it.
95 : return CheckTypenameType((NestedNameSpecifier *)SS->getScopeRep(),
96 3: II, SS->getRange()).getAsOpaquePtr();
97 : }
98 :
99 0: return 0;
100 : }
101 :
913: branch 1 taken
25: branch 2 taken
11: branch 4 taken
902: branch 5 taken
11: branch 6 taken
927: branch 7 taken
102 938: if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(*SS))
103 11: return 0;
104 : }
105 :
106 : // FIXME: LookupNestedNameSpecifierName isn't the right kind of
107 : // lookup for class-names.
108 : LookupNameKind Kind = isClassName ? LookupNestedNameSpecifierName :
815: branch 0 taken
32513: branch 1 taken
109 33328: LookupOrdinaryName;
110 33328: LookupResult Result(*this, &II, NameLoc, Kind);
934: branch 0 taken
32394: branch 1 taken
111 33328: if (LookupCtx) {
112 : // Perform "qualified" name lookup into the declaration context we
113 : // computed, which is either the type of the base of a member access
114 : // expression or the declaration context associated with a prior
115 : // nested-name-specifier.
116 934: LookupQualifiedName(Result, LookupCtx);
117 :
7: branch 0 taken
927: branch 1 taken
2: branch 3 taken
5: branch 4 taken
2: branch 5 taken
932: branch 6 taken
118 934: if (ObjectTypePtr && Result.empty()) {
119 : // C++ [basic.lookup.classref]p3:
120 : // If the unqualified-id is ~type-name, the type-name is looked up
121 : // in the context of the entire postfix-expression. If the type T of
122 : // the object expression is of a class type C, the type-name is also
123 : // looked up in the scope of class C. At least one of the lookups shall
124 : // find a name that refers to (possibly cv-qualified) T.
125 2: LookupName(Result, S);
126 : }
127 : } else {
128 : // Perform unqualified name lookup.
129 32394: LookupName(Result, S);
130 : }
131 :
132 33328: NamedDecl *IIDecl = 0;
1505: branch 1 taken
19: branch 2 taken
31804: branch 3 taken
0: branch 4 not taken
133 33328: switch (Result.getResultKind()) {
134 : case LookupResult::NotFound:
135 : case LookupResult::NotFoundInCurrentInstantiation:
136 : case LookupResult::FoundOverloaded:
137 : case LookupResult::FoundUnresolvedValue:
138 1505: Result.suppressDiagnostics();
139 1505: return 0;
140 :
141 : case LookupResult::Ambiguous:
142 : // Recover from type-hiding ambiguities by hiding the type. We'll
143 : // do the lookup again when looking for an object, and we can
144 : // diagnose the error then. If we don't do this, then the error
145 : // about hiding the type will be immediately followed by an error
146 : // that only makes sense if the identifier was treated like a type.
1: branch 1 taken
18: branch 2 taken
147 19: if (Result.getAmbiguityKind() == LookupResult::AmbiguousTagHiding) {
148 1: Result.suppressDiagnostics();
149 1: return 0;
150 : }
151 :
152 : // Look to see if we have a type anywhere in the list of results.
36: branch 4 taken
18: branch 5 taken
153 54: for (LookupResult::iterator Res = Result.begin(), ResEnd = Result.end();
154 : Res != ResEnd; ++Res) {
21: branch 2 taken
15: branch 3 taken
0: branch 6 not taken
21: branch 7 taken
15: branch 8 taken
21: branch 9 taken
155 36: if (isa<TypeDecl>(*Res) || isa<ObjCInterfaceDecl>(*Res)) {
8: branch 0 taken
7: branch 1 taken
1: branch 7 taken
7: branch 8 taken
8: branch 9 taken
7: branch 10 taken
156 15: if (!IIDecl ||
157 : (*Res)->getLocation().getRawEncoding() <
158 : IIDecl->getLocation().getRawEncoding())
159 8: IIDecl = *Res;
160 : }
161 : }
162 :
11: branch 0 taken
7: branch 1 taken
163 18: if (!IIDecl) {
164 : // None of the entities we found is a type, so there is no way
165 : // to even assume that the result is a type. In this case, don't
166 : // complain about the ambiguity. The parser will either try to
167 : // perform this lookup again (e.g., as an object name), which
168 : // will produce the ambiguity, or will complain that it expected
169 : // a type name.
170 11: Result.suppressDiagnostics();
171 11: return 0;
172 : }
173 :
174 : // We found a type within the ambiguous lookup; diagnose the
175 : // ambiguity and then return that type. This might be the right
176 : // answer, or it might not be, but it suppresses any attempt to
177 : // perform the name lookup again.
178 7: break;
179 :
180 : case LookupResult::Found:
181 31804: IIDecl = Result.getFoundDecl();
182 : break;
183 : }
184 :
0: branch 0 not taken
31811: branch 1 taken
185 31811: assert(IIDecl && "Didn't find decl");
186 :
187 31811: QualType T;
20212: branch 1 taken
11599: branch 2 taken
188 31811: if (TypeDecl *TD = dyn_cast<TypeDecl>(IIDecl)) {
189 20212: DiagnoseUseOfDecl(IIDecl, NameLoc);
190 :
191 : // C++ [temp.local]p2:
192 : // Within the scope of a class template specialization or
193 : // partial specialization, when the injected-class-name is
194 : // not followed by a <, it is equivalent to the
195 : // injected-class-name followed by the template-argument s
196 : // of the class template specialization or partial
197 : // specialization enclosed in <>.
5430: branch 1 taken
14782: branch 2 taken
198 20212: if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD))
1805: branch 1 taken
3625: branch 2 taken
199 5430: if (RD->isInjectedClassName())
281: branch 1 taken
1524: branch 2 taken
200 1805: if (ClassTemplateDecl *Template = RD->getDescribedClassTemplate())
201 281: T = Template->getInjectedClassNameType(Context);
202 :
19931: branch 1 taken
281: branch 2 taken
203 20212: if (T.isNull())
204 19931: T = Context.getTypeDeclType(TD);
205 :
11792: branch 0 taken
8420: branch 1 taken
206 20212: if (SS)
207 11792: T = getQualifiedNameType(*SS, T);
208 :
2472: branch 1 taken
9127: branch 2 taken
209 11599: } else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) {
210 2472: T = Context.getObjCInterfaceType(IDecl);
0: branch 0 not taken
9127: branch 1 taken
211 9127: } else if (UnresolvedUsingTypenameDecl *UUDecl =
212 9127: dyn_cast<UnresolvedUsingTypenameDecl>(IIDecl)) {
213 : // FIXME: preserve source structure information.
214 0: T = Context.getTypenameType(UUDecl->getTargetNestedNameSpecifier(), &II);
215 : } else {
216 : // If it's not plausibly a type, suppress diagnostics.
217 9127: Result.suppressDiagnostics();
218 9127: return 0;
219 : }
220 :
221 22684: return T.getAsOpaquePtr();
222 : }
223 :
224 : /// isTagName() - This method is called *for error recovery purposes only*
225 : /// to determine if the specified name is a valid tag name ("struct foo"). If
226 : /// so, this returns the TST for the tag corresponding to it (TST_enum,
227 : /// TST_union, TST_struct, TST_class). This is used to diagnose cases in C
228 : /// where the user forgot to specify the tag.
229 17: DeclSpec::TST Sema::isTagName(IdentifierInfo &II, Scope *S) {
230 : // Do a tag name lookup in this scope.
231 17: LookupResult R(*this, &II, SourceLocation(), LookupTagName);
232 17: LookupName(R, S, false);
233 17: R.suppressDiagnostics();
2: branch 1 taken
15: branch 2 taken
234 17: if (R.getResultKind() == LookupResult::Found)
2: branch 1 taken
0: branch 2 not taken
235 2: if (const TagDecl *TD = R.getAsSingle<TagDecl>()) {
1: branch 1 taken
0: branch 2 not taken
0: branch 3 not taken
1: branch 4 taken
0: branch 5 not taken
236 2: switch (TD->getTagKind()) {
237 1: case TagDecl::TK_struct: return DeclSpec::TST_struct;
238 0: case TagDecl::TK_union: return DeclSpec::TST_union;
239 0: case TagDecl::TK_class: return DeclSpec::TST_class;
240 1: case TagDecl::TK_enum: return DeclSpec::TST_enum;
241 : }
242 : }
243 :
244 15: return DeclSpec::TST_unspecified;
245 : }
246 :
247 : bool Sema::DiagnoseUnknownTypeName(const IdentifierInfo &II,
248 : SourceLocation IILoc,
249 : Scope *S,
250 : const CXXScopeSpec *SS,
251 31: TypeTy *&SuggestedType) {
252 : // We don't have anything to suggest (yet).
253 31: SuggestedType = 0;
254 :
255 : // There may have been a typo in the name of the type. Look up typo
256 : // results, in case we have something that we can suggest.
257 : LookupResult Lookup(*this, &II, IILoc, LookupOrdinaryName,
258 31: NotForRedeclaration);
259 :
260 : // FIXME: It would be nice if we could correct for typos in built-in
261 : // names, such as "itn" for "int".
262 :
4: branch 1 taken
27: branch 2 taken
4: branch 4 taken
0: branch 5 not taken
4: branch 6 taken
27: branch 7 taken
263 31: if (CorrectTypo(Lookup, S, SS) && Lookup.isSingleResult()) {
264 4: NamedDecl *Result = Lookup.getAsSingle<NamedDecl>();
0: branch 1 not taken
4: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
4: branch 7 taken
0: branch 8 not taken
4: branch 9 taken
0: branch 10 not taken
265 4: if ((isa<TypeDecl>(Result) || isa<ObjCInterfaceDecl>(Result)) &&
266 : !Result->isInvalidDecl()) {
267 : // We found a similarly-named type or interface; suggest that.
2: branch 0 taken
2: branch 1 taken
0: branch 3 not taken
2: branch 4 taken
2: branch 5 taken
2: branch 6 taken
268 4: if (!SS || !SS->isSet())
269 : Diag(IILoc, diag::err_unknown_typename_suggest)
270 : << &II << Lookup.getLookupName()
271 : << CodeModificationHint::CreateReplacement(SourceRange(IILoc),
272 2: Result->getNameAsString());
2: branch 1 taken
0: branch 2 not taken
273 2: else if (DeclContext *DC = computeDeclContext(*SS, false))
274 : Diag(IILoc, diag::err_unknown_nested_typename_suggest)
275 : << &II << DC << Lookup.getLookupName() << SS->getRange()
276 : << CodeModificationHint::CreateReplacement(SourceRange(IILoc),
277 2: Result->getNameAsString());
278 : else
279 0: llvm_unreachable("could not have corrected a typo here");
280 :
281 : Diag(Result->getLocation(), diag::note_previous_decl)
282 4: << Result->getDeclName();
283 :
284 4: SuggestedType = getTypeName(*Result->getIdentifier(), IILoc, S, SS);
285 4: return true;
286 : }
287 : }
288 :
289 : // FIXME: Should we move the logic that tries to recover from a missing tag
290 : // (struct, union, enum) from Parser::ParseImplicitInt here, instead?
291 :
14: branch 0 taken
13: branch 1 taken
2: branch 3 taken
12: branch 4 taken
0: branch 6 not taken
2: branch 7 taken
13: branch 8 taken
14: branch 9 taken
292 27: if (!SS || (!SS->isSet() && !SS->isInvalid()))
293 13: Diag(IILoc, diag::err_unknown_typename) << &II;
10: branch 1 taken
4: branch 2 taken
294 14: else if (DeclContext *DC = computeDeclContext(*SS, false))
295 : Diag(IILoc, diag::err_typename_nested_not_found)
296 10: << &II << DC << SS->getRange();
2: branch 1 taken
2: branch 2 taken
297 4: else if (isDependentScopeSpecifier(*SS)) {
298 : Diag(SS->getRange().getBegin(), diag::err_typename_missing)
299 : << (NestedNameSpecifier *)SS->getScopeRep() << II.getName()
300 : << SourceRange(SS->getRange().getBegin(), IILoc)
301 : << CodeModificationHint::CreateInsertion(SS->getRange().getBegin(),
302 2: "typename ");
303 2: SuggestedType = ActOnTypenameType(SourceLocation(), *SS, II, IILoc).get();
304 : } else {
305 : assert(SS && SS->isInvalid() &&
2: branch 0 taken
0: branch 1 not taken
2: branch 3 taken
0: branch 4 not taken
306 2: "Invalid scope specifier has already been diagnosed");
307 : }
308 :
309 27: return true;
310 : }
311 :
312 : // Determines the context to return to after temporarily entering a
313 : // context. This depends in an unnecessarily complicated way on the
314 : // exact ordering of callbacks from the parser.
315 32830: DeclContext *Sema::getContainingDC(DeclContext *DC) {
316 :
317 : // Functions defined inline within classes aren't parsed until we've
318 : // finished parsing the top-level class, so the top-level class is
319 : // the context we'll need to return to.
16432: branch 1 taken
16398: branch 2 taken
320 32830: if (isa<FunctionDecl>(DC)) {
321 16432: DC = DC->getLexicalParent();
322 :
323 : // A function not defined within a class will always return to its
324 : // lexical context.
14336: branch 1 taken
2096: branch 2 taken
325 16432: if (!isa<CXXRecordDecl>(DC))
326 14336: return DC;
327 :
328 : // A C++ inline method/friend is parsed *after* the topmost class
329 : // it was declared in is fully parsed ("complete"); the topmost
330 : // class is the context we need to return to.
60: branch 2 taken
2096: branch 3 taken
331 2216: while (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC->getLexicalParent()))
60: branch 0 taken
0: branch 1 not taken
332 60: DC = RD;
333 :
334 : // Return the declaration context of the topmost class the inline method is
335 : // declared in.
336 2096: return DC;
337 : }
338 :
1826: branch 1 taken
14572: branch 2 taken
339 16398: if (isa<ObjCMethodDecl>(DC))
1826: branch 1 taken
0: branch 2 not taken
340 1826: return Context.getTranslationUnitDecl();
341 :
342 14572: return DC->getLexicalParent();
343 : }
344 :
345 17533: void Sema::PushDeclContext(Scope *S, DeclContext *DC) {
346 : assert(getContainingDC(DC) == CurContext &&
17533: branch 1 taken
0: branch 2 not taken
347 17533: "The next DeclContext should be lexically contained in the current one.");
348 17533: CurContext = DC;
349 17533: S->setEntity(DC);
350 17533: }
351 :
352 15297: void Sema::PopDeclContext() {
0: branch 0 not taken
15297: branch 1 taken
353 15297: assert(CurContext && "DeclContext imbalance!");
354 :
355 15297: CurContext = getContainingDC(CurContext);
356 15297: }
357 :
358 : /// EnterDeclaratorContext - Used when we must lookup names in the context
359 : /// of a declarator's nested name specifier.
360 : ///
361 528: void Sema::EnterDeclaratorContext(Scope *S, DeclContext *DC) {
362 : // C++0x [basic.lookup.unqual]p13:
363 : // A name used in the definition of a static data member of class
364 : // X (after the qualified-id of the static member) is looked up as
365 : // if the name was used in a member function of X.
366 : // C++0x [basic.lookup.unqual]p14:
367 : // If a variable member of a namespace is defined outside of the
368 : // scope of its namespace then any name used in the definition of
369 : // the variable member (after the declarator-id) is looked up as
370 : // if the definition of the variable member occurred in its
371 : // namespace.
372 : // Both of these imply that we should push a scope whose context
373 : // is the semantic context of the declaration. We can't use
374 : // PushDeclContext here because that context is not necessarily
375 : // lexically contained in the current context. Fortunately,
376 : // the containing scope should have the appropriate information.
377 :
528: branch 1 taken
0: branch 2 not taken
378 528: assert(!S->getEntity() && "scope already has entity");
379 :
380 : #ifndef NDEBUG
381 528: Scope *Ancestor = S->getParent();
211: branch 2 taken
528: branch 3 taken
382 739: while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent();
528: branch 1 taken
0: branch 2 not taken
383 528: assert(Ancestor->getEntity() == CurContext && "ancestor context mismatch");
384 : #endif
385 :
386 528: CurContext = DC;
387 528: S->setEntity(DC);
388 528: }
389 :
390 528: void Sema::ExitDeclaratorContext(Scope *S) {
528: branch 1 taken
0: branch 2 not taken
391 528: assert(S->getEntity() == CurContext && "Context imbalance!");
392 :
393 : // Switch back to the lexical context. The safety of this is
394 : // enforced by an assert in EnterDeclaratorContext.
395 528: Scope *Ancestor = S->getParent();
211: branch 2 taken
528: branch 3 taken
396 739: while (!Ancestor->getEntity()) Ancestor = Ancestor->getParent();
397 528: CurContext = (DeclContext*) Ancestor->getEntity();
398 :
399 : // We don't need to do anything with the scope, which is going to
400 : // disappear.
401 528: }
402 :
403 : /// \brief Determine whether we allow overloading of the function
404 : /// PrevDecl with another declaration.
405 : ///
406 : /// This routine determines whether overloading is possible, not
407 : /// whether some new function is actually an overload. It will return
408 : /// true in C++ (where we can always provide overloads) or, as an
409 : /// extension, in C when the previous function is already an
410 : /// overloaded function declaration or has the "overloadable"
411 : /// attribute.
412 : static bool AllowOverloadingOfFunction(LookupResult &Previous,
413 1450: ASTContext &Context) {
1082: branch 1 taken
368: branch 2 taken
414 1450: if (Context.getLangOptions().CPlusPlus)
415 1082: return true;
416 :
12: branch 1 taken
356: branch 2 taken
417 368: if (Previous.getResultKind() == LookupResult::FoundOverloaded)
418 12: return true;
419 :
420 : return (Previous.getResultKind() == LookupResult::Found
356: branch 1 taken
0: branch 2 not taken
13: branch 5 taken
343: branch 6 taken
421 356: && Previous.getFoundDecl()->hasAttr<OverloadableAttr>());
422 : }
423 :
424 : /// Add this decl to the scope shadowed decl chains.
425 61248: void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext) {
426 : // Move up the scope chain until we find the nearest enclosing
427 : // non-transparent context. The declaration will be introduced into this
428 : // scope.
60634: branch 1 taken
1467: branch 2 taken
853: branch 5 taken
59781: branch 6 taken
853: branch 7 taken
61248: branch 8 taken
429 123349: while (S->getEntity() &&
430 : ((DeclContext *)S->getEntity())->isTransparentContext())
431 853: S = S->getParent();
432 :
433 : // Add scoped declarations into their context, so that they can be
434 : // found later. Declarations without a context won't be inserted
435 : // into any context.
59387: branch 0 taken
1861: branch 1 taken
436 61248: if (AddToContext)
437 59387: CurContext->addDecl(D);
438 :
439 : // Out-of-line function and variable definitions should not be pushed into
440 : // scope.
445: branch 1 taken
60803: branch 2 taken
426: branch 6 taken
19: branch 7 taken
14325: branch 9 taken
46904: branch 10 taken
14283: branch 13 taken
42: branch 14 taken
13984: branch 17 taken
299: branch 18 taken
18990: branch 20 taken
41898: branch 21 taken
64: branch 24 taken
18926: branch 25 taken
424: branch 26 taken
60824: branch 27 taken
441 61248: if ((isa<FunctionTemplateDecl>(D) &&
442 : cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->isOutOfLine()) ||
443 : (isa<FunctionDecl>(D) &&
444 : (cast<FunctionDecl>(D)->isFunctionTemplateSpecialization() ||
445 : cast<FunctionDecl>(D)->isOutOfLine())) ||
446 : (isa<VarDecl>(D) && cast<VarDecl>(D)->isOutOfLine()))
447 424: return;
448 :
449 : // If this replaces anything in the current scope,
450 60824: IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()),
451 60824: IEnd = IdResolver.end();
7435: branch 2 taken
60052: branch 3 taken
452 67487: for (; I != IEnd; ++I) {
3062: branch 3 taken
4373: branch 4 taken
772: branch 7 taken
2290: branch 8 taken
772: branch 9 taken
6663: branch 10 taken
453 7435: if (S->isDeclScope(DeclPtrTy::make(*I)) && D->declarationReplaces(*I)) {
454 772: S->RemoveDecl(DeclPtrTy::make(*I));
455 772: IdResolver.RemoveDecl(*I);
456 :
457 : // Should only need to replace one decl.
458 772: break;
459 : }
460 : }
461 :
462 60824: S->AddDecl(DeclPtrTy::make(D));
463 60824: IdResolver.AddDecl(D);
464 : }
465 :
466 3122: bool Sema::isDeclInScope(NamedDecl *&D, DeclContext *Ctx, Scope *S) {
467 3122: return IdResolver.isDeclInScope(D, Ctx, Context, S);
468 : }
469 :
470 : static bool isOutOfScopePreviousDeclaration(NamedDecl *,
471 : DeclContext*,
472 : ASTContext&);
473 :
474 : /// Filters out lookup results that don't fall within the given scope
475 : /// as determined by isDeclInScope.
476 : static void FilterLookupForScope(Sema &SemaRef, LookupResult &R,
477 : DeclContext *Ctx, Scope *S,
478 30017: bool ConsiderLinkage) {
479 30017: LookupResult::Filter F = R.makeFilter();
2794: branch 1 taken
30017: branch 2 taken
480 62828: while (F.hasNext()) {
481 2794: NamedDecl *D = F.next();
482 :
2487: branch 1 taken
307: branch 2 taken
483 2794: if (SemaRef.isDeclInScope(D, Ctx, S))
484 2487: continue;
485 :
156: branch 0 taken
151: branch 1 taken
32: branch 3 taken
124: branch 4 taken
32: branch 5 taken
275: branch 6 taken
486 307: if (ConsiderLinkage &&
487 : isOutOfScopePreviousDeclaration(D, Ctx, SemaRef.Context))
488 32: continue;
489 :
490 275: F.erase();
491 : }
492 :
493 30017: F.done();
494 30017: }
495 :
496 480: static bool isUsingDecl(NamedDecl *D) {
497 : return isa<UsingShadowDecl>(D) ||
498 : isa<UnresolvedUsingTypenameDecl>(D) ||
479: branch 1 taken
1: branch 2 taken
479: branch 4 taken
0: branch 5 not taken
1: branch 7 taken
478: branch 8 taken
499 480: isa<UnresolvedUsingValueDecl>(D);
500 : }
501 :
502 : /// Removes using shadow declarations from the lookup results.
503 391: static void RemoveUsingDecls(LookupResult &R) {
504 391: LookupResult::Filter F = R.makeFilter();
480: branch 1 taken
391: branch 2 taken
505 1262: while (F.hasNext())
2: branch 2 taken
478: branch 3 taken
506 480: if (isUsingDecl(F.next()))
507 2: F.erase();
508 :
509 391: F.done();
510 391: }
511 :
512 42060: static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
355: branch 1 taken
41705: branch 2 taken
513 42060: if (D->isInvalidDecl())
514 355: return false;
515 :
30904: branch 1 taken
10801: branch 2 taken
27: branch 4 taken
30877: branch 5 taken
10828: branch 6 taken
30877: branch 7 taken
516 41705: if (D->isUsed() || D->hasAttr<UnusedAttr>())
517 10828: return false;
518 :
519 : // White-list anything that isn't a local variable.
15639: branch 1 taken
15238: branch 2 taken
4583: branch 4 taken
11056: branch 5 taken
3102: branch 7 taken
1481: branch 8 taken
657: branch 11 taken
2445: branch 12 taken
28432: branch 13 taken
2445: branch 14 taken
520 30877: if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D) ||
521 : !D->getDeclContext()->isFunctionOrMethod())
522 28432: return false;
523 :
524 : // Types of valid local variables should be complete, so this should succeed.
2445: branch 1 taken
0: branch 2 not taken
525 2445: if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
341: branch 3 taken
2104: branch 4 taken
526 2445: if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
264: branch 2 taken
77: branch 3 taken
527 341: if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
144: branch 1 taken
120: branch 2 taken
528 264: if (!RD->hasTrivialConstructor())
529 144: return false;
4: branch 1 taken
116: branch 2 taken
530 120: if (!RD->hasTrivialDestructor())
531 4: return false;
532 : }
533 : }
534 : }
535 :
536 2297: return true;
537 : }
538 :
539 23710: void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
23710: branch 1 taken
0: branch 2 not taken
540 23710: if (S->decl_empty()) return;
541 : assert((S->getFlags() & (Scope::DeclScope | Scope::TemplateParamScope)) &&
23710: branch 1 taken
0: branch 2 not taken
542 23710: "Scope shouldn't contain decls!");
543 :
45852: branch 4 taken
23710: branch 5 taken
544 69562: for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end();
545 : I != E; ++I) {
546 45852: Decl *TmpD = (*I).getAs<Decl>();
0: branch 0 not taken
45852: branch 1 taken
547 45852: assert(TmpD && "This decl didn't get pushed??");
548 :
45852: branch 1 taken
0: branch 2 not taken
549 45852: assert(isa<NamedDecl>(TmpD) && "Decl isn't NamedDecl?");
550 45852: NamedDecl *D = cast<NamedDecl>(TmpD);
551 :
42060: branch 2 taken
3792: branch 3 taken
552 45852: if (!D->getDeclName()) continue;
553 :
554 : // Diagnose unused variables in this scope.
2297: branch 1 taken
39763: branch 2 taken
555 42060: if (ShouldDiagnoseUnusedDecl(D))
556 2297: Diag(D->getLocation(), diag::warn_unused_variable) << D->getDeclName();
557 :
558 : // Remove this name from our lexical scope.
559 42060: IdResolver.RemoveDecl(D);
560 : }
561 : }
562 :
563 : /// getObjCInterfaceDecl - Look up a for a class declaration in the scope.
564 : /// return 0 if one not found.
565 : ///
566 : /// \param Id the name of the Objective-C class we're looking for. If
567 : /// typo-correction fixes this name, the Id will be updated
568 : /// to the fixed name.
569 : ///
570 : /// \param RecoverLoc if provided, this routine will attempt to
571 : /// recover from a typo in the name of an existing Objective-C class
572 : /// and, if successful, will return the lookup that results from
573 : /// typo-correction.
574 : ObjCInterfaceDecl *Sema::getObjCInterfaceDecl(IdentifierInfo *&Id,
575 723: SourceLocation RecoverLoc) {
576 : // The third "scope" argument is 0 since we aren't enabling lazy built-in
577 : // creation from this context.
578 723: NamedDecl *IDecl = LookupSingleName(TUScope, Id, LookupOrdinaryName);
579 :
6: branch 0 taken
717: branch 1 taken
6: branch 3 taken
0: branch 4 not taken
6: branch 5 taken
717: branch 6 taken
580 723: if (!IDecl && !RecoverLoc.isInvalid()) {
581 : // Perform typo correction at the given location, but only if we
582 : // find an Objective-C class name.
583 6: LookupResult R(*this, Id, RecoverLoc, LookupOrdinaryName);
1: branch 1 taken
5: branch 2 taken
1: branch 4 taken
0: branch 5 not taken
1: branch 6 taken
5: branch 7 taken
584 6: if (CorrectTypo(R, TUScope, 0) &&
585 : (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
586 : Diag(RecoverLoc, diag::err_undef_interface_suggest)
587 : << Id << IDecl->getDeclName()
588 : << CodeModificationHint::CreateReplacement(RecoverLoc,
589 1: IDecl->getNameAsString());
590 : Diag(IDecl->getLocation(), diag::note_previous_decl)
591 1: << IDecl->getDeclName();
592 :
593 1: Id = IDecl->getIdentifier();
594 6: }
595 : }
596 :
597 723: return dyn_cast_or_null<ObjCInterfaceDecl>(IDecl);
598 : }
599 :
600 : /// getNonFieldDeclScope - Retrieves the innermost scope, starting
601 : /// from S, where a non-field would be declared. This routine copes
602 : /// with the difference between C and C++ scoping rules in structs and
603 : /// unions. For example, the following code is well-formed in C but
604 : /// ill-formed in C++:
605 : /// @code
606 : /// struct S6 {
607 : /// enum { BAR } e;
608 : /// };
609 : ///
610 : /// void test_S6() {
611 : /// struct S6 a;
612 : /// a.e = BAR;
613 : /// }
614 : /// @endcode
615 : /// For the declaration of BAR, this routine will return a different
616 : /// scope. The scope S will be the scope of the unnamed enumeration
617 : /// within S6. In C++, this routine will return the scope associated
618 : /// with S6, because the enumeration's scope is a transparent
619 : /// context but structures can contain non-field names. In C, this
620 : /// routine will return the translation unit scope, since the
621 : /// enumeration's scope is a transparent context and structures cannot
622 : /// contain non-field names.
623 5311: Scope *Sema::getNonFieldDeclScope(Scope *S) {
6629: branch 1 taken
24: branch 2 taken
6602: branch 4 taken
27: branch 5 taken
5346: branch 8 taken
1256: branch 9 taken
286: branch 11 taken
5087: branch 12 taken
62: branch 14 taken
224: branch 15 taken
1342: branch 16 taken
5311: branch 17 taken
624 11964: while (((S->getFlags() & Scope::DeclScope) == 0) ||
625 : (S->getEntity() &&
626 : ((DeclContext *)S->getEntity())->isTransparentContext()) ||
627 : (S->isClassScope() && !getLangOptions().CPlusPlus))
628 1342: S = S->getParent();
629 5311: return S;
630 : }
631 :
632 81: void Sema::InitBuiltinVaListType() {
46: branch 2 taken
35: branch 3 taken
633 81: if (!Context.getBuiltinVaListType().isNull())
634 46: return;
635 :
636 35: IdentifierInfo *VaIdent = &Context.Idents.get("__builtin_va_list");
637 35: NamedDecl *VaDecl = LookupSingleName(TUScope, VaIdent, LookupOrdinaryName);
638 35: TypedefDecl *VaTypedef = cast<TypedefDecl>(VaDecl);
639 35: Context.setBuiltinVaListType(Context.getTypedefType(VaTypedef));
640 : }
641 :
642 : /// LazilyCreateBuiltin - The specified Builtin-ID was first used at
643 : /// file scope. lazily create a decl for it. ForRedeclaration is true
644 : /// if we're creating this built-in in anticipation of redeclaring the
645 : /// built-in.
646 : NamedDecl *Sema::LazilyCreateBuiltin(IdentifierInfo *II, unsigned bid,
647 : Scope *S, bool ForRedeclaration,
648 1405: SourceLocation Loc) {
649 1405: Builtin::ID BID = (Builtin::ID)bid;
650 :
47: branch 1 taken
1358: branch 2 taken
651 1405: if (Context.BuiltinInfo.hasVAListUse(BID))
652 47: InitBuiltinVaListType();
653 :
654 : ASTContext::GetBuiltinTypeError Error;
655 1405: QualType R = Context.GetBuiltinType(BID, Error);
1403: branch 0 taken
2: branch 1 taken
0: branch 2 not taken
0: branch 3 not taken
656 1405: switch (Error) {
657 : case ASTContext::GE_None:
658 : // Okay
659 1403: break;
660 :
661 : case ASTContext::GE_Missing_stdio:
1: branch 0 taken
1: branch 1 taken
662 2: if (ForRedeclaration)
663 : Diag(Loc, diag::err_implicit_decl_requires_stdio)
664 1: << Context.BuiltinInfo.GetName(BID);
665 2: return 0;
666 :
667 : case ASTContext::GE_Missing_setjmp:
0: branch 0 not taken
0: branch 1 not taken
668 0: if (ForRedeclaration)
669 : Diag(Loc, diag::err_implicit_decl_requires_setjmp)
670 0: << Context.BuiltinInfo.GetName(BID);
671 0: return 0;
672 : }
673 :
1272: branch 0 taken
131: branch 1 taken
37: branch 3 taken
1235: branch 4 taken
37: branch 5 taken
1366: branch 6 taken
674 1403: if (!ForRedeclaration && Context.BuiltinInfo.isPredefinedLibFunction(BID)) {
675 : Diag(Loc, diag::ext_implicit_lib_function_decl)
676 : << Context.BuiltinInfo.GetName(BID)
677 37: << R;
37: branch 1 taken
0: branch 2 not taken
37: branch 4 taken
0: branch 5 not taken
37: branch 6 taken
0: branch 7 not taken
678 37: if (Context.BuiltinInfo.getHeaderName(BID) &&
679 : Diags.getDiagnosticLevel(diag::ext_implicit_lib_function_decl)
680 : != Diagnostic::Ignored)
681 : Diag(Loc, diag::note_please_include_header)
682 : << Context.BuiltinInfo.getHeaderName(BID)
683 37: << Context.BuiltinInfo.GetName(BID);
684 : }
685 :
686 : FunctionDecl *New = FunctionDecl::Create(Context,
687 : Context.getTranslationUnitDecl(),
688 : Loc, II, R, /*TInfo=*/0,
689 : FunctionDecl::Extern, false,
1403: branch 2 taken
0: branch 3 not taken
690 1403: /*hasPrototype=*/true);
691 1403: New->setImplicit();
692 :
693 : // Create Decl objects for each parameter, adding them to the
694 : // FunctionDecl.
1335: branch 1 taken
68: branch 2 taken
695 1403: if (FunctionProtoType *FT = dyn_cast<FunctionProtoType>(R)) {
696 1335: llvm::SmallVector<ParmVarDecl*, 16> Params;
2275: branch 1 taken
1335: branch 2 taken
697 3610: for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i)
698 : Params.push_back(ParmVarDecl::Create(Context, New, SourceLocation(), 0,
699 : FT->getArgType(i), /*TInfo=*/0,
2275: branch 2 taken
0: branch 3 not taken
700 2275: VarDecl::None, 0));
701 1335: New->setParams(Context, Params.data(), Params.size());
702 : }
703 :
704 1403: AddKnownFunctionAttributes(New);
705 :
706 : // TUScope is the translation-unit scope to insert this function into.
707 : // FIXME: This is hideous. We need to teach PushOnScopeChains to
708 : // relate Scopes to DeclContexts, and probably eliminate CurContext
709 : // entirely, but we're not there yet.
710 1403: DeclContext *SavedContext = CurContext;
1403: branch 1 taken
0: branch 2 not taken
711 1403: CurContext = Context.getTranslationUnitDecl();
712 1403: PushOnScopeChains(New, TUScope);
713 1403: CurContext = SavedContext;
714 1403: return New;
715 : }
716 :
717 : /// MergeTypeDefDecl - We just parsed a typedef 'New' which has the
718 : /// same name and scope as a previous declaration 'Old'. Figure out
719 : /// how to resolve this situation, merging decls or emitting
720 : /// diagnostics as appropriate. If there was an error, set New to be invalid.
721 : ///
722 168: void Sema::MergeTypeDefDecl(TypedefDecl *New, LookupResult &OldDecls) {
723 : // If the new decl is known invalid already, don't bother doing any
724 : // merging checks.
167: branch 1 taken
1: branch 2 taken
725 168: if (New->isInvalidDecl()) return;
726 :
727 : // Allow multiple definitions for ObjC built-in typedefs.
728 : // FIXME: Verify the underlying types are equivalent!
60: branch 1 taken
107: branch 2 taken
729 167: if (getLangOptions().ObjC1) {
730 60: const IdentifierInfo *TypeID = New->getIdentifier();
3: branch 1 taken
17: branch 2 taken
9: branch 3 taken
31: branch 4 taken
0: branch 5 not taken
731 60: switch (TypeID->getLength()) {
732 3: default: break;
733 : case 2:
17: branch 1 taken
0: branch 2 not taken
734 17: if (!TypeID->isStr("id"))
735 0: break;
736 17: Context.ObjCIdRedefinitionType = New->getUnderlyingType();
737 : // Install the built-in type for 'id', ignoring the current definition.
738 17: New->setTypeForDecl(Context.getObjCIdType().getTypePtr());
739 17: return;
740 : case 5:
0: branch 1 not taken
9: branch 2 taken
741 9: if (!TypeID->isStr("Class"))
742 0: break;
743 9: Context.ObjCClassRedefinitionType = New->getUnderlyingType();
744 : // Install the built-in type for 'Class', ignoring the current definition.
745 9: New->setTypeForDecl(Context.getObjCClassType().getTypePtr());
746 9: return;
747 : case 3:
1: branch 1 taken
30: branch 2 taken
748 31: if (!TypeID->isStr("SEL"))
749 1: break;
750 30: Context.ObjCSelRedefinitionType = New->getUnderlyingType();
751 : // Install the built-in type for 'SEL', ignoring the current definition.
752 30: New->setTypeForDecl(Context.getObjCSelType().getTypePtr());
753 30: return;
754 : case 8:
0: branch 1 not taken
0: branch 2 not taken
755 0: if (!TypeID->isStr("Protocol"))
756 0: break;
757 0: Context.setObjCProtoType(New->getUnderlyingType());
758 0: return;
759 : }
760 : // Fall through - the typedef name was not a builtin type.
761 : }
762 :
763 : // Verify the old decl was also a type.
764 111: TypeDecl *Old = OldDecls.getAsSingle<TypeDecl>();
3: branch 0 taken
108: branch 1 taken
765 111: if (!Old) {
766 : Diag(New->getLocation(), diag::err_redefinition_different_kind)
767 3: << New->getDeclName();
768 :
769 3: NamedDecl *OldD = OldDecls.getRepresentativeDecl();
3: branch 2 taken
0: branch 3 not taken
770 3: if (OldD->getLocation().isValid())
771 3: Diag(OldD->getLocation(), diag::note_previous_definition);
772 :
773 3: return New->setInvalidDecl();
774 : }
775 :
776 : // If the old declaration is invalid, just give up here.
0: branch 1 not taken
108: branch 2 taken
777 108: if (Old->isInvalidDecl())
778 0: return New->setInvalidDecl();
779 :
780 : // Determine the "old" type we'll use for checking and diagnostics.
781 108: QualType OldType;
20: branch 1 taken
88: branch 2 taken
782 108: if (TypedefDecl *OldTypedef = dyn_cast<TypedefDecl>(Old))
783 20: OldType = OldTypedef->getUnderlyingType();
784 : else
785 88: OldType = Context.getTypeDeclType(Old);
786 :
787 : // If the typedef types are not identical, reject them in all languages and
788 : // with any extensions enabled.
789 :
95: branch 2 taken
13: branch 3 taken
4: branch 8 taken
91: branch 9 taken
4: branch 10 taken
104: branch 11 taken
790 108: if (OldType != New->getUnderlyingType() &&
791 : Context.getCanonicalType(OldType) !=
792 : Context.getCanonicalType(New->getUnderlyingType())) {
793 : Diag(New->getLocation(), diag::err_redefinition_different_typedef)
794 4: << New->getUnderlyingType() << OldType;
4: branch 2 taken
0: branch 3 not taken
795 4: if (Old->getLocation().isValid())
796 4: Diag(Old->getLocation(), diag::note_previous_definition);
797 4: return New->setInvalidDecl();
798 : }
799 :
800 : // The types match. Link up the redeclaration chain if the old
801 : // declaration was a typedef.
802 : // FIXME: this is a potential source of wierdness if the type
803 : // spellings don't match exactly.
17: branch 1 taken
87: branch 2 taken
804 104: if (isa<TypedefDecl>(Old))
805 17: New->setPreviousDeclaration(cast<TypedefDecl>(Old));
806 :
0: branch 1 not taken
104: branch 2 taken
807 104: if (getLangOptions().Microsoft)
808 0: return;
809 :
96: branch 1 taken
8: branch 2 taken
810 104: if (getLangOptions().CPlusPlus) {
811 : // C++ [dcl.typedef]p2:
812 : // In a given non-class scope, a typedef specifier can be used to
813 : // redefine the name of any type declared in that scope to refer
814 : // to the type to which it already refers.
91: branch 1 taken
5: branch 2 taken
815 96: if (!isa<CXXRecordDecl>(CurContext))
816 91: return;
817 :
818 : // C++0x [dcl.typedef]p4:
819 : // In a given class scope, a typedef specifier can be used to redefine
820 : // any class-name declared in that scope that is not also a typedef-name
821 : // to refer to the type to which it already refers.
822 : //
823 : // This wording came in via DR424, which was a correction to the
824 : // wording in DR56, which accidentally banned code like:
825 : //
826 : // struct S {
827 : // typedef struct A { } A;
828 : // };
829 : //
830 : // in the C++03 standard. We implement the C++0x semantics, which
831 : // allow the above but disallow
832 : //
833 : // struct S {
834 : // typedef int I;
835 : // typedef int I;
836 : // };
837 : //
838 : // since that was the intent of DR56.
3: branch 1 taken
2: branch 2 taken
839 5: if (!isa<TypedefDecl >(Old))
840 3: return;
841 :
842 : Diag(New->getLocation(), diag::err_redefinition)
843 2: << New->getDeclName();
844 2: Diag(Old->getLocation(), diag::note_previous_definition);
845 2: return New->setInvalidDecl();
846 : }
847 :
848 : // If we have a redefinition of a typedef in C, emit a warning. This warning
849 : // is normally mapped to an error, but can be controlled with
850 : // -Wtypedef-redefinition. If either the original or the redefinition is
851 : // in a system header, don't emit this for compatibility with GCC.
8: branch 2 taken
0: branch 3 not taken
3: branch 7 taken
5: branch 8 taken
1: branch 12 taken
2: branch 13 taken
6: branch 14 taken
2: branch 15 taken
852 8: if (PP.getDiagnostics().getSuppressSystemWarnings() &&
853 : (Context.getSourceManager().isInSystemHeader(Old->getLocation()) ||
854 : Context.getSourceManager().isInSystemHeader(New->getLocation())))
855 6: return;
856 :
857 : Diag(New->getLocation(), diag::warn_redefinition_of_typedef)
858 2: << New->getDeclName();
859 2: Diag(Old->getLocation(), diag::note_previous_definition);
860 2: return;
861 : }
862 :
863 : /// DeclhasAttr - returns true if decl Declaration already has the target
864 : /// attribute.
865 : static bool
866 170: DeclHasAttr(const Decl *decl, const Attr *target) {
32: branch 2 taken
164: branch 3 taken
867 196: for (const Attr *attr = decl->getAttrs(); attr; attr = attr->getNext())
6: branch 2 taken
26: branch 3 taken
868 32: if (attr->getKind() == target->getKind())
869 6: return true;
870 :
871 164: return false;
872 : }
873 :
874 : /// MergeAttributes - append attributes from the Old decl to the New one.
875 1082: static void MergeAttributes(Decl *New, Decl *Old, ASTContext &C) {
170: branch 2 taken
1082: branch 3 taken
876 1252: for (const Attr *attr = Old->getAttrs(); attr; attr = attr->getNext()) {
164: branch 1 taken
6: branch 2 taken
163: branch 4 taken
1: branch 5 taken
163: branch 6 taken
7: branch 7 taken
877 170: if (!DeclHasAttr(New, attr) && attr->isMerged()) {
878 163: Attr *NewAttr = attr->clone(C);
879 163: NewAttr->setInherited(true);
880 163: New->addAttr(NewAttr);
881 : }
882 : }
883 1082: }
884 :
885 : /// Used in MergeFunctionDecl to keep track of function parameters in
886 : /// C.
887 2: struct GNUCompatibleParamWarning {
888 : ParmVarDecl *OldParm;
889 : ParmVarDecl *NewParm;
890 : QualType PromotedType;
891 : };
892 :
893 :
894 : /// getSpecialMember - get the special member enum for a method.
895 : static Sema::CXXSpecialMember getSpecialMember(ASTContext &Ctx,
896 4: const CXXMethodDecl *MD) {
2: branch 1 taken
2: branch 2 taken
897 4: if (const CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(MD)) {
1: branch 1 taken
1: branch 2 taken
898 2: if (Ctor->isDefaultConstructor())
899 1: return Sema::CXXDefaultConstructor;
1: branch 1 taken
0: branch 2 not taken
900 1: if (Ctor->isCopyConstructor())
901 1: return Sema::CXXCopyConstructor;
902 : }
903 :
1: branch 1 taken
1: branch 2 taken
904 2: if (isa<CXXDestructorDecl>(MD))
905 1: return Sema::CXXDestructor;
906 :
1: branch 1 taken
0: branch 2 not taken
907 1: assert(MD->isCopyAssignment() && "Must have copy assignment operator");
908 1: return Sema::CXXCopyAssignment;
909 : }
910 :
911 : /// MergeFunctionDecl - We just parsed a function 'New' from
912 : /// declarator D which has the same name and scope as a previous
913 : /// declaration 'Old'. Figure out how to resolve this situation,
914 : /// merging decls or emitting diagnostics as appropriate.
915 : ///
916 : /// In C++, New and Old must be declarations that are not
917 : /// overloaded. Use IsOverload to determine whether New and Old are
918 : /// overloaded, and to select the Old declaration that New should be
919 : /// merged with.
920 : ///
921 : /// Returns true if there was an error, false otherwise.
922 840: bool Sema::MergeFunctionDecl(FunctionDecl *New, Decl *OldD) {
923 : // Verify the old decl was also a function.
924 840: FunctionDecl *Old = 0;
35: branch 0 taken
805: branch 1 taken
925 840: if (FunctionTemplateDecl *OldFunctionTemplate
926 840: = dyn_cast<FunctionTemplateDecl>(OldD))
927 35: Old = OldFunctionTemplate->getTemplatedDecl();
928 : else
929 805: Old = dyn_cast<FunctionDecl>(OldD);
13: branch 0 taken
827: branch 1 taken
930 840: if (!Old) {
5: branch 1 taken
8: branch 2 taken
931 13: if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(OldD)) {
932 5: Diag(New->getLocation(), diag::err_using_decl_conflict_reverse);
933 : Diag(Shadow->getTargetDecl()->getLocation(),
934 5: diag::note_using_decl_target);
935 : Diag(Shadow->getUsingDecl()->getLocation(),
936 5: diag::note_using_decl) << 0;
937 5: return true;
938 : }
939 :
940 : Diag(New->getLocation(), diag::err_redefinition_different_kind)
941 8: << New->getDeclName();
942 8: Diag(OldD->getLocation(), diag::note_previous_definition);
943 8: return true;
944 : }
945 :
946 : // Determine whether the previous declaration was a definition,
947 : // implicit declaration, or a declaration.
948 : diag::kind PrevDiag;
33: branch 1 taken
794: branch 2 taken
949 827: if (Old->isThisDeclarationADefinition())
950 33: PrevDiag = diag::note_previous_definition;
141: branch 1 taken
653: branch 2 taken
951 794: else if (Old->isImplicit())
952 141: PrevDiag = diag::note_previous_implicit_declaration;
953 : else
954 653: PrevDiag = diag::note_previous_declaration;
955 :
956 827: QualType OldQType = Context.getCanonicalType(Old->getType());
957 827: QualType NewQType = Context.getCanonicalType(New->getType());
958 :
473: branch 1 taken
354: branch 2 taken
473: branch 4 taken
0: branch 5 not taken
7: branch 7 taken
466: branch 8 taken
1: branch 10 taken
6: branch 11 taken
1: branch 12 taken
826: branch 13 taken
959 827: if (!isa<CXXMethodDecl>(New) && !isa<CXXMethodDecl>(Old) &&
960 : New->getStorageClass() == FunctionDecl::Static &&
961 : Old->getStorageClass() != FunctionDecl::Static) {
962 : Diag(New->getLocation(), diag::err_static_non_static)
963 1: << New;
964 1: Diag(Old->getLocation(), PrevDiag);
965 1: return true;
966 : }
967 :
968 : // If a function is first declared with a calling convention, but is
969 : // later declared or defined without one, the second decl assumes the
970 : // calling convention of the first.
971 : //
972 : // For the new decl, we have to look at the NON-canonical type to tell the
973 : // difference between a function that really doesn't have a calling
974 : // convention and one that is declared cdecl. That's because in
975 : // canonicalization (see ASTContext.cpp), cdecl is canonicalized away
976 : // because it is the default calling convention.
977 : //
978 : // Note also that we DO NOT return at this point, because we still have
979 : // other tests to run.
980 826: const FunctionType *OldType = OldQType->getAs<FunctionType>();
981 826: const FunctionType *NewType = New->getType()->getAs<FunctionType>();
2: branch 1 taken
824: branch 2 taken
1: branch 4 taken
1: branch 5 taken
1: branch 6 taken
825: branch 7 taken
982 826: if (OldType->getCallConv() != CC_Default &&
983 : NewType->getCallConv() == CC_Default) {
984 1: NewQType = Context.getCallConvType(NewQType, OldType->getCallConv());
985 1: New->setType(NewQType);
986 1: NewQType = Context.getCanonicalType(NewQType);
1: branch 3 taken
824: branch 4 taken
987 825: } else if (!Context.isSameCallConv(OldType->getCallConv(),
988 : NewType->getCallConv())) {
989 : // Calling conventions really aren't compatible, so complain.
990 : Diag(New->getLocation(), diag::err_cconv_change)
991 : << FunctionType::getNameForCallConv(NewType->getCallConv())
992 : << (OldType->getCallConv() == CC_Default)
993 : << (OldType->getCallConv() == CC_Default ? "" :
0: branch 1 not taken
1: branch 2 taken
994 1: FunctionType::getNameForCallConv(OldType->getCallConv()));
995 1: Diag(Old->getLocation(), diag::note_previous_declaration);
996 1: return true;
997 : }
998 :
999 : // FIXME: diagnose the other way around?
37: branch 1 taken
788: branch 2 taken
20: branch 4 taken
17: branch 5 taken
20: branch 6 taken
805: branch 7 taken
1000 825: if (OldType->getNoReturnAttr() && !NewType->getNoReturnAttr()) {
1001 20: NewQType = Context.getNoReturnType(NewQType);
1002 20: New->setType(NewQType);
0: branch 1 not taken
20: branch 2 taken
1003 20: assert(NewQType.isCanonical());
1004 : }
1005 :
487: branch 1 taken
338: branch 2 taken
1006 825: if (getLangOptions().CPlusPlus) {
1007 : // (C++98 13.1p2):
1008 : // Certain function declarations cannot be overloaded:
1009 : // -- Function declarations that differ only in the return type
1010 : // cannot be overloaded.
1011 : QualType OldReturnType
1012 487: = cast<FunctionType>(OldQType.getTypePtr())->getResultType();
1013 : QualType NewReturnType
1014 487: = cast<FunctionType>(NewQType.getTypePtr())->getResultType();
8: branch 1 taken
479: branch 2 taken
1015 487: if (OldReturnType != NewReturnType) {
1016 8: Diag(New->getLocation(), diag::err_ovl_diff_return_type);
1017 8: Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
1018 8: return true;
1019 : }
1020 :
1021 479: const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
1022 479: const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
352: branch 0 taken
127: branch 1 taken
352: branch 2 taken
0: branch 3 not taken
1023 479: if (OldMethod && NewMethod) {
349: branch 1 taken
3: branch 2 taken
53: branch 5 taken
296: branch 6 taken
53: branch 7 taken
299: branch 8 taken
1024 352: if (!NewMethod->getFriendObjectKind() &&
1025 : NewMethod->getLexicalDeclContext()->isRecord()) {
1026 : // -- Member function declarations with the same name and the
1027 : // same parameter types cannot be overloaded if any of them
1028 : // is a static member function declaration.
53: branch 1 taken
0: branch 2 not taken
1: branch 4 taken
52: branch 5 taken
1: branch 6 taken
52: branch 7 taken
1029 53: if (OldMethod->isStatic() || NewMethod->isStatic()) {
1030 1: Diag(New->getLocation(), diag::err_ovl_static_nonstatic_member);
1031 1: Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
1032 1: return true;
1033 : }
1034 :
1035 : // C++ [class.mem]p1:
1036 : // [...] A member shall not be declared twice in the
1037 : // member-specification, except that a nested class or member
1038 : // class template can be declared and then later defined.
1039 : unsigned NewDiag;
1: branch 1 taken
51: branch 2 taken
1040 52: if (isa<CXXConstructorDecl>(OldMethod))
1041 1: NewDiag = diag::err_constructor_redeclared;
1: branch 1 taken
50: branch 2 taken
1042 51: else if (isa<CXXDestructorDecl>(NewMethod))
1043 1: NewDiag = diag::err_destructor_redeclared;
2: branch 1 taken
48: branch 2 taken
1044 50: else if (isa<CXXConversionDecl>(NewMethod))
1045 2: NewDiag = diag::err_conv_function_redeclared;
1046 : else
1047 48: NewDiag = diag::err_member_redeclared;
1048 :
1049 52: Diag(New->getLocation(), NewDiag);
1050 52: Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
1051 : } else {
4: branch 1 taken
295: branch 2 taken
1052 299: if (OldMethod->isImplicit()) {
1053 : Diag(NewMethod->getLocation(),
1054 : diag::err_definition_of_implicitly_declared_member)
1055 4: << New << getSpecialMember(Context, OldMethod);
1056 :
1057 : Diag(OldMethod->getLocation(),
1058 4: diag::note_previous_implicit_declaration);
1059 4: return true;
1060 : }
1061 : }
1062 : }
1063 :
1064 : // (C++98 8.3.5p3):
1065 : // All declarations for a function shall agree exactly in both the
1066 : // return type and the parameter-type-list.
1067 : // attributes should be ignored when comparing.
474: branch 3 taken
0: branch 4 not taken
1068 474: if (Context.getNoReturnType(OldQType, false) ==
1069 : Context.getNoReturnType(NewQType, false))
1070 474: return MergeCompatibleFunctionDecls(New, Old);
1071 :
1072 : // Fall through for conflicting redeclarations and redefinitions.
1073 : }
1074 :
1075 : // C: Function types need to be compatible, not identical. This handles
1076 : // duplicate function decls like "void f(int); void f(enum X);" properly.
338: branch 1 taken
0: branch 2 not taken
307: branch 4 taken
31: branch 5 taken
307: branch 6 taken
31: branch 7 taken
1077 338: if (!getLangOptions().CPlusPlus &&
1078 : Context.typesAreCompatible(OldQType, NewQType)) {
1079 307: const FunctionType *OldFuncType = OldQType->getAs<FunctionType>();
1080 307: const FunctionType *NewFuncType = NewQType->getAs<FunctionType>();
1081 307: const FunctionProtoType *OldProto = 0;
72: branch 1 taken
235: branch 2 taken
14: branch 4 taken
58: branch 5 taken
14: branch 6 taken
293: branch 7 taken
1082 307: if (isa<FunctionNoProtoType>(NewFuncType) &&
1083 : (OldProto = dyn_cast<FunctionProtoType>(OldFuncType))) {
1084 : // The old declaration provided a function prototype, but the
1085 : // new declaration does not. Merge in the prototype.
14: branch 1 taken
0: branch 2 not taken
1086 14: assert(!OldProto->hasExceptionSpec() && "Exception spec in C");
1087 : llvm::SmallVector<QualType, 16> ParamTypes(OldProto->arg_type_begin(),
1088 14: OldProto->arg_type_end());
1089 : NewQType = Context.getFunctionType(NewFuncType->getResultType(),
1090 : ParamTypes.data(), ParamTypes.size(),
1091 : OldProto->isVariadic(),
1092 14: OldProto->getTypeQuals());
1093 14: New->setType(NewQType);
1094 14: New->setHasInheritedPrototype();
1095 :
1096 : // Synthesize a parameter for each argument type.
1097 14: llvm::SmallVector<ParmVarDecl*, 16> Params;
16: branch 0 taken
14: branch 1 taken
1098 30: for (FunctionProtoType::arg_type_iterator
1099 14: ParamType = OldProto->arg_type_begin(),
1100 14: ParamEnd = OldProto->arg_type_end();
1101 : ParamType != ParamEnd; ++ParamType) {
1102 : ParmVarDecl *Param = ParmVarDecl::Create(Context, New,
1103 : SourceLocation(), 0,
1104 : *ParamType, /*TInfo=*/0,
16: branch 1 taken
0: branch 2 not taken
1105 16: VarDecl::None, 0);
1106 16: Param->setImplicit();
1107 16: Params.push_back(Param);
1108 : }
1109 :
1110 14: New->setParams(Context, Params.data(), Params.size());
1111 : }
1112 :
1113 307: return MergeCompatibleFunctionDecls(New, Old);
1114 : }
1115 :
1116 : // GNU C permits a K&R definition to follow a prototype declaration
1117 : // if the declared types of the parameters in the K&R definition
1118 : // match the types in the prototype declaration, even when the
1119 : // promoted types of the parameters from the K&R definition differ
1120 : // from the types in the prototype. GCC then keeps the types from
1121 : // the prototype.
1122 : //
1123 : // If a variadic prototype is followed by a non-variadic K&R definition,
1124 : // the K&R definition becomes variadic. This is sort of an edge case, but
1125 : // it's legal per the standard depending on how you read C99 6.7.5.3p15 and
1126 : // C99 6.9.1p8.
31: branch 1 taken
0: branch 2 not taken
27: branch 4 taken
4: branch 5 taken
8: branch 7 taken
19: branch 8 taken
4: branch 12 taken
4: branch 13 taken
3: branch 16 taken
1: branch 17 taken
3: branch 18 taken
28: branch 19 taken
1127 31: if (!getLangOptions().CPlusPlus &&
1128 : Old->hasPrototype() && !New->hasPrototype() &&
1129 : New->getType()->getAs<FunctionProtoType>() &&
1130 : Old->getNumParams() == New->getNumParams()) {
1131 3: llvm::SmallVector<QualType, 16> ArgTypes;
1132 3: llvm::SmallVector<GNUCompatibleParamWarning, 16> Warnings;
1133 : const FunctionProtoType *OldProto
1134 3: = Old->getType()->getAs<FunctionProtoType>();
1135 : const FunctionProtoType *NewProto
1136 3: = New->getType()->getAs<FunctionProtoType>();
1137 :
1138 : // Determine whether this is the GNU C extension.
1139 : QualType MergedReturn = Context.mergeTypes(OldProto->getResultType(),
1140 3: NewProto->getResultType());
1141 3: bool LooseCompatible = !MergedReturn.isNull();
7: branch 1 taken
0: branch 2 not taken
4: branch 3 taken
3: branch 4 taken
1142 7: for (unsigned Idx = 0, End = Old->getNumParams();
1143 : LooseCompatible && Idx != End; ++Idx) {
1144 4: ParmVarDecl *OldParm = Old->getParamDecl(Idx);
1145 4: ParmVarDecl *NewParm = New->getParamDecl(Idx);
2: branch 3 taken
2: branch 4 taken
1146 4: if (Context.typesAreCompatible(OldParm->getType(),
1147 : NewProto->getArgType(Idx))) {
1148 2: ArgTypes.push_back(NewParm->getType());
2: branch 3 taken
0: branch 4 not taken
1149 2: } else if (Context.typesAreCompatible(OldParm->getType(),
1150 : NewParm->getType())) {
1151 : GNUCompatibleParamWarning Warn
1152 2: = { OldParm, NewParm, NewProto->getArgType(Idx) };
1153 2: Warnings.push_back(Warn);
1154 2: ArgTypes.push_back(NewParm->getType());
1155 : } else
1156 0: LooseCompatible = false;
1157 : }
1158 :
3: branch 0 taken
0: branch 1 not taken
1159 3: if (LooseCompatible) {
2: branch 1 taken
3: branch 2 taken
1160 5: for (unsigned Warn = 0; Warn < Warnings.size(); ++Warn) {
1161 : Diag(Warnings[Warn].NewParm->getLocation(),
1162 : diag::ext_param_promoted_not_compatible_with_prototype)
1163 : << Warnings[Warn].PromotedType
1164 2: << Warnings[Warn].OldParm->getType();
1165 : Diag(Warnings[Warn].OldParm->getLocation(),
1166 2: diag::note_previous_declaration);
1167 : }
1168 :
1169 : New->setType(Context.getFunctionType(MergedReturn, &ArgTypes[0],
1170 : ArgTypes.size(),
1171 3: OldProto->isVariadic(), 0));
1172 6: return MergeCompatibleFunctionDecls(New, Old);
0: branch 1 not taken
3: branch 2 taken
0: branch 4 not taken
3: branch 5 taken
1173 3: }
1174 :
1175 : // Fall through to diagnose conflicting types.
1176 : }
1177 :
1178 : // A function that has already been declared has been redeclared or defined
1179 : // with a different type- show appropriate diagnostic
11: branch 1 taken
17: branch 2 taken
1180 28: if (unsigned BuiltinID = Old->getBuiltinID()) {
1181 : // The user has declared a builtin function with an incompatible
1182 : // signature.
10: branch 1 taken
1: branch 2 taken
1183 11: if (Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) {
1184 : // The function the user is redeclaring is a library-defined
1185 : // function like 'malloc' or 'printf'. Warn about the
1186 : // redeclaration, then pretend that we don't know about this
1187 : // library built-in.
1188 10: Diag(New->getLocation(), diag::warn_redecl_library_builtin) << New;
1189 : Diag(Old->getLocation(), diag::note_previous_builtin_declaration)
1190 10: << Old << Old->getType();
1191 10: New->getIdentifier()->setBuiltinID(Builtin::NotBuiltin);
1192 10: Old->setInvalidDecl();
1193 10: return false;
1194 : }
1195 :
1196 1: PrevDiag = diag::note_previous_builtin_declaration;
1197 : }
1198 :
1199 18: Diag(New->getLocation(), diag::err_conflicting_types) << New->getDeclName();
1200 18: Diag(Old->getLocation(), PrevDiag) << Old << Old->getType();
1201 18: return true;
1202 : }
1203 :
1204 : /// \brief Completes the merge of two function declarations that are
1205 : /// known to be compatible.
1206 : ///
1207 : /// This routine handles the merging of attributes and other
1208 : /// properties of function declarations form the old declaration to
1209 : /// the new declaration, once we know that New is in fact a
1210 : /// redeclaration of Old.
1211 : ///
1212 : /// \returns false
1213 784: bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old) {
1214 : // Merge the attributes
1215 784: MergeAttributes(New, Old, Context);
1216 :
1217 : // Merge the storage class.
617: branch 1 taken
167: branch 2 taken
16: branch 4 taken
601: branch 5 taken
16: branch 6 taken
768: branch 7 taken
1218 784: if (Old->getStorageClass() != FunctionDecl::Extern &&
1219 : Old->getStorageClass() != FunctionDecl::None)
1220 16: New->setStorageClass(Old->getStorageClass());
1221 :
1222 : // Merge "pure" flag.
0: branch 1 not taken
784: branch 2 taken
1223 784: if (Old->isPure())
1224 0: New->setPure();
1225 :
1226 : // Merge the "deleted" flag.
1: branch 1 taken
783: branch 2 taken
1227 784: if (Old->isDeleted())
1228 1: New->setDeleted();
1229 :
474: branch 1 taken
310: branch 2 taken
1230 784: if (getLangOptions().CPlusPlus)
1231 474: return MergeCXXFunctionDecl(New, Old);
1232 :
1233 310: return false;
1234 : }
1235 :
1236 : /// MergeVarDecl - We just parsed a variable 'New' which has the same name
1237 : /// and scope as a previous declaration 'Old'. Figure out how to resolve this
1238 : /// situation, merging decls or emitting diagnostics as appropriate.
1239 : ///
1240 : /// Tentative definition rules (C99 6.9.2p2) are checked by
1241 : /// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative
1242 : /// definitions here, since the initializer hasn't been attached.
1243 : ///
1244 301: void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
1245 : // If the new decl is already invalid, don't do any other checking.
0: branch 1 not taken
301: branch 2 taken
1246 301: if (New->isInvalidDecl())
1247 0: return;
1248 :
1249 : // Verify the old decl was also a variable.
1250 301: VarDecl *Old = 0;
301: branch 1 taken
0: branch 2 not taken
3: branch 5 taken
298: branch 6 taken
3: branch 7 taken
298: branch 8 taken
1251 301: if (!Previous.isSingleResult() ||
1252 : !(Old = dyn_cast<VarDecl>(Previous.getFoundDecl()))) {
1253 : Diag(New->getLocation(), diag::err_redefinition_different_kind)
1254 3: << New->getDeclName();
1255 : Diag(Previous.getRepresentativeDecl()->getLocation(),
1256 3: diag::note_previous_definition);
1257 3: return New->setInvalidDecl();
1258 : }
1259 :
1260 298: MergeAttributes(New, Old, Context);
1261 :
1262 : // Merge the types
1263 298: QualType MergedT;
129: branch 1 taken
169: branch 2 taken
1264 298: if (getLangOptions().CPlusPlus) {
121: branch 3 taken
8: branch 4 taken
1265 129: if (Context.hasSameType(New->getType(), Old->getType()))
1266 121: MergedT = New->getType();
1267 : // C++ [basic.link]p10:
1268 : // [...] the types specified by all declarations referring to a given
1269 : // object or function shall be identical, except that declarations for an
1270 : // array object can specify array types that differ by the presence or
1271 : // absence of a major array bound (8.3.4).
5: branch 3 taken
3: branch 4 taken
5: branch 8 taken
0: branch 9 not taken
5: branch 10 taken
3: branch 11 taken
1272 8: else if (Old->getType()->isIncompleteArrayType() &&
1273 : New->getType()->isArrayType()) {
1274 : CanQual<ArrayType> OldArray
1275 5: = Context.getCanonicalType(Old->getType())->getAs<ArrayType>();
1276 : CanQual<ArrayType> NewArray
1277 5: = Context.getCanonicalType(New->getType())->getAs<ArrayType>();
5: branch 7 taken
0: branch 8 not taken
1278 5: if (OldArray->getElementType() == NewArray->getElementType())
1279 5: MergedT = New->getType();
2: branch 3 taken
1: branch 4 taken
2: branch 8 taken
0: branch 9 not taken
2: branch 10 taken
1: branch 11 taken
1280 3: } else if (Old->getType()->isArrayType() &&
1281 : New->getType()->isIncompleteArrayType()) {
1282 : CanQual<ArrayType> OldArray
1283 2: = Context.getCanonicalType(Old->getType())->getAs<ArrayType>();
1284 : CanQual<ArrayType> NewArray
1285 2: = Context.getCanonicalType(New->getType())->getAs<ArrayType>();
2: branch 7 taken
0: branch 8 not taken
1286 2: if (OldArray->getElementType() == NewArray->getElementType())
1287 2: MergedT = Old->getType();
1288 : }
1289 : } else {
1290 169: MergedT = Context.mergeTypes(New->getType(), Old->getType());
1291 : }
32: branch 1 taken
266: branch 2 taken
1292 298: if (MergedT.isNull()) {
1293 : Diag(New->getLocation(), diag::err_redefinition_different_type)
1294 32: << New->getDeclName();
1295 32: Diag(Old->getLocation(), diag::note_previous_definition);
1296 32: return New->setInvalidDecl();
1297 : }
1298 266: New->setType(MergedT);
1299 :
1300 : // C99 6.2.2p4: Check if we have a static decl followed by a non-static.
104: branch 1 taken
162: branch 2 taken
103: branch 4 taken
1: branch 5 taken
4: branch 7 taken
99: branch 8 taken
5: branch 9 taken
261: branch 10 taken
1301 266: if (New->getStorageClass() == VarDecl::Static &&
1302 : (Old->getStorageClass() == VarDecl::None || Old->hasExternalStorage())) {
1303 5: Diag(New->getLocation(), diag::err_static_non_static) << New->getDeclName();
1304 5: Diag(Old->getLocation(), diag::note_previous_definition);
1305 5: return New->setInvalidDecl();
1306 : }
1307 : // C99 6.2.2p4:
1308 : // For an identifier declared with the storage-class specifier
1309 : // extern in a scope in which a prior declaration of that
1310 : // identifier is visible,23) if the prior declaration specifies
1311 : // internal or external linkage, the linkage of the identifier at
1312 : // the later declaration is the same as the linkage specified at
1313 : // the prior declaration. If no prior declaration is visible, or
1314 : // if the prior declaration specifies no linkage, then the
1315 : // identifier has external linkage.
39: branch 1 taken
222: branch 2 taken
37: branch 4 taken
2: branch 5 taken
224: branch 6 taken
37: branch 7 taken
1316 261: if (New->hasExternalStorage() && Old->hasLinkage())
1317 : /* Okay */;
125: branch 1 taken
99: branch 2 taken
2: branch 4 taken
123: branch 5 taken
2: branch 6 taken
222: branch 7 taken
1318 224: else if (New->getStorageClass() != VarDecl::Static &&
1319 : Old->getStorageClass() == VarDecl::Static) {
1320 2: Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName();
1321 2: Diag(Old->getLocation(), diag::note_previous_definition);
1322 2: return New->setInvalidDecl();
1323 : }
1324 :
1325 : // Variables with external linkage are analyzed in FinalizeDeclaratorGroup.
1326 :
1327 : // FIXME: The test for external storage here seems wrong? We still
1328 : // need to check for mismatches.
220: branch 1 taken
39: branch 2 taken
13: branch 4 taken
207: branch 5 taken
0: branch 8 not taken
13: branch 9 taken
0: branch 12 not taken
0: branch 13 not taken
13: branch 14 taken
246: branch 15 taken
1329 259: if (!New->hasExternalStorage() && !New->isFileVarDecl() &&
1330 : // Don't complain about out-of-line definitions of static members.
1331 : !(Old->getLexicalDeclContext()->isRecord() &&
1332 : !New->getLexicalDeclContext()->isRecord())) {
1333 13: Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName();
1334 13: Diag(Old->getLocation(), diag::note_previous_definition);
1335 13: return New->setInvalidDecl();
1336 : }
1337 :
1: branch 1 taken
245: branch 2 taken
1: branch 4 taken
0: branch 5 not taken
1: branch 6 taken
245: branch 7 taken
1338 246: if (New->isThreadSpecified() && !Old->isThreadSpecified()) {
1339 1: Diag(New->getLocation(), diag::err_thread_non_thread) << New->getDeclName();
1340 1: Diag(Old->getLocation(), diag::note_previous_definition);
245: branch 1 taken
0: branch 2 not taken
1: branch 4 taken
244: branch 5 taken
1: branch 6 taken
244: branch 7 taken
1341 245: } else if (!New->isThreadSpecified() && Old->isThreadSpecified()) {
1342 1: Diag(New->getLocation(), diag::err_non_thread_thread) << New->getDeclName();
1343 1: Diag(Old->getLocation(), diag::note_previous_definition);
1344 : }
1345 :
1346 : // C++ doesn't have tentative definitions, so go right ahead and check here.
1347 : const VarDecl *Def;
118: branch 1 taken
128: branch 2 taken
108: branch 4 taken
10: branch 5 taken
2: branch 7 taken
106: branch 8 taken
2: branch 9 taken
244: branch 10 taken
1348 246: if (getLangOptions().CPlusPlus &&
1349 : New->isThisDeclarationADefinition() == VarDecl::Definition &&
1350 : (Def = Old->getDefinition())) {
1351 : Diag(New->getLocation(), diag::err_redefinition)
1352 2: << New->getDeclName();
1353 2: Diag(Def->getLocation(), diag::note_previous_definition);
1354 2: New->setInvalidDecl();
1355 2: return;
1356 : }
1357 :
1358 : // Keep a chain of previous declarations.
1359 244: New->setPreviousDeclaration(Old);
1360 :
1361 : // Inherit access appropriately.
1362 244: New->setAccess(Old->getAccess());
1363 : }
1364 :
1365 : /// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
1366 : /// no declarator (e.g. "struct foo;") is parsed.
1367 4753: Sema::DeclPtrTy Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
1368 : // FIXME: Error on auto/register at file scope
1369 : // FIXME: Error on inline/virtual/explicit
1370 : // FIXME: Warn on useless __thread
1371 : // FIXME: Warn on useless const/volatile
1372 : // FIXME: Warn on useless static/extern/typedef/private_extern/mutable
1373 : // FIXME: Warn on useless attributes
1374 4753: Decl *TagD = 0;
1375 4753: TagDecl *Tag = 0;
3905: branch 1 taken
848: branch 2 taken
441: branch 4 taken
3464: branch 5 taken
318: branch 7 taken
123: branch 8 taken
282: branch 10 taken
36: branch 11 taken
4717: branch 12 taken
36: branch 13 taken
1376 4753: if (DS.getTypeSpecType() == DeclSpec::TST_class ||
1377 : DS.getTypeSpecType() == DeclSpec::TST_struct ||
1378 : DS.getTypeSpecType() == DeclSpec::TST_union ||
1379 : DS.getTypeSpecType() == DeclSpec::TST_enum) {
1380 4717: TagD = static_cast<Decl *>(DS.getTypeRep());
1381 :
6: branch 0 taken
4711: branch 1 taken
1382 4717: if (!TagD) // We probably had an error
1383 6: return DeclPtrTy();
1384 :
1385 : // Note that the above type specs guarantee that the
1386 : // type rep is a Decl, whereas in many of the others
1387 : // it's a Type.
1388 4711: Tag = dyn_cast<TagDecl>(TagD);
1389 : }
1390 :
2: branch 1 taken
4745: branch 2 taken
1391 4747: if (unsigned TypeQuals = DS.getTypeQualifiers()) {
1392 : // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
1393 : // or incomplete types shall not be restrict-qualified."
1: branch 0 taken
1: branch 1 taken
1394 2: if (TypeQuals & DeclSpec::TQ_restrict)
1395 : Diag(DS.getRestrictSpecLoc(),
1396 : diag::err_typecheck_invalid_restrict_not_pointer_noarg)
1397 1: << DS.getSourceRange();
1398 : }
1399 :
50: branch 1 taken
4697: branch 2 taken
1400 4747: if (DS.isFriendSpecified()) {
1401 : // If we're dealing with a class template decl, assume that the
1402 : // template routines are handling it.
43: branch 0 taken
7: branch 1 taken
13: branch 3 taken
30: branch 4 taken
13: branch 5 taken
37: branch 6 taken
1403 50: if (TagD && isa<ClassTemplateDecl>(TagD))
1404 13: return DeclPtrTy();
1405 37: return ActOnFriendTypeDecl(S, DS, MultiTemplateParamsArg(*this, 0, 0));
1406 : }
1407 :
3436: branch 1 taken
1261: branch 2 taken
1408 4697: if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) {
1409 : // If there are attributes in the DeclSpec, apply them to the record.
1: branch 1 taken
3435: branch 2 taken
1410 3436: if (const AttributeList *AL = DS.getAttributes())
1411 1: ProcessDeclAttributeList(S, Record, AL);
1412 :
74: branch 2 taken
3362: branch 3 taken
70: branch 5 taken
4: branch 6 taken
68: branch 8 taken
2: branch 9 taken
68: branch 10 taken
3368: branch 11 taken
1413 3436: if (!Record->getDeclName() && Record->isDefinition() &&
1414 : DS.getStorageClassSpec() != DeclSpec::SCS_typedef) {
25: branch 1 taken
43: branch 2 taken
21: branch 5 taken
4: branch 6 taken
64: branch 7 taken
4: branch 8 taken
1415 68: if (getLangOptions().CPlusPlus ||
1416 : Record->getDeclContext()->isRecord())
1417 64: return BuildAnonymousStructOrUnion(S, DS, Record);
1418 :
1419 : Diag(DS.getSourceRange().getBegin(), diag::err_no_declarators)
1420 4: << DS.getSourceRange();
1421 : }
1422 :
1423 : // Microsoft allows unnamed struct/union fields. Don't complain
1424 : // about them.
1425 : // FIXME: Should we support Microsoft's extensions in this area?
3362: branch 2 taken
10: branch 3 taken
12: branch 5 taken
3350: branch 6 taken
12: branch 7 taken
3360: branch 8 taken
1426 3372: if (Record->getDeclName() && getLangOptions().Microsoft)
1427 12: return DeclPtrTy::make(Tag);
1428 : }
1429 :
33: branch 1 taken
4588: branch 2 taken
15: branch 4 taken
18: branch 5 taken
15: branch 6 taken
4606: branch 7 taken
1430 4621: if (!DS.isMissingDeclaratorOk() &&
1431 : DS.getTypeSpecType() != DeclSpec::TST_error) {
1432 : // Warn about typedefs of enums without names, since this is an
1433 : // extension in both Microsoft an GNU.
6: branch 1 taken
9: branch 2 taken
4: branch 3 taken
2: branch 4 taken
1: branch 6 taken
3: branch 7 taken
1: branch 8 taken
14: branch 9 taken
1434 15: if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef &&
1435 : Tag && isa<EnumDecl>(Tag)) {
1436 : Diag(DS.getSourceRange().getBegin(), diag::ext_typedef_without_a_name)
1437 1: << DS.getSourceRange();
1438 1: return DeclPtrTy::make(Tag);
1439 : }
1440 :
1441 : Diag(DS.getSourceRange().getBegin(), diag::err_no_declarators)
1442 14: << DS.getSourceRange();
1443 14: return DeclPtrTy();
1444 : }
1445 :
1446 4606: return DeclPtrTy::make(Tag);
1447 : }
1448 :
1449 : /// We are trying to inject an anonymous member into the given scope;
1450 : /// check if there's an existing declaration that can't be overloaded.
1451 : ///
1452 : /// \return true if this is a forbidden redeclaration
1453 : static bool CheckAnonMemberRedeclaration(Sema &SemaRef,
1454 : Scope *S,
1455 : DeclContext *Owner,
1456 : DeclarationName Name,
1457 : SourceLocation NameLoc,
1458 126: unsigned diagnostic) {
1459 : LookupResult R(SemaRef, Name, NameLoc, Sema::LookupMemberName,
1460 126: Sema::ForRedeclaration);
121: branch 1 taken
5: branch 2 taken
1461 126: if (!SemaRef.LookupName(R, S)) return false;
1462 :
1: branch 1 taken
4: branch 2 taken
1463 5: if (R.getAsSingle<TagDecl>())
1464 1: return false;
1465 :
1466 : // Pick a representative declaration.
1467 4: NamedDecl *PrevDecl = R.getRepresentativeDecl()->getUnderlyingDecl();
4: branch 0 taken
0: branch 1 not taken
4: branch 3 taken
0: branch 4 not taken
4: branch 5 taken
0: branch 6 not taken
1468 4: if (PrevDecl && Owner->isRecord()) {
1469 4: RecordDecl *Record = cast<RecordDecl>(Owner);
4: branch 0 taken
0: branch 1 not taken
1: branch 3 taken
3: branch 4 taken
1470 4: if (!SemaRef.isDeclInScope(PrevDecl, Record, S))
1471 1: return false;
1472 : }
1473 :
1474 3: SemaRef.Diag(NameLoc, diagnostic) << Name;
1475 3: SemaRef.Diag(PrevDecl->getLocation(), diag::note_previous_declaration);
1476 :
1477 3: return true;
1478 : }
1479 :
1480 : /// InjectAnonymousStructOrUnionMembers - Inject the members of the
1481 : /// anonymous struct or union AnonRecord into the owning context Owner
1482 : /// and scope S. This routine will be invoked just after we realize
1483 : /// that an unnamed union or struct is actually an anonymous union or
1484 : /// struct, e.g.,
1485 : ///
1486 : /// @code
1487 : /// union {
1488 : /// int i;
1489 : /// float f;
1490 : /// }; // InjectAnonymousStructOrUnionMembers called here to inject i and
1491 : /// // f into the surrounding scope.x
1492 : /// @endcode
1493 : ///
1494 : /// This routine is recursive, injecting the names of nested anonymous
1495 : /// structs/unions into the owning context and scope as well.
1496 : bool Sema::InjectAnonymousStructOrUnionMembers(Scope *S, DeclContext *Owner,
1497 69: RecordDecl *AnonRecord) {
1498 : unsigned diagKind
1499 : = AnonRecord->isUnion() ? diag::err_anonymous_union_member_redecl
60: branch 1 taken
9: branch 2 taken
1500 69: : diag::err_anonymous_struct_member_redecl;
1501 :
1502 69: bool Invalid = false;
131: branch 3 taken
69: branch 4 taken
1503 269: for (RecordDecl::field_iterator F = AnonRecord->field_begin(),
1504 69: FEnd = AnonRecord->field_end();
1505 : F != FEnd; ++F) {
126: branch 3 taken
5: branch 4 taken
1506 131: if ((*F)->getDeclName()) {
3: branch 5 taken
123: branch 6 taken
1507 126: if (CheckAnonMemberRedeclaration(*this, S, Owner, (*F)->getDeclName(),
1508 : (*F)->getLocation(), diagKind)) {
1509 : // C++ [class.union]p2:
1510 : // The names of the members of an anonymous union shall be
1511 : // distinct from the names of any other entity in the
1512 : // scope in which the anonymous union is declared.
1513 3: Invalid = true;
1514 : } else {
1515 : // C++ [class.union]p2:
1516 : // For the purpose of name lookup, after the anonymous union
1517 : // definition, the members of the anonymous union are
1518 : // considered to have been defined in the scope in which the
1519 : // anonymous union is declared.
1520 123: Owner->makeDeclVisibleInContext(*F);
1521 123: S->AddDecl(DeclPtrTy::make(*F));
1522 123: IdResolver.AddDecl(*F);
1523 : }
5: branch 0 taken
0: branch 1 not taken
1524 5: } else if (const RecordType *InnerRecordType
1525 5: = (*F)->getType()->getAs<RecordType>()) {
1526 5: RecordDecl *InnerRecord = InnerRecordType->getDecl();
5: branch 1 taken
0: branch 2 not taken
1527 5: if (InnerRecord->isAnonymousStructOrUnion())
1528 : Invalid = Invalid ||
5: branch 0 taken
0: branch 1 not taken
0: branch 3 not taken
5: branch 4 taken
1529 5: InjectAnonymousStructOrUnionMembers(S, Owner, InnerRecord);
1530 : }
1531 : }
1532 :
1533 69: return Invalid;
1534 : }
1535 :
1536 : /// ActOnAnonymousStructOrUnion - Handle the declaration of an
1537 : /// anonymous structure or union. Anonymous unions are a C++ feature
1538 : /// (C++ [class.union]) and a GNU C extension; anonymous structures
1539 : /// are a GNU C and GNU C++ extension.
1540 : Sema::DeclPtrTy Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
1541 64: RecordDecl *Record) {
1542 64: DeclContext *Owner = Record->getDeclContext();
1543 :
1544 : // Diagnose whether this anonymous struct/union is an extension.
58: branch 1 taken
6: branch 2 taken
17: branch 4 taken
41: branch 5 taken
17: branch 6 taken
47: branch 7 taken
1545 64: if (Record->isUnion() && !getLangOptions().CPlusPlus)
1546 17: Diag(Record->getLocation(), diag::ext_anonymous_union);
6: branch 1 taken
41: branch 2 taken
1547 47: else if (!Record->isUnion())
1548 6: Diag(Record->getLocation(), diag::ext_anonymous_struct);
1549 :
1550 : // C and C++ require different kinds of checks for anonymous
1551 : // structs/unions.
1552 64: bool Invalid = false;
43: branch 1 taken
21: branch 2 taken
1553 64: if (getLangOptions().CPlusPlus) {
1554 43: const char* PrevSpec = 0;
1555 : unsigned DiagID;
1556 : // C++ [class.union]p3:
1557 : // Anonymous unions declared in a named namespace or in the
1558 : // global namespace shall be declared static.
41: branch 1 taken
2: branch 2 taken
40: branch 4 taken
1: branch 5 taken
0: branch 7 not taken
40: branch 8 taken
0: branch 12 not taken
0: branch 13 not taken
1: branch 14 taken
42: branch 15 taken
1559 43: if (DS.getStorageClassSpec() != DeclSpec::SCS_static &&
1560 : (isa<TranslationUnitDecl>(Owner) ||
1561 : (isa<NamespaceDecl>(Owner) &&
1562 : cast<NamespaceDecl>(Owner)->getDeclName()))) {
1563 1: Diag(Record->getLocation(), diag::err_anonymous_union_not_static);
1564 1: Invalid = true;
1565 :
1566 : // Recover by adding 'static'.
1567 : DS.SetStorageClassSpec(DeclSpec::SCS_static, SourceLocation(),
1568 1: PrevSpec, DiagID);
1569 : }
1570 : // C++ [class.union]p3:
1571 : // A storage class is not allowed in a declaration of an
1572 : // anonymous union in a class scope.
3: branch 1 taken
39: branch 2 taken
1: branch 4 taken
2: branch 5 taken
1: branch 6 taken
41: branch 7 taken
1573 42: else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1574 : isa<RecordDecl>(Owner)) {
1575 : Diag(DS.getStorageClassSpecLoc(),
1576 1: diag::err_anonymous_union_with_storage_spec);
1577 1: Invalid = true;
1578 :
1579 : // Recover by removing the storage specifier.
1580 : DS.SetStorageClassSpec(DeclSpec::SCS_unspecified, SourceLocation(),
1581 1: PrevSpec, DiagID);
1582 : }
1583 :
1584 : // C++ [class.union]p2:
1585 : // The member-specification of an anonymous union shall only
1586 : // define non-static data members. [Note: nested types and
1587 : // functions cannot be declared within an anonymous union. ]
238: branch 3 taken
43: branch 4 taken
1588 324: for (DeclContext::decl_iterator Mem = Record->decls_begin(),
1589 43: MemEnd = Record->decls_end();
1590 : Mem != MemEnd; ++Mem) {
84: branch 2 taken
154: branch 3 taken
1591 238: if (FieldDecl *FD = dyn_cast<FieldDecl>(*Mem)) {
1592 : // C++ [class.union]p3:
1593 : // An anonymous union shall not have private or protected
1594 : // members (clause 11).
83: branch 1 taken
1: branch 2 taken
1: branch 4 taken
82: branch 5 taken
2: branch 6 taken
82: branch 7 taken
1595 84: if (FD->getAccess() == AS_protected || FD->getAccess() == AS_private) {
1596 : Diag(FD->getLocation(), diag::err_anonymous_record_nonpublic_member)
1597 2: << (int)Record->isUnion() << (int)(FD->getAccess() == AS_protected);
1598 2: Invalid = true;
1599 : }
6: branch 2 taken
148: branch 3 taken
1600 154: } else if ((*Mem)->isImplicit()) {
1601 : // Any implicit members are fine.
5: branch 2 taken
1: branch 3 taken
5: branch 6 taken
0: branch 7 not taken
2: branch 8 taken
3: branch 9 taken
4: branch 10 taken
2: branch 11 taken
1602 6: } else if (isa<TagDecl>(*Mem) && (*Mem)->getDeclContext() != Record) {
1603 : // This is a type that showed up in an
1604 : // elaborated-type-specifier inside the anonymous struct or
1605 : // union, but which actually declares a type outside of the
1606 : // anonymous struct or union. It's okay.
3: branch 2 taken
1: branch 3 taken
1607 4: } else if (RecordDecl *MemRecord = dyn_cast<RecordDecl>(*Mem)) {
2: branch 1 taken
1: branch 2 taken
1: branch 5 taken
1: branch 6 taken
1: branch 7 taken
2: branch 8 taken
1608 3: if (!MemRecord->isAnonymousStructOrUnion() &&
1609 : MemRecord->getDeclName()) {
1610 : // This is a nested type declaration.
1611 : Diag(MemRecord->getLocation(), diag::err_anonymous_record_with_type)
1612 1: << (int)Record->isUnion();
1613 1: Invalid = true;
1614 : }
1615 : } else {
1616 : // We have something that isn't a non-static data
1617 : // member. Complain about it.
1618 1: unsigned DK = diag::err_anonymous_record_bad_member;
0: branch 2 not taken
1: branch 3 taken
1619 1: if (isa<TypeDecl>(*Mem))
1620 0: DK = diag::err_anonymous_record_with_type;
1: branch 2 taken
0: branch 3 not taken
1621 1: else if (isa<FunctionDecl>(*Mem))
1622 1: DK = diag::err_anonymous_record_with_function;
0: branch 2 not taken
0: branch 3 not taken
1623 0: else if (isa<VarDecl>(*Mem))
1624 0: DK = diag::err_anonymous_record_with_static;
1625 : Diag((*Mem)->getLocation(), DK)
1626 1: << (int)Record->isUnion();
1627 1: Invalid = true;
1628 : }
1629 : }
1630 : }
1631 :
6: branch 1 taken
58: branch 2 taken
0: branch 4 not taken
6: branch 5 taken
0: branch 6 not taken
64: branch 7 taken
1632 64: if (!Record->isUnion() && !Owner->isRecord()) {
1633 : Diag(Record->getLocation(), diag::err_anonymous_struct_not_member)
1634 0: << (int)getLangOptions().CPlusPlus;
1635 0: Invalid = true;
1636 : }
1637 :
1638 : // Mock up a declarator.
1639 64: Declarator Dc(DS, Declarator::TypeNameContext);
1640 64: TypeSourceInfo *TInfo = 0;
1641 64: GetTypeForDeclarator(Dc, S, &TInfo);
0: branch 0 not taken
64: branch 1 taken
1642 64: assert(TInfo && "couldn't build declarator info for anonymous struct/union");
1643 :
1644 : // Create a declaration for this anonymous struct/union.
1645 64: NamedDecl *Anon = 0;
60: branch 1 taken
4: branch 2 taken
1646 64: if (RecordDecl *OwningClass = dyn_cast<RecordDecl>(Owner)) {
1647 : Anon = FieldDecl::Create(Context, OwningClass, Record->getLocation(),
1648 : /*IdentifierInfo=*/0,
1649 : Context.getTypeDeclType(Record),
1650 : TInfo,
60: branch 2 taken
0: branch 3 not taken
1651 60: /*BitWidth=*/0, /*Mutable=*/false);
1652 60: Anon->setAccess(AS_public);
39: branch 1 taken
21: branch 2 taken
1653 60: if (getLangOptions().CPlusPlus)
1654 39: FieldCollector->Add(cast<FieldDecl>(Anon));
1655 : } else {
1656 : VarDecl::StorageClass SC;
0: branch 1 not taken
1: branch 2 taken
0: branch 3 not taken
3: branch 4 taken
0: branch 5 not taken
0: branch 6 not taken
0: branch 7 not taken
0: branch 8 not taken
1657 4: switch (DS.getStorageClassSpec()) {
1658 0: default: assert(0 && "Unknown storage class!");
1659 1: case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
1660 0: case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
1661 3: case DeclSpec::SCS_static: SC = VarDecl::Static; break;
1662 0: case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
1663 0: case DeclSpec::SCS_register: SC = VarDecl::Register; break;
1664 0: case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
1665 : case DeclSpec::SCS_mutable:
1666 : // mutable can only appear on non-static class members, so it's always
1667 : // an error here
1668 0: Diag(Record->getLocation(), diag::err_mutable_nonmember);
1669 0: Invalid = true;
1670 0: SC = VarDecl::None;
1671 : break;
1672 : }
1673 :
1674 : Anon = VarDecl::Create(Context, Owner, Record->getLocation(),
1675 : /*IdentifierInfo=*/0,
1676 : Context.getTypeDeclType(Record),
1677 : TInfo,
1678 4: SC);
1679 : }
1680 64: Anon->setImplicit();
1681 :
1682 : // Add the anonymous struct/union object to the current
1683 : // context. We'll be referencing this object when we refer to one of
1684 : // its members.
1685 64: Owner->addDecl(Anon);
1686 :
1687 : // Inject the members of the anonymous struct/union into the owning
1688 : // context and into the identifier resolver chain for name lookup
1689 : // purposes.
3: branch 1 taken
61: branch 2 taken
1690 64: if (InjectAnonymousStructOrUnionMembers(S, Owner, Record))
1691 3: Invalid = true;
1692 :
1693 : // Mark this as an anonymous struct/union type. Note that we do not
1694 : // do this until after we have already checked and injected the
1695 : // members of this anonymous struct/union type, because otherwise
1696 : // the members could be injected twice: once by DeclContext when it
1697 : // builds its lookup table, and once by
1698 : // InjectAnonymousStructOrUnionMembers.
1699 64: Record->setAnonymousStructOrUnion(true);
1700 :
6: branch 0 taken
58: branch 1 taken
1701 64: if (Invalid)
1702 6: Anon->setInvalidDecl();
1703 :
1704 64: return DeclPtrTy::make(Anon);
1705 : }
1706 :
1707 :
1708 : /// GetNameForDeclarator - Determine the full declaration name for the
1709 : /// given Declarator.
1710 58521: DeclarationName Sema::GetNameForDeclarator(Declarator &D) {
1711 58521: return GetNameFromUnqualifiedId(D.getName());
1712 : }
1713 :
1714 : /// \brief Retrieves the canonicalized name from a parsed unqualified-id.
1715 90201: DeclarationName Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) {
86098: branch 1 taken
742: branch 2 taken
43: branch 3 taken
780: branch 4 taken
1891: branch 5 taken
15: branch 6 taken
546: branch 7 taken
86: branch 8 taken
0: branch 9 not taken
1716 90201: switch (Name.getKind()) {
1717 : case UnqualifiedId::IK_Identifier:
1718 86098: return DeclarationName(Name.Identifier);
1719 :
1720 : case UnqualifiedId::IK_OperatorFunctionId:
1721 : return Context.DeclarationNames.getCXXOperatorName(
1722 742: Name.OperatorFunctionId.Operator);
1723 :
1724 : case UnqualifiedId::IK_LiteralOperatorId:
1725 : return Context.DeclarationNames.getCXXLiteralOperatorName(
1726 43: Name.Identifier);
1727 :
1728 : case UnqualifiedId::IK_ConversionFunctionId: {
1729 780: QualType Ty = GetTypeFromParser(Name.ConversionFunctionId);
0: branch 1 not taken
780: branch 2 taken
1730 780: if (Ty.isNull())
1731 0: return DeclarationName();
1732 :
1733 : return Context.DeclarationNames.getCXXConversionFunctionName(
1734 780: Context.getCanonicalType(Ty));
1735 : }
1736 :
1737 : case UnqualifiedId::IK_ConstructorName: {
1738 1891: QualType Ty = GetTypeFromParser(Name.ConstructorName);
0: branch 1 not taken
1891: branch 2 taken
1739 1891: if (Ty.isNull())
1740 0: return DeclarationName();
1741 :
1742 : return Context.DeclarationNames.getCXXConstructorName(
1743 1891: Context.getCanonicalType(Ty));
1744 : }
1745 :
1746 : case UnqualifiedId::IK_ConstructorTemplateId: {
1747 : // In well-formed code, we can only have a constructor
1748 : // template-id that refers to the current context, so go there
1749 : // to find the actual type being constructed.
1750 15: CXXRecordDecl *CurClass = dyn_cast<CXXRecordDecl>(CurContext);
15: branch 0 taken
0: branch 1 not taken
0: branch 3 not taken
15: branch 4 taken
0: branch 5 not taken
15: branch 6 taken
1751 15: if (!CurClass || CurClass->getIdentifier() != Name.TemplateId->Name)
1752 0: return DeclarationName();
1753 :
1754 : // Determine the type of the class being constructed.
1755 15: QualType CurClassType;
15: branch 0 taken
0: branch 1 not taken
1756 15: if (ClassTemplateDecl *ClassTemplate
1757 15: = CurClass->getDescribedClassTemplate())
1758 15: CurClassType = ClassTemplate->getInjectedClassNameType(Context);
1759 : else
1760 0: CurClassType = Context.getTypeDeclType(CurClass);
1761 :
1762 : // FIXME: Check two things: that the template-id names the same type as
1763 : // CurClassType, and that the template-id does not occur when the name
1764 : // was qualified.
1765 :
1766 : return Context.DeclarationNames.getCXXConstructorName(
1767 15: Context.getCanonicalType(CurClassType));
1768 : }
1769 :
1770 : case UnqualifiedId::IK_DestructorName: {
1771 546: QualType Ty = GetTypeFromParser(Name.DestructorName);
0: branch 1 not taken
546: branch 2 taken
1772 546: if (Ty.isNull())
1773 0: return DeclarationName();
1774 :
1775 : return Context.DeclarationNames.getCXXDestructorName(
1776 546: Context.getCanonicalType(Ty));
1777 : }
1778 :
1779 : case UnqualifiedId::IK_TemplateId: {
1780 : TemplateName TName
1781 86: = TemplateName::getFromVoidPointer(Name.TemplateId->Template);
1782 86: return Context.getNameForTemplate(TName);
1783 : }
1784 : }
1785 :
1786 0: assert(false && "Unknown name kind");
1787 : return DeclarationName();
1788 : }
1789 :
1790 : /// isNearlyMatchingFunction - Determine whether the C++ functions
1791 : /// Declaration and Definition are "nearly" matching. This heuristic
1792 : /// is used to improve diagnostics in the case where an out-of-line
1793 : /// function definition doesn't match any declaration within
1794 : /// the class or namespace.
1795 : static bool isNearlyMatchingFunction(ASTContext &Context,
1796 : FunctionDecl *Declaration,
1797 6: FunctionDecl *Definition) {
0: branch 2 not taken
6: branch 3 taken
1798 6: if (Declaration->param_size() != Definition->param_size())
1799 0: return false;
5: branch 1 taken
5: branch 2 taken
1800 10: for (unsigned Idx = 0; Idx < Declaration->param_size(); ++Idx) {
1801 5: QualType DeclParamTy = Declaration->getParamDecl(Idx)->getType();
1802 5: QualType DefParamTy = Definition->getParamDecl(Idx)->getType();
1803 :
1: branch 3 taken
4: branch 4 taken
1804 5: if (!Context.hasSameUnqualifiedType(DeclParamTy.getNonReferenceType(),
1805 : DefParamTy.getNonReferenceType()))
1806 1: return false;
1807 : }
1808 :
1809 5: return true;
1810 : }
1811 :
1812 : Sema::DeclPtrTy
1813 : Sema::HandleDeclarator(Scope *S, Declarator &D,
1814 : MultiTemplateParamsArg TemplateParamLists,
1815 29993: bool IsFunctionDefinition) {
1816 29993: DeclarationName Name = GetNameForDeclarator(D);
1817 :
1818 : // All of these full declarators require an identifier. If it doesn't have
1819 : // one, the ParsedFreeStandingDeclSpec action should be used.
1: branch 1 taken
29992: branch 2 taken
1820 29993: if (!Name) {
0: branch 1 not taken
1: branch 2 taken
1821 1: if (!D.isInvalidType()) // Reject this if we think it is valid.
1822 : Diag(D.getDeclSpec().getSourceRange().getBegin(),
1823 : diag::err_declarator_need_ident)
1824 0: << D.getDeclSpec().getSourceRange() << D.getSourceRange();
1825 1: return DeclPtrTy();
1826 : }
1827 :
1828 : // The scope passed in may not be a decl scope. Zip up the scope tree until
1829 : // we find one that is.
29992: branch 1 taken
633: branch 2 taken
0: branch 4 not taken
29992: branch 5 taken
633: branch 6 taken
29992: branch 7 taken
1830 60617: while ((S->getFlags() & Scope::DeclScope) == 0 ||
1831 : (S->getFlags() & Scope::TemplateParamScope) != 0)
1832 633: S = S->getParent();
1833 :
1834 : // If this is an out-of-line definition of a member of a class template
1835 : // or class template partial specialization, we may need to rebuild the
1836 : // type specifier in the declarator. See RebuildTypeInCurrentInstantiation()
1837 : // for more information.
1838 : // FIXME: cope with decltype(expr) and typeof(expr) once the rebuilder can
1839 : // handle expressions properly.
1840 29992: DeclSpec &DS = const_cast<DeclSpec&>(D.getDeclSpec());
394: branch 2 taken
29598: branch 3 taken
394: branch 6 taken
0: branch 7 not taken
108: branch 10 taken
286: branch 11 taken
64: branch 13 taken
44: branch 14 taken
64: branch 16 taken
0: branch 17 not taken
64: branch 19 taken
0: branch 20 not taken
0: branch 22 not taken
64: branch 23 taken
44: branch 24 taken
29948: branch 25 taken
1841 29992: if (D.getCXXScopeSpec().isSet() && !D.getCXXScopeSpec().isInvalid() &&
1842 : isDependentScopeSpecifier(D.getCXXScopeSpec()) &&
1843 : (DS.getTypeSpecType() == DeclSpec::TST_typename ||
1844 : DS.getTypeSpecType() == DeclSpec::TST_typeofType ||
1845 : DS.getTypeSpecType() == DeclSpec::TST_typeofExpr ||
1846 : DS.getTypeSpecType() == DeclSpec::TST_decltype)) {
43: branch 2 taken
1: branch 3 taken
1847 44: if (DeclContext *DC = computeDeclContext(D.getCXXScopeSpec(), true)) {
1848 : // FIXME: Preserve type source info.
1849 43: QualType T = GetTypeFromParser(DS.getTypeRep());
1850 :
1851 43: DeclContext *SavedContext = CurContext;
1852 43: CurContext = DC;
1853 43: T = RebuildTypeInCurrentInstantiation(T, D.getIdentifierLoc(), Name);
1854 43: CurContext = SavedContext;
1855 :
0: branch 1 not taken
43: branch 2 taken
1856 43: if (T.isNull())
1857 0: return DeclPtrTy();
1858 43: DS.UpdateTypeRep(T.getAsOpaquePtr());
1859 : }
1860 : }
1861 :
1862 : DeclContext *DC;
1863 : NamedDecl *New;
1864 :
1865 29992: TypeSourceInfo *TInfo = 0;
1866 29992: QualType R = GetTypeForDeclarator(D, S, &TInfo);
1867 :
1868 : LookupResult Previous(*this, Name, D.getIdentifierLoc(), LookupOrdinaryName,
1869 29992: ForRedeclaration);
1870 :
1871 : // See if this is a redefinition of a variable in the same scope.
0: branch 2 not taken
29992: branch 3 taken
1872 29992: if (D.getCXXScopeSpec().isInvalid()) {
1873 0: DC = CurContext;
1874 0: D.setInvalidType();
29598: branch 2 taken
394: branch 3 taken
1875 29992: } else if (!D.getCXXScopeSpec().isSet()) {
1876 29598: bool IsLinkageLookup = false;
1877 :
1878 : // If the declaration we're planning to build will be a function
1879 : // or object with linkage, then look for another declaration with
1880 : // linkage (C99 6.2.2p4-5 and C++ [basic.link]p6).
23301: branch 2 taken
6297: branch 3 taken
1881 29598: if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1882 : /* Do nothing*/;
13065: branch 2 taken
10236: branch 3 taken
1883 23301: else if (R->isFunctionType()) {
12899: branch 1 taken
166: branch 2 taken
11378: branch 5 taken
1521: branch 6 taken
11544: branch 7 taken
1521: branch 8 taken
1884 13065: if (CurContext->isFunctionOrMethod() ||
1885 : D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static)
1886 11544: IsLinkageLookup = true;
476: branch 2 taken
9760: branch 3 taken
1887 10236: } else if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern)
1888 476: IsLinkageLookup = true;
2561: branch 2 taken
7199: branch 3 taken
2402: branch 6 taken
159: branch 7 taken
2402: branch 8 taken
7358: branch 9 taken
1889 9760: else if (CurContext->getLookupContext()->isTranslationUnit() &&
1890 : D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static)
1891 2402: IsLinkageLookup = true;
1892 :
14422: branch 0 taken
15176: branch 1 taken
1893 29598: if (IsLinkageLookup)
1894 14422: Previous.clear(LookupRedeclarationWithLinkage);
1895 :
1896 29598: DC = CurContext;
1897 29598: LookupName(Previous, S, /* CreateBuiltins = */ IsLinkageLookup);
1898 : } else { // Something like "int foo::x;"
1899 394: DC = computeDeclContext(D.getCXXScopeSpec(), true);
1900 :
3: branch 0 taken
391: branch 1 taken
1901 394: if (!DC) {
1902 : // If we could not compute the declaration context, it's because the
1903 : // declaration context is dependent but does not refer to a class,
1904 : // class template, or class template partial specialization. Complain
1905 : // and return early, to avoid the coming semantic disaster.
1906 : Diag(D.getIdentifierLoc(),
1907 : diag::err_template_qualified_declarator_no_match)
1908 : << (NestedNameSpecifier*)D.getCXXScopeSpec().getScopeRep()
1909 3: << D.getCXXScopeSpec().getRange();
1910 3: return DeclPtrTy();
1911 : }
1912 :
286: branch 1 taken
105: branch 2 taken
0: branch 5 not taken
286: branch 6 taken
0: branch 7 not taken
391: branch 8 taken
1913 391: if (!DC->isDependentContext() &&
1914 : RequireCompleteDeclContext(D.getCXXScopeSpec()))
1915 0: return DeclPtrTy();
1916 :
374: branch 1 taken
17: branch 2 taken
1: branch 5 taken
373: branch 6 taken
1: branch 7 taken
390: branch 8 taken
1917 391: if (isa<CXXRecordDecl>(DC) && !cast<CXXRecordDecl>(DC)->hasDefinition()) {
1918 : Diag(D.getIdentifierLoc(),
1919 : diag::err_member_def_undefined_record)
1920 1: << Name << DC << D.getCXXScopeSpec().getRange();
1921 1: D.setInvalidType();
1922 : }
1923 :
1924 391: LookupQualifiedName(Previous, DC);
1925 :
1926 : // Don't consider using declarations as previous declarations for
1927 : // out-of-line members.
1928 391: RemoveUsingDecls(Previous);
1929 :
1930 : // C++ 7.3.1.2p2:
1931 : // Members (including explicit specializations of templates) of a named
1932 : // namespace can also be defined outside that namespace by explicit
1933 : // qualification of the name being defined, provided that the entity being
1934 : // defined was already declared in the namespace and the definition appears
1935 : // after the point of declaration in a namespace that encloses the
1936 : // declarations namespace.
1937 : //
1938 : // Note that we only check the context at this point. We don't yet
1939 : // have enough information to make sure that PrevDecl is actually
1940 : // the declaration we want to match. For example, given:
1941 : //
1942 : // class X {
1943 : // void f();
1944 : // void f(float);
1945 : // };
1946 : //
1947 : // void X::f(int) { } // ill-formed
1948 : //
1949 : // In this case, PrevDecl will point to the overload set
1950 : // containing the two f's declared in X, but neither of them
1951 : // matches.
1952 :
1953 : // First check whether we named the global scope.
2: branch 1 taken
389: branch 2 taken
1954 391: if (isa<TranslationUnitDecl>(DC)) {
1955 : Diag(D.getIdentifierLoc(), diag::err_invalid_declarator_global_scope)
1956 2: << Name << D.getCXXScopeSpec().getRange();
1957 : } else {
1958 389: DeclContext *Cur = CurContext;
1: branch 1 taken
389: branch 2 taken
1959 779: while (isa<LinkageSpecDecl>(Cur))
1960 1: Cur = Cur->getParent();
5: branch 1 taken
384: branch 2 taken
1961 389: if (!Cur->Encloses(DC)) {
1962 : // The qualifying scope doesn't enclose the original declaration.
1963 : // Emit diagnostic based on current scope.
1964 5: SourceLocation L = D.getIdentifierLoc();
1965 5: SourceRange R = D.getCXXScopeSpec().getRange();
1: branch 1 taken
4: branch 2 taken
1966 5: if (isa<FunctionDecl>(Cur))
1967 1: Diag(L, diag::err_invalid_declarator_in_function) << Name << R;
1968 : else
1969 : Diag(L, diag::err_invalid_declarator_scope)
1970 4: << Name << cast<NamedDecl>(DC) << R;
1971 5: D.setInvalidType();
1972 : }
1973 : }
1974 : }
1975 :
1674: branch 1 taken
28315: branch 2 taken
1: branch 5 taken
1673: branch 6 taken
1: branch 7 taken
29988: branch 8 taken
1976 29989: if (Previous.isSingleResult() &&
1977 : Previous.getFoundDecl()->isTemplateParameter()) {
1978 : // Maybe we will complain about the shadowed template parameter.
1: branch 1 taken
0: branch 2 not taken
1979 1: if (!D.isInvalidType())
1: branch 3 taken
0: branch 4 not taken
1980 1: if (DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
1981 : Previous.getFoundDecl()))
1982 1: D.setInvalidType();
1983 :
1984 : // Just pretend that we didn't see the previous declaration.
1985 1: Previous.clear();
1986 : }
1987 :
1988 : // In C++, the previous declaration we find might be a tag type
1989 : // (class or enum). In this case, the new declaration will hide the
1990 : // tag type. Note that this does does not apply if we're declaring a
1991 : // typedef (C++ [dcl.typedef]p4).
125: branch 1 taken
29864: branch 2 taken
36: branch 5 taken
89: branch 6 taken
36: branch 7 taken
29953: branch 8 taken
1992 29989: if (Previous.isSingleTagDecl() &&
1993 : D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef)
1994 36: Previous.clear();
1995 :
1996 29989: bool Redeclaration = false;
6298: branch 2 taken
23691: branch 3 taken
1997 29989: if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
1: branch 1 taken
6297: branch 2 taken
1998 6298: if (TemplateParamLists.size()) {
1999 1: Diag(D.getIdentifierLoc(), diag::err_template_typedef);
2000 1: return DeclPtrTy();
2001 : }
2002 :
2003 6297: New = ActOnTypedefDeclarator(S, D, DC, R, TInfo, Previous, Redeclaration);
13386: branch 2 taken
10305: branch 3 taken
2004 23691: } else if (R->isFunctionType()) {
2005 : New = ActOnFunctionDeclarator(S, D, DC, R, TInfo, Previous,
2006 : move(TemplateParamLists),
2007 13386: IsFunctionDefinition, Redeclaration);
2008 : } else {
2009 : New = ActOnVariableDeclarator(S, D, DC, R, TInfo, Previous,
2010 : move(TemplateParamLists),
2011 10305: Redeclaration);
2012 : }
2013 :
6: branch 0 taken
29982: branch 1 taken
2014 29988: if (New == 0)
2015 6: return DeclPtrTy();
2016 :
2017 : // If this has an identifier and is not an invalid redeclaration or
2018 : // function template specialization, add it to the scope stack.
29982: branch 1 taken
0: branch 2 not taken
1264: branch 3 taken
28718: branch 4 taken
1140: branch 6 taken
124: branch 7 taken
29858: branch 8 taken
124: branch 9 taken
2019 29982: if (Name && !(Redeclaration && New->isInvalidDecl()))
2020 29858: PushOnScopeChains(New, S);
2021 :
2022 29982: return DeclPtrTy::make(New);
2023 : }
2024 :
2025 : /// TryToFixInvalidVariablyModifiedType - Helper method to turn variable array
2026 : /// types into constant array types in certain situations which would otherwise
2027 : /// be errors (for GCC compatibility).
2028 : static QualType TryToFixInvalidVariablyModifiedType(QualType T,
2029 : ASTContext &Context,
2030 61: bool &SizeIsNegative) {
2031 : // This method tries to turn a variable array into a constant
2032 : // array even when the size isn't an ICE. This is necessary
2033 : // for compatibility with code that depends on gcc's buggy
2034 : // constant expression folding, like struct {char x[(int)(char*)2];}
2035 61: SizeIsNegative = false;
2036 :
2037 61: QualifierCollector Qs;
2038 61: const Type *Ty = Qs.strip(T);
2039 :
3: branch 1 taken
58: branch 2 taken
2040 61: if (const PointerType* PTy = dyn_cast<PointerType>(Ty)) {
2041 3: QualType Pointee = PTy->getPointeeType();
2042 : QualType FixedType =
2043 3: TryToFixInvalidVariablyModifiedType(Pointee, Context, SizeIsNegative);
2: branch 1 taken
1: branch 2 taken
2044 3: if (FixedType.isNull()) return FixedType;
2045 1: FixedType = Context.getPointerType(FixedType);
2046 1: return Qs.apply(FixedType);
2047 : }
2048 :
2049 58: const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T);
1: branch 0 taken
57: branch 1 taken
2050 58: if (!VLATy)
2051 1: return QualType();
2052 : // FIXME: We should probably handle this case
1: branch 3 taken
56: branch 4 taken
2053 57: if (VLATy->getElementType()->isVariablyModifiedType())
2054 1: return QualType();
2055 :
2056 56: Expr::EvalResult EvalResult;
56: branch 1 taken
0: branch 2 not taken
42: branch 5 taken
14: branch 6 taken
1: branch 8 taken
41: branch 9 taken
15: branch 10 taken
41: branch 11 taken
2057 56: if (!VLATy->getSizeExpr() ||
2058 : !VLATy->getSizeExpr()->Evaluate(EvalResult, Context) ||
2059 : !EvalResult.Val.isInt())
2060 15: return QualType();
2061 :
2062 41: llvm::APSInt &Res = EvalResult.Val.getInt();
38: branch 5 taken
3: branch 6 taken
2063 41: if (Res >= llvm::APSInt(Res.getBitWidth(), Res.isUnsigned())) {
2064 : // TODO: preserve the size expression in declarator info
2065 : return Context.getConstantArrayType(VLATy->getElementType(),
2066 38: Res, ArrayType::Normal, 0);
2067 : }
2068 :
2069 3: SizeIsNegative = true;
2070 3: return QualType();
2071 : }
2072 :
2073 : /// \brief Register the given locally-scoped external C declaration so
2074 : /// that it can be found later for redeclarations
2075 : void
2076 : Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND,
2077 : const LookupResult &Previous,
2078 162: Scope *S) {
2079 : assert(ND->getLexicalDeclContext()->isFunctionOrMethod() &&
162: branch 2 taken
0: branch 3 not taken
2080 162: "Decl is not a locally-scoped decl!");
2081 : // Note that we have a locally-scoped external with this name.
2082 162: LocallyScopedExternalDecls[ND->getDeclName()] = ND;
2083 :
143: branch 1 taken
19: branch 2 taken
2084 162: if (!Previous.isSingleResult())
2085 143: return;
2086 :
2087 19: NamedDecl *PrevDecl = Previous.getFoundDecl();
2088 :
2089 : // If there was a previous declaration of this variable, it may be
2090 : // in our identifier chain. Update the identifier chain with the new
2091 : // declaration.
19: branch 0 taken
0: branch 1 not taken
19: branch 3 taken
0: branch 4 not taken
19: branch 5 taken
0: branch 6 not taken
2092 19: if (S && IdResolver.ReplaceDecl(PrevDecl, ND)) {
2093 : // The previous declaration was found on the identifer resolver
2094 : // chain, so remove it from its scope.
31: branch 0 taken
0: branch 1 not taken
12: branch 4 taken
19: branch 5 taken
12: branch 6 taken
19: branch 7 taken
2095 50: while (S && !S->isDeclScope(DeclPtrTy::make(PrevDecl)))
2096 12: S = S->getParent();
2097 :
19: branch 0 taken
0: branch 1 not taken
2098 19: if (S)
2099 19: S->RemoveDecl(DeclPtrTy::make(PrevDecl));
2100 : }
2101 : }
2102 :
2103 : /// \brief Diagnose function specifiers on a declaration of an identifier that
2104 : /// does not identify a function.
2105 34612: void Sema::DiagnoseFunctionSpecifiers(Declarator& D) {
2106 : // FIXME: We should probably indicate the identifier in question to avoid
2107 : // confusion for constructs like "inline int a(), b;"
4: branch 2 taken
34608: branch 3 taken
2108 34612: if (D.getDeclSpec().isInlineSpecified())
2109 : Diag(D.getDeclSpec().getInlineSpecLoc(),
2110 4: diag::err_inline_non_function);
2111 :
1: branch 2 taken
34611: branch 3 taken
2112 34612: if (D.getDeclSpec().isVirtualSpecified())
2113 : Diag(D.getDeclSpec().getVirtualSpecLoc(),
2114 1: diag::err_virtual_non_function);
2115 :
0: branch 2 not taken
34612: branch 3 taken
2116 34612: if (D.getDeclSpec().isExplicitSpecified())
2117 : Diag(D.getDeclSpec().getExplicitSpecLoc(),
2118 0: diag::err_explicit_non_function);
2119 34612: }
2120 :
2121 : NamedDecl*
2122 : Sema::ActOnTypedefDeclarator(Scope* S, Declarator& D, DeclContext* DC,
2123 : QualType R, TypeSourceInfo *TInfo,
2124 6297: LookupResult &Previous, bool &Redeclaration) {
2125 : // Typedef declarators cannot be qualified (C++ [dcl.meaning]p1).
1: branch 2 taken
6296: branch 3 taken
2126 6297: if (D.getCXXScopeSpec().isSet()) {
2127 : Diag(D.getIdentifierLoc(), diag::err_qualified_typedef_declarator)
2128 1: << D.getCXXScopeSpec().getRange();
2129 1: D.setInvalidType();
2130 : // Pretend we didn't see the scope specifier.
2131 1: DC = 0;
2132 : }
2133 :
1838: branch 1 taken
4459: branch 2 taken
2134 6297: if (getLangOptions().CPlusPlus) {
2135 : // Check that there are no default arguments (C++ only).
2136 1838: CheckExtraCXXDefaultArguments(D);
2137 : }
2138 :
2139 6297: DiagnoseFunctionSpecifiers(D);
2140 :
1: branch 2 taken
6296: branch 3 taken
2141 6297: if (D.getDeclSpec().isThreadSpecified())
2142 1: Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2143 :
2144 6297: TypedefDecl *NewTD = ParseTypedefDecl(S, D, R, TInfo);
0: branch 0 not taken
6297: branch 1 taken
2145 6297: if (!NewTD) return 0;
2146 :
2147 : // Handle attributes prior to checking for duplicates in MergeVarDecl
2148 6297: ProcessDeclAttributes(S, NewTD, D);
2149 :
2150 : // Merge the decl with the existing one if appropriate. If the decl is
2151 : // in an outer scope, it isn't the same thing.
2152 6297: FilterLookupForScope(*this, Previous, DC, S, /*ConsiderLinkage*/ false);
168: branch 1 taken
6129: branch 2 taken
2153 6297: if (!Previous.empty()) {
2154 168: Redeclaration = true;
2155 168: MergeTypeDefDecl(NewTD, Previous);
2156 : }
2157 :
2158 : // C99 6.7.7p2: If a typedef name specifies a variably modified type
2159 : // then it shall have block scope.
2160 6297: QualType T = NewTD->getUnderlyingType();
16: branch 2 taken
6281: branch 3 taken
2161 6297: if (T->isVariablyModifiedType()) {
2162 16: CurFunctionNeedsScopeChecking = true;
2163 :
2: branch 1 taken
14: branch 2 taken
2164 16: if (S->getFnParent() == 0) {
2165 : bool SizeIsNegative;
2166 : QualType FixedTy =
2167 2: TryToFixInvalidVariablyModifiedType(T, Context, SizeIsNegative);
1: branch 1 taken
1: branch 2 taken
2168 2: if (!FixedTy.isNull()) {
2169 1: Diag(D.getIdentifierLoc(), diag::warn_illegal_constant_array_size);
2170 1: NewTD->setTypeSourceInfo(Context.getTrivialTypeSourceInfo(FixedTy));
2171 : } else {
0: branch 0 not taken
1: branch 1 taken
2172 1: if (SizeIsNegative)
2173 0: Diag(D.getIdentifierLoc(), diag::err_typecheck_negative_array_size);
1: branch 2 taken
0: branch 3 not taken
2174 1: else if (T->isVariableArrayType())
2175 1: Diag(D.getIdentifierLoc(), diag::err_vla_decl_in_file_scope);
2176 : else
2177 0: Diag(D.getIdentifierLoc(), diag::err_vm_decl_in_file_scope);
2178 1: NewTD->setInvalidDecl();
2179 : }
2180 : }
2181 : }
2182 :
2183 : // If this is the C FILE type, notify the AST context.
6297: branch 1 taken
0: branch 2 not taken
2184 6297: if (IdentifierInfo *II = NewTD->getIdentifier())
6280: branch 1 taken
17: branch 2 taken
5905: branch 6 taken
375: branch 7 taken
5905: branch 8 taken
392: branch 9 taken
2185 6297: if (!NewTD->isInvalidDecl() &&
2186 : NewTD->getDeclContext()->getLookupContext()->isTranslationUnit()) {
5: branch 1 taken
5900: branch 2 taken
2187 5905: if (II->isStr("FILE"))
2188 5: Context.setFILEDecl(NewTD);
1: branch 1 taken
5899: branch 2 taken
2189 5900: else if (II->isStr("jmp_buf"))
2190 1: Context.setjmp_bufDecl(NewTD);
1: branch 1 taken
5898: branch 2 taken
2191 5899: else if (II->isStr("sigjmp_buf"))
2192 1: Context.setsigjmp_bufDecl(NewTD);
2193 : }
2194 :
2195 6297: return NewTD;
2196 : }
2197 :
2198 : /// \brief Determines whether the given declaration is an out-of-scope
2199 : /// previous declaration.
2200 : ///
2201 : /// This routine should be invoked when name lookup has found a
2202 : /// previous declaration (PrevDecl) that is not in the scope where a
2203 : /// new declaration by the same name is being introduced. If the new
2204 : /// declaration occurs in a local scope, previous declarations with
2205 : /// linkage may still be considered previous declarations (C99
2206 : /// 6.2.2p4-5, C++ [basic.link]p6).
2207 : ///
2208 : /// \param PrevDecl the previous declaration found by name
2209 : /// lookup
2210 : ///
2211 : /// \param DC the context in which the new declaration is being
2212 : /// declared.
2213 : ///
2214 : /// \returns true if PrevDecl is an out-of-scope previous declaration
2215 : /// for a new delcaration with the same name.
2216 : static bool
2217 : isOutOfScopePreviousDeclaration(NamedDecl *PrevDecl, DeclContext *DC,
2218 156: ASTContext &Context) {
0: branch 0 not taken
156: branch 1 taken
2219 156: if (!PrevDecl)
2220 0: return 0;
2221 :
3: branch 1 taken
153: branch 2 taken
2222 156: if (!PrevDecl->hasLinkage())
2223 3: return false;
2224 :
124: branch 1 taken
29: branch 2 taken
2225 153: if (Context.getLangOptions().CPlusPlus) {
2226 : // C++ [basic.link]p6:
2227 : // If there is a visible declaration of an entity with linkage
2228 : // having the same name and type, ignoring entities declared
2229 : // outside the innermost enclosing namespace scope, the block
2230 : // scope declaration declares that same entity and receives the
2231 : // linkage of the previous declaration.
2232 124: DeclContext *OuterContext = DC->getLookupContext();
120: branch 1 taken
4: branch 2 taken
2233 124: if (!OuterContext->isFunctionOrMethod())
2234 : // This rule only applies to block-scope declarations.
2235 120: return false;
2236 : else {
2237 4: DeclContext *PrevOuterContext = PrevDecl->getDeclContext();
1: branch 1 taken
3: branch 2 taken
2238 4: if (PrevOuterContext->isRecord())
2239 : // We found a member function: ignore it.
2240 1: return false;
2241 : else {
2242 : // Find the innermost enclosing namespace for the new and
2243 : // previous declarations.
3: branch 1 taken
3: branch 2 taken
2244 9: while (!OuterContext->isFileContext())
2245 3: OuterContext = OuterContext->getParent();
0: branch 1 not taken
3: branch 2 taken
2246 6: while (!PrevOuterContext->isFileContext())
2247 0: PrevOuterContext = PrevOuterContext->getParent();
2248 :
2249 : // The previous declaration is in a different namespace, so it
2250 : // isn't the same function.
0: branch 2 not taken
3: branch 3 taken
2251 3: if (OuterContext->getPrimaryContext() !=
2252 : PrevOuterContext->getPrimaryContext())
2253 0: return false;
2254 : }
2255 : }
2256 : }
2257 :
2258 32: return true;
2259 : }
2260 :
2261 : NamedDecl*
2262 : Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
2263 : QualType R, TypeSourceInfo *TInfo,
2264 : LookupResult &Previous,
2265 : MultiTemplateParamsArg TemplateParamLists,
2266 10305: bool &Redeclaration) {
2267 10305: DeclarationName Name = GetNameForDeclarator(D);
2268 :
2269 : // Check that there are no default arguments (C++ only).
3552: branch 1 taken
6753: branch 2 taken
2270 10305: if (getLangOptions().CPlusPlus)
2271 3552: CheckExtraCXXDefaultArguments(D);
2272 :
2273 : VarDecl *NewVD;
2274 : VarDecl::StorageClass SC;
0: branch 2 not taken
9214: branch 3 taken
476: branch 4 taken
533: branch 5 taken
3: branch 6 taken
59: branch 7 taken
18: branch 8 taken
2: branch 9 taken
2275 10305: switch (D.getDeclSpec().getStorageClassSpec()) {
2276 0: default: assert(0 && "Unknown storage class!");
2277 9214: case DeclSpec::SCS_unspecified: SC = VarDecl::None; break;
2278 476: case DeclSpec::SCS_extern: SC = VarDecl::Extern; break;
2279 533: case DeclSpec::SCS_static: SC = VarDecl::Static; break;
2280 3: case DeclSpec::SCS_auto: SC = VarDecl::Auto; break;
2281 59: case DeclSpec::SCS_register: SC = VarDecl::Register; break;
2282 18: case DeclSpec::SCS_private_extern: SC = VarDecl::PrivateExtern; break;
2283 : case DeclSpec::SCS_mutable:
2284 : // mutable can only appear on non-static class members, so it's always
2285 : // an error here
2286 2: Diag(D.getIdentifierLoc(), diag::err_mutable_nonmember);
2287 2: D.setInvalidType();
2288 2: SC = VarDecl::None;
2289 : break;
2290 : }
2291 :
2292 10305: IdentifierInfo *II = Name.getAsIdentifierInfo();
2: branch 0 taken
10303: branch 1 taken
2293 10305: if (!II) {
2294 : Diag(D.getIdentifierLoc(), diag::err_bad_variable_name)
2295 2: << Name.getAsString();
2296 2: return 0;
2297 : }
2298 :
2299 10303: DiagnoseFunctionSpecifiers(D);
2300 :
10028: branch 1 taken
275: branch 2 taken
3117: branch 4 taken
6911: branch 5 taken
3117: branch 6 taken
7186: branch 7 taken
2301 10303: if (!DC->isRecord() && S->getFnParent() == 0) {
2302 : // C99 6.9p2: The storage-class specifiers auto and register shall not
2303 : // appear in the declaration specifiers in an external declaration.
3117: branch 0 taken
0: branch 1 not taken
2: branch 2 taken
3115: branch 3 taken
2304 3117: if (SC == VarDecl::Auto || SC == VarDecl::Register) {
2305 :
2306 : // If this is a register variable with an asm label specified, then this
2307 : // is a GNU extension.
2: branch 0 taken
0: branch 1 not taken
1: branch 3 taken
1: branch 4 taken
1: branch 5 taken
1: branch 6 taken
2308 2: if (SC == VarDecl::Register && D.getAsmLabel())
2309 1: Diag(D.getIdentifierLoc(), diag::err_unsupported_global_register);
2310 : else
2311 1: Diag(D.getIdentifierLoc(), diag::err_typecheck_sclass_fscope);
2312 2: D.setInvalidType();
2313 : }
2314 : }
275: branch 1 taken
10028: branch 2 taken
65: branch 4 taken
210: branch 5 taken
65: branch 6 taken
10238: branch 7 taken
2315 10303: if (DC->isRecord() && !CurContext->isRecord()) {
2316 : // This is an out-of-line definition of a static data member.
1: branch 0 taken
64: branch 1 taken
2317 65: if (SC == VarDecl::Static) {
2318 : Diag(D.getDeclSpec().getStorageClassSpecLoc(),
2319 : diag::err_static_out_of_line)
2320 : << CodeModificationHint::CreateRemoval(
2321 1: D.getDeclSpec().getStorageClassSpecLoc());
64: branch 0 taken
0: branch 1 not taken
2322 64: } else if (SC == VarDecl::None)
2323 64: SC = VarDecl::Static;
2324 : }
597: branch 0 taken
9706: branch 1 taken
2325 10303: if (SC == VarDecl::Static) {
275: branch 1 taken
322: branch 2 taken
2326 597: if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
1: branch 1 taken
274: branch 2 taken
2327 275: if (RD->isLocalClass())
2328 : Diag(D.getIdentifierLoc(),
2329 : diag::err_static_data_member_not_allowed_in_local_class)
2330 1: << Name << RD->getDeclName();
2331 : }
2332 : }
2333 :
2334 : // Match up the template parameter lists with the scope specifier, then
2335 : // determine whether we have a template or a template specialization.
2336 10303: bool isExplicitSpecialization = false;
2: branch 0 taken
10301: branch 1 taken
2337 10303: if (TemplateParameterList *TemplateParams
2338 : = MatchTemplateParametersToScopeSpecifier(
2339 : D.getDeclSpec().getSourceRange().getBegin(),
2340 : D.getCXXScopeSpec(),
2341 : (TemplateParameterList**)TemplateParamLists.get(),
2342 : TemplateParamLists.size(),
2343 10303: isExplicitSpecialization)) {
2: branch 1 taken
0: branch 2 not taken
2344 2: if (TemplateParams->size() > 0) {
2345 : // There is no such thing as a variable template.
2346 : Diag(D.getIdentifierLoc(), diag::err_template_variable)
2347 : << II
2348 : << SourceRange(TemplateParams->getTemplateLoc(),
2349 2: TemplateParams->getRAngleLoc());
2350 2: return 0;
2351 : } else {
2352 : // There is an extraneous 'template<>' for this variable. Complain
2353 : // about it, but allow the declaration of the variable.
2354 : Diag(TemplateParams->getTemplateLoc(),
2355 : diag::err_template_variable_noparams)
2356 : << II
2357 : << SourceRange(TemplateParams->getTemplateLoc(),
2358 0: TemplateParams->getRAngleLoc());
2359 :
2360 0: isExplicitSpecialization = true;
2361 : }
2362 : }
2363 :
2364 : NewVD = VarDecl::Create(Context, DC, D.getIdentifierLoc(),
2365 10301: II, R, TInfo, SC);
2366 :
122: branch 1 taken
10179: branch 2 taken
2367 10301: if (D.isInvalidType())
2368 122: NewVD->setInvalidDecl();
2369 :
16: branch 2 taken
10285: branch 3 taken
2370 10301: if (D.getDeclSpec().isThreadSpecified()) {
3: branch 1 taken
13: branch 2 taken
2371 16: if (NewVD->hasLocalStorage())
2372 3: Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_thread_non_global);
0: branch 1 not taken
13: branch 2 taken
2373 13: else if (!Context.Target.isTLSSupported())
2374 0: Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_thread_unsupported);
2375 : else
2376 13: NewVD->setThreadSpecified(true);
2377 : }
2378 :
2379 : // Set the lexical context. If the declarator has a C++ scope specifier, the
2380 : // lexical context will be different from the semantic context.
2381 10301: NewVD->setLexicalDeclContext(CurContext);
2382 :
2383 : // Handle attributes prior to checking for duplicates in MergeVarDecl
2384 10301: ProcessDeclAttributes(S, NewVD, D);
2385 :
2386 : // Handle GNU asm-label extension (encoded as an attribute).
10: branch 1 taken
10291: branch 2 taken
2387 10301: if (Expr *E = (Expr*) D.getAsmLabel()) {
2388 : // The parser guarantees this is a string.
2389 10: StringLiteral *SE = cast<StringLiteral>(E);
10: branch 2 taken
0: branch 3 not taken
2390 10: NewVD->addAttr(::new (Context) AsmLabelAttr(SE->getString()));
2391 : }
2392 :
2393 : // Don't consider existing declarations that are in a different
2394 : // scope and are out-of-semantic-context declarations (if the new
2395 : // declaration has linkage).
2396 10301: FilterLookupForScope(*this, Previous, DC, S, NewVD->hasLinkage());
2397 :
2398 : // Merge the decl with the existing one if appropriate.
273: branch 1 taken
10028: branch 2 taken
2399 10301: if (!Previous.empty()) {
273: branch 1 taken
0: branch 2 not taken
1: branch 5 taken
272: branch 6 taken
1: branch 9 taken
0: branch 10 not taken
1: branch 11 taken
272: branch 12 taken
2400 273: if (Previous.isSingleResult() &&
2401 : isa<FieldDecl>(Previous.getFoundDecl()) &&
2402 : D.getCXXScopeSpec().isSet()) {
2403 : // The user tried to define a non-static data member
2404 : // out-of-line (C++ [dcl.meaning]p1).
2405 : Diag(NewVD->getLocation(), diag::err_nonstatic_member_out_of_line)
2406 1: << D.getCXXScopeSpec().getRange();
2407 1: Previous.clear();
2408 1: NewVD->setInvalidDecl();
2409 : }
4: branch 2 taken
10024: branch 3 taken
2410 10028: } else if (D.getCXXScopeSpec().isSet()) {
2411 : // No previous declaration in the qualifying scope.
2412 : Diag(D.getIdentifierLoc(), diag::err_no_member)
2413 : << Name << computeDeclContext(D.getCXXScopeSpec(), true)
2414 4: << D.getCXXScopeSpec().getRange();
2415 4: NewVD->setInvalidDecl();
2416 : }
2417 :
2418 10301: CheckVariableDeclaration(NewVD, Previous, Redeclaration);
2419 :
2420 : // This is an explicit specialization of a static data member. Check it.
19: branch 0 taken
10282: branch 1 taken
18: branch 3 taken
1: branch 4 taken
1: branch 6 taken
17: branch 7 taken
1: branch 8 taken
10300: branch 9 taken
2421 10301: if (isExplicitSpecialization && !NewVD->isInvalidDecl() &&
2422 : CheckMemberSpecialization(NewVD, Previous))
2423 1: NewVD->setInvalidDecl();
2424 :
2425 : // attributes declared post-definition are currently ignored
274: branch 1 taken
10027: branch 2 taken
2426 10301: if (Previous.isSingleResult()) {
2427 274: VarDecl *Def = dyn_cast<VarDecl>(Previous.getFoundDecl());
271: branch 0 taken
3: branch 1 taken
90: branch 3 taken
181: branch 4 taken
33: branch 5 taken
57: branch 6 taken
1: branch 8 taken
32: branch 9 taken
1: branch 10 taken
273: branch 11 taken
2428 274: if (Def && (Def = Def->getDefinition()) &&
2429 : Def != NewVD && D.hasAttributes()) {
2430 1: Diag(NewVD->getLocation(), diag::warn_attribute_precede_definition);
2431 1: Diag(Def->getLocation(), diag::note_previous_definition);
2432 : }
2433 : }
2434 :
2435 : // If this is a locally-scoped extern C variable, update the map of
2436 : // such variables.
6910: branch 1 taken
3391: branch 2 taken
54: branch 4 taken
6856: branch 5 taken
44: branch 7 taken
10: branch 8 taken
44: branch 9 taken
10257: branch 10 taken
2437 10301: if (CurContext->isFunctionOrMethod() && NewVD->isExternC() &&
2438 : !NewVD->isInvalidDecl())
2439 44: RegisterLocallyScopedExternCDecl(NewVD, Previous, S);
2440 :
2441 10301: return NewVD;
2442 : }
2443 :
2444 : /// \brief Perform semantic checking on a newly-created variable
2445 : /// declaration.
2446 : ///
2447 : /// This routine performs all of the type-checking required for a
2448 : /// variable declaration once it has been built. It is used both to
2449 : /// check variables after they have been parsed and their declarators
2450 : /// have been translated into a declaration, and to check variables
2451 : /// that have been instantiated from a template.
2452 : ///
2453 : /// Sets NewVD->isInvalidDecl() if an error was encountered.
2454 : void Sema::CheckVariableDeclaration(VarDecl *NewVD,
2455 : LookupResult &Previous,
2456 10753: bool &Redeclaration) {
2457 : // If the decl is already known invalid, don't check it.
127: branch 1 taken
10626: branch 2 taken
2458 10753: if (NewVD->isInvalidDecl())
2459 127: return;
2460 :
2461 10626: QualType T = NewVD->getType();
2462 :
3: branch 2 taken
10623: branch 3 taken
2463 10626: if (T->isObjCInterfaceType()) {
2464 3: Diag(NewVD->getLocation(), diag::err_statically_allocated_object);
2465 3: return NewVD->setInvalidDecl();
2466 : }
2467 :
2468 : // Emit an error if an address space was applied to decl with local storage.
2469 : // This includes arrays of objects with address space qualifiers, but not
2470 : // automatic variables that point to other address spaces.
2471 : // ISO/IEC TR 18037 S5.1.2
6831: branch 1 taken
3792: branch 2 taken
3: branch 4 taken
6828: branch 5 taken
3: branch 6 taken
10620: branch 7 taken
2472 10623: if (NewVD->hasLocalStorage() && (T.getAddressSpace() != 0)) {
2473 3: Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl);
2474 3: return NewVD->setInvalidDecl();
2475 : }
2476 :
6828: branch 1 taken
3792: branch 2 taken
6: branch 4 taken
6822: branch 5 taken
1: branch 7 taken
5: branch 8 taken
1: branch 9 taken
10619: branch 10 taken
2477 10620: if (NewVD->hasLocalStorage() && T.isObjCGCWeak()
2478 : && !NewVD->hasAttr<BlocksAttr>())
2479 1: Diag(NewVD->getLocation(), diag::warn_attribute_weak_on_local);
2480 :
2481 10620: bool isVM = T->isVariablyModifiedType();
10514: branch 0 taken
106: branch 1 taken
10506: branch 3 taken
8: branch 4 taken
109: branch 6 taken
10397: branch 7 taken
223: branch 8 taken
10397: branch 9 taken
2482 10620: if (isVM || NewVD->hasAttr<CleanupAttr>() ||
2483 : NewVD->hasAttr<BlocksAttr>())
2484 223: CurFunctionNeedsScopeChecking = true;
2485 :
106: branch 0 taken
10514: branch 1 taken
91: branch 3 taken
15: branch 4 taken
80: branch 7 taken
10525: branch 8 taken
3: branch 10 taken
77: branch 11 taken
18: branch 12 taken
10602: branch 13 taken
2486 10620: if ((isVM && NewVD->hasLinkage()) ||
2487 : (T->isVariableArrayType() && NewVD->hasGlobalStorage())) {
2488 : bool SizeIsNegative;
2489 : QualType FixedTy =
2490 18: TryToFixInvalidVariablyModifiedType(T, Context, SizeIsNegative);
2491 :
13: branch 1 taken
5: branch 2 taken
10: branch 5 taken
3: branch 6 taken
10: branch 7 taken
8: branch 8 taken
2492 18: if (FixedTy.isNull() && T->isVariableArrayType()) {
2493 10: const VariableArrayType *VAT = Context.getAsVariableArrayType(T);
2494 : // FIXME: This won't give the correct result for
2495 : // int a[10][n];
2496 10: SourceRange SizeRange = VAT->getSizeExpr()->getSourceRange();
2497 :
5: branch 1 taken
5: branch 2 taken
2498 10: if (NewVD->isFileVarDecl())
2499 : Diag(NewVD->getLocation(), diag::err_vla_decl_in_file_scope)
2500 5: << SizeRange;
3: branch 1 taken
2: branch 2 taken
2501 5: else if (NewVD->getStorageClass() == VarDecl::Static)
2502 : Diag(NewVD->getLocation(), diag::err_vla_decl_has_static_storage)
2503 3: << SizeRange;
2504 : else
2505 : Diag(NewVD->getLocation(), diag::err_vla_decl_has_extern_linkage)
2506 2: << SizeRange;
2507 10: return NewVD->setInvalidDecl();
2508 : }
2509 :
3: branch 1 taken
5: branch 2 taken
2510 8: if (FixedTy.isNull()) {
2: branch 1 taken
1: branch 2 taken
2511 3: if (NewVD->isFileVarDecl())
2512 2: Diag(NewVD->getLocation(), diag::err_vm_decl_in_file_scope);
2513 : else
2514 1: Diag(NewVD->getLocation(), diag::err_vm_decl_has_extern_linkage);
2515 3: return NewVD->setInvalidDecl();
2516 : }
2517 :
2518 5: Diag(NewVD->getLocation(), diag::warn_illegal_constant_array_size);
2519 5: NewVD->setType(FixedTy);
2520 : }
2521 :
10308: branch 1 taken
299: branch 2 taken
1708: branch 4 taken
8600: branch 5 taken
1708: branch 6 taken
8899: branch 7 taken
2522 10607: if (Previous.empty() && NewVD->isExternC()) {
2523 : // Since we did not find anything by this name and we're declaring
2524 : // an extern "C" variable, look for a non-visible extern "C"
2525 : // declaration with the same name.
2526 : llvm::DenseMap<DeclarationName, NamedDecl *>::iterator Pos
2527 1708: = LocallyScopedExternalDecls.find(NewVD->getDeclName());
2: branch 3 taken
1706: branch 4 taken
2528 1708: if (Pos != LocallyScopedExternalDecls.end())
2529 2: Previous.addDecl(Pos->second);
2530 : }
2531 :
9: branch 2 taken
10598: branch 3 taken
5: branch 5 taken
4: branch 6 taken
5: branch 7 taken
10602: branch 8 taken
2532 10607: if (T->isVoidType() && !NewVD->hasExternalStorage()) {
2533 : Diag(NewVD->getLocation(), diag::err_typecheck_decl_incomplete_type)
2534 5: << T;
2535 5: return NewVD->setInvalidDecl();
2536 : }
2537 :
3776: branch 1 taken
6826: branch 2 taken
3: branch 4 taken
3773: branch 5 taken
3: branch 6 taken
10599: branch 7 taken
2538 10602: if (!NewVD->hasLocalStorage() && NewVD->hasAttr<BlocksAttr>()) {
2539 3: Diag(NewVD->getLocation(), diag::err_block_on_nonlocal);
2540 3: return NewVD->setInvalidDecl();
2541 : }
2542 :
93: branch 0 taken
10506: branch 1 taken
2: branch 3 taken
91: branch 4 taken
2: branch 5 taken
10597: branch 6 taken
2543 10599: if (isVM && NewVD->hasAttr<BlocksAttr>()) {
2544 2: Diag(NewVD->getLocation(), diag::err_block_on_vm);
2545 2: return NewVD->setInvalidDecl();
2546 : }
2547 :
301: branch 1 taken
10296: branch 2 taken
2548 10597: if (!Previous.empty()) {
2549 301: Redeclaration = true;
2550 301: MergeVarDecl(NewVD, Previous);
2551 : }
2552 : }
2553 :
2554 : /// \brief Data used with FindOverriddenMethod
2555 : struct FindOverriddenMethodData {
2556 : Sema *S;
2557 : CXXMethodDecl *Method;
2558 : };
2559 :
2560 : /// \brief Member lookup function that determines whether a given C++
2561 : /// method overrides a method in a base class, to be used with
2562 : /// CXXRecordDecl::lookupInBases().
2563 : static bool FindOverriddenMethod(const CXXBaseSpecifier *Specifier,
2564 : CXXBasePath &Path,
2565 3854: void *UserData) {
2566 3854: RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
2567 :
2568 : FindOverriddenMethodData *Data
2569 3854: = reinterpret_cast<FindOverriddenMethodData*>(UserData);
2570 :
2571 3854: DeclarationName Name = Data->Method->getDeclName();
2572 :
2573 : // FIXME: Do we care about other names here too?
1444: branch 1 taken
2410: branch 2 taken
2574 3854: if (Name.getNameKind() == DeclarationName::CXXDestructorName) {
2575 : // We really want to find the base class constructor here.
2576 1444: QualType T = Data->S->Context.getTypeDeclType(BaseRecord);
2577 1444: CanQualType CT = Data->S->Context.getCanonicalType(T);
2578 :
2579 1444: Name = Data->S->Context.DeclarationNames.getCXXDestructorName(CT);
2580 : }
2581 :
3105: branch 1 taken
3691: branch 2 taken
2582 6796: for (Path.Decls = BaseRecord->lookup(Name);
2583 : Path.Decls.first != Path.Decls.second;
2584 : ++Path.Decls.first) {
3103: branch 1 taken
2: branch 2 taken
2585 3105: if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*Path.Decls.first)) {
167: branch 1 taken
2936: branch 2 taken
163: branch 4 taken
4: branch 5 taken
163: branch 6 taken
2940: branch 7 taken
2586 3103: if (MD->isVirtual() && !Data->S->IsOverload(Data->Method, MD))
2587 163: return true;
2588 : }
2589 : }
2590 :
2591 3691: return false;
2592 : }
2593 :
2594 : /// AddOverriddenMethods - See if a method overrides any in the base classes,
2595 : /// and if so, check that it's a valid override and remember it.
2596 10990: void Sema::AddOverriddenMethods(CXXRecordDecl *DC, CXXMethodDecl *MD) {
2597 : // Look for virtual methods in base classes that this method might override.
2598 10990: CXXBasePaths Paths;
2599 : FindOverriddenMethodData Data;
2600 10990: Data.Method = MD;
2601 10990: Data.S = this;
157: branch 1 taken
10833: branch 2 taken
2602 10990: if (DC->lookupInBases(&FindOverriddenMethod, &Data, Paths)) {
163: branch 1 taken
157: branch 2 taken
2603 477: for (CXXBasePaths::decl_iterator I = Paths.found_decls_begin(),
2604 157: E = Paths.found_decls_end(); I != E; ++I) {
163: branch 1 taken
0: branch 2 not taken
2605 163: if (CXXMethodDecl *OldMD = dyn_cast<CXXMethodDecl>(*I)) {
153: branch 1 taken
10: branch 2 taken
148: branch 4 taken
5: branch 5 taken
147: branch 7 taken
1: branch 8 taken
147: branch 9 taken
16: branch 10 taken
2606 163: if (!CheckOverridingFunctionReturnType(MD, OldMD) &&
2607 : !CheckOverridingFunctionExceptionSpec(MD, OldMD) &&
2608 : !CheckOverridingFunctionAttributes(MD, OldMD))
2609 147: MD->addOverriddenMethod(OldMD->getCanonicalDecl());
2610 : }
2611 : }
2612 10990: }
2613 10990: }
2614 :
2615 : NamedDecl*
2616 : Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
2617 : QualType R, TypeSourceInfo *TInfo,
2618 : LookupResult &Previous,
2619 : MultiTemplateParamsArg TemplateParamLists,
2620 13421: bool IsFunctionDefinition, bool &Redeclaration) {
0: branch 2 not taken
13421: branch 3 taken
2621 13421: assert(R.getTypePtr()->isFunctionType());
2622 :
2623 13421: DeclarationName Name = GetNameForDeclarator(D);
2624 13421: FunctionDecl::StorageClass SC = FunctionDecl::None;
0: branch 2 not taken
2: branch 3 taken
10498: branch 4 taken
1396: branch 5 taken
1524: branch 6 taken
1: branch 7 taken
2625 13421: switch (D.getDeclSpec().getStorageClassSpec()) {
2626 0: default: assert(0 && "Unknown storage class!");
2627 : case DeclSpec::SCS_auto:
2628 : case DeclSpec::SCS_register:
2629 : case DeclSpec::SCS_mutable:
2630 : Diag(D.getDeclSpec().getStorageClassSpecLoc(),
2631 2: diag::err_typecheck_sclass_func);
2632 2: D.setInvalidType();
2633 2: break;
2634 10498: case DeclSpec::SCS_unspecified: SC = FunctionDecl::None; break;
2635 1396: case DeclSpec::SCS_extern: SC = FunctionDecl::Extern; break;
2636 : case DeclSpec::SCS_static: {
1: branch 2 taken
1523: branch 3 taken
2637 1524: if (CurContext->getLookupContext()->isFunctionOrMethod()) {
2638 : // C99 6.7.1p5:
2639 : // The declaration of an identifier for a function that has
2640 : // block scope shall have no explicit storage-class specifier
2641 : // other than extern
2642 : // See also (C++ [dcl.stc]p4).
2643 : Diag(D.getDeclSpec().getStorageClassSpecLoc(),
2644 1: diag::err_static_block_func);
2645 1: SC = FunctionDecl::None;
2646 : } else
2647 1523: SC = FunctionDecl::Static;
2648 1524: break;
2649 : }
2650 1: case DeclSpec::SCS_private_extern: SC = FunctionDecl::PrivateExtern;break;
2651 : }
2652 :
1: branch 2 taken
13420: branch 3 taken
2653 13421: if (D.getDeclSpec().isThreadSpecified())
2654 1: Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2655 :
2656 13421: bool isFriend = D.getDeclSpec().isFriendSpecified();
2657 13421: bool isInline = D.getDeclSpec().isInlineSpecified();
2658 13421: bool isVirtual = D.getDeclSpec().isVirtualSpecified();
2659 13421: bool isExplicit = D.getDeclSpec().isExplicitSpecified();
2660 :
2661 : // Check that the return type is not an abstract class type.
2662 : // For record types, this is done by the AbstractClassUsageDiagnoser once
2663 : // the class has been completely parsed.
10408: branch 1 taken
3013: branch 2 taken
0: branch 8 not taken
10408: branch 9 taken
0: branch 10 not taken
13421: branch 11 taken
2664 13421: if (!DC->isRecord() &&
2665 : RequireNonAbstractType(D.getIdentifierLoc(),
2666 : R->getAs<FunctionType>()->getResultType(),
2667 : diag::err_abstract_type_in_decl,
2668 : AbstractReturnType))
2669 0: D.setInvalidType();
2670 :
2671 : // Do not allow returning a objc interface by-value.
2: branch 5 taken
13419: branch 6 taken
2672 13421: if (R->getAs<FunctionType>()->getResultType()->isObjCInterfaceType()) {
2673 : Diag(D.getIdentifierLoc(),
2674 : diag::err_object_cannot_be_passed_returned_by_value) << 0
2675 2: << R->getAs<FunctionType>()->getResultType();
2676 2: D.setInvalidType();
2677 : }
2678 :
2679 13421: bool isVirtualOkay = false;
2680 : FunctionDecl *NewFD;
2681 :
35: branch 0 taken
13386: branch 1 taken
2682 13421: if (isFriend) {
2683 : // C++ [class.friend]p5
2684 : // A function can be defined in a friend declaration of a
2685 : // class . . . . Such a function is implicitly inline.
2686 35: isInline |= IsFunctionDefinition;
2687 : }
2688 :
653: branch 1 taken
12768: branch 2 taken
2689 13421: if (Name.getNameKind() == DeclarationName::CXXConstructorName) {
2690 : // This is a C++ constructor declaration.
2691 : assert(DC->isRecord() &&
653: branch 1 taken
0: branch 2 not taken
2692 653: "Constructors can only be declared in a member context");
2693 :
2694 653: R = CheckConstructorDeclarator(D, R, SC);
2695 :
2696 : // Create the new declaration
2697 : NewFD = CXXConstructorDecl::Create(Context,
2698 : cast<CXXRecordDecl>(DC),
2699 : D.getIdentifierLoc(), Name, R, TInfo,
2700 : isExplicit, isInline,
2701 653: /*isImplicitlyDeclared=*/false);
182: branch 1 taken
12586: branch 2 taken
2702 12768: } else if (Name.getNameKind() == DeclarationName::CXXDestructorName) {
2703 : // This is a C++ destructor declaration.
181: branch 1 taken
1: branch 2 taken
2704 182: if (DC->isRecord()) {
2705 181: R = CheckDestructorDeclarator(D, SC);
2706 :
2707 : NewFD = CXXDestructorDecl::Create(Context,
2708 : cast<CXXRecordDecl>(DC),
2709 : D.getIdentifierLoc(), Name, R,
2710 : isInline,
2711 181: /*isImplicitlyDeclared=*/false);
2712 :
2713 181: isVirtualOkay = true;
2714 : } else {
2715 1: Diag(D.getIdentifierLoc(), diag::err_destructor_not_member);
2716 :
2717 : // Create a FunctionDecl to satisfy the function definition parsing
2718 : // code path.
2719 : NewFD = FunctionDecl::Create(Context, DC, D.getIdentifierLoc(),
2720 : Name, R, TInfo, SC, isInline,
2721 1: /*hasPrototype=*/true);
2722 1: D.setInvalidType();
2723 : }
255: branch 1 taken
12331: branch 2 taken
2724 12586: } else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
1: branch 1 taken
254: branch 2 taken
2725 255: if (!DC->isRecord()) {
2726 : Diag(D.getIdentifierLoc(),
2727 1: diag::err_conv_function_not_member);
2728 1: return 0;
2729 : }
2730 :
2731 254: CheckConversionDeclarator(D, R, SC);
2732 : NewFD = CXXConversionDecl::Create(Context, cast<CXXRecordDecl>(DC),
2733 : D.getIdentifierLoc(), Name, R, TInfo,
2734 254: isInline, isExplicit);
2735 :
2736 254: isVirtualOkay = true;
1925: branch 1 taken
10406: branch 2 taken
2737 12331: } else if (DC->isRecord()) {
2738 : // If the of the function is the same as the name of the record, then this
2739 : // must be an invalid constructor that has a return type.
2740 : // (The parser checks for a return type and makes the declarator a
2741 : // constructor if it has no return type).
2742 : // must have an invalid constructor that has a return type
1734: branch 1 taken
191: branch 2 taken
1: branch 6 taken
1733: branch 7 taken
1: branch 8 taken
1924: branch 9 taken
2743 1925: if (Name.getAsIdentifierInfo() &&
2744 : Name.getAsIdentifierInfo() == cast<CXXRecordDecl>(DC)->getIdentifier()){
2745 : Diag(D.getIdentifierLoc(), diag::err_constructor_return_type)
2746 : << SourceRange(D.getDeclSpec().getTypeSpecTypeLoc())
2747 1: << SourceRange(D.getIdentifierLoc());
2748 1: return 0;
2749 : }
2750 :
2751 1924: bool isStatic = SC == FunctionDecl::Static;
2752 :
2753 : // [class.free]p1:
2754 : // Any allocation function for a class T is a static member
2755 : // (even if not explicitly declared static).
1904: branch 1 taken
20: branch 2 taken
3: branch 4 taken
1901: branch 5 taken
23: branch 6 taken
1901: branch 7 taken
2756 1924: if (Name.getCXXOverloadedOperator() == OO_New ||
2757 : Name.getCXXOverloadedOperator() == OO_Array_New)
2758 23: isStatic = true;
2759 :
2760 : // [class.free]p6 Any deallocation function for a class X is a static member
2761 : // (even if not explicitly declared static).
1908: branch 1 taken
16: branch 2 taken
1: branch 4 taken
1907: branch 5 taken
17: branch 6 taken
1907: branch 7 taken
2762 1924: if (Name.getCXXOverloadedOperator() == OO_Delete ||
2763 : Name.getCXXOverloadedOperator() == OO_Array_Delete)
2764 17: isStatic = true;
2765 :
2766 : // This is a C++ method declaration.
2767 : NewFD = CXXMethodDecl::Create(Context, cast<CXXRecordDecl>(DC),
2768 : D.getIdentifierLoc(), Name, R, TInfo,
2769 1924: isStatic, isInline);
2770 :
2771 1924: isVirtualOkay = !isStatic;
2772 : } else {
2773 : // Determine whether the function was written with a
2774 : // prototype. This true when:
2775 : // - we're in C++ (where every function has a prototype),
2776 : // - there is a prototype in the declarator, or
2777 : // - the type R of the function is some kind of typedef or other reference
2778 : // to a type name (which eventually refers to a function type).
2779 : bool HasPrototype =
2780 : getLangOptions().CPlusPlus ||
2781 : (D.getNumTypeObjects() && D.getTypeObject(0).Fun.hasPrototype) ||
7044: branch 1 taken
3362: branch 2 taken
7024: branch 4 taken
20: branch 5 taken
1994: branch 7 taken
5030: branch 8 taken
20: branch 11 taken
1994: branch 12 taken
12: branch 15 taken
8: branch 16 taken
2782 10406: (!isa<FunctionType>(R.getTypePtr()) && R->isFunctionProtoType());
2783 :
2784 : NewFD = FunctionDecl::Create(Context, DC,
2785 : D.getIdentifierLoc(),
2786 10406: Name, R, TInfo, SC, isInline, HasPrototype);
2787 : }
2788 :
31: branch 1 taken
13388: branch 2 taken
2789 13419: if (D.isInvalidType())
2790 31: NewFD->setInvalidDecl();
2791 :
2792 : // Set the lexical context. If the declarator has a C++
2793 : // scope specifier, or is the object of a friend declaration, the
2794 : // lexical context will be different from the semantic context.
2795 13419: NewFD->setLexicalDeclContext(CurContext);
2796 :
2797 : // Match up the template parameter lists with the scope specifier, then
2798 : // determine whether we have a template or a template specialization.
2799 13419: FunctionTemplateDecl *FunctionTemplate = 0;
2800 13419: bool isExplicitSpecialization = false;
2801 13419: bool isFunctionTemplateSpecialization = false;
498: branch 0 taken
12921: branch 1 taken
2802 13419: if (TemplateParameterList *TemplateParams
2803 : = MatchTemplateParametersToScopeSpecifier(
2804 : D.getDeclSpec().getSourceRange().getBegin(),
2805 : D.getCXXScopeSpec(),
2806 : (TemplateParameterList**)TemplateParamLists.get(),
2807 : TemplateParamLists.size(),
2808 13419: isExplicitSpecialization)) {
449: branch 1 taken
49: branch 2 taken
2809 498: if (TemplateParams->size() > 0) {
2810 : // This is a function template
2811 :
2812 : // Check that we can declare a template here.
0: branch 1 not taken
449: branch 2 taken
2813 449: if (CheckTemplateDeclScope(S, TemplateParams))
2814 0: return 0;
2815 :
2816 : FunctionTemplate = FunctionTemplateDecl::Create(Context, DC,
2817 : NewFD->getLocation(),
2818 : Name, TemplateParams,
2819 449: NewFD);
2820 449: FunctionTemplate->setLexicalDeclContext(CurContext);
2821 449: NewFD->setDescribedFunctionTemplate(FunctionTemplate);
2822 : } else {
2823 : // This is a function template specialization.
2824 49: isFunctionTemplateSpecialization = true;
2825 : }
2826 :
2827 : // FIXME: Free this memory properly.
2828 498: TemplateParamLists.release();
2829 : }
2830 :
2831 : // C++ [dcl.fct.spec]p5:
2832 : // The virtual specifier shall only be used in declarations of
2833 : // nonstatic class member functions that appear within a
2834 : // member-specification of a class declaration; see 10.3.
2835 : //
557: branch 0 taken
12862: branch 1 taken
555: branch 3 taken
2: branch 4 taken
555: branch 5 taken
12864: branch 6 taken
2836 13419: if (isVirtual && !NewFD->isInvalidDecl()) {
1: branch 0 taken
554: branch 1 taken
2837 555: if (!isVirtualOkay) {
2838 : Diag(D.getDeclSpec().getVirtualSpecLoc(),
2839 1: diag::err_virtual_non_function);
2: branch 1 taken
552: branch 2 taken
2840 554: } else if (!CurContext->isRecord()) {
2841 : // 'virtual' was specified outside of the class.
2842 : Diag(D.getDeclSpec().getVirtualSpecLoc(), diag::err_virtual_out_of_class)
2843 : << CodeModificationHint::CreateRemoval(
2844 2: D.getDeclSpec().getVirtualSpecLoc());
2845 : } else {
2846 : // Okay: Add virtual to the method.
2847 552: CXXRecordDecl *CurClass = cast<CXXRecordDecl>(DC);
2848 552: CurClass->setMethodAsVirtual(NewFD);
2849 : }
2850 : }
2851 :
2852 : // C++ [dcl.fct.spec]p6:
2853 : // The explicit specifier shall be used only in the declaration of a
2854 : // constructor or conversion function within its class definition; see 12.3.1
2855 : // and 12.3.2.
31: branch 0 taken
13388: branch 1 taken
31: branch 3 taken
0: branch 4 not taken
31: branch 5 taken
13388: branch 6 taken
2856 13419: if (isExplicit && !NewFD->isInvalidDecl()) {
2: branch 1 taken
29: branch 2 taken
2857 31: if (!CurContext->isRecord()) {
2858 : // 'explicit' was specified outside of the class.
2859 : Diag(D.getDeclSpec().getExplicitSpecLoc(),
2860 : diag::err_explicit_out_of_class)
2861 : << CodeModificationHint::CreateRemoval(
2862 2: D.getDeclSpec().getExplicitSpecLoc());
10: branch 1 taken
19: branch 2 taken
1: branch 4 taken
9: branch 5 taken
1: branch 6 taken
28: branch 7 taken
2863 29: } else if (!isa<CXXConstructorDecl>(NewFD) &&
2864 : !isa<CXXConversionDecl>(NewFD)) {
2865 : // 'explicit' was specified on a function that wasn't a constructor
2866 : // or conversion function.
2867 : Diag(D.getDeclSpec().getExplicitSpecLoc(),
2868 : diag::err_explicit_non_ctor_or_conv_function)
2869 : << CodeModificationHint::CreateRemoval(
2870 1: D.getDeclSpec().getExplicitSpecLoc());
2871 : }
2872 : }
2873 :
2874 : // Filter out previous declarations that don't match the scope.
2875 13419: FilterLookupForScope(*this, Previous, DC, S, NewFD->hasLinkage());
2876 :