Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 1 | //===------ 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" |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // CheckDefaultArgumentVisitor |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | /// CheckDefaultArgumentVisitor - C++ [dcl.fct.default] Traverses |
| 31 | /// the default argument of a parameter to determine whether it |
| 32 | /// contains any ill-formed subexpressions. For example, this will |
| 33 | /// diagnose the use of local variables or parameters within the |
| 34 | /// default argument expression. |
| 35 | class VISIBILITY_HIDDEN CheckDefaultArgumentVisitor |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 36 | : public StmtVisitor<CheckDefaultArgumentVisitor, bool> { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 37 | Expr *DefaultArg; |
| 38 | Sema *S; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 40 | public: |
| 41 | CheckDefaultArgumentVisitor(Expr *defarg, Sema *s) |
| 42 | : DefaultArg(defarg), S(s) {} |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 44 | bool VisitExpr(Expr *Node); |
| 45 | bool VisitDeclRefExpr(DeclRefExpr *DRE); |
| 46 | }; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 48 | /// VisitExpr - Visit all of the children of this expression. |
| 49 | bool CheckDefaultArgumentVisitor::VisitExpr(Expr *Node) { |
| 50 | bool IsInvalid = false; |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 51 | for (Stmt::child_iterator I = Node->child_begin(), |
| 52 | E = Node->child_end(); I != E; ++I) |
| 53 | IsInvalid |= Visit(*I); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 54 | return IsInvalid; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 57 | /// VisitDeclRefExpr - Visit a reference to a declaration, to |
| 58 | /// determine whether this declaration can be used in the default |
| 59 | /// argument expression. |
| 60 | bool CheckDefaultArgumentVisitor::VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 61 | ValueDecl *Decl = DRE->getDecl(); |
| 62 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Decl)) { |
| 63 | // C++ [dcl.fct.default]p9 |
| 64 | // Default arguments are evaluated each time the function is |
| 65 | // called. The order of evaluation of function arguments is |
| 66 | // unspecified. Consequently, parameters of a function shall not |
| 67 | // be used in default argument expressions, even if they are not |
| 68 | // evaluated. Parameters of a function declared before a default |
| 69 | // argument expression are in scope and can hide namespace and |
| 70 | // class member names. |
| 71 | return S->Diag(DRE->getSourceRange().getBegin(), |
| 72 | diag::err_param_default_argument_references_param, |
| 73 | Param->getName(), DefaultArg->getSourceRange()); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 74 | } else if (VarDecl *VDecl = dyn_cast<VarDecl>(Decl)) { |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 75 | // C++ [dcl.fct.default]p7 |
| 76 | // Local variables shall not be used in default argument |
| 77 | // expressions. |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 78 | if (VDecl->isBlockVarDecl()) |
| 79 | return S->Diag(DRE->getSourceRange().getBegin(), |
| 80 | diag::err_param_default_argument_references_local, |
| 81 | VDecl->getName(), DefaultArg->getSourceRange()); |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 82 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 84 | // FIXME: when Clang has support for member functions, "this" |
| 85 | // will also need to be diagnosed. |
| 86 | |
| 87 | return false; |
| 88 | } |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /// ActOnParamDefaultArgument - Check whether the default argument |
| 92 | /// provided for a function parameter is well-formed. If so, attach it |
| 93 | /// to the parameter declaration. |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 94 | void |
| 95 | Sema::ActOnParamDefaultArgument(DeclTy *param, SourceLocation EqualLoc, |
| 96 | ExprTy *defarg) { |
| 97 | ParmVarDecl *Param = (ParmVarDecl *)param; |
| 98 | llvm::OwningPtr<Expr> DefaultArg((Expr *)defarg); |
| 99 | QualType ParamType = Param->getType(); |
| 100 | |
| 101 | // Default arguments are only permitted in C++ |
| 102 | if (!getLangOptions().CPlusPlus) { |
| 103 | Diag(EqualLoc, diag::err_param_default_argument, |
| 104 | DefaultArg->getSourceRange()); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // C++ [dcl.fct.default]p5 |
| 109 | // A default argument expression is implicitly converted (clause |
| 110 | // 4) to the parameter type. The default argument expression has |
| 111 | // the same semantic constraints as the initializer expression in |
| 112 | // a declaration of a variable of the parameter type, using the |
| 113 | // copy-initialization semantics (8.5). |
| 114 | // |
| 115 | // FIXME: CheckSingleAssignmentConstraints has the wrong semantics |
| 116 | // for C++ (since we want copy-initialization, not copy-assignment), |
| 117 | // but we don't have the right semantics implemented yet. Because of |
| 118 | // this, our error message is also very poor. |
| 119 | QualType DefaultArgType = DefaultArg->getType(); |
| 120 | Expr *DefaultArgPtr = DefaultArg.get(); |
| 121 | AssignConvertType ConvTy = CheckSingleAssignmentConstraints(ParamType, |
| 122 | DefaultArgPtr); |
| 123 | if (DefaultArgPtr != DefaultArg.get()) { |
| 124 | DefaultArg.take(); |
| 125 | DefaultArg.reset(DefaultArgPtr); |
| 126 | } |
| 127 | if (DiagnoseAssignmentResult(ConvTy, DefaultArg->getLocStart(), |
| 128 | ParamType, DefaultArgType, DefaultArg.get(), |
| 129 | "in default argument")) { |
| 130 | return; |
| 131 | } |
| 132 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 133 | // Check that the default argument is well-formed |
Chris Lattner | 9e97955 | 2008-04-12 23:52:44 +0000 | [diff] [blame] | 134 | CheckDefaultArgumentVisitor DefaultArgChecker(DefaultArg.get(), this); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 135 | if (DefaultArgChecker.Visit(DefaultArg.get())) |
| 136 | return; |
| 137 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 138 | // Okay: add the default argument to the parameter |
| 139 | Param->setDefaultArg(DefaultArg.take()); |
| 140 | } |
| 141 | |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 142 | /// CheckExtraCXXDefaultArguments - Check for any extra default |
| 143 | /// arguments in the declarator, which is not a function declaration |
| 144 | /// or definition and therefore is not permitted to have default |
| 145 | /// arguments. This routine should be invoked for every declarator |
| 146 | /// that is not a function declaration or definition. |
| 147 | void Sema::CheckExtraCXXDefaultArguments(Declarator &D) { |
| 148 | // C++ [dcl.fct.default]p3 |
| 149 | // A default argument expression shall be specified only in the |
| 150 | // parameter-declaration-clause of a function declaration or in a |
| 151 | // template-parameter (14.1). It shall not be specified for a |
| 152 | // parameter pack. If it is specified in a |
| 153 | // parameter-declaration-clause, it shall not occur within a |
| 154 | // declarator or abstract-declarator of a parameter-declaration. |
| 155 | for (unsigned i = 0; i < D.getNumTypeObjects(); ++i) { |
| 156 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 157 | if (chunk.Kind == DeclaratorChunk::Function) { |
| 158 | for (unsigned argIdx = 0; argIdx < chunk.Fun.NumArgs; ++argIdx) { |
| 159 | ParmVarDecl *Param = (ParmVarDecl *)chunk.Fun.ArgInfo[argIdx].Param; |
| 160 | if (Param->getDefaultArg()) { |
| 161 | Diag(Param->getLocation(), diag::err_param_default_argument_nonfunc, |
| 162 | Param->getDefaultArg()->getSourceRange()); |
| 163 | Param->setDefaultArg(0); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Chris Lattner | 3d1cee3 | 2008-04-08 05:04:30 +0000 | [diff] [blame] | 170 | // MergeCXXFunctionDecl - Merge two declarations of the same C++ |
| 171 | // function, once we already know that they have the same |
| 172 | // type. Subroutine of MergeFunctionDecl. |
| 173 | FunctionDecl * |
| 174 | Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old) { |
| 175 | // C++ [dcl.fct.default]p4: |
| 176 | // |
| 177 | // For non-template functions, default arguments can be added in |
| 178 | // later declarations of a function in the same |
| 179 | // scope. Declarations in different scopes have completely |
| 180 | // distinct sets of default arguments. That is, declarations in |
| 181 | // inner scopes do not acquire default arguments from |
| 182 | // declarations in outer scopes, and vice versa. In a given |
| 183 | // function declaration, all parameters subsequent to a |
| 184 | // parameter with a default argument shall have default |
| 185 | // arguments supplied in this or previous declarations. A |
| 186 | // default argument shall not be redefined by a later |
| 187 | // declaration (not even to the same value). |
| 188 | for (unsigned p = 0, NumParams = Old->getNumParams(); p < NumParams; ++p) { |
| 189 | ParmVarDecl *OldParam = Old->getParamDecl(p); |
| 190 | ParmVarDecl *NewParam = New->getParamDecl(p); |
| 191 | |
| 192 | if(OldParam->getDefaultArg() && NewParam->getDefaultArg()) { |
| 193 | Diag(NewParam->getLocation(), |
| 194 | diag::err_param_default_argument_redefinition, |
| 195 | NewParam->getDefaultArg()->getSourceRange()); |
| 196 | Diag(OldParam->getLocation(), diag::err_previous_definition); |
| 197 | } else if (OldParam->getDefaultArg()) { |
| 198 | // Merge the old default argument into the new parameter |
| 199 | NewParam->setDefaultArg(OldParam->getDefaultArg()); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return New; |
| 204 | } |
| 205 | |
| 206 | /// CheckCXXDefaultArguments - Verify that the default arguments for a |
| 207 | /// function declaration are well-formed according to C++ |
| 208 | /// [dcl.fct.default]. |
| 209 | void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) { |
| 210 | unsigned NumParams = FD->getNumParams(); |
| 211 | unsigned p; |
| 212 | |
| 213 | // Find first parameter with a default argument |
| 214 | for (p = 0; p < NumParams; ++p) { |
| 215 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 216 | if (Param->getDefaultArg()) |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | // C++ [dcl.fct.default]p4: |
| 221 | // In a given function declaration, all parameters |
| 222 | // subsequent to a parameter with a default argument shall |
| 223 | // have default arguments supplied in this or previous |
| 224 | // declarations. A default argument shall not be redefined |
| 225 | // by a later declaration (not even to the same value). |
| 226 | unsigned LastMissingDefaultArg = 0; |
| 227 | for(; p < NumParams; ++p) { |
| 228 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 229 | if (!Param->getDefaultArg()) { |
| 230 | if (Param->getIdentifier()) |
| 231 | Diag(Param->getLocation(), |
| 232 | diag::err_param_default_argument_missing_name, |
| 233 | Param->getIdentifier()->getName()); |
| 234 | else |
| 235 | Diag(Param->getLocation(), |
| 236 | diag::err_param_default_argument_missing); |
| 237 | |
| 238 | LastMissingDefaultArg = p; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (LastMissingDefaultArg > 0) { |
| 243 | // Some default arguments were missing. Clear out all of the |
| 244 | // default arguments up to (and including) the last missing |
| 245 | // default argument, so that we leave the function parameters |
| 246 | // in a semantically valid state. |
| 247 | for (p = 0; p <= LastMissingDefaultArg; ++p) { |
| 248 | ParmVarDecl *Param = FD->getParamDecl(p); |
| 249 | if (Param->getDefaultArg()) { |
| 250 | delete Param->getDefaultArg(); |
| 251 | Param->setDefaultArg(0); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 256 | |
| 257 | /// ActOnBaseSpecifier - Parsed a base specifier. A base specifier is |
| 258 | /// one entry in the base class list of a class specifier, for |
| 259 | /// example: |
| 260 | /// class foo : public bar, virtual private baz { |
| 261 | /// 'public bar' and 'virtual private baz' are each base-specifiers. |
| 262 | void Sema::ActOnBaseSpecifier(DeclTy *classdecl, SourceRange SpecifierRange, |
| 263 | bool Virtual, AccessSpecifier Access, |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 264 | TypeTy *basetype, SourceLocation BaseLoc) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 265 | RecordDecl *Decl = (RecordDecl*)classdecl; |
| 266 | QualType BaseType = Context.getTypeDeclType((TypeDecl*)basetype); |
| 267 | |
| 268 | // Base specifiers must be record types. |
| 269 | if (!BaseType->isRecordType()) { |
| 270 | Diag(BaseLoc, diag::err_base_must_be_class, SpecifierRange); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // C++ [class.union]p1: |
| 275 | // A union shall not be used as a base class. |
| 276 | if (BaseType->isUnionType()) { |
| 277 | Diag(BaseLoc, diag::err_union_as_base_class, SpecifierRange); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | // C++ [class.union]p1: |
| 282 | // A union shall not have base classes. |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 283 | if (Decl->isUnion()) { |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 284 | Diag(Decl->getLocation(), diag::err_base_clause_on_union, |
| 285 | SpecifierRange); |
| 286 | Decl->setInvalidDecl(); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | // C++ [class.derived]p2: |
| 291 | // The class-name in a base-specifier shall not be an incompletely |
| 292 | // defined class. |
| 293 | if (BaseType->isIncompleteType()) { |
| 294 | Diag(BaseLoc, diag::err_incomplete_base_class, SpecifierRange); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | // FIXME: C++ [class.mi]p3: |
| 299 | // A class shall not be specified as a direct base class of a |
| 300 | // derived class more than once. |
| 301 | |
| 302 | // FIXME: Attach base class to the record. |
| 303 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 304 | |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 305 | //===----------------------------------------------------------------------===// |
| 306 | // C++ class member Handling |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | |
| 309 | /// ActOnStartCXXClassDef - This is called at the start of a class/struct/union |
| 310 | /// definition, when on C++. |
| 311 | void Sema::ActOnStartCXXClassDef(Scope *S, DeclTy *D, SourceLocation LBrace) { |
| 312 | Decl *Dcl = static_cast<Decl *>(D); |
| 313 | PushDeclContext(cast<CXXRecordDecl>(Dcl)); |
| 314 | FieldCollector->StartClass(); |
| 315 | } |
| 316 | |
| 317 | /// ActOnCXXMemberDeclarator - This is invoked when a C++ class member |
| 318 | /// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the |
| 319 | /// bitfield width if there is one and 'InitExpr' specifies the initializer if |
| 320 | /// any. 'LastInGroup' is non-null for cases where one declspec has multiple |
| 321 | /// declarators on it. |
| 322 | /// |
| 323 | /// NOTE: Because of CXXFieldDecl's inability to be chained like ScopedDecls, if |
| 324 | /// an instance field is declared, a new CXXFieldDecl is created but the method |
| 325 | /// does *not* return it; it returns LastInGroup instead. The other C++ members |
| 326 | /// (which are all ScopedDecls) are returned after appending them to |
| 327 | /// LastInGroup. |
| 328 | Sema::DeclTy * |
| 329 | Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D, |
| 330 | ExprTy *BW, ExprTy *InitExpr, |
| 331 | DeclTy *LastInGroup) { |
| 332 | const DeclSpec &DS = D.getDeclSpec(); |
| 333 | IdentifierInfo *II = D.getIdentifier(); |
| 334 | Expr *BitWidth = static_cast<Expr*>(BW); |
| 335 | Expr *Init = static_cast<Expr*>(InitExpr); |
| 336 | SourceLocation Loc = D.getIdentifierLoc(); |
| 337 | |
| 338 | // C++ 9.2p6: A member shall not be declared to have automatic storage |
| 339 | // duration (auto, register) or with the extern storage-class-specifier. |
| 340 | switch (DS.getStorageClassSpec()) { |
| 341 | case DeclSpec::SCS_unspecified: |
| 342 | case DeclSpec::SCS_typedef: |
| 343 | case DeclSpec::SCS_static: |
| 344 | // FALL THROUGH. |
| 345 | break; |
| 346 | default: |
| 347 | if (DS.getStorageClassSpecLoc().isValid()) |
| 348 | Diag(DS.getStorageClassSpecLoc(), |
| 349 | diag::err_storageclass_invalid_for_member); |
| 350 | else |
| 351 | Diag(DS.getThreadSpecLoc(), diag::err_storageclass_invalid_for_member); |
| 352 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 353 | } |
| 354 | |
| 355 | QualType T = GetTypeForDeclarator(D, S); |
| 356 | |
| 357 | // T->isFunctionType() is used instead of D.isFunctionDeclarator() to cover |
| 358 | // this case: |
| 359 | // |
| 360 | // typedef int f(); |
| 361 | // f a; |
| 362 | bool isInstField = (DS.getStorageClassSpec() == DeclSpec::SCS_unspecified && |
| 363 | !T->isFunctionType()); |
| 364 | |
| 365 | Decl *Member; |
| 366 | bool InvalidDecl = false; |
| 367 | |
| 368 | if (isInstField) |
| 369 | Member = static_cast<Decl*>(ActOnField(S, Loc, D, BitWidth)); |
| 370 | else |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 371 | Member = static_cast<Decl*>(ActOnDeclarator(S, D, LastInGroup)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 372 | |
| 373 | if (!Member) return LastInGroup; |
| 374 | |
| 375 | assert(II || isInstField && "No identifier for non-field ?"); |
| 376 | |
| 377 | // set/getAccess is not part of Decl's interface to avoid bloating it with C++ |
| 378 | // specific methods. Use a wrapper class that can be used with all C++ class |
| 379 | // member decls. |
| 380 | CXXClassMemberWrapper(Member).setAccess(AS); |
| 381 | |
| 382 | if (BitWidth) { |
| 383 | // C++ 9.6p2: Only when declaring an unnamed bit-field may the |
| 384 | // constant-expression be a value equal to zero. |
| 385 | // FIXME: Check this. |
| 386 | |
| 387 | if (D.isFunctionDeclarator()) { |
| 388 | // FIXME: Emit diagnostic about only constructors taking base initializers |
| 389 | // or something similar, when constructor support is in place. |
| 390 | Diag(Loc, diag::err_not_bitfield_type, |
| 391 | II->getName(), BitWidth->getSourceRange()); |
| 392 | InvalidDecl = true; |
| 393 | |
| 394 | } else if (isInstField || isa<FunctionDecl>(Member)) { |
| 395 | // An instance field or a function typedef ("typedef int f(); f a;"). |
| 396 | // C++ 9.6p3: A bit-field shall have integral or enumeration type. |
| 397 | if (!T->isIntegralType()) { |
| 398 | Diag(Loc, diag::err_not_integral_type_bitfield, |
| 399 | II->getName(), BitWidth->getSourceRange()); |
| 400 | InvalidDecl = true; |
| 401 | } |
| 402 | |
| 403 | } else if (isa<TypedefDecl>(Member)) { |
| 404 | // "cannot declare 'A' to be a bit-field type" |
| 405 | Diag(Loc, diag::err_not_bitfield_type, II->getName(), |
| 406 | BitWidth->getSourceRange()); |
| 407 | InvalidDecl = true; |
| 408 | |
| 409 | } else { |
| 410 | assert(isa<CXXClassVarDecl>(Member) && |
| 411 | "Didn't we cover all member kinds?"); |
| 412 | // C++ 9.6p3: A bit-field shall not be a static member. |
| 413 | // "static member 'A' cannot be a bit-field" |
| 414 | Diag(Loc, diag::err_static_not_bitfield, II->getName(), |
| 415 | BitWidth->getSourceRange()); |
| 416 | InvalidDecl = true; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (Init) { |
| 421 | // C++ 9.2p4: A member-declarator can contain a constant-initializer only |
| 422 | // if it declares a static member of const integral or const enumeration |
| 423 | // type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 424 | if (CXXClassVarDecl *CVD = dyn_cast<CXXClassVarDecl>(Member)) { |
| 425 | // ...static member of... |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 426 | CVD->setInit(Init); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 427 | // ...const integral or const enumeration type. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 428 | if (Context.getCanonicalType(CVD->getType()).isConstQualified() && |
| 429 | CVD->getType()->isIntegralType()) { |
| 430 | // constant-initializer |
| 431 | if (CheckForConstantInitializer(Init, CVD->getType())) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 432 | InvalidDecl = true; |
| 433 | |
| 434 | } else { |
| 435 | // not const integral. |
| 436 | Diag(Loc, diag::err_member_initialization, |
| 437 | II->getName(), Init->getSourceRange()); |
| 438 | InvalidDecl = true; |
| 439 | } |
| 440 | |
| 441 | } else { |
| 442 | // not static member. |
| 443 | Diag(Loc, diag::err_member_initialization, |
| 444 | II->getName(), Init->getSourceRange()); |
| 445 | InvalidDecl = true; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (InvalidDecl) |
| 450 | Member->setInvalidDecl(); |
| 451 | |
| 452 | if (isInstField) { |
| 453 | FieldCollector->Add(cast<CXXFieldDecl>(Member)); |
| 454 | return LastInGroup; |
| 455 | } |
| 456 | return Member; |
| 457 | } |
| 458 | |
| 459 | void Sema::ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc, |
| 460 | DeclTy *TagDecl, |
| 461 | SourceLocation LBrac, |
| 462 | SourceLocation RBrac) { |
| 463 | ActOnFields(S, RLoc, TagDecl, |
| 464 | (DeclTy**)FieldCollector->getCurFields(), |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 465 | FieldCollector->getCurNumFields(), LBrac, RBrac, 0); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Argyrios Kyrtzidis | 5b7f0c8 | 2008-08-09 00:39:29 +0000 | [diff] [blame] | 468 | void Sema::ActOnFinishCXXClassDef(DeclTy *D) { |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 469 | CXXRecordDecl *Rec = cast<CXXRecordDecl>(static_cast<Decl *>(D)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 470 | FieldCollector->FinishClass(); |
| 471 | PopDeclContext(); |
Argyrios Kyrtzidis | a4755c6 | 2008-08-09 00:58:37 +0000 | [diff] [blame] | 472 | |
| 473 | // Everything, including inline method definitions, have been parsed. |
| 474 | // Let the consumer know of the new TagDecl definition. |
| 475 | Consumer.HandleTagDeclDefinition(Rec); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 476 | } |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 477 | |
| 478 | //===----------------------------------------------------------------------===// |
| 479 | // Namespace Handling |
| 480 | //===----------------------------------------------------------------------===// |
| 481 | |
| 482 | /// ActOnStartNamespaceDef - This is called at the start of a namespace |
| 483 | /// definition. |
| 484 | Sema::DeclTy *Sema::ActOnStartNamespaceDef(Scope *NamespcScope, |
| 485 | SourceLocation IdentLoc, |
| 486 | IdentifierInfo *II, |
| 487 | SourceLocation LBrace) { |
| 488 | NamespaceDecl *Namespc = |
| 489 | NamespaceDecl::Create(Context, CurContext, IdentLoc, II); |
| 490 | Namespc->setLBracLoc(LBrace); |
| 491 | |
| 492 | Scope *DeclRegionScope = NamespcScope->getParent(); |
| 493 | |
| 494 | if (II) { |
| 495 | // C++ [namespace.def]p2: |
| 496 | // The identifier in an original-namespace-definition shall not have been |
| 497 | // previously defined in the declarative region in which the |
| 498 | // original-namespace-definition appears. The identifier in an |
| 499 | // original-namespace-definition is the name of the namespace. Subsequently |
| 500 | // in that declarative region, it is treated as an original-namespace-name. |
| 501 | |
| 502 | Decl *PrevDecl = |
| 503 | LookupDecl(II, Decl::IDNS_Tag | Decl::IDNS_Ordinary, DeclRegionScope, |
| 504 | /*enableLazyBuiltinCreation=*/false); |
| 505 | |
Argyrios Kyrtzidis | 2fac626 | 2008-09-10 02:11:07 +0000 | [diff] [blame] | 506 | if (PrevDecl && isDeclInScope(PrevDecl, CurContext, DeclRegionScope)) { |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 507 | if (NamespaceDecl *OrigNS = dyn_cast<NamespaceDecl>(PrevDecl)) { |
| 508 | // This is an extended namespace definition. |
| 509 | // Attach this namespace decl to the chain of extended namespace |
| 510 | // definitions. |
| 511 | NamespaceDecl *NextNS = OrigNS; |
| 512 | while (NextNS->getNextNamespace()) |
| 513 | NextNS = NextNS->getNextNamespace(); |
| 514 | |
| 515 | NextNS->setNextNamespace(Namespc); |
| 516 | Namespc->setOriginalNamespace(OrigNS); |
| 517 | |
| 518 | // We won't add this decl to the current scope. We want the namespace |
| 519 | // name to return the original namespace decl during a name lookup. |
| 520 | } else { |
| 521 | // This is an invalid name redefinition. |
| 522 | Diag(Namespc->getLocation(), diag::err_redefinition_different_kind, |
| 523 | Namespc->getName()); |
| 524 | Diag(PrevDecl->getLocation(), diag::err_previous_definition); |
| 525 | Namespc->setInvalidDecl(); |
| 526 | // Continue on to push Namespc as current DeclContext and return it. |
| 527 | } |
| 528 | } else { |
| 529 | // This namespace name is declared for the first time. |
| 530 | PushOnScopeChains(Namespc, DeclRegionScope); |
| 531 | } |
| 532 | } |
| 533 | else { |
| 534 | // FIXME: Handle anonymous namespaces |
| 535 | } |
| 536 | |
| 537 | // Although we could have an invalid decl (i.e. the namespace name is a |
| 538 | // redefinition), push it as current DeclContext and try to continue parsing. |
| 539 | PushDeclContext(Namespc->getOriginalNamespace()); |
| 540 | return Namespc; |
| 541 | } |
| 542 | |
| 543 | /// ActOnFinishNamespaceDef - This callback is called after a namespace is |
| 544 | /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef. |
| 545 | void Sema::ActOnFinishNamespaceDef(DeclTy *D, SourceLocation RBrace) { |
| 546 | Decl *Dcl = static_cast<Decl *>(D); |
| 547 | NamespaceDecl *Namespc = dyn_cast_or_null<NamespaceDecl>(Dcl); |
| 548 | assert(Namespc && "Invalid parameter, expected NamespaceDecl"); |
| 549 | Namespc->setRBracLoc(RBrace); |
| 550 | PopDeclContext(); |
| 551 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 552 | |
| 553 | |
| 554 | /// AddCXXDirectInitializerToDecl - This action is called immediately after |
| 555 | /// ActOnDeclarator, when a C++ direct initializer is present. |
| 556 | /// e.g: "int x(1);" |
| 557 | void Sema::AddCXXDirectInitializerToDecl(DeclTy *Dcl, SourceLocation LParenLoc, |
| 558 | ExprTy **ExprTys, unsigned NumExprs, |
| 559 | SourceLocation *CommaLocs, |
| 560 | SourceLocation RParenLoc) { |
| 561 | Decl *RealDecl = static_cast<Decl *>(Dcl); |
| 562 | assert(NumExprs != 0 && ExprTys && "missing expressions"); |
| 563 | |
| 564 | // If there is no declaration, there was an error parsing it. Just ignore |
| 565 | // the initializer. |
| 566 | if (RealDecl == 0) { |
Ted Kremenek | 15f6139 | 2008-10-06 20:35:04 +0000 | [diff] [blame^] | 567 | for (unsigned i = 0; i != NumExprs; ++i) |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 568 | delete static_cast<Expr *>(ExprTys[i]); |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | VarDecl *VDecl = dyn_cast<VarDecl>(RealDecl); |
| 573 | if (!VDecl) { |
| 574 | Diag(RealDecl->getLocation(), diag::err_illegal_initializer); |
| 575 | RealDecl->setInvalidDecl(); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | // We will treat direct-initialization as a copy-initialization with a |
| 580 | // type-construction expression of the variable's type. In plain english: |
| 581 | // We will treat: |
| 582 | // int x(1); -as-> int x = int(1); |
| 583 | // and for class types: |
| 584 | // ClassType x(a,b,c); -as-> ClassType x = ClassType(a,b,c); |
| 585 | // |
| 586 | // Clients that want to distinguish between the two forms, can check for |
| 587 | // direct initializer using VarDecl::hasCXXDirectInitializer(). |
| 588 | // A major benefit is that clients that don't particularly care about which |
| 589 | // exactly form was it (like the CodeGen) can handle both cases without |
| 590 | // special case code. |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 591 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 592 | // C++ 8.5p11: |
| 593 | // The form of initialization (using parentheses or '=') is generally |
| 594 | // insignificant, but does matter when the entity being initialized has a |
Argyrios Kyrtzidis | 06ad1f5 | 2008-10-06 18:37:09 +0000 | [diff] [blame] | 595 | // class type. |
| 596 | |
| 597 | // FIXME: When constructors for class types are supported, determine how |
| 598 | // exactly semantic checking will be done for direct initializers. |
| 599 | if (VDecl->getType()->isRecordType()) { |
| 600 | unsigned DiagID = PP.getDiagnostics().getCustomDiagID(Diagnostic::Error, |
| 601 | "initialization for class types is not handled yet"); |
| 602 | Diag(VDecl->getLocation(), DiagID); |
| 603 | RealDecl->setInvalidDecl(); |
| 604 | return; |
| 605 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 606 | |
| 607 | // Get an expression for constructing the type of the variable, using the |
| 608 | // expression list of the initializer. |
| 609 | ExprResult Res = ActOnCXXTypeConstructExpr(VDecl->getLocation(), |
| 610 | VDecl->getType().getAsOpaquePtr(), |
| 611 | LParenLoc, ExprTys, NumExprs, |
| 612 | CommaLocs, RParenLoc); |
| 613 | if (Res.isInvalid) { |
| 614 | RealDecl->setInvalidDecl(); |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | // Performs additional semantic checks. |
| 619 | AddInitializerToDecl(Dcl, Res.Val); |
| 620 | // Let clients know that initialization was done with a direct initializer. |
| 621 | VDecl->setCXXDirectInitializer(true); |
| 622 | } |