blob: 554e7426694543c637b1f794181628932b2022db [file] [log] [blame]
Chris Lattnerac7b83a2008-04-08 05:04:30 +00001//===------ SemaDeclCXX.cpp - Semantic Analysis for C++ 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 C++ declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/Basic/LangOptions.h"
Argiris Kirtzidis7c210ea2008-08-09 00:58:37 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorec93f442008-04-13 21:30:24 +000017#include "clang/AST/ASTContext.h"
Argiris Kirtzidis38f16712008-07-01 10:37:29 +000018#include "clang/AST/DeclCXX.h"
Chris Lattnerac7b83a2008-04-08 05:04:30 +000019#include "clang/AST/Expr.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000020#include "clang/AST/ExprObjC.h"
Chris Lattner97316c02008-04-10 02:22:51 +000021#include "clang/AST/StmtVisitor.h"
Chris Lattnerac7b83a2008-04-08 05:04:30 +000022#include "clang/AST/Type.h"
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000023#include "clang/Parse/Scope.h"
Chris Lattnerac7b83a2008-04-08 05:04:30 +000024#include "llvm/ADT/OwningPtr.h"
Chris Lattner97316c02008-04-10 02:22:51 +000025#include "llvm/Support/Compiler.h"
Chris Lattnerac7b83a2008-04-08 05:04:30 +000026
27using namespace clang;
28
Chris Lattner97316c02008-04-10 02:22:51 +000029//===----------------------------------------------------------------------===//
30// CheckDefaultArgumentVisitor
31//===----------------------------------------------------------------------===//
32
Chris Lattnerb1856db2008-04-12 23:52:44 +000033namespace {
34 /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses
35 /// the default argument of a parameter to determine whether it
36 /// contains any ill-formed subexpressions. For example, this will
37 /// diagnose the use of local variables or parameters within the
38 /// default argument expression.
39 class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000040 : public StmtVisitor<CheckDefaultArgumentVisitor, bool> {
Chris Lattnerb1856db2008-04-12 23:52:44 +000041 Expr *DefaultArg;
42 Sema *S;
Chris Lattner97316c02008-04-10 02:22:51 +000043
Chris Lattnerb1856db2008-04-12 23:52:44 +000044 public:
45 CheckDefaultArgumentVisitor(Expr *defarg, Sema *s)
46 : DefaultArg(defarg), S(s) {}
Chris Lattner97316c02008-04-10 02:22:51 +000047
Chris Lattnerb1856db2008-04-12 23:52:44 +000048 bool VisitExpr(Expr *Node);
49 bool VisitDeclRefExpr(DeclRefExpr *DRE);
50 };
Chris Lattner97316c02008-04-10 02:22:51 +000051
Chris Lattnerb1856db2008-04-12 23:52:44 +000052 /// VisitExpr - Visit all of the children of this expression.
53 bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) {
54 bool IsInvalid = false;
Chris Lattnerd5a56aa2008-07-26 22:17:49 +000055 for (Stmt::child_iterator I = Node->child_begin(),
56 E = Node->child_end(); I != E; ++I)
57 IsInvalid |= Visit(*I);
Chris Lattnerb1856db2008-04-12 23:52:44 +000058 return IsInvalid;
Chris Lattner97316c02008-04-10 02:22:51 +000059 }
60
Chris Lattnerb1856db2008-04-12 23:52:44 +000061 /// VisitDeclRefExpr - Visit a reference to a declaration, to
62 /// determine whether this declaration can be used in the default
63 /// argument expression.
64 bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) {
65 ValueDecl *Decl = DRE->getDecl();
66 if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) {
67 // C++ [dcl.fct.default]p9
68 // Default arguments are evaluated each time the function is
69 // called. The order of evaluation of function arguments is
70 // unspecified. Consequently, parameters of a function shall not
71 // be used in default argument expressions, even if they are not
72 // evaluated. Parameters of a function declared before a default
73 // argument expression are in scope and can hide namespace and
74 // class member names.
75 return S->Diag(DRE->getSourceRange().getBegin(),
76 diag::err_param_default_argument_references_param,
77 Param->getName(), DefaultArg->getSourceRange());
Steve Naroff72a6ebc2008-04-15 22:42:06 +000078 } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) {
Chris Lattnerb1856db2008-04-12 23:52:44 +000079 // C++ [dcl.fct.default]p7
80 // Local variables shall not be used in default argument
81 // expressions.
Steve Naroff72a6ebc2008-04-15 22:42:06 +000082 if (VDecl->isBlockVarDecl())
83 return S->Diag(DRE->getSourceRange().getBegin(),
84 diag::err_param_default_argument_references_local,
85 VDecl->getName(), DefaultArg->getSourceRange());
Chris Lattnerb1856db2008-04-12 23:52:44 +000086 }
Chris Lattner97316c02008-04-10 02:22:51 +000087
Chris Lattnerb1856db2008-04-12 23:52:44 +000088 // FIXME: when Clang has support for member functions, "this"
89 // will also need to be diagnosed.
90
91 return false;
92 }
Chris Lattner97316c02008-04-10 02:22:51 +000093}
94
95/// ActOnParamDefaultArgument - Check whether the default argument
96/// provided for a function parameter is well-formed. If so, attach it
97/// to the parameter declaration.
Chris Lattnerac7b83a2008-04-08 05:04:30 +000098void
99Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc,
100 ExprTy *defarg) {
101 ParmVarDecl *Param = (ParmVarDecl *)param;
102 llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg);
103 QualType ParamType = Param->getType();
104
105 // Default arguments are only permitted in C++
106 if (!getLangOptions().CPlusPlus) {
107 Diag(EqualLoc, diag::err_param_default_argument,
108 DefaultArg->getSourceRange());
109 return;
110 }
111
112 // C++ [dcl.fct.default]p5
113 // A default argument expression is implicitly converted (clause
114 // 4) to the parameter type. The default argument expression has
115 // the same semantic constraints as the initializer expression in
116 // a declaration of a variable of the parameter type, using the
117 // copy-initialization semantics (8.5).
118 //
119 // FIXME: CheckSingleAssignmentConstraints has the wrong semantics
120 // for C++ (since we want copy-initialization, not copy-assignment),
121 // but we don't have the right semantics implemented yet. Because of
122 // this, our error message is also very poor.
123 QualType DefaultArgType = DefaultArg->getType();
124 Expr *DefaultArgPtr = DefaultArg.get();
125 AssignConvertType ConvTy = CheckSingleAssignmentConstraints(ParamType,
126 DefaultArgPtr);
127 if (DefaultArgPtr != DefaultArg.get()) {
128 DefaultArg.take();
129 DefaultArg.reset(DefaultArgPtr);
130 }
131 if (DiagnoseAssignmentResult(ConvTy, DefaultArg->getLocStart(),
132 ParamType, DefaultArgType, DefaultArg.get(),
133 "in default argument")) {
134 return;
135 }
136
Chris Lattner97316c02008-04-10 02:22:51 +0000137 // Check that the default argument is well-formed
Chris Lattnerb1856db2008-04-12 23:52:44 +0000138 CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this);
Chris Lattner97316c02008-04-10 02:22:51 +0000139 if (DefaultArgChecker.Visit(DefaultArg.get()))
140 return;
141
Chris Lattnerac7b83a2008-04-08 05:04:30 +0000142 // Okay: add the default argument to the parameter
143 Param->setDefaultArg(DefaultArg.take());
144}
145
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000146/// CheckExtraCXXDefaultArguments - Check for any extra default
147/// arguments in the declarator, which is not a function declaration
148/// or definition and therefore is not permitted to have default
149/// arguments. This routine should be invoked for every declarator
150/// that is not a function declaration or definition.
151void Sema::CheckExtraCXXDefaultArguments(Declarator &D) {
152 // C++ [dcl.fct.default]p3
153 // A default argument expression shall be specified only in the
154 // parameter-declaration-clause of a function declaration or in a
155 // template-parameter (14.1). It shall not be specified for a
156 // parameter pack. If it is specified in a
157 // parameter-declaration-clause, it shall not occur within a
158 // declarator or abstract-declarator of a parameter-declaration.
159 for (unsigned i = 0; i < D.getNumTypeObjects(); ++i) {
160 DeclaratorChunk &chunk = D.getTypeObject(i);
161 if (chunk.Kind == DeclaratorChunk::Function) {
162 for (unsigned argIdx = 0; argIdx < chunk.Fun.NumArgs; ++argIdx) {
163 ParmVarDecl *Param = (ParmVarDecl *)chunk.Fun.ArgInfo[argIdx].Param;
164 if (Param->getDefaultArg()) {
165 Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc,
166 Param->getDefaultArg()->getSourceRange());
167 Param->setDefaultArg(0);
168 }
169 }
170 }
171 }
172}
173
Chris Lattnerac7b83a2008-04-08 05:04:30 +0000174// MergeCXXFunctionDecl - Merge two declarations of the same C++
175// function, once we already know that they have the same
176// type. Subroutine of MergeFunctionDecl.
177FunctionDecl *
178Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) {
179 // C++ [dcl.fct.default]p4:
180 //
181 // For non-template functions, default arguments can be added in
182 // later declarations of a function in the same
183 // scope. Declarations in different scopes have completely
184 // distinct sets of default arguments. That is, declarations in
185 // inner scopes do not acquire default arguments from
186 // declarations in outer scopes, and vice versa. In a given
187 // function declaration, all parameters subsequent to a
188 // parameter with a default argument shall have default
189 // arguments supplied in this or previous declarations. A
190 // default argument shall not be redefined by a later
191 // declaration (not even to the same value).
192 for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) {
193 ParmVarDecl *OldParam = Old->getParamDecl(p);
194 ParmVarDecl *NewParam = New->getParamDecl(p);
195
196 if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) {
197 Diag(NewParam->getLocation(),
198 diag::err_param_default_argument_redefinition,
199 NewParam->getDefaultArg()->getSourceRange());
200 Diag(OldParam->getLocation(), diag::err_previous_definition);
201 } else if (OldParam->getDefaultArg()) {
202 // Merge the old default argument into the new parameter
203 NewParam->setDefaultArg(OldParam->getDefaultArg());
204 }
205 }
206
207 return New;
208}
209
210/// CheckCXXDefaultArguments - Verify that the default arguments for a
211/// function declaration are well-formed according to C++
212/// [dcl.fct.default].
213void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) {
214 unsigned NumParams = FD->getNumParams();
215 unsigned p;
216
217 // Find first parameter with a default argument
218 for (p = 0; p < NumParams; ++p) {
219 ParmVarDecl *Param = FD->getParamDecl(p);
220 if (Param->getDefaultArg())
221 break;
222 }
223
224 // C++ [dcl.fct.default]p4:
225 // In a given function declaration, all parameters
226 // subsequent to a parameter with a default argument shall
227 // have default arguments supplied in this or previous
228 // declarations. A default argument shall not be redefined
229 // by a later declaration (not even to the same value).
230 unsigned LastMissingDefaultArg = 0;
231 for(; p < NumParams; ++p) {
232 ParmVarDecl *Param = FD->getParamDecl(p);
233 if (!Param->getDefaultArg()) {
234 if (Param->getIdentifier())
235 Diag(Param->getLocation(),
236 diag::err_param_default_argument_missing_name,
237 Param->getIdentifier()->getName());
238 else
239 Diag(Param->getLocation(),
240 diag::err_param_default_argument_missing);
241
242 LastMissingDefaultArg = p;
243 }
244 }
245
246 if (LastMissingDefaultArg > 0) {
247 // Some default arguments were missing. Clear out all of the
248 // default arguments up to (and including) the last missing
249 // default argument, so that we leave the function parameters
250 // in a semantically valid state.
251 for (p = 0; p <= LastMissingDefaultArg; ++p) {
252 ParmVarDecl *Param = FD->getParamDecl(p);
253 if (Param->getDefaultArg()) {
254 delete Param->getDefaultArg();
255 Param->setDefaultArg(0);
256 }
257 }
258 }
259}
Douglas Gregorec93f442008-04-13 21:30:24 +0000260
261/// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is
262/// one entry in the base class list of a class specifier, for
263/// example:
264/// class foo : public bar, virtual private baz {
265/// 'public bar' and 'virtual private baz' are each base-specifiers.
266void Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange,
267 bool Virtual, AccessSpecifier Access,
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000268 TypeTy *basetype, SourceLocation BaseLoc) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000269 RecordDecl *Decl = (RecordDecl*)classdecl;
270 QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype);
271
272 // Base specifiers must be record types.
273 if (!BaseType->isRecordType()) {
274 Diag(BaseLoc, diag::err_base_must_be_class, SpecifierRange);
275 return;
276 }
277
278 // C++ [class.union]p1:
279 // A union shall not be used as a base class.
280 if (BaseType->isUnionType()) {
281 Diag(BaseLoc, diag::err_union_as_base_class, SpecifierRange);
282 return;
283 }
284
285 // C++ [class.union]p1:
286 // A union shall not have base classes.
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000287 if (Decl->isUnion()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000288 Diag(Decl->getLocation(), diag::err_base_clause_on_union,
289 SpecifierRange);
290 Decl->setInvalidDecl();
291 return;
292 }
293
294 // C++ [class.derived]p2:
295 // The class-name in a base-specifier shall not be an incompletely
296 // defined class.
297 if (BaseType->isIncompleteType()) {
298 Diag(BaseLoc, diag::err_incomplete_base_class, SpecifierRange);
299 return;
300 }
301
302 // FIXME: C++ [class.mi]p3:
303 // A class shall not be specified as a direct base class of a
304 // derived class more than once.
305
306 // FIXME: Attach base class to the record.
307}
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000308
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000309//===----------------------------------------------------------------------===//
310// C++ class member Handling
311//===----------------------------------------------------------------------===//
312
313/// ActOnStartCXXClassDef - This is called at the start of a class/struct/union
314/// definition, when on C++.
315void Sema::ActOnStartCXXClassDef(Scope *S, DeclTy *D, SourceLocation LBrace) {
316 Decl *Dcl = static_cast<Decl *>(D);
317 PushDeclContext(cast<CXXRecordDecl>(Dcl));
318 FieldCollector->StartClass();
319}
320
321/// ActOnCXXMemberDeclarator - This is invoked when a C++ class member
322/// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the
323/// bitfield width if there is one and 'InitExpr' specifies the initializer if
324/// any. 'LastInGroup' is non-null for cases where one declspec has multiple
325/// declarators on it.
326///
327/// NOTE: Because of CXXFieldDecl's inability to be chained like ScopedDecls, if
328/// an instance field is declared, a new CXXFieldDecl is created but the method
329/// does *not* return it; it returns LastInGroup instead. The other C++ members
330/// (which are all ScopedDecls) are returned after appending them to
331/// LastInGroup.
332Sema::DeclTy *
333Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
334 ExprTy *BW, ExprTy *InitExpr,
335 DeclTy *LastInGroup) {
336 const DeclSpec &DS = D.getDeclSpec();
337 IdentifierInfo *II = D.getIdentifier();
338 Expr *BitWidth = static_cast<Expr*>(BW);
339 Expr *Init = static_cast<Expr*>(InitExpr);
340 SourceLocation Loc = D.getIdentifierLoc();
341
342 // C++ 9.2p6: A member shall not be declared to have automatic storage
343 // duration (auto, register) or with the extern storage-class-specifier.
344 switch (DS.getStorageClassSpec()) {
345 case DeclSpec::SCS_unspecified:
346 case DeclSpec::SCS_typedef:
347 case DeclSpec::SCS_static:
348 // FALL THROUGH.
349 break;
350 default:
351 if (DS.getStorageClassSpecLoc().isValid())
352 Diag(DS.getStorageClassSpecLoc(),
353 diag::err_storageclass_invalid_for_member);
354 else
355 Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member);
356 D.getMutableDeclSpec().ClearStorageClassSpecs();
357 }
358
359 QualType T = GetTypeForDeclarator(D, S);
360
361 // T->isFunctionType() is used instead of D.isFunctionDeclarator() to cover
362 // this case:
363 //
364 // typedef int f();
365 // f a;
366 bool isInstField = (DS.getStorageClassSpec() == DeclSpec::SCS_unspecified &&
367 !T->isFunctionType());
368
369 Decl *Member;
370 bool InvalidDecl = false;
371
372 if (isInstField)
373 Member = static_cast<Decl*>(ActOnField(S, Loc, D, BitWidth));
374 else
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000375 Member = static_cast<Decl*>(ActOnDeclarator(S, D, LastInGroup));
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000376
377 if (!Member) return LastInGroup;
378
379 assert(II || isInstField && "No identifier for non-field ?");
380
381 // set/getAccess is not part of Decl's interface to avoid bloating it with C++
382 // specific methods. Use a wrapper class that can be used with all C++ class
383 // member decls.
384 CXXClassMemberWrapper(Member).setAccess(AS);
385
386 if (BitWidth) {
387 // C++ 9.6p2: Only when declaring an unnamed bit-field may the
388 // constant-expression be a value equal to zero.
389 // FIXME: Check this.
390
391 if (D.isFunctionDeclarator()) {
392 // FIXME: Emit diagnostic about only constructors taking base initializers
393 // or something similar, when constructor support is in place.
394 Diag(Loc, diag::err_not_bitfield_type,
395 II->getName(), BitWidth->getSourceRange());
396 InvalidDecl = true;
397
398 } else if (isInstField || isa<FunctionDecl>(Member)) {
399 // An instance field or a function typedef ("typedef int f(); f a;").
400 // C++ 9.6p3: A bit-field shall have integral or enumeration type.
401 if (!T->isIntegralType()) {
402 Diag(Loc, diag::err_not_integral_type_bitfield,
403 II->getName(), BitWidth->getSourceRange());
404 InvalidDecl = true;
405 }
406
407 } else if (isa<TypedefDecl>(Member)) {
408 // "cannot declare 'A' to be a bit-field type"
409 Diag(Loc, diag::err_not_bitfield_type, II->getName(),
410 BitWidth->getSourceRange());
411 InvalidDecl = true;
412
413 } else {
414 assert(isa<CXXClassVarDecl>(Member) &&
415 "Didn't we cover all member kinds?");
416 // C++ 9.6p3: A bit-field shall not be a static member.
417 // "static member 'A' cannot be a bit-field"
418 Diag(Loc, diag::err_static_not_bitfield, II->getName(),
419 BitWidth->getSourceRange());
420 InvalidDecl = true;
421 }
422 }
423
424 if (Init) {
425 // C++ 9.2p4: A member-declarator can contain a constant-initializer only
426 // if it declares a static member of const integral or const enumeration
427 // type.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000428 if (CXXClassVarDecl *CVD = dyn_cast<CXXClassVarDecl>(Member)) {
429 // ...static member of...
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000430 CVD->setInit(Init);
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000431 // ...const integral or const enumeration type.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000432 if (Context.getCanonicalType(CVD->getType()).isConstQualified() &&
433 CVD->getType()->isIntegralType()) {
434 // constant-initializer
435 if (CheckForConstantInitializer(Init, CVD->getType()))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000436 InvalidDecl = true;
437
438 } else {
439 // not const integral.
440 Diag(Loc, diag::err_member_initialization,
441 II->getName(), Init->getSourceRange());
442 InvalidDecl = true;
443 }
444
445 } else {
446 // not static member.
447 Diag(Loc, diag::err_member_initialization,
448 II->getName(), Init->getSourceRange());
449 InvalidDecl = true;
450 }
451 }
452
453 if (InvalidDecl)
454 Member->setInvalidDecl();
455
456 if (isInstField) {
457 FieldCollector->Add(cast<CXXFieldDecl>(Member));
458 return LastInGroup;
459 }
460 return Member;
461}
462
463void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc,
464 DeclTy *TagDecl,
465 SourceLocation LBrac,
466 SourceLocation RBrac) {
467 ActOnFields(S, RLoc, TagDecl,
468 (DeclTy**)FieldCollector->getCurFields(),
469 FieldCollector->getCurNumFields(), LBrac, RBrac);
470}
471
Argiris Kirtzidis448b4e42008-08-09 00:39:29 +0000472void Sema::ActOnFinishCXXClassDef(DeclTy *D) {
Argiris Kirtzidis7c210ea2008-08-09 00:58:37 +0000473 CXXRecordDecl *Rec = cast<CXXRecordDecl>(static_cast<Decl *>(D));
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000474 FieldCollector->FinishClass();
475 PopDeclContext();
Argiris Kirtzidis7c210ea2008-08-09 00:58:37 +0000476
477 // Everything, including inline method definitions, have been parsed.
478 // Let the consumer know of the new TagDecl definition.
479 Consumer.HandleTagDeclDefinition(Rec);
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000480}
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000481
482//===----------------------------------------------------------------------===//
483// Namespace Handling
484//===----------------------------------------------------------------------===//
485
486/// ActOnStartNamespaceDef - This is called at the start of a namespace
487/// definition.
488Sema::DeclTy *Sema::ActOnStartNamespaceDef(Scope *NamespcScope,
489 SourceLocation IdentLoc,
490 IdentifierInfo *II,
491 SourceLocation LBrace) {
492 NamespaceDecl *Namespc =
493 NamespaceDecl::Create(Context, CurContext, IdentLoc, II);
494 Namespc->setLBracLoc(LBrace);
495
496 Scope *DeclRegionScope = NamespcScope->getParent();
497
498 if (II) {
499 // C++ [namespace.def]p2:
500 // The identifier in an original-namespace-definition shall not have been
501 // previously defined in the declarative region in which the
502 // original-namespace-definition appears. The identifier in an
503 // original-namespace-definition is the name of the namespace. Subsequently
504 // in that declarative region, it is treated as an original-namespace-name.
505
506 Decl *PrevDecl =
507 LookupDecl(II, Decl::IDNS_Tag | Decl::IDNS_Ordinary, DeclRegionScope,
508 /*enableLazyBuiltinCreation=*/false);
509
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +0000510 if (PrevDecl &&
511 IdResolver.isDeclInScope(PrevDecl, CurContext, DeclRegionScope)) {
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000512 if (NamespaceDecl *OrigNS = dyn_cast<NamespaceDecl>(PrevDecl)) {
513 // This is an extended namespace definition.
514 // Attach this namespace decl to the chain of extended namespace
515 // definitions.
516 NamespaceDecl *NextNS = OrigNS;
517 while (NextNS->getNextNamespace())
518 NextNS = NextNS->getNextNamespace();
519
520 NextNS->setNextNamespace(Namespc);
521 Namespc->setOriginalNamespace(OrigNS);
522
523 // We won't add this decl to the current scope. We want the namespace
524 // name to return the original namespace decl during a name lookup.
525 } else {
526 // This is an invalid name redefinition.
527 Diag(Namespc->getLocation(), diag::err_redefinition_different_kind,
528 Namespc->getName());
529 Diag(PrevDecl->getLocation(), diag::err_previous_definition);
530 Namespc->setInvalidDecl();
531 // Continue on to push Namespc as current DeclContext and return it.
532 }
533 } else {
534 // This namespace name is declared for the first time.
535 PushOnScopeChains(Namespc, DeclRegionScope);
536 }
537 }
538 else {
539 // FIXME: Handle anonymous namespaces
540 }
541
542 // Although we could have an invalid decl (i.e. the namespace name is a
543 // redefinition), push it as current DeclContext and try to continue parsing.
544 PushDeclContext(Namespc->getOriginalNamespace());
545 return Namespc;
546}
547
548/// ActOnFinishNamespaceDef - This callback is called after a namespace is
549/// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef.
550void Sema::ActOnFinishNamespaceDef(DeclTy *D, SourceLocation RBrace) {
551 Decl *Dcl = static_cast<Decl *>(D);
552 NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl);
553 assert(Namespc && "Invalid parameter, expected NamespaceDecl");
554 Namespc->setRBracLoc(RBrace);
555 PopDeclContext();
556}