Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 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 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Designator.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 26 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 27 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Sema Initialization Checking |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 33 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 34 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 35 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 36 | return 0; |
| 37 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 38 | // See if this is a string literal or @encode. |
| 39 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 41 | // Handle @encode, which is a narrow string. |
| 42 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 43 | return Init; |
| 44 | |
| 45 | // Otherwise we can only handle string literals. |
| 46 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 012b339 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 47 | if (SL == 0) return 0; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 48 | |
| 49 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 50 | |
| 51 | switch (SL->getKind()) { |
| 52 | case StringLiteral::Ascii: |
| 53 | case StringLiteral::UTF8: |
| 54 | // char array can be initialized with a narrow string. |
| 55 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 57 | case StringLiteral::UTF16: |
| 58 | return ElemTy->isChar16Type() ? Init : 0; |
| 59 | case StringLiteral::UTF32: |
| 60 | return ElemTy->isChar32Type() ? Init : 0; |
| 61 | case StringLiteral::Wide: |
| 62 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 63 | // correction from DR343): "An array with element type compatible with a |
| 64 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 65 | // string literal, optionally enclosed in braces." |
| 66 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 67 | ElemTy.getUnqualifiedType())) |
| 68 | return Init; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 73 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 74 | } |
| 75 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 76 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 77 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 78 | if (!arrayType) return 0; |
| 79 | |
| 80 | return IsStringInit(init, arrayType, Context); |
| 81 | } |
| 82 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 83 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 84 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 85 | // Get the length of the string as parsed. |
| 86 | uint64_t StrLength = |
| 87 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 88 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 90 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | // being initialized to a string literal. |
| 93 | llvm::APSInt ConstVal(32); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 94 | ConstVal = StrLength; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 95 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 96 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 97 | ConstVal, |
| 98 | ArrayType::Normal, 0); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 99 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 102 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 104 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 105 | // the size may be smaller or larger than the string we are initializing. |
| 106 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 107 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 108 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 109 | // For Pascal strings it's OK to strip off the terminating null character, |
| 110 | // so the example below is valid: |
| 111 | // |
| 112 | // unsigned char a[2] = "\pa"; |
| 113 | if (SL->isPascal()) |
| 114 | StrLength--; |
| 115 | } |
| 116 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 117 | // [dcl.init.string]p2 |
| 118 | if (StrLength > CAT->getSize().getZExtValue()) |
| 119 | S.Diag(Str->getSourceRange().getBegin(), |
| 120 | diag::err_initializer_string_for_char_array_too_long) |
| 121 | << Str->getSourceRange(); |
| 122 | } else { |
| 123 | // C99 6.7.8p14. |
| 124 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 125 | S.Diag(Str->getSourceRange().getBegin(), |
| 126 | diag::warn_initializer_string_for_char_array_too_long) |
| 127 | << Str->getSourceRange(); |
| 128 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 130 | // Set the type to the actual size that we are initializing. If we have |
| 131 | // something like: |
| 132 | // char x[1] = "foo"; |
| 133 | // then this will set the string literal's type to char[1]. |
| 134 | Str->setType(DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | //===----------------------------------------------------------------------===// |
| 138 | // Semantic checking for initializer lists. |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 141 | /// @brief Semantic checking for initializer lists. |
| 142 | /// |
| 143 | /// The InitListChecker class contains a set of routines that each |
| 144 | /// handle the initialization of a certain kind of entity, e.g., |
| 145 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 146 | /// InitListChecker itself performs a recursive walk of the subobject |
| 147 | /// structure of the type to be initialized, while stepping through |
| 148 | /// the initializer list one element at a time. The IList and Index |
| 149 | /// parameters to each of the Check* routines contain the active |
| 150 | /// (syntactic) initializer list and the index into that initializer |
| 151 | /// list that represents the current initializer. Each routine is |
| 152 | /// responsible for moving that Index forward as it consumes elements. |
| 153 | /// |
| 154 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 155 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 156 | /// initializer list and the index into that initializer list where we |
| 157 | /// are copying initializers as we map them over to the semantic |
| 158 | /// list. Once we have completed our recursive walk of the subobject |
| 159 | /// structure, we will have constructed a full semantic initializer |
| 160 | /// list. |
| 161 | /// |
| 162 | /// C99 designators cause changes in the initializer list traversal, |
| 163 | /// because they make the initialization "jump" into a specific |
| 164 | /// subobject and then continue the initialization from that |
| 165 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 166 | /// designated subobject and manages backing out the recursion to |
| 167 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 168 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 169 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 170 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | bool hadError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 172 | bool VerifyOnly; // no diagnostics, no structure building |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 174 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 176 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 177 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 178 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 179 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 180 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 181 | InitListExpr *IList, QualType &T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 182 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 183 | unsigned &StructuredIndex, |
| 184 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 185 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 186 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 188 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 189 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 190 | unsigned &StructuredIndex, |
| 191 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 192 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 193 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 194 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 195 | InitListExpr *StructuredList, |
| 196 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 197 | void CheckComplexType(const InitializedEntity &Entity, |
| 198 | InitListExpr *IList, QualType DeclType, |
| 199 | unsigned &Index, |
| 200 | InitListExpr *StructuredList, |
| 201 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 202 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 203 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 204 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 205 | InitListExpr *StructuredList, |
| 206 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 207 | void CheckReferenceType(const InitializedEntity &Entity, |
| 208 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 209 | unsigned &Index, |
| 210 | InitListExpr *StructuredList, |
| 211 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 212 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 213 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 214 | InitListExpr *StructuredList, |
| 215 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 216 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 217 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 219 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 220 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 221 | unsigned &StructuredIndex, |
| 222 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 223 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 224 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 226 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 227 | InitListExpr *StructuredList, |
| 228 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 229 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 230 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 231 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 233 | RecordDecl::field_iterator *NextField, |
| 234 | llvm::APSInt *NextElementIndex, |
| 235 | unsigned &Index, |
| 236 | InitListExpr *StructuredList, |
| 237 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 238 | bool FinishSubobjectInit, |
| 239 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 241 | QualType CurrentObjectType, |
| 242 | InitListExpr *StructuredList, |
| 243 | unsigned StructuredIndex, |
| 244 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 245 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 246 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 247 | Expr *expr); |
| 248 | int numArrayElements(QualType DeclType); |
| 249 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 251 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 252 | const InitializedEntity &ParentEntity, |
| 253 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 254 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 255 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 256 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 257 | Expr *InitExpr, FieldDecl *Field, |
| 258 | bool TopLevelObject); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 259 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 260 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 261 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 262 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 263 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 264 | bool HadError() { return hadError; } |
| 265 | |
| 266 | // @brief Retrieves the fully-structured initializer list used for |
| 267 | // semantic analysis and code generation. |
| 268 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 269 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 270 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 271 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 272 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 273 | assert(VerifyOnly && |
| 274 | "CheckValueInitializable is only inteded for verification mode."); |
| 275 | |
| 276 | SourceLocation Loc; |
| 277 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 278 | true); |
| 279 | InitializationSequence InitSeq(SemaRef, Entity, Kind, 0, 0); |
| 280 | if (InitSeq.Failed()) |
| 281 | hadError = true; |
| 282 | } |
| 283 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 284 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 285 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 286 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 287 | bool &RequiresSecondPass) { |
| 288 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 289 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 291 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 292 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 293 | // FIXME: We probably don't need to handle references |
| 294 | // specially here, since value-initialization of references is |
| 295 | // handled in InitializationSequence. |
| 296 | if (Field->getType()->isReferenceType()) { |
| 297 | // C++ [dcl.init.aggr]p9: |
| 298 | // If an incomplete or empty initializer-list leaves a |
| 299 | // member of reference type uninitialized, the program is |
| 300 | // ill-formed. |
| 301 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 302 | << Field->getType() |
| 303 | << ILE->getSyntacticForm()->getSourceRange(); |
| 304 | SemaRef.Diag(Field->getLocation(), |
| 305 | diag::note_uninit_reference_member); |
| 306 | hadError = true; |
| 307 | return; |
| 308 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 309 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 310 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 311 | true); |
| 312 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 313 | if (!InitSeq) { |
| 314 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 315 | hadError = true; |
| 316 | return; |
| 317 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 318 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 319 | ExprResult MemberInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 320 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 321 | if (MemberInit.isInvalid()) { |
| 322 | hadError = true; |
| 323 | return; |
| 324 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 325 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 326 | if (hadError) { |
| 327 | // Do nothing |
| 328 | } else if (Init < NumInits) { |
| 329 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 330 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 331 | // Value-initialization requires a constructor call, so |
| 332 | // extend the initializer list to include the constructor |
| 333 | // call and make a note that we'll need to take another pass |
| 334 | // through the initializer list. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 335 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 336 | RequiresSecondPass = true; |
| 337 | } |
| 338 | } else if (InitListExpr *InnerILE |
| 339 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 340 | FillInValueInitializations(MemberEntity, InnerILE, |
| 341 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 344 | /// Recursively replaces NULL values within the given initializer list |
| 345 | /// with expressions that perform value-initialization of the |
| 346 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 347 | void |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 348 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 349 | InitListExpr *ILE, |
| 350 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 352 | "Should not have void type"); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 353 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 354 | if (ILE->getSyntacticForm()) |
| 355 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 357 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 358 | if (RType->getDecl()->isUnion() && |
| 359 | ILE->getInitializedFieldInUnion()) |
| 360 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 361 | Entity, ILE, RequiresSecondPass); |
| 362 | else { |
| 363 | unsigned Init = 0; |
| 364 | for (RecordDecl::field_iterator |
| 365 | Field = RType->getDecl()->field_begin(), |
| 366 | FieldEnd = RType->getDecl()->field_end(); |
| 367 | Field != FieldEnd; ++Field) { |
| 368 | if (Field->isUnnamedBitfield()) |
| 369 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 371 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 372 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 373 | |
| 374 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 375 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 376 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 378 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 380 | // Only look at the first initialization of a union. |
| 381 | if (RType->getDecl()->isUnion()) |
| 382 | break; |
| 383 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 388 | |
| 389 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 391 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 392 | unsigned NumInits = ILE->getNumInits(); |
| 393 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 394 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 395 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 396 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 397 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 398 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 399 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 400 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 401 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 402 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 403 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 404 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 406 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 408 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 409 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 410 | if (hadError) |
| 411 | return; |
| 412 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 413 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 414 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 415 | ElementEntity.setElementIndex(Init); |
| 416 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 417 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 418 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 419 | true); |
| 420 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 421 | if (!InitSeq) { |
| 422 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 423 | hadError = true; |
| 424 | return; |
| 425 | } |
| 426 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 427 | ExprResult ElementInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 428 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 429 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 430 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
| 434 | if (hadError) { |
| 435 | // Do nothing |
| 436 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 437 | // For arrays, just set the expression used for value-initialization |
| 438 | // of the "holes" in the array. |
| 439 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 440 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 441 | else |
| 442 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 443 | } else { |
| 444 | // For arrays, just set the expression used for value-initialization |
| 445 | // of the rest of elements and exit. |
| 446 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 447 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 448 | return; |
| 449 | } |
| 450 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 451 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 452 | // Value-initialization requires a constructor call, so |
| 453 | // extend the initializer list to include the constructor |
| 454 | // call and make a note that we'll need to take another pass |
| 455 | // through the initializer list. |
| 456 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 457 | RequiresSecondPass = true; |
| 458 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 459 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 460 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 461 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 462 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 467 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 468 | InitListExpr *IL, QualType &T, |
| 469 | bool VerifyOnly) |
| 470 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 471 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 472 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 473 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 474 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | FullyStructuredList |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 476 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 477 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 478 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 479 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 480 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 481 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 482 | bool RequiresSecondPass = false; |
| 483 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 484 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 485 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 486 | RequiresSecondPass); |
| 487 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 491 | // FIXME: use a proper constant |
| 492 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 493 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 494 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 495 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 496 | } |
| 497 | return maxElements; |
| 498 | } |
| 499 | |
| 500 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 501 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 502 | int InitializableMembers = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 504 | Field = structDecl->field_begin(), |
| 505 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 506 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 507 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 508 | ++InitializableMembers; |
| 509 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 510 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 511 | return std::min(InitializableMembers, 1); |
| 512 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 515 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 516 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 517 | QualType T, unsigned &Index, |
| 518 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 519 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 520 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 522 | if (T->isArrayType()) |
| 523 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 524 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 525 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 526 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 527 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 528 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 529 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 530 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 531 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 532 | if (!VerifyOnly) |
| 533 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 534 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 535 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 536 | hadError = true; |
| 537 | return; |
| 538 | } |
| 539 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 540 | // Build a structured initializer list corresponding to this subobject. |
| 541 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 542 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 543 | StructuredIndex, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 544 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 545 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 546 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 547 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 548 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 549 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 550 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 551 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 553 | StructuredSubobjectInitIndex); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 554 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 555 | if (!VerifyOnly) { |
| 556 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 557 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 558 | // Update the structured sub-object initializer so that it's ending |
| 559 | // range corresponds with the end of the last initializer it used. |
| 560 | if (EndIndex < ParentIList->getNumInits()) { |
| 561 | SourceLocation EndLoc |
| 562 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 563 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 564 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 565 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 566 | // Warn about missing braces. |
| 567 | if (T->isArrayType() || T->isRecordType()) { |
| 568 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 569 | diag::warn_missing_braces) |
| 570 | << StructuredSubobjectInitList->getSourceRange() |
| 571 | << FixItHint::CreateInsertion( |
| 572 | StructuredSubobjectInitList->getLocStart(), "{") |
| 573 | << FixItHint::CreateInsertion( |
| 574 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 575 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 576 | "}"); |
| 577 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 578 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 581 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 582 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 583 | unsigned &Index, |
| 584 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 585 | unsigned &StructuredIndex, |
| 586 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 587 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 588 | if (!VerifyOnly) { |
| 589 | SyntacticToSemantic[IList] = StructuredList; |
| 590 | StructuredList->setSyntacticForm(IList); |
| 591 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 592 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 593 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 594 | if (!VerifyOnly) { |
| 595 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 596 | IList->setType(ExprTy); |
| 597 | StructuredList->setType(ExprTy); |
| 598 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 599 | if (hadError) |
| 600 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 601 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 602 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 603 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 604 | if (VerifyOnly) { |
| 605 | if (SemaRef.getLangOptions().CPlusPlus || |
| 606 | (SemaRef.getLangOptions().OpenCL && |
| 607 | IList->getType()->isVectorType())) { |
| 608 | hadError = true; |
| 609 | } |
| 610 | return; |
| 611 | } |
| 612 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 613 | if (StructuredIndex == 1 && |
| 614 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 615 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 616 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 617 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 618 | hadError = true; |
| 619 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 620 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 621 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 622 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 623 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 624 | // Don't complain for incomplete types, since we'll get an error |
| 625 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 626 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 628 | CurrentObjectType->isArrayType()? 0 : |
| 629 | CurrentObjectType->isVectorType()? 1 : |
| 630 | CurrentObjectType->isScalarType()? 2 : |
| 631 | CurrentObjectType->isUnionType()? 3 : |
| 632 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 633 | |
| 634 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 635 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 636 | DK = diag::err_excess_initializers; |
| 637 | hadError = true; |
| 638 | } |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 639 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 640 | DK = diag::err_excess_initializers; |
| 641 | hadError = true; |
| 642 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 643 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 644 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 645 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 648 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 649 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 650 | !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 651 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 652 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 653 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 654 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 657 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 658 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 660 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 661 | unsigned &Index, |
| 662 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 663 | unsigned &StructuredIndex, |
| 664 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 665 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 666 | // Explicitly braced initializer for complex type can be real+imaginary |
| 667 | // parts. |
| 668 | CheckComplexType(Entity, IList, DeclType, Index, |
| 669 | StructuredList, StructuredIndex); |
| 670 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 671 | CheckScalarType(Entity, IList, DeclType, Index, |
| 672 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 673 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 674 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 675 | StructuredList, StructuredIndex); |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 676 | } else if (DeclType->isAggregateType()) { |
| 677 | if (DeclType->isRecordType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 678 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 679 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 680 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 681 | StructuredList, StructuredIndex, |
| 682 | TopLevelObject); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 683 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 684 | llvm::APSInt Zero( |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 685 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 686 | false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 687 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 688 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 689 | StructuredList, StructuredIndex); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 690 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 691 | llvm_unreachable("Aggregate that isn't a structure or array?!"); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 692 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 693 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 694 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 695 | if (!VerifyOnly) |
| 696 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 697 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 698 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 699 | } else if (DeclType->isRecordType()) { |
| 700 | // C++ [dcl.init]p14: |
| 701 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 702 | // is a brace-enclosed list, see 8.5.1. |
| 703 | // |
| 704 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 705 | // we have an initializer list and a destination type that is not |
| 706 | // an aggregate. |
| 707 | // FIXME: In C++0x, this is yet another form of initialization. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 708 | if (!VerifyOnly) |
| 709 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 710 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 711 | hadError = true; |
| 712 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 713 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 714 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 715 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 716 | if (!VerifyOnly) |
| 717 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 718 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 719 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 720 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 721 | if (!VerifyOnly) |
| 722 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 723 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 724 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 728 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 729 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | unsigned &Index, |
| 732 | InitListExpr *StructuredList, |
| 733 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 734 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 735 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 736 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 737 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | InitListExpr *newStructuredList |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 739 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 740 | StructuredList, StructuredIndex, |
| 741 | SubInitList->getSourceRange()); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 742 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 743 | newStructuredList, newStructuredIndex); |
| 744 | ++StructuredIndex; |
| 745 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 746 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 747 | } else if (ElemType->isScalarType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 748 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 749 | StructuredList, StructuredIndex); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 750 | } else if (ElemType->isReferenceType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 751 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 752 | StructuredList, StructuredIndex); |
| 753 | } |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 754 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 755 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 756 | // arrayType can be incomplete if we're initializing a flexible |
| 757 | // array member. There's nothing we can do with the completed |
| 758 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 760 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 761 | if (!VerifyOnly) { |
| 762 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 763 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 764 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 765 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 766 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 767 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 768 | |
| 769 | // Fall through for subaggregate initialization. |
| 770 | |
| 771 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 772 | // C++ [dcl.init.aggr]p12: |
| 773 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 774 | // initializing the aggregate member with an initializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 775 | // an initializer-list. If the initializer can initialize a |
| 776 | // member, the member is initialized. [...] |
| 777 | |
| 778 | // FIXME: Better EqualLoc? |
| 779 | InitializationKind Kind = |
| 780 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 781 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 782 | |
| 783 | if (Seq) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 784 | if (!VerifyOnly) { |
| 785 | ExprResult Result = |
| 786 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 787 | if (Result.isInvalid()) |
| 788 | hadError = true; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 789 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 790 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 791 | Result.takeAs<Expr>()); |
| 792 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 793 | ++Index; |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | // Fall through for subaggregate initialization |
| 798 | } else { |
| 799 | // C99 6.7.8p13: |
| 800 | // |
| 801 | // The initializer for a structure or union object that has |
| 802 | // automatic storage duration shall be either an initializer |
| 803 | // list as described below, or a single expression that has |
| 804 | // compatible structure or union type. In the latter case, the |
| 805 | // initial value of the object, including unnamed members, is |
| 806 | // that of the expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 807 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 808 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 809 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 810 | !VerifyOnly) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 811 | == Sema::Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 812 | if (ExprRes.isInvalid()) |
| 813 | hadError = true; |
| 814 | else { |
| 815 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 816 | if (ExprRes.isInvalid()) |
| 817 | hadError = true; |
| 818 | } |
| 819 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 820 | ExprRes.takeAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 821 | ++Index; |
| 822 | return; |
| 823 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 824 | ExprRes.release(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 825 | // Fall through for subaggregate initialization |
| 826 | } |
| 827 | |
| 828 | // C++ [dcl.init.aggr]p12: |
| 829 | // |
| 830 | // [...] Otherwise, if the member is itself a non-empty |
| 831 | // subaggregate, brace elision is assumed and the initializer is |
| 832 | // considered for the initialization of the first member of |
| 833 | // the subaggregate. |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 834 | if (!SemaRef.getLangOptions().OpenCL && |
| 835 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 836 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 837 | StructuredIndex); |
| 838 | ++StructuredIndex; |
| 839 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 840 | if (!VerifyOnly) { |
| 841 | // We cannot initialize this element, so let |
| 842 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 843 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 844 | SemaRef.Owned(expr), |
| 845 | /*TopLevelOfInitList=*/true); |
| 846 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 847 | hadError = true; |
| 848 | ++Index; |
| 849 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 850 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 853 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 854 | InitListExpr *IList, QualType DeclType, |
| 855 | unsigned &Index, |
| 856 | InitListExpr *StructuredList, |
| 857 | unsigned &StructuredIndex) { |
| 858 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 859 | |
| 860 | // As an extension, clang supports complex initializers, which initialize |
| 861 | // a complex number component-wise. When an explicit initializer list for |
| 862 | // a complex number contains two two initializers, this extension kicks in: |
| 863 | // it exepcts the initializer list to contain two elements convertible to |
| 864 | // the element type of the complex type. The first element initializes |
| 865 | // the real part, and the second element intitializes the imaginary part. |
| 866 | |
| 867 | if (IList->getNumInits() != 2) |
| 868 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 869 | StructuredIndex); |
| 870 | |
| 871 | // This is an extension in C. (The builtin _Complex type does not exist |
| 872 | // in the C++ standard.) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 873 | if (!SemaRef.getLangOptions().CPlusPlus && !VerifyOnly) |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 874 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 875 | << IList->getSourceRange(); |
| 876 | |
| 877 | // Initialize the complex number. |
| 878 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 879 | InitializedEntity ElementEntity = |
| 880 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 881 | |
| 882 | for (unsigned i = 0; i < 2; ++i) { |
| 883 | ElementEntity.setElementIndex(Index); |
| 884 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 885 | StructuredList, StructuredIndex); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 890 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 891 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 892 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 893 | InitListExpr *StructuredList, |
| 894 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 895 | if (Index >= IList->getNumInits()) { |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 896 | if (!SemaRef.getLangOptions().CPlusPlus0x) { |
| 897 | if (!VerifyOnly) |
| 898 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
| 899 | << IList->getSourceRange(); |
| 900 | hadError = true; |
| 901 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 902 | ++Index; |
| 903 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 904 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 905 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 906 | |
| 907 | Expr *expr = IList->getInit(Index); |
| 908 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 909 | if (!VerifyOnly) |
| 910 | SemaRef.Diag(SubIList->getLocStart(), |
| 911 | diag::warn_many_braces_around_scalar_init) |
| 912 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 913 | |
| 914 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 915 | StructuredIndex); |
| 916 | return; |
| 917 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 918 | if (!VerifyOnly) |
| 919 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 920 | diag::err_designator_for_scalar_init) |
| 921 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 922 | hadError = true; |
| 923 | ++Index; |
| 924 | ++StructuredIndex; |
| 925 | return; |
| 926 | } |
| 927 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 928 | if (VerifyOnly) { |
| 929 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 930 | hadError = true; |
| 931 | ++Index; |
| 932 | return; |
| 933 | } |
| 934 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 935 | ExprResult Result = |
| 936 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 937 | SemaRef.Owned(expr), |
| 938 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 939 | |
| 940 | Expr *ResultExpr = 0; |
| 941 | |
| 942 | if (Result.isInvalid()) |
| 943 | hadError = true; // types weren't compatible. |
| 944 | else { |
| 945 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 946 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 947 | if (ResultExpr != expr) { |
| 948 | // The type was promoted, update initializer list. |
| 949 | IList->setInit(Index, ResultExpr); |
| 950 | } |
| 951 | } |
| 952 | if (hadError) |
| 953 | ++StructuredIndex; |
| 954 | else |
| 955 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 956 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 959 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 960 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 961 | unsigned &Index, |
| 962 | InitListExpr *StructuredList, |
| 963 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 964 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 965 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 966 | // general, it would be useful to pass location information down the stack, |
| 967 | // so that we know the location (or decl) of the "current object" being |
| 968 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 969 | if (!VerifyOnly) |
| 970 | SemaRef.Diag(IList->getLocStart(), |
| 971 | diag::err_init_reference_member_uninitialized) |
| 972 | << DeclType |
| 973 | << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 974 | hadError = true; |
| 975 | ++Index; |
| 976 | ++StructuredIndex; |
| 977 | return; |
| 978 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 979 | |
| 980 | Expr *expr = IList->getInit(Index); |
| 981 | if (isa<InitListExpr>(expr)) { |
| 982 | // FIXME: Allowed in C++11. |
| 983 | if (!VerifyOnly) |
| 984 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 985 | << DeclType << IList->getSourceRange(); |
| 986 | hadError = true; |
| 987 | ++Index; |
| 988 | ++StructuredIndex; |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | if (VerifyOnly) { |
| 993 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 994 | hadError = true; |
| 995 | ++Index; |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | ExprResult Result = |
| 1000 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1001 | SemaRef.Owned(expr), |
| 1002 | /*TopLevelOfInitList=*/true); |
| 1003 | |
| 1004 | if (Result.isInvalid()) |
| 1005 | hadError = true; |
| 1006 | |
| 1007 | expr = Result.takeAs<Expr>(); |
| 1008 | IList->setInit(Index, expr); |
| 1009 | |
| 1010 | if (hadError) |
| 1011 | ++StructuredIndex; |
| 1012 | else |
| 1013 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1014 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1017 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1018 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1019 | unsigned &Index, |
| 1020 | InitListExpr *StructuredList, |
| 1021 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1022 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1023 | unsigned maxElements = VT->getNumElements(); |
| 1024 | unsigned numEltsInit = 0; |
| 1025 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1026 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1027 | if (Index >= IList->getNumInits()) { |
| 1028 | // Make sure the element type can be value-initialized. |
| 1029 | if (VerifyOnly) |
| 1030 | CheckValueInitializable( |
| 1031 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1032 | return; |
| 1033 | } |
| 1034 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1035 | if (!SemaRef.getLangOptions().OpenCL) { |
| 1036 | // If the initializing element is a vector, try to copy-initialize |
| 1037 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1038 | Expr *Init = IList->getInit(Index); |
| 1039 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1040 | if (VerifyOnly) { |
| 1041 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1042 | hadError = true; |
| 1043 | ++Index; |
| 1044 | return; |
| 1045 | } |
| 1046 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1047 | ExprResult Result = |
| 1048 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1049 | SemaRef.Owned(Init), |
| 1050 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1051 | |
| 1052 | Expr *ResultExpr = 0; |
| 1053 | if (Result.isInvalid()) |
| 1054 | hadError = true; // types weren't compatible. |
| 1055 | else { |
| 1056 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1057 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1058 | if (ResultExpr != Init) { |
| 1059 | // The type was promoted, update initializer list. |
| 1060 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1063 | if (hadError) |
| 1064 | ++StructuredIndex; |
| 1065 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1066 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1067 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1068 | ++Index; |
| 1069 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1070 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1072 | InitializedEntity ElementEntity = |
| 1073 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1074 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1075 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1076 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1077 | if (Index >= IList->getNumInits()) { |
| 1078 | if (VerifyOnly) |
| 1079 | CheckValueInitializable(ElementEntity); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1080 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1081 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1082 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1083 | ElementEntity.setElementIndex(Index); |
| 1084 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1085 | StructuredList, StructuredIndex); |
| 1086 | } |
| 1087 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1088 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1089 | |
| 1090 | InitializedEntity ElementEntity = |
| 1091 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1092 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1093 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1094 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1095 | // Don't attempt to go past the end of the init list |
| 1096 | if (Index >= IList->getNumInits()) |
| 1097 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1098 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1099 | ElementEntity.setElementIndex(Index); |
| 1100 | |
| 1101 | QualType IType = IList->getInit(Index)->getType(); |
| 1102 | if (!IType->isVectorType()) { |
| 1103 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1104 | StructuredList, StructuredIndex); |
| 1105 | ++numEltsInit; |
| 1106 | } else { |
| 1107 | QualType VecType; |
| 1108 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1109 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1110 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1111 | if (IType->isExtVectorType()) |
| 1112 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1113 | else |
| 1114 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1115 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1116 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1117 | StructuredList, StructuredIndex); |
| 1118 | numEltsInit += numIElts; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1123 | if (numEltsInit != maxElements) { |
| 1124 | if (!VerifyOnly) |
| 1125 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 1126 | diag::err_vector_incorrect_num_initializers) |
| 1127 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1128 | hadError = true; |
| 1129 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1132 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1133 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1134 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1136 | unsigned &Index, |
| 1137 | InitListExpr *StructuredList, |
| 1138 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1139 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1140 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1141 | // Check for the special-case of initializing an array with a string. |
| 1142 | if (Index < IList->getNumInits()) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1143 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1144 | SemaRef.Context)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1145 | // We place the string literal directly into the resulting |
| 1146 | // initializer list. This is the only place where the structure |
| 1147 | // of the structured initializer list doesn't match exactly, |
| 1148 | // because doing so would involve allocating one character |
| 1149 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1150 | if (!VerifyOnly) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1151 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1152 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1153 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1154 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1155 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1156 | return; |
| 1157 | } |
| 1158 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1159 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1160 | // Check for VLAs; in standard C it would be possible to check this |
| 1161 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1162 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1163 | if (!VerifyOnly) |
| 1164 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1165 | diag::err_variable_object_no_init) |
| 1166 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1167 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1168 | ++Index; |
| 1169 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1170 | return; |
| 1171 | } |
| 1172 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1173 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1174 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1175 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1176 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1177 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1178 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1179 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1180 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1181 | maxElementsKnown = true; |
| 1182 | } |
| 1183 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1184 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1185 | while (Index < IList->getNumInits()) { |
| 1186 | Expr *Init = IList->getInit(Index); |
| 1187 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1188 | // If we're not the subobject that matches up with the '{' for |
| 1189 | // the designator, we shouldn't be handling the |
| 1190 | // designator. Return immediately. |
| 1191 | if (!SubobjectIsDesignatorContext) |
| 1192 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1193 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1194 | // Handle this designated initializer. elementIndex will be |
| 1195 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1196 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1197 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1198 | StructuredList, StructuredIndex, true, |
| 1199 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1200 | hadError = true; |
| 1201 | continue; |
| 1202 | } |
| 1203 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1204 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1205 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1206 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1207 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1208 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1209 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1210 | // If the array is of incomplete type, keep track of the number of |
| 1211 | // elements in the initializer. |
| 1212 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1213 | maxElements = elementIndex; |
| 1214 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1215 | continue; |
| 1216 | } |
| 1217 | |
| 1218 | // If we know the maximum number of elements, and we've already |
| 1219 | // hit it, stop consuming elements in the initializer list. |
| 1220 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1221 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1222 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1223 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1224 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1225 | Entity); |
| 1226 | // Check this element. |
| 1227 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1228 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1229 | ++elementIndex; |
| 1230 | |
| 1231 | // If the array is of incomplete type, keep track of the number of |
| 1232 | // elements in the initializer. |
| 1233 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1234 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1235 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1236 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1237 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1238 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1239 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1240 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1241 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1242 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1243 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1244 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1245 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1246 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1248 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1249 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1250 | if (!hadError && VerifyOnly) { |
| 1251 | // Check if there are any members of the array that get value-initialized. |
| 1252 | // If so, check if doing that is possible. |
| 1253 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1254 | if (maxElementsKnown && elementIndex < maxElements) |
| 1255 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1256 | SemaRef.Context, 0, Entity)); |
| 1257 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1260 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1261 | Expr *InitExpr, |
| 1262 | FieldDecl *Field, |
| 1263 | bool TopLevelObject) { |
| 1264 | // Handle GNU flexible array initializers. |
| 1265 | unsigned FlexArrayDiag; |
| 1266 | if (isa<InitListExpr>(InitExpr) && |
| 1267 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1268 | // Empty flexible array init always allowed as an extension |
| 1269 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1270 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 1271 | // Disallow flexible array init in C++; it is not required for gcc |
| 1272 | // compatibility, and it needs work to IRGen correctly in general. |
| 1273 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1274 | } else if (!TopLevelObject) { |
| 1275 | // Disallow flexible array init on non-top-level object |
| 1276 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1277 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1278 | // Disallow flexible array init on anything which is not a variable. |
| 1279 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1280 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1281 | // Disallow flexible array init on local variables. |
| 1282 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1283 | } else { |
| 1284 | // Allow other cases. |
| 1285 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1286 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (!VerifyOnly) { |
| 1289 | SemaRef.Diag(InitExpr->getSourceRange().getBegin(), |
| 1290 | FlexArrayDiag) |
| 1291 | << InitExpr->getSourceRange().getBegin(); |
| 1292 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1293 | << Field; |
| 1294 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1295 | |
| 1296 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1297 | } |
| 1298 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1299 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1300 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1302 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1304 | unsigned &Index, |
| 1305 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1306 | unsigned &StructuredIndex, |
| 1307 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1308 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1309 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1310 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1311 | // confusion, we forgo checking the intializer for the entire record. |
| 1312 | if (structDecl->isInvalidDecl()) { |
| 1313 | hadError = true; |
| 1314 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1316 | |
| 1317 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1318 | // Value-initialize the first named member of the union. |
| 1319 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1320 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1321 | Field != FieldEnd; ++Field) { |
| 1322 | if (Field->getDeclName()) { |
| 1323 | if (VerifyOnly) |
| 1324 | CheckValueInitializable( |
| 1325 | InitializedEntity::InitializeMember(*Field, &Entity)); |
| 1326 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1327 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1328 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | return; |
| 1332 | } |
| 1333 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1334 | // If structDecl is a forward declaration, this loop won't do |
| 1335 | // anything except look at designated initializers; That's okay, |
| 1336 | // because an error should get printed out elsewhere. It might be |
| 1337 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1338 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1339 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1340 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1341 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1342 | while (Index < IList->getNumInits()) { |
| 1343 | Expr *Init = IList->getInit(Index); |
| 1344 | |
| 1345 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1346 | // If we're not the subobject that matches up with the '{' for |
| 1347 | // the designator, we shouldn't be handling the |
| 1348 | // designator. Return immediately. |
| 1349 | if (!SubobjectIsDesignatorContext) |
| 1350 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1351 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1352 | // Handle this designated initializer. Field will be updated to |
| 1353 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1354 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1355 | DeclType, &Field, 0, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1356 | StructuredList, StructuredIndex, |
| 1357 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1358 | hadError = true; |
| 1359 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1360 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1361 | |
| 1362 | // Disable check for missing fields when designators are used. |
| 1363 | // This matches gcc behaviour. |
| 1364 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1365 | continue; |
| 1366 | } |
| 1367 | |
| 1368 | if (Field == FieldEnd) { |
| 1369 | // We've run out of fields. We're done. |
| 1370 | break; |
| 1371 | } |
| 1372 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1373 | // We've already initialized a member of a union. We're done. |
| 1374 | if (InitializedSomething && DeclType->isUnionType()) |
| 1375 | break; |
| 1376 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1377 | // If we've hit the flexible array member at the end, we're done. |
| 1378 | if (Field->getType()->isIncompleteArrayType()) |
| 1379 | break; |
| 1380 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1381 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1382 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1383 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1384 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1385 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1386 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1387 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1388 | bool InvalidUse; |
| 1389 | if (VerifyOnly) |
| 1390 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1391 | else |
| 1392 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
| 1393 | IList->getInit(Index)->getLocStart()); |
| 1394 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1395 | ++Index; |
| 1396 | ++Field; |
| 1397 | hadError = true; |
| 1398 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1399 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1400 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1401 | InitializedEntity MemberEntity = |
| 1402 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1403 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1404 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1405 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1406 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1407 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1408 | // Initialize the first field within the union. |
| 1409 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1410 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1411 | |
| 1412 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1413 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1414 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1415 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1416 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1417 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1418 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1419 | // It is possible we have one or more unnamed bitfields remaining. |
| 1420 | // Find first (if any) named field and emit warning. |
| 1421 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1422 | it != end; ++it) { |
| 1423 | if (!it->isUnnamedBitfield()) { |
| 1424 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1425 | diag::warn_missing_field_initializers) << it->getName(); |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame^] | 1431 | // Check that any remaining fields can be value-initialized. |
| 1432 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1433 | !Field->getType()->isIncompleteArrayType()) { |
| 1434 | // FIXME: Should check for holes left by designated initializers too. |
| 1435 | for (; Field != FieldEnd && !hadError; ++Field) { |
| 1436 | if (!Field->isUnnamedBitfield()) |
| 1437 | CheckValueInitializable( |
| 1438 | InitializedEntity::InitializeMember(*Field, &Entity)); |
| 1439 | } |
| 1440 | } |
| 1441 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1443 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1446 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 1447 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1448 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1449 | ++Index; |
| 1450 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1453 | InitializedEntity MemberEntity = |
| 1454 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1455 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1456 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1457 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1458 | StructuredList, StructuredIndex); |
| 1459 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1460 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1461 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1462 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1463 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1464 | /// \brief Expand a field designator that refers to a member of an |
| 1465 | /// anonymous struct or union into a series of field designators that |
| 1466 | /// refers to the field within the appropriate subobject. |
| 1467 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1468 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | DesignatedInitExpr *DIE, |
| 1470 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1471 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1472 | typedef DesignatedInitExpr::Designator Designator; |
| 1473 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1474 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1475 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1476 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1477 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1478 | if (PI + 1 == PE) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1480 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1481 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1482 | else |
| 1483 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1484 | SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1485 | assert(isa<FieldDecl>(*PI)); |
| 1486 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | // Expand the current designator into the set of replacement |
| 1490 | // designators, so we have a full subobject path down to where the |
| 1491 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1492 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1493 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1494 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1496 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1497 | /// corresponds to FieldName. |
| 1498 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1499 | IdentifierInfo *FieldName) { |
| 1500 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1501 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1502 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1503 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1504 | return IF; |
| 1505 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1506 | } |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1507 | return 0; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1510 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1511 | DesignatedInitExpr *DIE) { |
| 1512 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1513 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1514 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1515 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1516 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
| 1517 | DIE->size(), IndexExprs.data(), |
| 1518 | NumIndexExprs, DIE->getEqualOrColonLoc(), |
| 1519 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1520 | } |
| 1521 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1522 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1523 | /// |
| 1524 | /// Determines whether the designated initializer @p DIE, which |
| 1525 | /// resides at the given @p Index within the initializer list @p |
| 1526 | /// IList, is well-formed for a current object of type @p DeclType |
| 1527 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1529 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1530 | /// |
| 1531 | /// @param IList The initializer list in which this designated |
| 1532 | /// initializer occurs. |
| 1533 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1534 | /// @param DIE The designated initializer expression. |
| 1535 | /// |
| 1536 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1537 | /// |
| 1538 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1539 | /// into which the designation in @p DIE should refer. |
| 1540 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1541 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1542 | /// a field, this will be set to the field declaration corresponding |
| 1543 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1544 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1545 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1546 | /// DIE is an array designator or GNU array-range designator, this |
| 1547 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1548 | /// |
| 1549 | /// @param Index Index into @p IList where the designated initializer |
| 1550 | /// @p DIE occurs. |
| 1551 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1552 | /// @param StructuredList The initializer list expression that |
| 1553 | /// describes all of the subobject initializers in the order they'll |
| 1554 | /// actually be initialized. |
| 1555 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1556 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1558 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1559 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1560 | DesignatedInitExpr *DIE, |
| 1561 | unsigned DesigIdx, |
| 1562 | QualType &CurrentObjectType, |
| 1563 | RecordDecl::field_iterator *NextField, |
| 1564 | llvm::APSInt *NextElementIndex, |
| 1565 | unsigned &Index, |
| 1566 | InitListExpr *StructuredList, |
| 1567 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1568 | bool FinishSubobjectInit, |
| 1569 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1570 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1571 | // Check the actual initialization for the designated object type. |
| 1572 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1573 | |
| 1574 | // Temporarily remove the designator expression from the |
| 1575 | // initializer list that the child calls see, so that we don't try |
| 1576 | // to re-process the designator. |
| 1577 | unsigned OldIndex = Index; |
| 1578 | IList->setInit(OldIndex, DIE->getInit()); |
| 1579 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1580 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1581 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1582 | |
| 1583 | // Restore the designated initializer expression in the syntactic |
| 1584 | // form of the initializer list. |
| 1585 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1586 | DIE->setInit(IList->getInit(OldIndex)); |
| 1587 | IList->setInit(OldIndex, DIE); |
| 1588 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1589 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1592 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1593 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1594 | if (!VerifyOnly) { |
| 1595 | assert((IsFirstDesignator || StructuredList) && |
| 1596 | "Need a non-designated initializer list to start from"); |
| 1597 | |
| 1598 | // Determine the structural initializer list that corresponds to the |
| 1599 | // current subobject. |
| 1600 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
| 1601 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1602 | StructuredList, StructuredIndex, |
| 1603 | SourceRange(D->getStartLocation(), |
| 1604 | DIE->getSourceRange().getEnd())); |
| 1605 | assert(StructuredList && "Expected a structured initializer list"); |
| 1606 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1608 | if (D->isFieldDesignator()) { |
| 1609 | // C99 6.7.8p7: |
| 1610 | // |
| 1611 | // If a designator has the form |
| 1612 | // |
| 1613 | // . identifier |
| 1614 | // |
| 1615 | // then the current object (defined below) shall have |
| 1616 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1618 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1619 | if (!RT) { |
| 1620 | SourceLocation Loc = D->getDotLoc(); |
| 1621 | if (Loc.isInvalid()) |
| 1622 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1623 | if (!VerifyOnly) |
| 1624 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1625 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1626 | ++Index; |
| 1627 | return true; |
| 1628 | } |
| 1629 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1630 | // Note: we perform a linear search of the fields here, despite |
| 1631 | // the fact that we have a faster lookup method, because we always |
| 1632 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1633 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1634 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1635 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1637 | Field = RT->getDecl()->field_begin(), |
| 1638 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1639 | for (; Field != FieldEnd; ++Field) { |
| 1640 | if (Field->isUnnamedBitfield()) |
| 1641 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1642 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1643 | // If we find a field representing an anonymous field, look in the |
| 1644 | // IndirectFieldDecl that follow for the designated initializer. |
| 1645 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1646 | if (IndirectFieldDecl *IF = |
| 1647 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1648 | // In verify mode, don't modify the original. |
| 1649 | if (VerifyOnly) |
| 1650 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1651 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1652 | D = DIE->getDesignator(DesigIdx); |
| 1653 | break; |
| 1654 | } |
| 1655 | } |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1656 | if (KnownField && KnownField == *Field) |
| 1657 | break; |
| 1658 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1659 | break; |
| 1660 | |
| 1661 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1664 | if (Field == FieldEnd) { |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1665 | if (VerifyOnly) { |
| 1666 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1667 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1668 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1669 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1670 | // There was no normal field in the struct with the designated |
| 1671 | // name. Perform another lookup for this name, which may find |
| 1672 | // something that we can't designate (e.g., a member function), |
| 1673 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1675 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1676 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1677 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1678 | // Name lookup didn't find anything. Determine whether this |
| 1679 | // was a typo for another field name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1680 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1681 | Sema::LookupMemberName); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1682 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1683 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1684 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1685 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1686 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1687 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1688 | ->Equals(RT->getDecl())) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1689 | std::string CorrectedStr( |
| 1690 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1691 | std::string CorrectedQuotedStr( |
| 1692 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1693 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1694 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1695 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1696 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1697 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1698 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1699 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1700 | } else { |
| 1701 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1702 | << FieldName << CurrentObjectType; |
| 1703 | ++Index; |
| 1704 | return true; |
| 1705 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1706 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1708 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1709 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1710 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1711 | << FieldName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1713 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1714 | ++Index; |
| 1715 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1716 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1717 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1718 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1719 | // The replacement field comes from typo correction; find it |
| 1720 | // in the list of fields. |
| 1721 | FieldIndex = 0; |
| 1722 | Field = RT->getDecl()->field_begin(); |
| 1723 | for (; Field != FieldEnd; ++Field) { |
| 1724 | if (Field->isUnnamedBitfield()) |
| 1725 | continue; |
| 1726 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1727 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1728 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1729 | break; |
| 1730 | |
| 1731 | ++FieldIndex; |
| 1732 | } |
| 1733 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1734 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1735 | |
| 1736 | // All of the fields of a union are located at the same place in |
| 1737 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1738 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1739 | FieldIndex = 0; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1740 | if (!VerifyOnly) |
| 1741 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1742 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1743 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1744 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1745 | bool InvalidUse; |
| 1746 | if (VerifyOnly) |
| 1747 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1748 | else |
| 1749 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
| 1750 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1751 | ++Index; |
| 1752 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1753 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1754 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1755 | if (!VerifyOnly) { |
| 1756 | // Update the designator with the field declaration. |
| 1757 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1759 | // Make sure that our non-designated initializer list has space |
| 1760 | // for a subobject corresponding to this field. |
| 1761 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1762 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1763 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1764 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1765 | // This designator names a flexible array member. |
| 1766 | if (Field->getType()->isIncompleteArrayType()) { |
| 1767 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1768 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1769 | // We can't designate an object within the flexible array |
| 1770 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1771 | if (!VerifyOnly) { |
| 1772 | DesignatedInitExpr::Designator *NextD |
| 1773 | = DIE->getDesignator(DesigIdx + 1); |
| 1774 | SemaRef.Diag(NextD->getStartLocation(), |
| 1775 | diag::err_designator_into_flexible_array_member) |
| 1776 | << SourceRange(NextD->getStartLocation(), |
| 1777 | DIE->getSourceRange().getEnd()); |
| 1778 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1779 | << *Field; |
| 1780 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1781 | Invalid = true; |
| 1782 | } |
| 1783 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1784 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1785 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1786 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1787 | if (!VerifyOnly) { |
| 1788 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
| 1789 | diag::err_flexible_array_init_needs_braces) |
| 1790 | << DIE->getInit()->getSourceRange(); |
| 1791 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1792 | << *Field; |
| 1793 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1794 | Invalid = true; |
| 1795 | } |
| 1796 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1797 | // Check GNU flexible array initializer. |
| 1798 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 1799 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1800 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1801 | |
| 1802 | if (Invalid) { |
| 1803 | ++Index; |
| 1804 | return true; |
| 1805 | } |
| 1806 | |
| 1807 | // Initialize the array. |
| 1808 | bool prevHadError = hadError; |
| 1809 | unsigned newStructuredIndex = FieldIndex; |
| 1810 | unsigned OldIndex = Index; |
| 1811 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1812 | |
| 1813 | InitializedEntity MemberEntity = |
| 1814 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1815 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1816 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1818 | IList->setInit(OldIndex, DIE); |
| 1819 | if (hadError && !prevHadError) { |
| 1820 | ++Field; |
| 1821 | ++FieldIndex; |
| 1822 | if (NextField) |
| 1823 | *NextField = Field; |
| 1824 | StructuredIndex = FieldIndex; |
| 1825 | return true; |
| 1826 | } |
| 1827 | } else { |
| 1828 | // Recurse to check later designated subobjects. |
| 1829 | QualType FieldType = (*Field)->getType(); |
| 1830 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1831 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1832 | InitializedEntity MemberEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1833 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1834 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1835 | FieldType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1836 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1837 | true, false)) |
| 1838 | return true; |
| 1839 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1840 | |
| 1841 | // Find the position of the next field to be initialized in this |
| 1842 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1843 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1844 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1845 | |
| 1846 | // If this the first designator, our caller will continue checking |
| 1847 | // the rest of this struct/class/union subobject. |
| 1848 | if (IsFirstDesignator) { |
| 1849 | if (NextField) |
| 1850 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1851 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1852 | return false; |
| 1853 | } |
| 1854 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1855 | if (!FinishSubobjectInit) |
| 1856 | return false; |
| 1857 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1858 | // We've already initialized something in the union; we're done. |
| 1859 | if (RT->getDecl()->isUnion()) |
| 1860 | return hadError; |
| 1861 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1862 | // Check the remaining fields within this class/struct/union subobject. |
| 1863 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1864 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1865 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1866 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1867 | return hadError && !prevHadError; |
| 1868 | } |
| 1869 | |
| 1870 | // C99 6.7.8p6: |
| 1871 | // |
| 1872 | // If a designator has the form |
| 1873 | // |
| 1874 | // [ constant-expression ] |
| 1875 | // |
| 1876 | // then the current object (defined below) shall have array |
| 1877 | // type and the expression shall be an integer constant |
| 1878 | // expression. If the array is of unknown size, any |
| 1879 | // nonnegative value is valid. |
| 1880 | // |
| 1881 | // Additionally, cope with the GNU extension that permits |
| 1882 | // designators of the form |
| 1883 | // |
| 1884 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1885 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1886 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1887 | if (!VerifyOnly) |
| 1888 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1889 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1890 | ++Index; |
| 1891 | return true; |
| 1892 | } |
| 1893 | |
| 1894 | Expr *IndexExpr = 0; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1895 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1896 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1897 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1898 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1899 | DesignatedEndIndex = DesignatedStartIndex; |
| 1900 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1901 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1902 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1904 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1906 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1907 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1908 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1909 | // Codegen can't handle evaluating array range designators that have side |
| 1910 | // effects, because we replicate the AST value for each initialized element. |
| 1911 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1912 | // elements with something that has a side effect, so codegen can emit an |
| 1913 | // "error unsupported" error instead of miscompiling the app. |
| 1914 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1915 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1916 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1919 | if (isa<ConstantArrayType>(AT)) { |
| 1920 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1921 | DesignatedStartIndex |
| 1922 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1923 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1924 | DesignatedEndIndex |
| 1925 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1926 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1927 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 1928 | if (!VerifyOnly) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1929 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
| 1930 | diag::err_array_designator_too_large) |
| 1931 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1932 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1933 | ++Index; |
| 1934 | return true; |
| 1935 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1936 | } else { |
| 1937 | // Make sure the bit-widths and signedness match. |
| 1938 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1939 | DesignatedEndIndex |
| 1940 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1941 | else if (DesignatedStartIndex.getBitWidth() < |
| 1942 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1943 | DesignatedStartIndex |
| 1944 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1945 | DesignatedStartIndex.setIsUnsigned(true); |
| 1946 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1947 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1949 | // Make sure that our non-designated initializer list has space |
| 1950 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1951 | if (!VerifyOnly && |
| 1952 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1954 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1955 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1956 | // Repeatedly perform subobject initializations in the range |
| 1957 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1959 | // Move to the next designator |
| 1960 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1961 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1962 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1963 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1964 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1965 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1966 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1967 | // Recurse to check later designated subobjects. |
| 1968 | QualType ElementType = AT->getElementType(); |
| 1969 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1970 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1971 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1972 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1973 | ElementType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1974 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1975 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1976 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1977 | return true; |
| 1978 | |
| 1979 | // Move to the next index in the array that we'll be initializing. |
| 1980 | ++DesignatedStartIndex; |
| 1981 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1982 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1983 | |
| 1984 | // If this the first designator, our caller will continue checking |
| 1985 | // the rest of this array subobject. |
| 1986 | if (IsFirstDesignator) { |
| 1987 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1988 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1989 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1990 | return false; |
| 1991 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1993 | if (!FinishSubobjectInit) |
| 1994 | return false; |
| 1995 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1996 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1997 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1999 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2000 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2004 | // Get the structured initializer list for a subobject of type |
| 2005 | // @p CurrentObjectType. |
| 2006 | InitListExpr * |
| 2007 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2008 | QualType CurrentObjectType, |
| 2009 | InitListExpr *StructuredList, |
| 2010 | unsigned StructuredIndex, |
| 2011 | SourceRange InitRange) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2012 | if (VerifyOnly) |
| 2013 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2014 | Expr *ExistingInit = 0; |
| 2015 | if (!StructuredList) |
| 2016 | ExistingInit = SyntacticToSemantic[IList]; |
| 2017 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2018 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2020 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2021 | return Result; |
| 2022 | |
| 2023 | if (ExistingInit) { |
| 2024 | // We are creating an initializer list that initializes the |
| 2025 | // subobjects of the current object, but there was already an |
| 2026 | // initialization that completely initialized the current |
| 2027 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2029 | // struct X { int a, b; }; |
| 2030 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2032 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2033 | // designated initializer re-initializes the whole |
| 2034 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2036 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2037 | << InitRange; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2039 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2040 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2041 | << ExistingInit->getSourceRange(); |
| 2042 | } |
| 2043 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2045 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 2046 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2047 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2049 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2050 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2051 | // Pre-allocate storage for the structured initializer list. |
| 2052 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2053 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2054 | bool GotNumInits = false; |
| 2055 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2056 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2057 | GotNumInits = true; |
| 2058 | } else if (Index < IList->getNumInits()) { |
| 2059 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2060 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2061 | GotNumInits = true; |
| 2062 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2066 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2067 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2068 | NumElements = CAType->getSize().getZExtValue(); |
| 2069 | // Simple heuristic so that we don't allocate a very large |
| 2070 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2071 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2072 | NumElements = 0; |
| 2073 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2074 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2075 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2076 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2077 | RecordDecl *RDecl = RType->getDecl(); |
| 2078 | if (RDecl->isUnion()) |
| 2079 | NumElements = 1; |
| 2080 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2082 | RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2085 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2087 | // Link this new initializer list into the structured initializer |
| 2088 | // lists. |
| 2089 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2090 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2091 | else { |
| 2092 | Result->setSyntacticForm(IList); |
| 2093 | SyntacticToSemantic[IList] = Result; |
| 2094 | } |
| 2095 | |
| 2096 | return Result; |
| 2097 | } |
| 2098 | |
| 2099 | /// Update the initializer at index @p StructuredIndex within the |
| 2100 | /// structured initializer list to the value @p expr. |
| 2101 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2102 | unsigned &StructuredIndex, |
| 2103 | Expr *expr) { |
| 2104 | // No structured initializer list to update |
| 2105 | if (!StructuredList) |
| 2106 | return; |
| 2107 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2108 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2109 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2110 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2112 | diag::warn_initializer_overrides) |
| 2113 | << expr->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2115 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2116 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2117 | << PrevInit->getSourceRange(); |
| 2118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2120 | ++StructuredIndex; |
| 2121 | } |
| 2122 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2123 | /// Check that the given Index expression is a valid array designator |
| 2124 | /// value. This is essentailly just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2125 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2126 | /// and produces a reasonable diagnostic if there is a |
| 2127 | /// failure. Returns true if there was an error, false otherwise. If |
| 2128 | /// everything went okay, Value will receive the value of the constant |
| 2129 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | static bool |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2131 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2132 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 2133 | |
| 2134 | // Make sure this is an integer constant expression. |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2135 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 2136 | return true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2137 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2138 | if (Value.isSigned() && Value.isNegative()) |
| 2139 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2140 | << Value.toString(10) << Index->getSourceRange(); |
| 2141 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2142 | Value.setIsUnsigned(true); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2143 | return false; |
| 2144 | } |
| 2145 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2146 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2147 | SourceLocation Loc, |
| 2148 | bool GNUSyntax, |
| 2149 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2150 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2151 | |
| 2152 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2153 | SmallVector<ASTDesignator, 32> Designators; |
| 2154 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2155 | |
| 2156 | // Build designators and check array designator expressions. |
| 2157 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2158 | const Designator &D = Desig.getDesignator(Idx); |
| 2159 | switch (D.getKind()) { |
| 2160 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2162 | D.getFieldLoc())); |
| 2163 | break; |
| 2164 | |
| 2165 | case Designator::ArrayDesignator: { |
| 2166 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2167 | llvm::APSInt IndexValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2168 | if (!Index->isTypeDependent() && |
| 2169 | !Index->isValueDependent() && |
| 2170 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2171 | Invalid = true; |
| 2172 | else { |
| 2173 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2175 | D.getRBracketLoc())); |
| 2176 | InitExpressions.push_back(Index); |
| 2177 | } |
| 2178 | break; |
| 2179 | } |
| 2180 | |
| 2181 | case Designator::ArrayRangeDesignator: { |
| 2182 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2183 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2184 | llvm::APSInt StartValue; |
| 2185 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2186 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2187 | StartIndex->isValueDependent(); |
| 2188 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2189 | EndIndex->isValueDependent(); |
| 2190 | if ((!StartDependent && |
| 2191 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 2192 | (!EndDependent && |
| 2193 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2194 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2195 | else { |
| 2196 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2197 | if (StartDependent || EndDependent) { |
| 2198 | // Nothing to compute. |
| 2199 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2200 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2201 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2202 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2203 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2204 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2205 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2207 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2208 | Invalid = true; |
| 2209 | } else { |
| 2210 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2211 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2212 | D.getEllipsisLoc(), |
| 2213 | D.getRBracketLoc())); |
| 2214 | InitExpressions.push_back(StartIndex); |
| 2215 | InitExpressions.push_back(EndIndex); |
| 2216 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2217 | } |
| 2218 | break; |
| 2219 | } |
| 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | if (Invalid || Init.isInvalid()) |
| 2224 | return ExprError(); |
| 2225 | |
| 2226 | // Clear out the expressions within the designation. |
| 2227 | Desig.ClearExprs(*this); |
| 2228 | |
| 2229 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2230 | = DesignatedInitExpr::Create(Context, |
| 2231 | Designators.data(), Designators.size(), |
| 2232 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2233 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2234 | |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2235 | if (getLangOptions().CPlusPlus) |
Eli Friedman | ea7b85b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2236 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2237 | << DIE->getSourceRange(); |
| 2238 | else if (!getLangOptions().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2239 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2240 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2241 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2242 | return Owned(DIE); |
| 2243 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2244 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2245 | //===----------------------------------------------------------------------===// |
| 2246 | // Initialization entity |
| 2247 | //===----------------------------------------------------------------------===// |
| 2248 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2249 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2250 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2251 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2252 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2253 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2254 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2255 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2256 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2257 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2258 | Type = VT->getElementType(); |
| 2259 | } else { |
| 2260 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2261 | assert(CT && "Unexpected type"); |
| 2262 | Kind = EK_ComplexElement; |
| 2263 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2264 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2267 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2268 | CXXBaseSpecifier *Base, |
| 2269 | bool IsInheritedVirtualBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2270 | { |
| 2271 | InitializedEntity Result; |
| 2272 | Result.Kind = EK_Base; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2273 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2274 | if (IsInheritedVirtualBase) |
| 2275 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2276 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2277 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2278 | return Result; |
| 2279 | } |
| 2280 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2281 | DeclarationName InitializedEntity::getName() const { |
| 2282 | switch (getKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2283 | case EK_Parameter: { |
| 2284 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2285 | return (D ? D->getDeclName() : DeclarationName()); |
| 2286 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2287 | |
| 2288 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2289 | case EK_Member: |
| 2290 | return VariableOrMember->getDeclName(); |
| 2291 | |
| 2292 | case EK_Result: |
| 2293 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2294 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2295 | case EK_Temporary: |
| 2296 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2297 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2298 | case EK_ArrayElement: |
| 2299 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2300 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2301 | case EK_BlockElement: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2302 | return DeclarationName(); |
| 2303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2304 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2305 | // Silence GCC warning |
| 2306 | return DeclarationName(); |
| 2307 | } |
| 2308 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2309 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2310 | switch (getKind()) { |
| 2311 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2312 | case EK_Member: |
| 2313 | return VariableOrMember; |
| 2314 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2315 | case EK_Parameter: |
| 2316 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2317 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2318 | case EK_Result: |
| 2319 | case EK_Exception: |
| 2320 | case EK_New: |
| 2321 | case EK_Temporary: |
| 2322 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2323 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2324 | case EK_ArrayElement: |
| 2325 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2326 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2327 | case EK_BlockElement: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2328 | return 0; |
| 2329 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2330 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2331 | // Silence GCC warning |
| 2332 | return 0; |
| 2333 | } |
| 2334 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2335 | bool InitializedEntity::allowsNRVO() const { |
| 2336 | switch (getKind()) { |
| 2337 | case EK_Result: |
| 2338 | case EK_Exception: |
| 2339 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2341 | case EK_Variable: |
| 2342 | case EK_Parameter: |
| 2343 | case EK_Member: |
| 2344 | case EK_New: |
| 2345 | case EK_Temporary: |
| 2346 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2347 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2348 | case EK_ArrayElement: |
| 2349 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2350 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2351 | case EK_BlockElement: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2352 | break; |
| 2353 | } |
| 2354 | |
| 2355 | return false; |
| 2356 | } |
| 2357 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2358 | //===----------------------------------------------------------------------===// |
| 2359 | // Initialization sequence |
| 2360 | //===----------------------------------------------------------------------===// |
| 2361 | |
| 2362 | void InitializationSequence::Step::Destroy() { |
| 2363 | switch (Kind) { |
| 2364 | case SK_ResolveAddressOfOverloadedFunction: |
| 2365 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2366 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2367 | case SK_CastDerivedToBaseLValue: |
| 2368 | case SK_BindReference: |
| 2369 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2370 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2371 | case SK_UserConversion: |
| 2372 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2373 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2374 | case SK_QualificationConversionLValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2375 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2376 | case SK_ListConstructorCall: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2377 | case SK_ConstructorInitialization: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2378 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2379 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2380 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2381 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2382 | case SK_ArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2383 | case SK_PassByIndirectCopyRestore: |
| 2384 | case SK_PassByIndirectRestore: |
| 2385 | case SK_ProduceObjCObject: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2386 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2387 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2388 | case SK_ConversionSequence: |
| 2389 | delete ICS; |
| 2390 | } |
| 2391 | } |
| 2392 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2393 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2394 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2398 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2399 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2400 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2401 | switch (getFailureKind()) { |
| 2402 | case FK_TooManyInitsForReference: |
| 2403 | case FK_ArrayNeedsInitList: |
| 2404 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2405 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2406 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2407 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2408 | case FK_RValueReferenceBindingToLValue: |
| 2409 | case FK_ReferenceInitDropsQualifiers: |
| 2410 | case FK_ReferenceInitFailed: |
| 2411 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2412 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2413 | case FK_TooManyInitsForScalar: |
| 2414 | case FK_ReferenceBindingToInitList: |
| 2415 | case FK_InitListBadDestinationType: |
| 2416 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2417 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2418 | case FK_ArrayTypeMismatch: |
| 2419 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2420 | case FK_ListInitializationFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2421 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2422 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2423 | case FK_ReferenceInitOverloadFailed: |
| 2424 | case FK_UserConversionOverloadFailed: |
| 2425 | case FK_ConstructorOverloadFailed: |
| 2426 | return FailedOverloadResult == OR_Ambiguous; |
| 2427 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2429 | return false; |
| 2430 | } |
| 2431 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2432 | bool InitializationSequence::isConstructorInitialization() const { |
| 2433 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2434 | } |
| 2435 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2436 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2437 | const Expr *Initializer, |
| 2438 | bool *isInitializerConstant, |
| 2439 | APValue *ConstantValue) const { |
| 2440 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2441 | return false; |
| 2442 | |
| 2443 | const Step &LastStep = Steps.back(); |
| 2444 | if (LastStep.Kind != SK_ConversionSequence) |
| 2445 | return false; |
| 2446 | |
| 2447 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2448 | const StandardConversionSequence *SCS = NULL; |
| 2449 | switch (ICS.getKind()) { |
| 2450 | case ImplicitConversionSequence::StandardConversion: |
| 2451 | SCS = &ICS.Standard; |
| 2452 | break; |
| 2453 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2454 | SCS = &ICS.UserDefined.After; |
| 2455 | break; |
| 2456 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2457 | case ImplicitConversionSequence::EllipsisConversion: |
| 2458 | case ImplicitConversionSequence::BadConversion: |
| 2459 | return false; |
| 2460 | } |
| 2461 | |
| 2462 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2463 | // [dcl.init.list]p7: |
| 2464 | // |
| 2465 | // A narrowing conversion is an implicit conversion ... |
| 2466 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2467 | QualType FromType = SCS->getToType(0); |
| 2468 | QualType ToType = SCS->getToType(1); |
| 2469 | switch (PossibleNarrowing) { |
| 2470 | // * from a floating-point type to an integer type, or |
| 2471 | // |
| 2472 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2473 | // type, except where the source is a constant expression and the actual |
| 2474 | // value after conversion will fit into the target type and will produce |
| 2475 | // the original value when converted back to the original type, or |
| 2476 | case ICK_Floating_Integral: |
| 2477 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2478 | *isInitializerConstant = false; |
| 2479 | return true; |
| 2480 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2481 | llvm::APSInt IntConstantValue; |
| 2482 | if (Initializer && |
| 2483 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2484 | // Convert the integer to the floating type. |
| 2485 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2486 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2487 | llvm::APFloat::rmNearestTiesToEven); |
| 2488 | // And back. |
| 2489 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2490 | bool ignored; |
| 2491 | Result.convertToInteger(ConvertedValue, |
| 2492 | llvm::APFloat::rmTowardZero, &ignored); |
| 2493 | // If the resulting value is different, this was a narrowing conversion. |
| 2494 | if (IntConstantValue != ConvertedValue) { |
| 2495 | *isInitializerConstant = true; |
| 2496 | *ConstantValue = APValue(IntConstantValue); |
| 2497 | return true; |
| 2498 | } |
| 2499 | } else { |
| 2500 | // Variables are always narrowings. |
| 2501 | *isInitializerConstant = false; |
| 2502 | return true; |
| 2503 | } |
| 2504 | } |
| 2505 | return false; |
| 2506 | |
| 2507 | // * from long double to double or float, or from double to float, except |
| 2508 | // where the source is a constant expression and the actual value after |
| 2509 | // conversion is within the range of values that can be represented (even |
| 2510 | // if it cannot be represented exactly), or |
| 2511 | case ICK_Floating_Conversion: |
| 2512 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2513 | // FromType is larger than ToType. |
| 2514 | Expr::EvalResult InitializerValue; |
| 2515 | // FIXME: Check whether Initializer is a constant expression according |
| 2516 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2517 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2518 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2519 | // Constant! (Except for FIXME above.) |
| 2520 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2521 | // Convert the source value into the target type. |
| 2522 | bool ignored; |
| 2523 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2524 | Ctx.getFloatTypeSemantics(ToType), |
| 2525 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2526 | // If there was no overflow, the source value is within the range of |
| 2527 | // values that can be represented. |
| 2528 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2529 | *isInitializerConstant = true; |
| 2530 | *ConstantValue = InitializerValue.Val; |
| 2531 | return true; |
| 2532 | } |
| 2533 | } else { |
| 2534 | *isInitializerConstant = false; |
| 2535 | return true; |
| 2536 | } |
| 2537 | } |
| 2538 | return false; |
| 2539 | |
| 2540 | // * from an integer type or unscoped enumeration type to an integer type |
| 2541 | // that cannot represent all the values of the original type, except where |
| 2542 | // the source is a constant expression and the actual value after |
| 2543 | // conversion will fit into the target type and will produce the original |
| 2544 | // value when converted back to the original type. |
Jeffrey Yasskin | 94f8c77 | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2545 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | 9242558 | 2011-08-30 22:25:41 +0000 | [diff] [blame] | 2546 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 2547 | // Boolean conversions can be from pointers and pointers to members |
| 2548 | // [conv.bool], and those aren't considered narrowing conversions. |
| 2549 | return false; |
| 2550 | } // Otherwise, fall through to the integral case. |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2551 | case ICK_Integral_Conversion: { |
| 2552 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2553 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2554 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2555 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2556 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2557 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2558 | |
| 2559 | if (FromWidth > ToWidth || |
| 2560 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2561 | // Not all values of FromType can be represented in ToType. |
| 2562 | llvm::APSInt InitializerValue; |
| 2563 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2564 | *isInitializerConstant = true; |
| 2565 | *ConstantValue = APValue(InitializerValue); |
| 2566 | |
| 2567 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2568 | // signed vs. unsigned comparisons. |
| 2569 | InitializerValue = InitializerValue.extend( |
| 2570 | InitializerValue.getBitWidth() + 1); |
| 2571 | // Convert the initializer to and from the target width and signed-ness. |
| 2572 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2573 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2574 | ConvertedValue.setIsSigned(ToSigned); |
| 2575 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2576 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2577 | // If the result is different, this was a narrowing conversion. |
| 2578 | return ConvertedValue != InitializerValue; |
| 2579 | } else { |
| 2580 | // Variables are always narrowings. |
| 2581 | *isInitializerConstant = false; |
| 2582 | return true; |
| 2583 | } |
| 2584 | } |
| 2585 | return false; |
| 2586 | } |
| 2587 | |
| 2588 | default: |
| 2589 | // Other kinds of conversions are not narrowings. |
| 2590 | return false; |
| 2591 | } |
| 2592 | } |
| 2593 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2594 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2595 | FunctionDecl *Function, |
| 2596 | DeclAccessPair Found) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2597 | Step S; |
| 2598 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2599 | S.Type = Function->getType(); |
Benjamin Kramer | ec44099 | 2011-10-09 17:58:25 +0000 | [diff] [blame] | 2600 | S.Function.HadMultipleCandidates = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2601 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2602 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2603 | Steps.push_back(S); |
| 2604 | } |
| 2605 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2606 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2607 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2608 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2609 | switch (VK) { |
| 2610 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2611 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2612 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2613 | default: llvm_unreachable("No such category"); |
| 2614 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2615 | S.Type = BaseType; |
| 2616 | Steps.push_back(S); |
| 2617 | } |
| 2618 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2619 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2620 | bool BindingTemporary) { |
| 2621 | Step S; |
| 2622 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2623 | S.Type = T; |
| 2624 | Steps.push_back(S); |
| 2625 | } |
| 2626 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2627 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2628 | Step S; |
| 2629 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2630 | S.Type = T; |
| 2631 | Steps.push_back(S); |
| 2632 | } |
| 2633 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2634 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2635 | DeclAccessPair FoundDecl, |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2636 | QualType T) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2637 | Step S; |
| 2638 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2639 | S.Type = T; |
Benjamin Kramer | ec44099 | 2011-10-09 17:58:25 +0000 | [diff] [blame] | 2640 | S.Function.HadMultipleCandidates = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2641 | S.Function.Function = Function; |
| 2642 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2643 | Steps.push_back(S); |
| 2644 | } |
| 2645 | |
| 2646 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2647 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2648 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2649 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2650 | switch (VK) { |
| 2651 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2652 | S.Kind = SK_QualificationConversionRValue; |
| 2653 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2654 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2655 | S.Kind = SK_QualificationConversionXValue; |
| 2656 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2657 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2658 | S.Kind = SK_QualificationConversionLValue; |
| 2659 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2660 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2661 | S.Type = Ty; |
| 2662 | Steps.push_back(S); |
| 2663 | } |
| 2664 | |
| 2665 | void InitializationSequence::AddConversionSequenceStep( |
| 2666 | const ImplicitConversionSequence &ICS, |
| 2667 | QualType T) { |
| 2668 | Step S; |
| 2669 | S.Kind = SK_ConversionSequence; |
| 2670 | S.Type = T; |
| 2671 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2672 | Steps.push_back(S); |
| 2673 | } |
| 2674 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2675 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2676 | Step S; |
| 2677 | S.Kind = SK_ListInitialization; |
| 2678 | S.Type = T; |
| 2679 | Steps.push_back(S); |
| 2680 | } |
| 2681 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2682 | void |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2683 | InitializationSequence::AddConstructorInitializationStep( |
| 2684 | CXXConstructorDecl *Constructor, |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2685 | AccessSpecifier Access, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2686 | QualType T) { |
| 2687 | Step S; |
| 2688 | S.Kind = SK_ConstructorInitialization; |
| 2689 | S.Type = T; |
Benjamin Kramer | ec44099 | 2011-10-09 17:58:25 +0000 | [diff] [blame] | 2690 | S.Function.HadMultipleCandidates = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2691 | S.Function.Function = Constructor; |
| 2692 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2693 | Steps.push_back(S); |
| 2694 | } |
| 2695 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2696 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2697 | Step S; |
| 2698 | S.Kind = SK_ZeroInitialization; |
| 2699 | S.Type = T; |
| 2700 | Steps.push_back(S); |
| 2701 | } |
| 2702 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2703 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2704 | Step S; |
| 2705 | S.Kind = SK_CAssignment; |
| 2706 | S.Type = T; |
| 2707 | Steps.push_back(S); |
| 2708 | } |
| 2709 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2710 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2711 | Step S; |
| 2712 | S.Kind = SK_StringInit; |
| 2713 | S.Type = T; |
| 2714 | Steps.push_back(S); |
| 2715 | } |
| 2716 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2717 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2718 | Step S; |
| 2719 | S.Kind = SK_ObjCObjectConversion; |
| 2720 | S.Type = T; |
| 2721 | Steps.push_back(S); |
| 2722 | } |
| 2723 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2724 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2725 | Step S; |
| 2726 | S.Kind = SK_ArrayInit; |
| 2727 | S.Type = T; |
| 2728 | Steps.push_back(S); |
| 2729 | } |
| 2730 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2731 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2732 | bool shouldCopy) { |
| 2733 | Step s; |
| 2734 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2735 | : SK_PassByIndirectRestore); |
| 2736 | s.Type = type; |
| 2737 | Steps.push_back(s); |
| 2738 | } |
| 2739 | |
| 2740 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2741 | Step S; |
| 2742 | S.Kind = SK_ProduceObjCObject; |
| 2743 | S.Type = T; |
| 2744 | Steps.push_back(S); |
| 2745 | } |
| 2746 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2747 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2748 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2749 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2750 | this->Failure = Failure; |
| 2751 | this->FailedOverloadResult = Result; |
| 2752 | } |
| 2753 | |
| 2754 | //===----------------------------------------------------------------------===// |
| 2755 | // Attempt initialization |
| 2756 | //===----------------------------------------------------------------------===// |
| 2757 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2758 | static void MaybeProduceObjCObject(Sema &S, |
| 2759 | InitializationSequence &Sequence, |
| 2760 | const InitializedEntity &Entity) { |
| 2761 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2762 | |
| 2763 | /// When initializing a parameter, produce the value if it's marked |
| 2764 | /// __attribute__((ns_consumed)). |
| 2765 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2766 | if (!Entity.isParameterConsumed()) |
| 2767 | return; |
| 2768 | |
| 2769 | assert(Entity.getType()->isObjCRetainableType() && |
| 2770 | "consuming an object of unretainable type?"); |
| 2771 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2772 | |
| 2773 | /// When initializing a return value, if the return type is a |
| 2774 | /// retainable type, then returns need to immediately retain the |
| 2775 | /// object. If an autorelease is required, it will be done at the |
| 2776 | /// last instant. |
| 2777 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2778 | if (!Entity.getType()->isObjCRetainableType()) |
| 2779 | return; |
| 2780 | |
| 2781 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2782 | } |
| 2783 | } |
| 2784 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2785 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2786 | static void TryListInitialization(Sema &S, |
| 2787 | const InitializedEntity &Entity, |
| 2788 | const InitializationKind &Kind, |
| 2789 | InitListExpr *InitList, |
| 2790 | InitializationSequence &Sequence) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2791 | QualType DestType = Entity.getType(); |
| 2792 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2793 | // C++ doesn't allow scalar initialization with more than one argument. |
| 2794 | // But C99 complex numbers are scalars and it makes sense there. |
| 2795 | if (S.getLangOptions().CPlusPlus && DestType->isScalarType() && |
| 2796 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 2797 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2798 | return; |
| 2799 | } |
| 2800 | // FIXME: C++0x defines behavior for these two cases. |
| 2801 | if (DestType->isReferenceType()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2802 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2803 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2804 | } |
| 2805 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2806 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2807 | return; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2810 | InitListChecker CheckInitList(S, Entity, InitList, |
| 2811 | DestType, /*VerifyOnly=*/true); |
| 2812 | if (CheckInitList.HadError()) { |
| 2813 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 2814 | return; |
| 2815 | } |
| 2816 | |
| 2817 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2818 | Sequence.AddListInitializationStep(DestType); |
| 2819 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2820 | |
| 2821 | /// \brief Try a reference initialization that involves calling a conversion |
| 2822 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2823 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2824 | const InitializedEntity &Entity, |
| 2825 | const InitializationKind &Kind, |
| 2826 | Expr *Initializer, |
| 2827 | bool AllowRValues, |
| 2828 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2829 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2830 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2831 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2832 | QualType cv2T2 = Initializer->getType(); |
| 2833 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2834 | |
| 2835 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2836 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2837 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2838 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2839 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2840 | ObjCConversion, |
| 2841 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2842 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2843 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2844 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2845 | (void)ObjCLifetimeConversion; |
| 2846 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2847 | // Build the candidate set directly in the initialization sequence |
| 2848 | // structure, so that it will persist if we fail. |
| 2849 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2850 | CandidateSet.clear(); |
| 2851 | |
| 2852 | // Determine whether we are allowed to call explicit constructors or |
| 2853 | // explicit conversion operators. |
| 2854 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2856 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2857 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2858 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2859 | // The type we're converting to is a class type. Enumerate its constructors |
| 2860 | // to see if there is a suitable conversion. |
| 2861 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2863 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2864 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2865 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2866 | NamedDecl *D = *Con; |
| 2867 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2868 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2869 | // Find the constructor (which may be a template). |
| 2870 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2871 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2872 | if (ConstructorTmpl) |
| 2873 | Constructor = cast<CXXConstructorDecl>( |
| 2874 | ConstructorTmpl->getTemplatedDecl()); |
| 2875 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2876 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2877 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2878 | if (!Constructor->isInvalidDecl() && |
| 2879 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2880 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2881 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2882 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2883 | &Initializer, 1, CandidateSet, |
| 2884 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2885 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2886 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2887 | &Initializer, 1, CandidateSet, |
| 2888 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2889 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2890 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2891 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2892 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2893 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2895 | const RecordType *T2RecordType = 0; |
| 2896 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2897 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2898 | // The type we're converting from is a class type, enumerate its conversion |
| 2899 | // functions. |
| 2900 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2901 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2902 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2903 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2904 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2905 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2906 | NamedDecl *D = *I; |
| 2907 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2908 | if (isa<UsingShadowDecl>(D)) |
| 2909 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2910 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2911 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2912 | CXXConversionDecl *Conv; |
| 2913 | if (ConvTemplate) |
| 2914 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2915 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2916 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2917 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2918 | // If the conversion function doesn't return a reference type, |
| 2919 | // it can't be considered for this conversion unless we're allowed to |
| 2920 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2921 | // FIXME: Do we need to make sure that we only consider conversion |
| 2922 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2923 | // break recursion. |
| 2924 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2925 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2926 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2927 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2928 | ActingDC, Initializer, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2929 | DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2930 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2931 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2932 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2933 | } |
| 2934 | } |
| 2935 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2936 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2937 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2939 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2940 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2941 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2942 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2943 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2944 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2945 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2946 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2947 | FunctionDecl *Function = Best->Function; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2948 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2949 | // This is the overload that will actually be used for the initialization, so |
| 2950 | // mark it as used. |
| 2951 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2952 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2953 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2954 | if (isa<CXXConversionDecl>(Function)) |
| 2955 | T2 = Function->getResultType(); |
| 2956 | else |
| 2957 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2958 | |
| 2959 | // Add the user-defined conversion step. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2960 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2961 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2962 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2963 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2964 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2965 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2966 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2967 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2968 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2969 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2971 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2972 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2973 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2974 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2975 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2976 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2977 | NewDerivedToBase, NewObjCConversion, |
| 2978 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2979 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2980 | // If the type we've converted to is not reference-related to the |
| 2981 | // type we're looking for, then there is another conversion step |
| 2982 | // we need to perform to produce a temporary of the right type |
| 2983 | // that we'll be binding to. |
| 2984 | ImplicitConversionSequence ICS; |
| 2985 | ICS.setStandard(); |
| 2986 | ICS.Standard = Best->FinalConversion; |
| 2987 | T2 = ICS.Standard.getToType(2); |
| 2988 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2989 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2990 | Sequence.AddDerivedToBaseCastStep( |
| 2991 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2992 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2993 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2994 | else if (NewObjCConversion) |
| 2995 | Sequence.AddObjCObjectConversionStep( |
| 2996 | S.Context.getQualifiedType(T1, |
| 2997 | T2.getNonReferenceType().getQualifiers())); |
| 2998 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2999 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3000 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3001 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3002 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3003 | return OR_Success; |
| 3004 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3005 | |
| 3006 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3007 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3008 | const InitializedEntity &Entity, |
| 3009 | const InitializationKind &Kind, |
| 3010 | Expr *Initializer, |
| 3011 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3012 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3013 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3014 | Qualifiers T1Quals; |
| 3015 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3016 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3017 | Qualifiers T2Quals; |
| 3018 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3019 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3020 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3021 | // If the initializer is the address of an overloaded function, try |
| 3022 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3023 | // type of the resulting function. |
| 3024 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3025 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3026 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3027 | T1, |
| 3028 | false, |
| 3029 | Found)) { |
| 3030 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 3031 | cv2T2 = Fn->getType(); |
| 3032 | T2 = cv2T2.getUnqualifiedType(); |
| 3033 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3034 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3035 | return; |
| 3036 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3037 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3038 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3039 | // Compute some basic properties of the types and the initializer. |
| 3040 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3041 | bool isRValueRef = !isLValueRef; |
| 3042 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3043 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3044 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3045 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3046 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3047 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3048 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3049 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3050 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3051 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3052 | // "cv2 T2" as follows: |
| 3053 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3054 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3055 | // expression |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3056 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3057 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3058 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3059 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3060 | bool T1Function = T1->isFunctionType(); |
| 3061 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3062 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3063 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3064 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3065 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3066 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3067 | // reference-compatible with "cv2 T2," or |
| 3068 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3069 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3070 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3071 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3072 | // to decide whether we're actually binding to a temporary created from |
| 3073 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3074 | if (DerivedToBase) |
| 3075 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3076 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3077 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3078 | else if (ObjCConversion) |
| 3079 | Sequence.AddObjCObjectConversionStep( |
| 3080 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3081 | |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3082 | if (T1Quals != T2Quals) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3083 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3084 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3085 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3086 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3087 | return; |
| 3088 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3089 | |
| 3090 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3091 | // reference-related to T2, and can be implicitly converted to an |
| 3092 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3093 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3094 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3095 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3096 | // If we have an rvalue ref to function type here, the rhs must be |
| 3097 | // an rvalue. |
| 3098 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3099 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3100 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3101 | Initializer, |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3102 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3103 | Sequence); |
| 3104 | if (ConvOvlResult == OR_Success) |
| 3105 | return; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3106 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3107 | Sequence.SetOverloadFailure( |
| 3108 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3109 | ConvOvlResult); |
| 3110 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3111 | } |
| 3112 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3113 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3114 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3115 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 7a2a116 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 3116 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3117 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3118 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3119 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3120 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3121 | Sequence.SetOverloadFailure( |
| 3122 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3123 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3124 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3125 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3126 | ? (RefRelationship == Sema::Ref_Related |
| 3127 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3128 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3129 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3130 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3131 | return; |
| 3132 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3133 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3134 | // - If the initializer expression |
| 3135 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3136 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3137 | // Note: functions are handled below. |
| 3138 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3139 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3140 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3141 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3142 | (InitCategory.isXValue() || |
| 3143 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3144 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3145 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3146 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3147 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3148 | // compiler the freedom to perform a copy here or bind to the |
| 3149 | // object, while C++0x requires that we bind directly to the |
| 3150 | // object. Hence, we always bind to the object without making an |
| 3151 | // extra copy. However, in C++03 requires that we check for the |
| 3152 | // presence of a suitable copy constructor: |
| 3153 | // |
| 3154 | // The constructor that would be used to make the copy shall |
| 3155 | // be callable whether or not the copy is actually done. |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3156 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3157 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3158 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3160 | if (DerivedToBase) |
| 3161 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3162 | ValueKind); |
| 3163 | else if (ObjCConversion) |
| 3164 | Sequence.AddObjCObjectConversionStep( |
| 3165 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3166 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3167 | if (T1Quals != T2Quals) |
| 3168 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3169 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3170 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3171 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3172 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3173 | |
| 3174 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3175 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3176 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3177 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3178 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3179 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3180 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3181 | Kind, Initializer, |
| 3182 | /*AllowRValues=*/true, |
| 3183 | Sequence); |
| 3184 | if (ConvOvlResult) |
| 3185 | Sequence.SetOverloadFailure( |
| 3186 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3187 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3188 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3189 | return; |
| 3190 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3191 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3192 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3193 | return; |
| 3194 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3195 | |
| 3196 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3197 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3198 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3199 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3201 | // Determine whether we are allowed to call explicit constructors or |
| 3202 | // explicit conversion operators. |
| 3203 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3204 | |
| 3205 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3206 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3207 | ImplicitConversionSequence ICS |
| 3208 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3209 | /*SuppressUserConversions*/ false, |
| 3210 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3211 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3212 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3213 | /*AllowObjCWritebackConversion=*/false); |
| 3214 | |
| 3215 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3216 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3217 | // this into an overloading ambiguity diagnostic. However, we need |
| 3218 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3219 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3220 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3221 | Sequence.SetOverloadFailure( |
| 3222 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3223 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3224 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3225 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3226 | else |
| 3227 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3228 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3229 | } else { |
| 3230 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3234 | // same cv-qualification as, or greater cv-qualification |
| 3235 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3236 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3237 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3238 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3239 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3240 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3241 | return; |
| 3242 | } |
| 3243 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3244 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3245 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3246 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3247 | InitCategory.isLValue()) { |
| 3248 | Sequence.SetFailed( |
| 3249 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3250 | return; |
| 3251 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3252 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3253 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3254 | return; |
| 3255 | } |
| 3256 | |
| 3257 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3258 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3259 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3260 | const InitializedEntity &Entity, |
| 3261 | const InitializationKind &Kind, |
| 3262 | Expr *Initializer, |
| 3263 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3264 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3267 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3268 | /// enumerates the constructors of the initialized entity and performs overload |
| 3269 | /// resolution to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3270 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3271 | const InitializedEntity &Entity, |
| 3272 | const InitializationKind &Kind, |
| 3273 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3274 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3275 | InitializationSequence &Sequence) { |
Richard Trieu | a04ad1a | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 3276 | // Check constructor arguments for self reference. |
| 3277 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 3278 | // Parameters arguments are occassionially constructed with itself, |
| 3279 | // for instance, in recursive functions. Skip them. |
| 3280 | if (!isa<ParmVarDecl>(DD)) |
| 3281 | for (unsigned i = 0; i < NumArgs; ++i) |
| 3282 | S.CheckSelfReference(DD, Args[i]); |
| 3283 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3284 | // Build the candidate set directly in the initialization sequence |
| 3285 | // structure, so that it will persist if we fail. |
| 3286 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3287 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3289 | // Determine whether we are allowed to call explicit constructors or |
| 3290 | // explicit conversion operators. |
| 3291 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3292 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3293 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3294 | |
| 3295 | // The type we're constructing needs to be complete. |
| 3296 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3297 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3298 | return; |
| 3299 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3301 | // The type we're converting to is a class type. Enumerate its constructors |
| 3302 | // to see if one is suitable. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3303 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3304 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3305 | CXXRecordDecl *DestRecordDecl |
| 3306 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3307 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3308 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3309 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3310 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3311 | NamedDecl *D = *Con; |
| 3312 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3313 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3314 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3315 | // Find the constructor (which may be a template). |
| 3316 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3317 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3318 | if (ConstructorTmpl) |
| 3319 | Constructor = cast<CXXConstructorDecl>( |
| 3320 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3321 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3322 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3323 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3325 | // suppress user-defined conversions on the arguments. |
| 3326 | // FIXME: Move constructors? |
| 3327 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3328 | Constructor->isCopyConstructor()) |
| 3329 | SuppressUserConversions = true; |
| 3330 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3332 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3333 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3334 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3335 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3336 | /*ExplicitArgs*/ 0, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3337 | Args, NumArgs, CandidateSet, |
| 3338 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3339 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3340 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3341 | Args, NumArgs, CandidateSet, |
| 3342 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3343 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3346 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3347 | |
| 3348 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3349 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3350 | if (OverloadingResult Result |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3351 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3352 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3353 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3354 | Result); |
| 3355 | return; |
| 3356 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3357 | |
| 3358 | // C++0x [dcl.init]p6: |
| 3359 | // If a program calls for the default initialization of an object |
| 3360 | // of a const-qualified type T, T shall be a class type with a |
| 3361 | // user-provided default constructor. |
| 3362 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3363 | Entity.getType().isConstQualified() && |
| 3364 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3365 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3366 | return; |
| 3367 | } |
| 3368 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3369 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3370 | // subsumed by the initialization. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3371 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3372 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3373 | Best->FoundDecl.getAccess(), |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3374 | DestType); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3377 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3378 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3379 | const InitializedEntity &Entity, |
| 3380 | const InitializationKind &Kind, |
| 3381 | InitializationSequence &Sequence) { |
| 3382 | // C++ [dcl.init]p5: |
| 3383 | // |
| 3384 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3385 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3387 | // -- if T is an array type, then each element is value-initialized; |
| 3388 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3389 | T = AT->getElementType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3391 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3392 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3393 | // -- if T is a class type (clause 9) with a user-declared |
| 3394 | // constructor (12.1), then the default constructor for T is |
| 3395 | // called (and the initialization is ill-formed if T has no |
| 3396 | // accessible default constructor); |
| 3397 | // |
| 3398 | // FIXME: we really want to refer to a single subobject of the array, |
| 3399 | // but Entity doesn't have a way to capture that (yet). |
| 3400 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3401 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3402 | |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3403 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3404 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3405 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3406 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3407 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3408 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3409 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3410 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3411 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3412 | } |
| 3413 | } |
| 3414 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3415 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3418 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3419 | static void TryDefaultInitialization(Sema &S, |
| 3420 | const InitializedEntity &Entity, |
| 3421 | const InitializationKind &Kind, |
| 3422 | InitializationSequence &Sequence) { |
| 3423 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3424 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3425 | // C++ [dcl.init]p6: |
| 3426 | // To default-initialize an object of type T means: |
| 3427 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3428 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3429 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3430 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3431 | // constructor for T is called (and the initialization is ill-formed if |
| 3432 | // T has no accessible default constructor); |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3433 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3434 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3435 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3436 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3438 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3440 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3441 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3442 | // default constructor. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3443 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3444 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3445 | return; |
| 3446 | } |
| 3447 | |
| 3448 | // If the destination type has a lifetime property, zero-initialize it. |
| 3449 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3450 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3451 | return; |
| 3452 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3455 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3456 | /// which enumerates all conversion functions and performs overload resolution |
| 3457 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3458 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3459 | const InitializedEntity &Entity, |
| 3460 | const InitializationKind &Kind, |
| 3461 | Expr *Initializer, |
| 3462 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3463 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3464 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3465 | QualType SourceType = Initializer->getType(); |
| 3466 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3467 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3469 | // Build the candidate set directly in the initialization sequence |
| 3470 | // structure, so that it will persist if we fail. |
| 3471 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3472 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3473 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3474 | // Determine whether we are allowed to call explicit constructors or |
| 3475 | // explicit conversion operators. |
| 3476 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3477 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3478 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3479 | // The type we're converting to is a class type. Enumerate its constructors |
| 3480 | // to see if there is a suitable conversion. |
| 3481 | CXXRecordDecl *DestRecordDecl |
| 3482 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3483 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3484 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3485 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3486 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3487 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3488 | Con != ConEnd; ++Con) { |
| 3489 | NamedDecl *D = *Con; |
| 3490 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3492 | // Find the constructor (which may be a template). |
| 3493 | CXXConstructorDecl *Constructor = 0; |
| 3494 | FunctionTemplateDecl *ConstructorTmpl |
| 3495 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3496 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3497 | Constructor = cast<CXXConstructorDecl>( |
| 3498 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3499 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3500 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3502 | if (!Constructor->isInvalidDecl() && |
| 3503 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3504 | if (ConstructorTmpl) |
| 3505 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3506 | /*ExplicitArgs*/ 0, |
| 3507 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3508 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3509 | else |
| 3510 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3511 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3512 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3513 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3514 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3515 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3516 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3517 | |
| 3518 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3519 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3520 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3521 | // The type we're converting from is a class type, enumerate its conversion |
| 3522 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3523 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3524 | // We can only enumerate the conversion functions for a complete type; if |
| 3525 | // the type isn't complete, simply skip this step. |
| 3526 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3527 | CXXRecordDecl *SourceRecordDecl |
| 3528 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3529 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3530 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3531 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3532 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3533 | E = Conversions->end(); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3534 | I != E; ++I) { |
| 3535 | NamedDecl *D = *I; |
| 3536 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3537 | if (isa<UsingShadowDecl>(D)) |
| 3538 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3539 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3540 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3541 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3542 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3543 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3544 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3545 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3547 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3548 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3549 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3550 | ActingDC, Initializer, DestType, |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3551 | CandidateSet); |
| 3552 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3553 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3554 | Initializer, DestType, CandidateSet); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3555 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | |
| 3560 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3561 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3562 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3563 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3564 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3565 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3566 | Result); |
| 3567 | return; |
| 3568 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3569 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3570 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3571 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3573 | if (isa<CXXConstructorDecl>(Function)) { |
| 3574 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3575 | // subsumed by the initialization. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3576 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3577 | return; |
| 3578 | } |
| 3579 | |
| 3580 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3581 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3582 | if (ConvType->getAs<RecordType>()) { |
| 3583 | // If we're converting to a class type, there may be an copy if |
| 3584 | // the resulting temporary object (possible to create an object of |
| 3585 | // a base class type). That copy is not a separate conversion, so |
| 3586 | // we just make a note of the actual destination type (possibly a |
| 3587 | // base class of the type returned by the conversion function) and |
| 3588 | // let the user-defined conversion step handle the conversion. |
| 3589 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3590 | return; |
| 3591 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3593 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3594 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3595 | // If the conversion following the call to the conversion function |
| 3596 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3597 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3598 | Best->FinalConversion.Third) { |
| 3599 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3600 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3601 | ICS.Standard = Best->FinalConversion; |
| 3602 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3603 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3606 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3607 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3608 | |
| 3609 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3610 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3611 | bool isAddressOf) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3612 | // Skip parens. |
| 3613 | e = e->IgnoreParens(); |
| 3614 | |
| 3615 | // Skip address-of nodes. |
| 3616 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3617 | if (op->getOpcode() == UO_AddrOf) |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3618 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3619 | |
| 3620 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3621 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3622 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3623 | case CK_Dependent: |
| 3624 | case CK_BitCast: |
| 3625 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3626 | case CK_NoOp: |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3627 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3628 | |
| 3629 | case CK_ArrayToPointerDecay: |
| 3630 | return IIK_nonscalar; |
| 3631 | |
| 3632 | case CK_NullToPointer: |
| 3633 | return IIK_okay; |
| 3634 | |
| 3635 | default: |
| 3636 | break; |
| 3637 | } |
| 3638 | |
| 3639 | // If we have a declaration reference, it had better be a local variable. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3640 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3641 | if (!isAddressOf) return IIK_nonlocal; |
| 3642 | |
| 3643 | VarDecl *var; |
| 3644 | if (isa<DeclRefExpr>(e)) { |
| 3645 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3646 | if (!var) return IIK_nonlocal; |
| 3647 | } else { |
| 3648 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3649 | } |
| 3650 | |
| 3651 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3652 | |
| 3653 | // If we have a conditional operator, check both sides. |
| 3654 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3655 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3656 | return iik; |
| 3657 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3658 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3659 | |
| 3660 | // These are never scalar. |
| 3661 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3662 | return IIK_nonscalar; |
| 3663 | |
| 3664 | // Otherwise, it needs to be a null pointer constant. |
| 3665 | } else { |
| 3666 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3667 | ? IIK_okay : IIK_nonlocal); |
| 3668 | } |
| 3669 | |
| 3670 | return IIK_nonlocal; |
| 3671 | } |
| 3672 | |
| 3673 | /// Check whether the given expression is a valid operand for an |
| 3674 | /// indirect copy/restore. |
| 3675 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3676 | assert(src->isRValue()); |
| 3677 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3678 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3679 | if (iik == IIK_okay) return; |
| 3680 | |
| 3681 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3682 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3683 | << src->getSourceRange(); |
| 3684 | } |
| 3685 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3686 | /// \brief Determine whether we have compatible array types for the |
| 3687 | /// purposes of GNU by-copy array initialization. |
| 3688 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3689 | const ArrayType *Dest, |
| 3690 | const ArrayType *Source) { |
| 3691 | // If the source and destination array types are equivalent, we're |
| 3692 | // done. |
| 3693 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3694 | return true; |
| 3695 | |
| 3696 | // Make sure that the element types are the same. |
| 3697 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3698 | return false; |
| 3699 | |
| 3700 | // The only mismatch we allow is when the destination is an |
| 3701 | // incomplete array type and the source is a constant array type. |
| 3702 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3703 | } |
| 3704 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3705 | static bool tryObjCWritebackConversion(Sema &S, |
| 3706 | InitializationSequence &Sequence, |
| 3707 | const InitializedEntity &Entity, |
| 3708 | Expr *Initializer) { |
| 3709 | bool ArrayDecay = false; |
| 3710 | QualType ArgType = Initializer->getType(); |
| 3711 | QualType ArgPointee; |
| 3712 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3713 | ArrayDecay = true; |
| 3714 | ArgPointee = ArgArrayType->getElementType(); |
| 3715 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3716 | } |
| 3717 | |
| 3718 | // Handle write-back conversion. |
| 3719 | QualType ConvertedArgType; |
| 3720 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3721 | ConvertedArgType)) |
| 3722 | return false; |
| 3723 | |
| 3724 | // We should copy unless we're passing to an argument explicitly |
| 3725 | // marked 'out'. |
| 3726 | bool ShouldCopy = true; |
| 3727 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3728 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3729 | |
| 3730 | // Do we need an lvalue conversion? |
| 3731 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3732 | ImplicitConversionSequence ICS; |
| 3733 | ICS.setStandard(); |
| 3734 | ICS.Standard.setAsIdentityConversion(); |
| 3735 | |
| 3736 | QualType ResultType; |
| 3737 | if (ArrayDecay) { |
| 3738 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3739 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3740 | } else { |
| 3741 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3742 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3743 | } |
| 3744 | |
| 3745 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3746 | } |
| 3747 | |
| 3748 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3749 | return true; |
| 3750 | } |
| 3751 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3752 | InitializationSequence::InitializationSequence(Sema &S, |
| 3753 | const InitializedEntity &Entity, |
| 3754 | const InitializationKind &Kind, |
| 3755 | Expr **Args, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3756 | unsigned NumArgs) |
| 3757 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3758 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3759 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3760 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3761 | // The semantics of initializers are as follows. The destination type is |
| 3762 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3763 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3764 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3765 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3766 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3767 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3768 | if (DestType->isDependentType() || |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3769 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3770 | SequenceKind = DependentSequence; |
| 3771 | return; |
| 3772 | } |
| 3773 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3774 | // Almost everything is a normal sequence. |
| 3775 | setSequenceKind(NormalSequence); |
| 3776 | |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3777 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3778 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3779 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3780 | if (Result.isInvalid()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3781 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3782 | return; |
| 3783 | } |
| 3784 | Args[I] = Result.take(); |
| 3785 | } |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3786 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3787 | QualType SourceType; |
| 3788 | Expr *Initializer = 0; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3789 | if (NumArgs == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3790 | Initializer = Args[0]; |
| 3791 | if (!isa<InitListExpr>(Initializer)) |
| 3792 | SourceType = Initializer->getType(); |
| 3793 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3794 | |
| 3795 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3796 | // list-initialized (8.5.4). |
| 3797 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3798 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3799 | return; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3800 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3802 | // - If the destination type is a reference type, see 8.5.3. |
| 3803 | if (DestType->isReferenceType()) { |
| 3804 | // C++0x [dcl.init.ref]p1: |
| 3805 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3806 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3807 | // by an object that can be converted into a T. |
| 3808 | // (Therefore, multiple arguments are not permitted.) |
| 3809 | if (NumArgs != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3810 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3811 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3812 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3813 | return; |
| 3814 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3815 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3816 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3817 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3818 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3819 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3820 | return; |
| 3821 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3822 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3823 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3824 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3825 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3826 | return; |
| 3827 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3828 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3829 | // - If the destination type is an array of characters, an array of |
| 3830 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3831 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3832 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3833 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3834 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3835 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3836 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3837 | return; |
| 3838 | } |
| 3839 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3840 | // Note: as an GNU C extension, we allow initialization of an |
| 3841 | // array from a compound literal that creates an array of the same |
| 3842 | // type, so long as the initializer has no side effects. |
| 3843 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3844 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3845 | Initializer->getType()->isArrayType()) { |
| 3846 | const ArrayType *SourceAT |
| 3847 | = Context.getAsArrayType(Initializer->getType()); |
| 3848 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3849 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3850 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3851 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3852 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3853 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3854 | } |
| 3855 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3856 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3857 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3858 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3859 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3860 | return; |
| 3861 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3862 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3863 | // Determine whether we should consider writeback conversions for |
| 3864 | // Objective-C ARC. |
| 3865 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3866 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3867 | |
| 3868 | // We're at the end of the line for C: it's either a write-back conversion |
| 3869 | // or it's a C assignment. There's no need to check anything else. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3870 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3871 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3872 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3873 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3874 | return; |
| 3875 | } |
| 3876 | |
| 3877 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3878 | AddCAssignmentStep(DestType); |
| 3879 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3880 | return; |
| 3881 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3883 | assert(S.getLangOptions().CPlusPlus); |
| 3884 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3885 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3886 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3887 | // - If the initialization is direct-initialization, or if it is |
| 3888 | // copy-initialization where the cv-unqualified version of the |
| 3889 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3890 | // class of the destination, constructors are considered. [...] |
| 3891 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3892 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3893 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3894 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3895 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3896 | Entity.getType(), *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3898 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3900 | // used) to a derived class thereof are enumerated as described in |
| 3901 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3902 | // (13.3). |
| 3903 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3904 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3905 | return; |
| 3906 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3907 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3908 | if (NumArgs > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3909 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3910 | return; |
| 3911 | } |
| 3912 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3913 | |
| 3914 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3915 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3916 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3917 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3918 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3919 | return; |
| 3920 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3921 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3922 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3923 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3924 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3925 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3926 | // destination type; no user-defined conversions are considered. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3927 | |
| 3928 | ImplicitConversionSequence ICS |
| 3929 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3930 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3931 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3932 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3933 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3934 | allowObjCWritebackConversion); |
| 3935 | |
| 3936 | if (ICS.isStandard() && |
| 3937 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3938 | // Objective-C ARC writeback conversion. |
| 3939 | |
| 3940 | // We should copy unless we're passing to an argument explicitly |
| 3941 | // marked 'out'. |
| 3942 | bool ShouldCopy = true; |
| 3943 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3944 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3945 | |
| 3946 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3947 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3948 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3949 | ImplicitConversionSequence LvalueICS; |
| 3950 | LvalueICS.setStandard(); |
| 3951 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3952 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3953 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3954 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3955 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3956 | |
| 3957 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3958 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3959 | DeclAccessPair dap; |
| 3960 | if (Initializer->getType() == Context.OverloadTy && |
| 3961 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3962 | , DestType, false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3963 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3964 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3965 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3966 | } else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3967 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3968 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3969 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3970 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3974 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3975 | StepEnd = Steps.end(); |
| 3976 | Step != StepEnd; ++Step) |
| 3977 | Step->Destroy(); |
| 3978 | } |
| 3979 | |
| 3980 | //===----------------------------------------------------------------------===// |
| 3981 | // Perform initialization |
| 3982 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3983 | static Sema::AssignmentAction |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3984 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3985 | switch(Entity.getKind()) { |
| 3986 | case InitializedEntity::EK_Variable: |
| 3987 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3988 | case InitializedEntity::EK_Exception: |
| 3989 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3990 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3991 | return Sema::AA_Initializing; |
| 3992 | |
| 3993 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3994 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3995 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3996 | return Sema::AA_Sending; |
| 3997 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3998 | return Sema::AA_Passing; |
| 3999 | |
| 4000 | case InitializedEntity::EK_Result: |
| 4001 | return Sema::AA_Returning; |
| 4002 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4003 | case InitializedEntity::EK_Temporary: |
| 4004 | // FIXME: Can we tell apart casting vs. converting? |
| 4005 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4006 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4007 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4008 | case InitializedEntity::EK_ArrayElement: |
| 4009 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4010 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4011 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4012 | return Sema::AA_Initializing; |
| 4013 | } |
| 4014 | |
| 4015 | return Sema::AA_Converting; |
| 4016 | } |
| 4017 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4018 | /// \brief Whether we should binding a created object as a temporary when |
| 4019 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4020 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4021 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4022 | case InitializedEntity::EK_ArrayElement: |
| 4023 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4024 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4025 | case InitializedEntity::EK_New: |
| 4026 | case InitializedEntity::EK_Variable: |
| 4027 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4028 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4029 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4030 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4031 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4032 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4033 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4034 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4035 | case InitializedEntity::EK_Parameter: |
| 4036 | case InitializedEntity::EK_Temporary: |
| 4037 | return true; |
| 4038 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4039 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4040 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4041 | } |
| 4042 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4043 | /// \brief Whether the given entity, when initialized with an object |
| 4044 | /// created for that initialization, requires destruction. |
| 4045 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4046 | switch (Entity.getKind()) { |
| 4047 | case InitializedEntity::EK_Member: |
| 4048 | case InitializedEntity::EK_Result: |
| 4049 | case InitializedEntity::EK_New: |
| 4050 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4051 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4052 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4053 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4054 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4055 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4056 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4057 | case InitializedEntity::EK_Variable: |
| 4058 | case InitializedEntity::EK_Parameter: |
| 4059 | case InitializedEntity::EK_Temporary: |
| 4060 | case InitializedEntity::EK_ArrayElement: |
| 4061 | case InitializedEntity::EK_Exception: |
| 4062 | return true; |
| 4063 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | |
| 4065 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4068 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4069 | /// provided by the given initializer by calling the appropriate copy |
| 4070 | /// constructor. |
| 4071 | /// |
| 4072 | /// \param S The Sema object used for type-checking. |
| 4073 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4074 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4075 | /// the type of the initializer expression or a superclass thereof. |
| 4076 | /// |
| 4077 | /// \param Enter The entity being initialized. |
| 4078 | /// |
| 4079 | /// \param CurInit The initializer expression. |
| 4080 | /// |
| 4081 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4082 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4083 | /// an rvalue. |
| 4084 | /// |
| 4085 | /// \returns An expression that copies the initializer expression into |
| 4086 | /// a temporary object, or an error expression if a copy could not be |
| 4087 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4088 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4089 | QualType T, |
| 4090 | const InitializedEntity &Entity, |
| 4091 | ExprResult CurInit, |
| 4092 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4093 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4094 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4095 | CXXRecordDecl *Class = 0; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4096 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4097 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4098 | if (!Class) |
| 4099 | return move(CurInit); |
| 4100 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4101 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4102 | // When certain criteria are met, an implementation is allowed to |
| 4103 | // omit the copy/move construction of a class object, even if the |
| 4104 | // copy/move constructor and/or destructor for the object have |
| 4105 | // side effects. [...] |
| 4106 | // - when a temporary class object that has not been bound to a |
| 4107 | // reference (12.2) would be copied/moved to a class object |
| 4108 | // with the same cv-unqualified type, the copy/move operation |
| 4109 | // can be omitted by constructing the temporary object |
| 4110 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4111 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4112 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4113 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4114 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4115 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4116 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4117 | SourceLocation Loc; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4118 | switch (Entity.getKind()) { |
| 4119 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4120 | Loc = Entity.getReturnLoc(); |
| 4121 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4122 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4123 | case InitializedEntity::EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4124 | Loc = Entity.getThrowLoc(); |
| 4125 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4126 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4127 | case InitializedEntity::EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4128 | Loc = Entity.getDecl()->getLocation(); |
| 4129 | break; |
| 4130 | |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4131 | case InitializedEntity::EK_ArrayElement: |
| 4132 | case InitializedEntity::EK_Member: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4133 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4134 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4135 | case InitializedEntity::EK_New: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4136 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4137 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4138 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4139 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4140 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4141 | Loc = CurInitExpr->getLocStart(); |
| 4142 | break; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4143 | } |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4144 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4145 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4146 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 4147 | return move(CurInit); |
| 4148 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4149 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4150 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4151 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 4152 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4153 | Con != ConEnd; ++Con) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4154 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4155 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 4156 | // initialization is direct-initialization. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4157 | CXXConstructorDecl *Constructor = 0; |
| 4158 | |
| 4159 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4160 | // Handle copy/moveconstructors, only. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4161 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4162 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4163 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4164 | continue; |
| 4165 | |
| 4166 | DeclAccessPair FoundDecl |
| 4167 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4168 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 4169 | &CurInitExpr, 1, CandidateSet); |
| 4170 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4171 | } |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4172 | |
| 4173 | // Handle constructor templates. |
| 4174 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4175 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4176 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4177 | |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4178 | Constructor = cast<CXXConstructorDecl>( |
| 4179 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 4180 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4181 | continue; |
| 4182 | |
| 4183 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4184 | // candidates? |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4185 | DeclAccessPair FoundDecl |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 4186 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4187 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 4188 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4189 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4190 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4191 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4192 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4193 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4194 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4195 | case OR_Success: |
| 4196 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4197 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4198 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4199 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4200 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4201 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4202 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4203 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4204 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4205 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4206 | return ExprError(); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4207 | return move(CurInit); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4208 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4209 | case OR_Ambiguous: |
| 4210 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4211 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4212 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4213 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4214 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4216 | case OR_Deleted: |
| 4217 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4218 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4219 | << CurInitExpr->getSourceRange(); |
| 4220 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4221 | << 1 << Best->Function->isDeleted(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4222 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4225 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4226 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4227 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4228 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4229 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4230 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4231 | |
| 4232 | if (IsExtraneousCopy) { |
| 4233 | // If this is a totally extraneous copy for C++03 reference |
| 4234 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4235 | // expression. We don't generate an (elided) copy operation here |
| 4236 | // because doing so would require us to pass down a flag to avoid |
| 4237 | // infinite recursion, where each step adds another extraneous, |
| 4238 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4239 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4240 | // Instantiate the default arguments of any extra parameters in |
| 4241 | // the selected copy constructor, as if we were going to create a |
| 4242 | // proper call to the copy constructor. |
| 4243 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4244 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4245 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4246 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4247 | break; |
| 4248 | |
| 4249 | // Build the default argument expression; we don't actually care |
| 4250 | // if this succeeds or not, because this routine will complain |
| 4251 | // if there was a problem. |
| 4252 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4253 | } |
| 4254 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4255 | return S.Owned(CurInitExpr); |
| 4256 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4258 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4259 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4260 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4261 | // constructor call (we might have derived-to-base conversions, or |
| 4262 | // the copy constructor may have default arguments). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4263 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4264 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4265 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4267 | // Actually perform the constructor call. |
| 4268 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4269 | move_arg(ConstructorArgs), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4270 | HadMultipleCandidates, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4271 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4272 | CXXConstructExpr::CK_Complete, |
| 4273 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4275 | // If we're supposed to bind temporaries, do so. |
| 4276 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4277 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4278 | return move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4279 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4281 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4282 | const InitializedEntity &Entity) { |
| 4283 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4284 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4285 | return; |
| 4286 | |
| 4287 | if (Entity.getDecl()->getDeclName()) |
| 4288 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4289 | << Entity.getDecl()->getDeclName(); |
| 4290 | else |
| 4291 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4292 | } |
| 4293 | } |
| 4294 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4295 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4296 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4297 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4298 | } |
| 4299 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4300 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4301 | InitializationSequence::Perform(Sema &S, |
| 4302 | const InitializedEntity &Entity, |
| 4303 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4304 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4305 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4306 | if (Failed()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4307 | unsigned NumArgs = Args.size(); |
| 4308 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4309 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4310 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4311 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4312 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4313 | // If the declaration is a non-dependent, incomplete array type |
| 4314 | // that has an initializer, then its type will be completed once |
| 4315 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4316 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4317 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4318 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4319 | if (const IncompleteArrayType *ArrayT |
| 4320 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4321 | // FIXME: We don't currently have the ability to accurately |
| 4322 | // compute the length of an initializer list without |
| 4323 | // performing full type-checking of the initializer list |
| 4324 | // (since we have to determine where braces are implicitly |
| 4325 | // introduced and such). So, we fall back to making the array |
| 4326 | // type a dependently-sized array type with no specified |
| 4327 | // bound. |
| 4328 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4329 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4331 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4332 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4333 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4334 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4335 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4336 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4337 | Brackets = ArrayLoc->getBracketsRange(); |
| 4338 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
| 4341 | *ResultType |
| 4342 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4343 | /*NumElts=*/0, |
| 4344 | ArrayT->getSizeModifier(), |
| 4345 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4346 | Brackets); |
| 4347 | } |
| 4348 | |
| 4349 | } |
| 4350 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4351 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4352 | Kind.isExplicitCast()); |
| 4353 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4356 | // No steps means no initialization. |
| 4357 | if (Steps.empty()) |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4358 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4360 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4361 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4362 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4363 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4364 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4365 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4366 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4367 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4368 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4369 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4371 | // grab the only argument out the Args and place it into the "current" |
| 4372 | // initializer. |
| 4373 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4374 | case SK_ResolveAddressOfOverloadedFunction: |
| 4375 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4376 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4377 | case SK_CastDerivedToBaseLValue: |
| 4378 | case SK_BindReference: |
| 4379 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4380 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4381 | case SK_UserConversion: |
| 4382 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4383 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4384 | case SK_QualificationConversionRValue: |
| 4385 | case SK_ConversionSequence: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4386 | case SK_ListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4387 | case SK_ListInitialization: |
| 4388 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4389 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4390 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4391 | case SK_ArrayInit: |
| 4392 | case SK_PassByIndirectCopyRestore: |
| 4393 | case SK_PassByIndirectRestore: |
| 4394 | case SK_ProduceObjCObject: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4395 | assert(Args.size() == 1); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4396 | CurInit = Args.get()[0]; |
| 4397 | if (!CurInit.get()) return ExprError(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4398 | |
| 4399 | // Read from a property when initializing something with it. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4400 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4401 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4402 | if (CurInit.isInvalid()) |
| 4403 | return ExprError(); |
| 4404 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4405 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4408 | case SK_ConstructorInitialization: |
| 4409 | case SK_ZeroInitialization: |
| 4410 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4411 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4412 | |
| 4413 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4414 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4415 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4416 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4417 | Step != StepEnd; ++Step) { |
| 4418 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4419 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4421 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4423 | switch (Step->Kind) { |
| 4424 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4426 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4427 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4428 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4429 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4430 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4431 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4432 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4433 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4434 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4435 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4436 | case SK_CastDerivedToBaseLValue: { |
| 4437 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4438 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4439 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4440 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4441 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4442 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4443 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4444 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4445 | CurInit.get()->getLocStart(), |
| 4446 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4447 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4448 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4450 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4451 | QualType T = SourceType; |
| 4452 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4453 | T = Pointer->getPointeeType(); |
| 4454 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4455 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4456 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4457 | } |
| 4458 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4459 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4460 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4461 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4462 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4463 | VK_XValue : |
| 4464 | VK_RValue); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4465 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4466 | Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4467 | CK_DerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4468 | CurInit.get(), |
| 4469 | &BasePath, VK)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4470 | break; |
| 4471 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4473 | case SK_BindReference: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4474 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4475 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4476 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4477 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4478 | << BitField->getDeclName() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4479 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4480 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4481 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4482 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4483 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4484 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4485 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4486 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4487 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4488 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4489 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4490 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4491 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4492 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4493 | // Reference binding does not have any corresponding ASTs. |
| 4494 | |
| 4495 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4496 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4497 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4498 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4499 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4500 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4501 | case SK_BindReferenceToTemporary: |
| 4502 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4503 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4504 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4505 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4506 | // Materialize the temporary into memory. |
Douglas Gregor | 2fa40a3 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4507 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4508 | Entity.getType().getNonReferenceType(), |
| 4509 | CurInit.get(), |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4510 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4511 | |
| 4512 | // If we're binding to an Objective-C object that has lifetime, we |
| 4513 | // need cleanups. |
| 4514 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4515 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4516 | S.ExprNeedsCleanups = true; |
| 4517 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4518 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4520 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4521 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4522 | /*IsExtraneousCopy=*/true); |
| 4523 | break; |
| 4524 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4525 | case SK_UserConversion: { |
| 4526 | // We have a user-defined conversion that invokes either a constructor |
| 4527 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4528 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4529 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4530 | FunctionDecl *Fn = Step->Function.Function; |
| 4531 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4532 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4533 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4534 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4535 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4536 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4537 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4538 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4539 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4540 | // Determine the arguments required to actually perform the constructor |
| 4541 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4542 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4543 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4544 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4545 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4546 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4548 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4549 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4550 | move_arg(ConstructorArgs), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4551 | HadMultipleCandidates, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4552 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4553 | CXXConstructExpr::CK_Complete, |
| 4554 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4555 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4556 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4557 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4558 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4559 | FoundFn.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4560 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4561 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4562 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4563 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4564 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4565 | S.IsDerivedFrom(SourceType, Class)) |
| 4566 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4567 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4568 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4569 | } else { |
| 4570 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4571 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4572 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4573 | FoundFn); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4574 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4575 | |
| 4576 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4577 | // derived-to-base conversion? I believe the answer is "no", because |
| 4578 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4579 | ExprResult CurInitExprRes = |
| 4580 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4581 | FoundFn, Conversion); |
| 4582 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4583 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4584 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4586 | // Build the actual call to the conversion function. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4587 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 4588 | HadMultipleCandidates); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4589 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4590 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4591 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4592 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4593 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4594 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4595 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4596 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4597 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4598 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4599 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4600 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4601 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4602 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4604 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4605 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4606 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4607 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4608 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4609 | } |
| 4610 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4611 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4612 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4613 | CurInit.get()->getType(), |
| 4614 | CastKind, CurInit.get(), 0, |
Eli Friedman | f272d40 | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 4615 | CurInit.get()->getValueKind())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4616 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4617 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4618 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4619 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4620 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4621 | break; |
| 4622 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4623 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4624 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4625 | case SK_QualificationConversionXValue: |
| 4626 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4627 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4628 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4629 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4630 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4631 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4632 | VK_XValue : |
| 4633 | VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4634 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4635 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4638 | case SK_ConversionSequence: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4639 | Sema::CheckedConversionKind CCK |
| 4640 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4641 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4642 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4643 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4644 | ExprResult CurInitExprRes = |
| 4645 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4646 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4647 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4648 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4649 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4650 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4651 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4652 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4653 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4654 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4655 | QualType Ty = Step->Type; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4656 | InitListChecker PerformInitList(S, Entity, InitList, |
| 4657 | ResultType ? *ResultType : Ty, /*VerifyOnly=*/false); |
| 4658 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4659 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4660 | |
| 4661 | CurInit.release(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4662 | CurInit = S.Owned(PerformInitList.getFullyStructuredList()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4663 | break; |
| 4664 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4665 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4666 | case SK_ListConstructorCall: |
| 4667 | assert(false && "List constructor calls not yet supported."); |
| 4668 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4669 | case SK_ConstructorInitialization: { |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4670 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4671 | CXXConstructorDecl *Constructor |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4672 | = cast<CXXConstructorDecl>(Step->Function.Function); |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4673 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4675 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4676 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | da2da9c | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4677 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4678 | ? Kind.getEqualLoc() |
| 4679 | : Kind.getLocation(); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4680 | |
| 4681 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4682 | // Force even a trivial, implicit default constructor to be |
| 4683 | // semantically checked. We do this explicitly because we don't build |
| 4684 | // the definition for completely trivial constructors. |
| 4685 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4686 | assert(ClassDecl && "No parent class for constructor."); |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4687 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4688 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4689 | !Constructor->isUsed(false)) |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4690 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4691 | } |
| 4692 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4693 | // Determine the arguments required to actually perform the constructor |
| 4694 | // call. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4695 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4696 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4697 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4698 | |
| 4699 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4700 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4701 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4702 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4703 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4704 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4705 | unsigned NumExprs = ConstructorArgs.size(); |
| 4706 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 3fd2a55 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4707 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4708 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4710 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4711 | if (!TSInfo) |
| 4712 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4714 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4715 | Constructor, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4716 | TSInfo, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | Exprs, |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4718 | NumExprs, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4719 | Kind.getParenRange(), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4720 | HadMultipleCandidates, |
Douglas Gregor | 199db36 | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4721 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4722 | } else { |
| 4723 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4724 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4725 | |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4726 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4727 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4729 | CXXConstructExpr::CK_NonVirtualBase; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4730 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4731 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4732 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4734 | // Only get the parenthesis range if it is a direct construction. |
| 4735 | SourceRange parenRange = |
| 4736 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4737 | Kind.getParenRange() : SourceRange(); |
| 4738 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4739 | // If the entity allows NRVO, mark the construction as elidable |
| 4740 | // unconditionally. |
| 4741 | if (Entity.allowsNRVO()) |
| 4742 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4743 | Constructor, /*Elidable=*/true, |
| 4744 | move_arg(ConstructorArgs), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4745 | HadMultipleCandidates, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4746 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4747 | ConstructKind, |
| 4748 | parenRange); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4749 | else |
| 4750 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4751 | Constructor, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4752 | move_arg(ConstructorArgs), |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4753 | HadMultipleCandidates, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4754 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4755 | ConstructKind, |
| 4756 | parenRange); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4757 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4758 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4759 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4760 | |
| 4761 | // Only check access if all of that succeeded. |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4762 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4763 | Step->Function.FoundDecl.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4764 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4765 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4766 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4767 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4769 | break; |
| 4770 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4771 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4772 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4773 | step_iterator NextStep = Step; |
| 4774 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | if (NextStep != StepEnd && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4776 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4777 | // The need for zero-initialization is recorded directly into |
| 4778 | // the call to the object's constructor within the next step. |
| 4779 | ConstructorInitRequiresZeroInit = true; |
| 4780 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4781 | S.getLangOptions().CPlusPlus && |
| 4782 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4783 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4784 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4786 | Kind.getRange().getBegin()); |
| 4787 | |
| 4788 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4789 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4790 | TSInfo, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4791 | Kind.getRange().getEnd())); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4792 | } else { |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4793 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4794 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4795 | break; |
| 4796 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4797 | |
| 4798 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4799 | QualType SourceType = CurInit.get()->getType(); |
| 4800 | ExprResult Result = move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4801 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4802 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4803 | if (Result.isInvalid()) |
| 4804 | return ExprError(); |
| 4805 | CurInit = move(Result); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4806 | |
| 4807 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4808 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4809 | if (ConvTy != Sema::Compatible && |
| 4810 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4811 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4812 | == Sema::Compatible) |
| 4813 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4814 | if (CurInitExprRes.isInvalid()) |
| 4815 | return ExprError(); |
| 4816 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4817 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4818 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4819 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4820 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4821 | CurInit.get(), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4822 | getAssignmentAction(Entity), |
| 4823 | &Complained)) { |
| 4824 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4825 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4826 | } else if (Complained) |
| 4827 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4828 | break; |
| 4829 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4830 | |
| 4831 | case SK_StringInit: { |
| 4832 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4833 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4834 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4835 | break; |
| 4836 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4837 | |
| 4838 | case SK_ObjCObjectConversion: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4839 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4840 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4841 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4842 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4843 | |
| 4844 | case SK_ArrayInit: |
| 4845 | // Okay: we checked everything before creating this step. Note that |
| 4846 | // this is a GNU extension. |
| 4847 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4848 | << Step->Type << CurInit.get()->getType() |
| 4849 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4850 | |
| 4851 | // If the destination type is an incomplete array type, update the |
| 4852 | // type accordingly. |
| 4853 | if (ResultType) { |
| 4854 | if (const IncompleteArrayType *IncompleteDest |
| 4855 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4856 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4857 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4858 | *ResultType = S.Context.getConstantArrayType( |
| 4859 | IncompleteDest->getElementType(), |
| 4860 | ConstantSource->getSize(), |
| 4861 | ArrayType::Normal, 0); |
| 4862 | } |
| 4863 | } |
| 4864 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4865 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4866 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4867 | case SK_PassByIndirectCopyRestore: |
| 4868 | case SK_PassByIndirectRestore: |
| 4869 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4870 | CurInit = S.Owned(new (S.Context) |
| 4871 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4872 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4873 | break; |
| 4874 | |
| 4875 | case SK_ProduceObjCObject: |
| 4876 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4877 | CK_ARCProduceObject, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4878 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4879 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4880 | } |
| 4881 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4882 | |
| 4883 | // Diagnose non-fatal problems with the completed initialization. |
| 4884 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4885 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4886 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4887 | cast<FieldDecl>(Entity.getDecl()), |
| 4888 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4889 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4890 | return move(CurInit); |
| 4891 | } |
| 4892 | |
| 4893 | //===----------------------------------------------------------------------===// |
| 4894 | // Diagnose initialization failures |
| 4895 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4896 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4897 | const InitializedEntity &Entity, |
| 4898 | const InitializationKind &Kind, |
| 4899 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4900 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4901 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4902 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4903 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4904 | switch (Failure) { |
| 4905 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4906 | // FIXME: Customize for the initialized entity? |
| 4907 | if (NumArgs == 0) |
| 4908 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4909 | << DestType.getNonReferenceType(); |
| 4910 | else // FIXME: diagnostic below could be better! |
| 4911 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4912 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4913 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4915 | case FK_ArrayNeedsInitList: |
| 4916 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4917 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4918 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4919 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4920 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4921 | case FK_ArrayTypeMismatch: |
| 4922 | case FK_NonConstantArrayInit: |
| 4923 | S.Diag(Kind.getLocation(), |
| 4924 | (Failure == FK_ArrayTypeMismatch |
| 4925 | ? diag::err_array_init_different_type |
| 4926 | : diag::err_array_init_non_constant_array)) |
| 4927 | << DestType.getNonReferenceType() |
| 4928 | << Args[0]->getType() |
| 4929 | << Args[0]->getSourceRange(); |
| 4930 | break; |
| 4931 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4932 | case FK_AddressOfOverloadFailed: { |
| 4933 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4934 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4935 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4936 | true, |
| 4937 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4938 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4939 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4941 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4942 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4943 | switch (FailedOverloadResult) { |
| 4944 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4945 | if (Failure == FK_UserConversionOverloadFailed) |
| 4946 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4947 | << Args[0]->getType() << DestType |
| 4948 | << Args[0]->getSourceRange(); |
| 4949 | else |
| 4950 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4951 | << DestType << Args[0]->getType() |
| 4952 | << Args[0]->getSourceRange(); |
| 4953 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4954 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4955 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4957 | case OR_No_Viable_Function: |
| 4958 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4959 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4960 | << Args[0]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4961 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4962 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4963 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4964 | case OR_Deleted: { |
| 4965 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4966 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4967 | << Args[0]->getSourceRange(); |
| 4968 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4969 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4970 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4971 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4972 | if (Ovl == OR_Deleted) { |
| 4973 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4974 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4975 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4976 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4977 | } |
| 4978 | break; |
| 4979 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4980 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4981 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4982 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4983 | break; |
| 4984 | } |
| 4985 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4986 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4987 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4988 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4989 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4990 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4991 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4992 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4993 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4994 | << DestType.getNonReferenceType() |
| 4995 | << Args[0]->getType() |
| 4996 | << Args[0]->getSourceRange(); |
| 4997 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4998 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4999 | case FK_RValueReferenceBindingToLValue: |
| 5000 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5001 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5002 | << Args[0]->getSourceRange(); |
| 5003 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5005 | case FK_ReferenceInitDropsQualifiers: |
| 5006 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5007 | << DestType.getNonReferenceType() |
| 5008 | << Args[0]->getType() |
| 5009 | << Args[0]->getSourceRange(); |
| 5010 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5011 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5012 | case FK_ReferenceInitFailed: |
| 5013 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5014 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5015 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5016 | << Args[0]->getType() |
| 5017 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5018 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5019 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5020 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5021 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5023 | case FK_ConversionFailed: { |
| 5024 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5025 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 5026 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5027 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5028 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5029 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5030 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5031 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5032 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5033 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5034 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5035 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5036 | |
| 5037 | case FK_ConversionFromPropertyFailed: |
| 5038 | // No-op. This error has already been reported. |
| 5039 | break; |
| 5040 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5041 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5042 | SourceRange R; |
| 5043 | |
| 5044 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5045 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5046 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5047 | else |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5048 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5049 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5050 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5051 | if (Kind.isCStyleOrFunctionalCast()) |
| 5052 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5053 | << R; |
| 5054 | else |
| 5055 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5056 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5057 | break; |
| 5058 | } |
| 5059 | |
| 5060 | case FK_ReferenceBindingToInitList: |
| 5061 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5062 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5063 | break; |
| 5064 | |
| 5065 | case FK_InitListBadDestinationType: |
| 5066 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5067 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5068 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5069 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5070 | case FK_ConstructorOverloadFailed: { |
| 5071 | SourceRange ArgsRange; |
| 5072 | if (NumArgs) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5073 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5074 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5075 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5076 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5077 | // bad. |
| 5078 | switch (FailedOverloadResult) { |
| 5079 | case OR_Ambiguous: |
| 5080 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5081 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5082 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5083 | Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5084 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5085 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5086 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5087 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5088 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5089 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5090 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5091 | // This is implicit default initialization of a member or |
| 5092 | // base within a constructor. If no viable function was |
| 5093 | // found, notify the user that she needs to explicitly |
| 5094 | // initialize this base/member. |
| 5095 | CXXConstructorDecl *Constructor |
| 5096 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5097 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5098 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5099 | << Constructor->isImplicit() |
| 5100 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5101 | << /*base=*/0 |
| 5102 | << Entity.getType(); |
| 5103 | |
| 5104 | RecordDecl *BaseDecl |
| 5105 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5106 | ->getDecl(); |
| 5107 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5108 | << S.Context.getTagDeclType(BaseDecl); |
| 5109 | } else { |
| 5110 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5111 | << Constructor->isImplicit() |
| 5112 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5113 | << /*member=*/1 |
| 5114 | << Entity.getName(); |
| 5115 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5116 | |
| 5117 | if (const RecordType *Record |
| 5118 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5119 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5120 | diag::note_previous_decl) |
| 5121 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5122 | } |
| 5123 | break; |
| 5124 | } |
| 5125 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5126 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5127 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5128 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5129 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5130 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5131 | case OR_Deleted: { |
| 5132 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5133 | << true << DestType << ArgsRange; |
| 5134 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5135 | OverloadingResult Ovl |
| 5136 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5137 | if (Ovl == OR_Deleted) { |
| 5138 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5139 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5140 | } else { |
| 5141 | llvm_unreachable("Inconsistent overload resolution?"); |
| 5142 | } |
| 5143 | break; |
| 5144 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5146 | case OR_Success: |
| 5147 | llvm_unreachable("Conversion did not fail!"); |
| 5148 | break; |
| 5149 | } |
| 5150 | break; |
| 5151 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5152 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5153 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5154 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5155 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5156 | // This is implicit default-initialization of a const member in |
| 5157 | // a constructor. Complain that it needs to be explicitly |
| 5158 | // initialized. |
| 5159 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5160 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5161 | << Constructor->isImplicit() |
| 5162 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5163 | << /*const=*/1 |
| 5164 | << Entity.getName(); |
| 5165 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5166 | << Entity.getName(); |
| 5167 | } else { |
| 5168 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5169 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5170 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5171 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5172 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5173 | case FK_Incomplete: |
| 5174 | S.RequireCompleteType(Kind.getLocation(), DestType, |
| 5175 | diag::err_init_incomplete_type); |
| 5176 | break; |
| 5177 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5178 | case FK_ListInitializationFailed: { |
| 5179 | // Run the init list checker again to emit diagnostics. |
| 5180 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5181 | QualType DestType = Entity.getType(); |
| 5182 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
| 5183 | DestType, /*VerifyOnly=*/false); |
| 5184 | assert(DiagnoseInitList.HadError() && |
| 5185 | "Inconsistent init list check result."); |
| 5186 | break; |
| 5187 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5188 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5189 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5190 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5191 | return true; |
| 5192 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5193 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5194 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5195 | switch (SequenceKind) { |
| 5196 | case FailedSequence: { |
| 5197 | OS << "Failed sequence: "; |
| 5198 | switch (Failure) { |
| 5199 | case FK_TooManyInitsForReference: |
| 5200 | OS << "too many initializers for reference"; |
| 5201 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5202 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5203 | case FK_ArrayNeedsInitList: |
| 5204 | OS << "array requires initializer list"; |
| 5205 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5206 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5207 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5208 | OS << "array requires initializer list or string literal"; |
| 5209 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5210 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5211 | case FK_ArrayTypeMismatch: |
| 5212 | OS << "array type mismatch"; |
| 5213 | break; |
| 5214 | |
| 5215 | case FK_NonConstantArrayInit: |
| 5216 | OS << "non-constant array initializer"; |
| 5217 | break; |
| 5218 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5219 | case FK_AddressOfOverloadFailed: |
| 5220 | OS << "address of overloaded function failed"; |
| 5221 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5222 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5223 | case FK_ReferenceInitOverloadFailed: |
| 5224 | OS << "overload resolution for reference initialization failed"; |
| 5225 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5226 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5227 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5228 | OS << "non-const lvalue reference bound to temporary"; |
| 5229 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5230 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5231 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5232 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5233 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5234 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5235 | case FK_RValueReferenceBindingToLValue: |
| 5236 | OS << "rvalue reference bound to an lvalue"; |
| 5237 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5238 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5239 | case FK_ReferenceInitDropsQualifiers: |
| 5240 | OS << "reference initialization drops qualifiers"; |
| 5241 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5242 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5243 | case FK_ReferenceInitFailed: |
| 5244 | OS << "reference initialization failed"; |
| 5245 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5247 | case FK_ConversionFailed: |
| 5248 | OS << "conversion failed"; |
| 5249 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5250 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5251 | case FK_ConversionFromPropertyFailed: |
| 5252 | OS << "conversion from property failed"; |
| 5253 | break; |
| 5254 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5255 | case FK_TooManyInitsForScalar: |
| 5256 | OS << "too many initializers for scalar"; |
| 5257 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5258 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5259 | case FK_ReferenceBindingToInitList: |
| 5260 | OS << "referencing binding to initializer list"; |
| 5261 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5263 | case FK_InitListBadDestinationType: |
| 5264 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5265 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5266 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5267 | case FK_UserConversionOverloadFailed: |
| 5268 | OS << "overloading failed for user-defined conversion"; |
| 5269 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5270 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5271 | case FK_ConstructorOverloadFailed: |
| 5272 | OS << "constructor overloading failed"; |
| 5273 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5274 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5275 | case FK_DefaultInitOfConst: |
| 5276 | OS << "default initialization of a const variable"; |
| 5277 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5278 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5279 | case FK_Incomplete: |
| 5280 | OS << "initialization of incomplete type"; |
| 5281 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5282 | |
| 5283 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5284 | OS << "list initialization checker failure"; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5285 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5286 | OS << '\n'; |
| 5287 | return; |
| 5288 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5290 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5291 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5292 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5293 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5294 | case NormalSequence: |
| 5295 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5296 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5297 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5298 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5299 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5300 | if (S != step_begin()) { |
| 5301 | OS << " -> "; |
| 5302 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5303 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5304 | switch (S->Kind) { |
| 5305 | case SK_ResolveAddressOfOverloadedFunction: |
| 5306 | OS << "resolve address of overloaded function"; |
| 5307 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5308 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5309 | case SK_CastDerivedToBaseRValue: |
| 5310 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5311 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5312 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5313 | case SK_CastDerivedToBaseXValue: |
| 5314 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5315 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5316 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5317 | case SK_CastDerivedToBaseLValue: |
| 5318 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5319 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5320 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5321 | case SK_BindReference: |
| 5322 | OS << "bind reference to lvalue"; |
| 5323 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5324 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5325 | case SK_BindReferenceToTemporary: |
| 5326 | OS << "bind reference to a temporary"; |
| 5327 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5328 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5329 | case SK_ExtraneousCopyToTemporary: |
| 5330 | OS << "extraneous C++03 copy to temporary"; |
| 5331 | break; |
| 5332 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5333 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5334 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5335 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5336 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5337 | case SK_QualificationConversionRValue: |
| 5338 | OS << "qualification conversion (rvalue)"; |
| 5339 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5340 | case SK_QualificationConversionXValue: |
| 5341 | OS << "qualification conversion (xvalue)"; |
| 5342 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5343 | case SK_QualificationConversionLValue: |
| 5344 | OS << "qualification conversion (lvalue)"; |
| 5345 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5346 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5347 | case SK_ConversionSequence: |
| 5348 | OS << "implicit conversion sequence ("; |
| 5349 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5350 | OS << ")"; |
| 5351 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5352 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5353 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5354 | OS << "list aggregate initialization"; |
| 5355 | break; |
| 5356 | |
| 5357 | case SK_ListConstructorCall: |
| 5358 | OS << "list initialization via constructor"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5359 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5360 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5361 | case SK_ConstructorInitialization: |
| 5362 | OS << "constructor initialization"; |
| 5363 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5364 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5365 | case SK_ZeroInitialization: |
| 5366 | OS << "zero initialization"; |
| 5367 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5368 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5369 | case SK_CAssignment: |
| 5370 | OS << "C assignment"; |
| 5371 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5372 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5373 | case SK_StringInit: |
| 5374 | OS << "string initialization"; |
| 5375 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5376 | |
| 5377 | case SK_ObjCObjectConversion: |
| 5378 | OS << "Objective-C object conversion"; |
| 5379 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5380 | |
| 5381 | case SK_ArrayInit: |
| 5382 | OS << "array initialization"; |
| 5383 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5384 | |
| 5385 | case SK_PassByIndirectCopyRestore: |
| 5386 | OS << "pass by indirect copy and restore"; |
| 5387 | break; |
| 5388 | |
| 5389 | case SK_PassByIndirectRestore: |
| 5390 | OS << "pass by indirect restore"; |
| 5391 | break; |
| 5392 | |
| 5393 | case SK_ProduceObjCObject: |
| 5394 | OS << "Objective-C object retension"; |
| 5395 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5396 | } |
| 5397 | } |
| 5398 | } |
| 5399 | |
| 5400 | void InitializationSequence::dump() const { |
| 5401 | dump(llvm::errs()); |
| 5402 | } |
| 5403 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5404 | static void DiagnoseNarrowingInInitList( |
| 5405 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5406 | bool Constant, const APValue &ConstantValue) { |
| 5407 | if (Constant) { |
| 5408 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5409 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5410 | ? diag::err_init_list_constant_narrowing |
| 5411 | : diag::warn_init_list_constant_narrowing) |
| 5412 | << InitE->getSourceRange() |
| 5413 | << ConstantValue |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5414 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5415 | } else |
| 5416 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5417 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5418 | ? diag::err_init_list_variable_narrowing |
| 5419 | : diag::warn_init_list_variable_narrowing) |
| 5420 | << InitE->getSourceRange() |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5421 | << InitE->getType().getLocalUnqualifiedType() |
| 5422 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5423 | |
| 5424 | llvm::SmallString<128> StaticCast; |
| 5425 | llvm::raw_svector_ostream OS(StaticCast); |
| 5426 | OS << "static_cast<"; |
| 5427 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5428 | // It's important to use the typedef's name if there is one so that the |
| 5429 | // fixit doesn't break code using types like int64_t. |
| 5430 | // |
| 5431 | // FIXME: This will break if the typedef requires qualification. But |
| 5432 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5433 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5434 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5435 | OS << BT->getName(S.getLangOptions()); |
| 5436 | else { |
| 5437 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5438 | // with a broken cast. |
| 5439 | return; |
| 5440 | } |
| 5441 | OS << ">("; |
| 5442 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5443 | << InitE->getSourceRange() |
| 5444 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5445 | << FixItHint::CreateInsertion( |
| 5446 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5447 | } |
| 5448 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5449 | //===----------------------------------------------------------------------===// |
| 5450 | // Initialization helper functions |
| 5451 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5452 | bool |
| 5453 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5454 | ExprResult Init) { |
| 5455 | if (Init.isInvalid()) |
| 5456 | return false; |
| 5457 | |
| 5458 | Expr *InitE = Init.get(); |
| 5459 | assert(InitE && "No initialization expression"); |
| 5460 | |
| 5461 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5462 | SourceLocation()); |
| 5463 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5464 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5467 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5468 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5469 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5470 | ExprResult Init, |
| 5471 | bool TopLevelOfInitList) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5472 | if (Init.isInvalid()) |
| 5473 | return ExprError(); |
| 5474 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5475 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5476 | assert(InitE && "No initialization expression?"); |
| 5477 | |
| 5478 | if (EqualLoc.isInvalid()) |
| 5479 | EqualLoc = InitE->getLocStart(); |
| 5480 | |
| 5481 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5482 | EqualLoc); |
| 5483 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5484 | Init.release(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5485 | |
| 5486 | bool Constant = false; |
| 5487 | APValue Result; |
| 5488 | if (TopLevelOfInitList && |
| 5489 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5490 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5491 | Constant, Result); |
| 5492 | } |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5493 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5494 | } |