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 | // |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The main entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 13 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Designator.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
| 18 | #include "clang/Sema/Lookup.h" |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 19 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 35 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 36 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 37 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 38 | return 0; |
| 39 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // See if this is a string literal or @encode. |
| 41 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 43 | // Handle @encode, which is a narrow string. |
| 44 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 45 | return Init; |
| 46 | |
| 47 | // Otherwise we can only handle string literals. |
| 48 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 012b339 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 49 | if (SL == 0) return 0; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 50 | |
| 51 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 52 | |
| 53 | switch (SL->getKind()) { |
| 54 | case StringLiteral::Ascii: |
| 55 | case StringLiteral::UTF8: |
| 56 | // char array can be initialized with a narrow string. |
| 57 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 59 | case StringLiteral::UTF16: |
| 60 | return ElemTy->isChar16Type() ? Init : 0; |
| 61 | case StringLiteral::UTF32: |
| 62 | return ElemTy->isChar32Type() ? Init : 0; |
| 63 | case StringLiteral::Wide: |
| 64 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 65 | // correction from DR343): "An array with element type compatible with a |
| 66 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 67 | // string literal, optionally enclosed in braces." |
| 68 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 69 | ElemTy.getUnqualifiedType())) |
| 70 | return Init; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 75 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 78 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 79 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 80 | if (!arrayType) return 0; |
| 81 | |
| 82 | return IsStringInit(init, arrayType, Context); |
| 83 | } |
| 84 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 85 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 86 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 87 | // Get the length of the string as parsed. |
| 88 | uint64_t StrLength = |
| 89 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 90 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | // 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] | 94 | // being initialized to a string literal. |
| 95 | llvm::APSInt ConstVal(32); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 96 | ConstVal = StrLength; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 97 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 98 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 99 | ConstVal, |
| 100 | ArrayType::Normal, 0); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 101 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 102 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 104 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 106 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 107 | // the size may be smaller or larger than the string we are initializing. |
| 108 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 109 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 110 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 111 | // For Pascal strings it's OK to strip off the terminating null character, |
| 112 | // so the example below is valid: |
| 113 | // |
| 114 | // unsigned char a[2] = "\pa"; |
| 115 | if (SL->isPascal()) |
| 116 | StrLength--; |
| 117 | } |
| 118 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 119 | // [dcl.init.string]p2 |
| 120 | if (StrLength > CAT->getSize().getZExtValue()) |
| 121 | S.Diag(Str->getSourceRange().getBegin(), |
| 122 | diag::err_initializer_string_for_char_array_too_long) |
| 123 | << Str->getSourceRange(); |
| 124 | } else { |
| 125 | // C99 6.7.8p14. |
| 126 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 127 | S.Diag(Str->getSourceRange().getBegin(), |
| 128 | diag::warn_initializer_string_for_char_array_too_long) |
| 129 | << Str->getSourceRange(); |
| 130 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 132 | // Set the type to the actual size that we are initializing. If we have |
| 133 | // something like: |
| 134 | // char x[1] = "foo"; |
| 135 | // then this will set the string literal's type to char[1]. |
| 136 | Str->setType(DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
| 140 | // Semantic checking for initializer lists. |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 143 | /// @brief Semantic checking for initializer lists. |
| 144 | /// |
| 145 | /// The InitListChecker class contains a set of routines that each |
| 146 | /// handle the initialization of a certain kind of entity, e.g., |
| 147 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 148 | /// InitListChecker itself performs a recursive walk of the subobject |
| 149 | /// structure of the type to be initialized, while stepping through |
| 150 | /// the initializer list one element at a time. The IList and Index |
| 151 | /// parameters to each of the Check* routines contain the active |
| 152 | /// (syntactic) initializer list and the index into that initializer |
| 153 | /// list that represents the current initializer. Each routine is |
| 154 | /// responsible for moving that Index forward as it consumes elements. |
| 155 | /// |
| 156 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 157 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 158 | /// initializer list and the index into that initializer list where we |
| 159 | /// are copying initializers as we map them over to the semantic |
| 160 | /// list. Once we have completed our recursive walk of the subobject |
| 161 | /// structure, we will have constructed a full semantic initializer |
| 162 | /// list. |
| 163 | /// |
| 164 | /// C99 designators cause changes in the initializer list traversal, |
| 165 | /// because they make the initialization "jump" into a specific |
| 166 | /// subobject and then continue the initialization from that |
| 167 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 168 | /// designated subobject and manages backing out the recursion to |
| 169 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 170 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 172 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | bool hadError; |
| 174 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 175 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 177 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 178 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 179 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame^] | 180 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 181 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 182 | InitListExpr *IList, QualType &T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 183 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 184 | unsigned &StructuredIndex, |
| 185 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 186 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 187 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 189 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 190 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 191 | unsigned &StructuredIndex, |
| 192 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 193 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 194 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 195 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 196 | InitListExpr *StructuredList, |
| 197 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 198 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 199 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 200 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 201 | InitListExpr *StructuredList, |
| 202 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 203 | void CheckReferenceType(const InitializedEntity &Entity, |
| 204 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 205 | unsigned &Index, |
| 206 | InitListExpr *StructuredList, |
| 207 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 208 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 209 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 210 | InitListExpr *StructuredList, |
| 211 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 212 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 213 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 215 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 216 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 217 | unsigned &StructuredIndex, |
| 218 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 219 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 220 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 222 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 223 | InitListExpr *StructuredList, |
| 224 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 225 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 226 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 227 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 229 | RecordDecl::field_iterator *NextField, |
| 230 | llvm::APSInt *NextElementIndex, |
| 231 | unsigned &Index, |
| 232 | InitListExpr *StructuredList, |
| 233 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 234 | bool FinishSubobjectInit, |
| 235 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 236 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 237 | QualType CurrentObjectType, |
| 238 | InitListExpr *StructuredList, |
| 239 | unsigned StructuredIndex, |
| 240 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 241 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 242 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 243 | Expr *expr); |
| 244 | int numArrayElements(QualType DeclType); |
| 245 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 246 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 247 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 248 | const InitializedEntity &ParentEntity, |
| 249 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 250 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 251 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 252 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 253 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 254 | InitListExpr *IL, QualType &T); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 255 | bool HadError() { return hadError; } |
| 256 | |
| 257 | // @brief Retrieves the fully-structured initializer list used for |
| 258 | // semantic analysis and code generation. |
| 259 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 260 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 261 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 263 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 264 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 265 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 266 | bool &RequiresSecondPass) { |
| 267 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 268 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 269 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 270 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 271 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 272 | // FIXME: We probably don't need to handle references |
| 273 | // specially here, since value-initialization of references is |
| 274 | // handled in InitializationSequence. |
| 275 | if (Field->getType()->isReferenceType()) { |
| 276 | // C++ [dcl.init.aggr]p9: |
| 277 | // If an incomplete or empty initializer-list leaves a |
| 278 | // member of reference type uninitialized, the program is |
| 279 | // ill-formed. |
| 280 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 281 | << Field->getType() |
| 282 | << ILE->getSyntacticForm()->getSourceRange(); |
| 283 | SemaRef.Diag(Field->getLocation(), |
| 284 | diag::note_uninit_reference_member); |
| 285 | hadError = true; |
| 286 | return; |
| 287 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 289 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 290 | true); |
| 291 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 292 | if (!InitSeq) { |
| 293 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 294 | hadError = true; |
| 295 | return; |
| 296 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 297 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 298 | ExprResult MemberInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 299 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 300 | if (MemberInit.isInvalid()) { |
| 301 | hadError = true; |
| 302 | return; |
| 303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 305 | if (hadError) { |
| 306 | // Do nothing |
| 307 | } else if (Init < NumInits) { |
| 308 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 309 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 310 | // Value-initialization requires a constructor call, so |
| 311 | // extend the initializer list to include the constructor |
| 312 | // call and make a note that we'll need to take another pass |
| 313 | // through the initializer list. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 314 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 315 | RequiresSecondPass = true; |
| 316 | } |
| 317 | } else if (InitListExpr *InnerILE |
| 318 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 319 | FillInValueInitializations(MemberEntity, InnerILE, |
| 320 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 323 | /// Recursively replaces NULL values within the given initializer list |
| 324 | /// with expressions that perform value-initialization of the |
| 325 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 326 | void |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 327 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 328 | InitListExpr *ILE, |
| 329 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 331 | "Should not have void type"); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 332 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 333 | if (ILE->getSyntacticForm()) |
| 334 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 336 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 337 | if (RType->getDecl()->isUnion() && |
| 338 | ILE->getInitializedFieldInUnion()) |
| 339 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 340 | Entity, ILE, RequiresSecondPass); |
| 341 | else { |
| 342 | unsigned Init = 0; |
| 343 | for (RecordDecl::field_iterator |
| 344 | Field = RType->getDecl()->field_begin(), |
| 345 | FieldEnd = RType->getDecl()->field_end(); |
| 346 | Field != FieldEnd; ++Field) { |
| 347 | if (Field->isUnnamedBitfield()) |
| 348 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 349 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 350 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 351 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 352 | |
| 353 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 354 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 355 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 357 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 359 | // Only look at the first initialization of a union. |
| 360 | if (RType->getDecl()->isUnion()) |
| 361 | break; |
| 362 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 367 | |
| 368 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 370 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 371 | unsigned NumInits = ILE->getNumInits(); |
| 372 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 373 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 374 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 375 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 376 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 377 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 378 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 379 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 380 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 381 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 382 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 383 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 385 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 388 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 389 | if (hadError) |
| 390 | return; |
| 391 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 392 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 393 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 394 | ElementEntity.setElementIndex(Init); |
| 395 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 396 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 397 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 398 | true); |
| 399 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 400 | if (!InitSeq) { |
| 401 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 402 | hadError = true; |
| 403 | return; |
| 404 | } |
| 405 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 406 | ExprResult ElementInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 407 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 408 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 409 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | |
| 413 | if (hadError) { |
| 414 | // Do nothing |
| 415 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 416 | // For arrays, just set the expression used for value-initialization |
| 417 | // of the "holes" in the array. |
| 418 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 419 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 420 | else |
| 421 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 422 | } else { |
| 423 | // For arrays, just set the expression used for value-initialization |
| 424 | // of the rest of elements and exit. |
| 425 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 426 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 427 | return; |
| 428 | } |
| 429 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 430 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 431 | // Value-initialization requires a constructor call, so |
| 432 | // extend the initializer list to include the constructor |
| 433 | // call and make a note that we'll need to take another pass |
| 434 | // through the initializer list. |
| 435 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 436 | RequiresSecondPass = true; |
| 437 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 438 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 439 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 440 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 441 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 446 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 447 | InitListExpr *IL, QualType &T) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 448 | : SemaRef(S) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 449 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 450 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 451 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 452 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | FullyStructuredList |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 454 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 455 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 456 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 457 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 459 | if (!hadError) { |
| 460 | bool RequiresSecondPass = false; |
| 461 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 462 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 464 | RequiresSecondPass); |
| 465 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 469 | // FIXME: use a proper constant |
| 470 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 471 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 472 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 473 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 474 | } |
| 475 | return maxElements; |
| 476 | } |
| 477 | |
| 478 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 479 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 480 | int InitializableMembers = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 482 | Field = structDecl->field_begin(), |
| 483 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 484 | Field != FieldEnd; ++Field) { |
| 485 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 486 | ++InitializableMembers; |
| 487 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 488 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 489 | return std::min(InitializableMembers, 1); |
| 490 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 493 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 494 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 495 | QualType T, unsigned &Index, |
| 496 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame^] | 497 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 498 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 500 | if (T->isArrayType()) |
| 501 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 502 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 503 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 504 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 505 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 506 | else |
| 507 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 508 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 509 | if (maxElements == 0) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 510 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 511 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 512 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 513 | hadError = true; |
| 514 | return; |
| 515 | } |
| 516 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 517 | // Build a structured initializer list corresponding to this subobject. |
| 518 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 520 | StructuredIndex, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 521 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 522 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 523 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 525 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 526 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 527 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 528 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame^] | 530 | StructuredSubobjectInitIndex); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 531 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 532 | StructuredSubobjectInitList->setType(T); |
| 533 | |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 534 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 535 | // range corresponds with the end of the last initializer it used. |
| 536 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | SourceLocation EndLoc |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 538 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 539 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 540 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 541 | |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 542 | // Warn about missing braces. |
| 543 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 5cbff48 | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 544 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 545 | diag::warn_missing_braces) |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 546 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 547 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 548 | "{") |
| 549 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 550 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 551 | "}"); |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 552 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 555 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 556 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 557 | unsigned &Index, |
| 558 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 559 | unsigned &StructuredIndex, |
| 560 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 561 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 562 | SyntacticToSemantic[IList] = StructuredList; |
| 563 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 565 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 566 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 567 | IList->setType(ExprTy); |
| 568 | StructuredList->setType(ExprTy); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 569 | if (hadError) |
| 570 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 571 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 572 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 573 | // We have leftover initializers |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 574 | if (StructuredIndex == 1 && |
| 575 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 576 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 577 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 578 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 579 | hadError = true; |
| 580 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 581 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 582 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 583 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 584 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 585 | // Don't complain for incomplete types, since we'll get an error |
| 586 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 587 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 589 | CurrentObjectType->isArrayType()? 0 : |
| 590 | CurrentObjectType->isVectorType()? 1 : |
| 591 | CurrentObjectType->isScalarType()? 2 : |
| 592 | CurrentObjectType->isUnionType()? 3 : |
| 593 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 594 | |
| 595 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 596 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 597 | DK = diag::err_excess_initializers; |
| 598 | hadError = true; |
| 599 | } |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 600 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 601 | DK = diag::err_excess_initializers; |
| 602 | hadError = true; |
| 603 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 604 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 605 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 606 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 609 | |
Eli Friedman | 0b4af8f | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 610 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 611 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 612 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 613 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 614 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 617 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 618 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 619 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 620 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 621 | unsigned &Index, |
| 622 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 623 | unsigned &StructuredIndex, |
| 624 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 625 | if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 626 | CheckScalarType(Entity, IList, DeclType, Index, |
| 627 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 628 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 629 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 630 | StructuredList, StructuredIndex); |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 631 | } else if (DeclType->isAggregateType()) { |
| 632 | if (DeclType->isRecordType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 633 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 634 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 635 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 636 | StructuredList, StructuredIndex, |
| 637 | TopLevelObject); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 638 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 639 | llvm::APSInt Zero( |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 640 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 641 | false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 643 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 644 | StructuredList, StructuredIndex); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 645 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 646 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 647 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 648 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 649 | ++Index; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 650 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 651 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 652 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 653 | } else if (DeclType->isRecordType()) { |
| 654 | // C++ [dcl.init]p14: |
| 655 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 656 | // is a brace-enclosed list, see 8.5.1. |
| 657 | // |
| 658 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 659 | // we have an initializer list and a destination type that is not |
| 660 | // an aggregate. |
| 661 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 662 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 663 | << DeclType << IList->getSourceRange(); |
| 664 | hadError = true; |
| 665 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 666 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 667 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 668 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 669 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 670 | << DeclType; |
| 671 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 672 | } else { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 673 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 674 | << DeclType; |
| 675 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 679 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 680 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 682 | unsigned &Index, |
| 683 | InitListExpr *StructuredList, |
| 684 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 685 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 686 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 687 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 688 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | InitListExpr *newStructuredList |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 690 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 691 | StructuredList, StructuredIndex, |
| 692 | SubInitList->getSourceRange()); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 693 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 694 | newStructuredList, newStructuredIndex); |
| 695 | ++StructuredIndex; |
| 696 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 697 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 698 | } else if (ElemType->isScalarType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 699 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 700 | StructuredList, StructuredIndex); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 701 | } else if (ElemType->isReferenceType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 702 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 703 | StructuredList, StructuredIndex); |
| 704 | } |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 705 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 706 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 707 | // arrayType can be incomplete if we're initializing a flexible |
| 708 | // array member. There's nothing we can do with the completed |
| 709 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 710 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 711 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 712 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 713 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 714 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 715 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 716 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 717 | |
| 718 | // Fall through for subaggregate initialization. |
| 719 | |
| 720 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 721 | // C++ [dcl.init.aggr]p12: |
| 722 | // All implicit type conversions (clause 4) are considered when |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 723 | // initializing the aggregate member with an ini- tializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 724 | // an initializer-list. If the initializer can initialize a |
| 725 | // member, the member is initialized. [...] |
| 726 | |
| 727 | // FIXME: Better EqualLoc? |
| 728 | InitializationKind Kind = |
| 729 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 730 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 731 | |
| 732 | if (Seq) { |
| 733 | ExprResult Result = |
| 734 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 735 | if (Result.isInvalid()) |
| 736 | hadError = true; |
| 737 | |
| 738 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 739 | Result.takeAs<Expr>()); |
| 740 | ++Index; |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | // Fall through for subaggregate initialization |
| 745 | } else { |
| 746 | // C99 6.7.8p13: |
| 747 | // |
| 748 | // The initializer for a structure or union object that has |
| 749 | // automatic storage duration shall be either an initializer |
| 750 | // list as described below, or a single expression that has |
| 751 | // compatible structure or union type. In the latter case, the |
| 752 | // initial value of the object, including unnamed members, is |
| 753 | // that of the expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 754 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 755 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 756 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 757 | == Sema::Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 758 | if (ExprRes.isInvalid()) |
| 759 | hadError = true; |
| 760 | else { |
| 761 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 762 | if (ExprRes.isInvalid()) |
| 763 | hadError = true; |
| 764 | } |
| 765 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 766 | ExprRes.takeAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 767 | ++Index; |
| 768 | return; |
| 769 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 770 | ExprRes.release(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 771 | // Fall through for subaggregate initialization |
| 772 | } |
| 773 | |
| 774 | // C++ [dcl.init.aggr]p12: |
| 775 | // |
| 776 | // [...] Otherwise, if the member is itself a non-empty |
| 777 | // subaggregate, brace elision is assumed and the initializer is |
| 778 | // considered for the initialization of the first member of |
| 779 | // the subaggregate. |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 780 | if (!SemaRef.getLangOptions().OpenCL && |
| 781 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 782 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 783 | StructuredIndex); |
| 784 | ++StructuredIndex; |
| 785 | } else { |
| 786 | // We cannot initialize this element, so let |
| 787 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 788 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 789 | SemaRef.Owned(expr), |
| 790 | /*TopLevelOfInitList=*/true); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 791 | hadError = true; |
| 792 | ++Index; |
| 793 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 794 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 797 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 798 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 799 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 800 | InitListExpr *StructuredList, |
| 801 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 802 | if (Index >= IList->getNumInits()) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 803 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 804 | << IList->getSourceRange(); |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 805 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 806 | ++Index; |
| 807 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 808 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 809 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 810 | |
| 811 | Expr *expr = IList->getInit(Index); |
| 812 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 813 | SemaRef.Diag(SubIList->getLocStart(), |
| 814 | diag::warn_many_braces_around_scalar_init) |
| 815 | << SubIList->getSourceRange(); |
| 816 | |
| 817 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 818 | StructuredIndex); |
| 819 | return; |
| 820 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 821 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 822 | diag::err_designator_for_scalar_init) |
| 823 | << DeclType << expr->getSourceRange(); |
| 824 | hadError = true; |
| 825 | ++Index; |
| 826 | ++StructuredIndex; |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | ExprResult Result = |
| 831 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 832 | SemaRef.Owned(expr), |
| 833 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 834 | |
| 835 | Expr *ResultExpr = 0; |
| 836 | |
| 837 | if (Result.isInvalid()) |
| 838 | hadError = true; // types weren't compatible. |
| 839 | else { |
| 840 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 841 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 842 | if (ResultExpr != expr) { |
| 843 | // The type was promoted, update initializer list. |
| 844 | IList->setInit(Index, ResultExpr); |
| 845 | } |
| 846 | } |
| 847 | if (hadError) |
| 848 | ++StructuredIndex; |
| 849 | else |
| 850 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 851 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 854 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 855 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 856 | unsigned &Index, |
| 857 | InitListExpr *StructuredList, |
| 858 | unsigned &StructuredIndex) { |
| 859 | if (Index < IList->getNumInits()) { |
| 860 | Expr *expr = IList->getInit(Index); |
| 861 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 862 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 863 | << DeclType << IList->getSourceRange(); |
| 864 | hadError = true; |
| 865 | ++Index; |
| 866 | ++StructuredIndex; |
| 867 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 869 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 870 | ExprResult Result = |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 871 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 872 | SemaRef.Owned(expr), |
| 873 | /*TopLevelOfInitList=*/true); |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 874 | |
| 875 | if (Result.isInvalid()) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 876 | hadError = true; |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 877 | |
| 878 | expr = Result.takeAs<Expr>(); |
| 879 | IList->setInit(Index, expr); |
| 880 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 881 | if (hadError) |
| 882 | ++StructuredIndex; |
| 883 | else |
| 884 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 885 | ++Index; |
| 886 | } else { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 887 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 888 | // general, it would be useful to pass location information down the stack, |
| 889 | // so that we know the location (or decl) of the "current object" being |
| 890 | // initialized. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 892 | diag::err_init_reference_member_uninitialized) |
| 893 | << DeclType |
| 894 | << IList->getSourceRange(); |
| 895 | hadError = true; |
| 896 | ++Index; |
| 897 | ++StructuredIndex; |
| 898 | return; |
| 899 | } |
| 900 | } |
| 901 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 902 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 903 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 904 | unsigned &Index, |
| 905 | InitListExpr *StructuredList, |
| 906 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 907 | if (Index >= IList->getNumInits()) |
| 908 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 910 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 911 | unsigned maxElements = VT->getNumElements(); |
| 912 | unsigned numEltsInit = 0; |
| 913 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 914 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 915 | if (!SemaRef.getLangOptions().OpenCL) { |
| 916 | // If the initializing element is a vector, try to copy-initialize |
| 917 | // instead of breaking it apart (which is doomed to failure anyway). |
| 918 | Expr *Init = IList->getInit(Index); |
| 919 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 920 | ExprResult Result = |
| 921 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 922 | SemaRef.Owned(Init), |
| 923 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 924 | |
| 925 | Expr *ResultExpr = 0; |
| 926 | if (Result.isInvalid()) |
| 927 | hadError = true; // types weren't compatible. |
| 928 | else { |
| 929 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 930 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 931 | if (ResultExpr != Init) { |
| 932 | // The type was promoted, update initializer list. |
| 933 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 936 | if (hadError) |
| 937 | ++StructuredIndex; |
| 938 | else |
| 939 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 940 | ++Index; |
| 941 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 942 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 944 | InitializedEntity ElementEntity = |
| 945 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 946 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 947 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 948 | // Don't attempt to go past the end of the init list |
| 949 | if (Index >= IList->getNumInits()) |
| 950 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 952 | ElementEntity.setElementIndex(Index); |
| 953 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 954 | StructuredList, StructuredIndex); |
| 955 | } |
| 956 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 957 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 958 | |
| 959 | InitializedEntity ElementEntity = |
| 960 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 962 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 963 | for (unsigned i = 0; i < maxElements; ++i) { |
| 964 | // Don't attempt to go past the end of the init list |
| 965 | if (Index >= IList->getNumInits()) |
| 966 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 967 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 968 | ElementEntity.setElementIndex(Index); |
| 969 | |
| 970 | QualType IType = IList->getInit(Index)->getType(); |
| 971 | if (!IType->isVectorType()) { |
| 972 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 973 | StructuredList, StructuredIndex); |
| 974 | ++numEltsInit; |
| 975 | } else { |
| 976 | QualType VecType; |
| 977 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 978 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 979 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 980 | if (IType->isExtVectorType()) |
| 981 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 982 | else |
| 983 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 984 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 985 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 986 | StructuredList, StructuredIndex); |
| 987 | numEltsInit += numIElts; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | // OpenCL requires all elements to be initialized. |
| 992 | if (numEltsInit != maxElements) |
| 993 | if (SemaRef.getLangOptions().OpenCL) |
| 994 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 995 | diag::err_vector_incorrect_num_initializers) |
| 996 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 999 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1000 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1001 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1003 | unsigned &Index, |
| 1004 | InitListExpr *StructuredList, |
| 1005 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1006 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1007 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1008 | // Check for the special-case of initializing an array with a string. |
| 1009 | if (Index < IList->getNumInits()) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1010 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1011 | SemaRef.Context)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1012 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1013 | // We place the string literal directly into the resulting |
| 1014 | // initializer list. This is the only place where the structure |
| 1015 | // of the structured initializer list doesn't match exactly, |
| 1016 | // because doing so would involve allocating one character |
| 1017 | // constant for each string. |
Chris Lattner | edbf3ba | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 1018 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1019 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1020 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1021 | return; |
| 1022 | } |
| 1023 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1024 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1025 | // Check for VLAs; in standard C it would be possible to check this |
| 1026 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1027 | // them in all sorts of strange places). |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1028 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1029 | diag::err_variable_object_no_init) |
| 1030 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1031 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1032 | ++Index; |
| 1033 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | } |
| 1036 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1037 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1038 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1039 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1040 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1041 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1042 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1043 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1044 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1045 | maxElementsKnown = true; |
| 1046 | } |
| 1047 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1048 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1049 | while (Index < IList->getNumInits()) { |
| 1050 | Expr *Init = IList->getInit(Index); |
| 1051 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1052 | // If we're not the subobject that matches up with the '{' for |
| 1053 | // the designator, we shouldn't be handling the |
| 1054 | // designator. Return immediately. |
| 1055 | if (!SubobjectIsDesignatorContext) |
| 1056 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1057 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1058 | // Handle this designated initializer. elementIndex will be |
| 1059 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1060 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1061 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1062 | StructuredList, StructuredIndex, true, |
| 1063 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1064 | hadError = true; |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1068 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1069 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1070 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1071 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1072 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1074 | // If the array is of incomplete type, keep track of the number of |
| 1075 | // elements in the initializer. |
| 1076 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1077 | maxElements = elementIndex; |
| 1078 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | // If we know the maximum number of elements, and we've already |
| 1083 | // hit it, stop consuming elements in the initializer list. |
| 1084 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1085 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1086 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1087 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1089 | Entity); |
| 1090 | // Check this element. |
| 1091 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1092 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1093 | ++elementIndex; |
| 1094 | |
| 1095 | // If the array is of incomplete type, keep track of the number of |
| 1096 | // elements in the initializer. |
| 1097 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1098 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1099 | } |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1100 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1101 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1102 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1103 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1104 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1105 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1106 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1107 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1108 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1109 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1110 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1112 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1116 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1117 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1119 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1121 | unsigned &Index, |
| 1122 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1123 | unsigned &StructuredIndex, |
| 1124 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1125 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1127 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1128 | // confusion, we forgo checking the intializer for the entire record. |
| 1129 | if (structDecl->isInvalidDecl()) { |
| 1130 | hadError = true; |
| 1131 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1135 | // Value-initialize the first named member of the union. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1136 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1137 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1138 | Field != FieldEnd; ++Field) { |
| 1139 | if (Field->getDeclName()) { |
| 1140 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1141 | break; |
| 1142 | } |
| 1143 | } |
| 1144 | return; |
| 1145 | } |
| 1146 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1147 | // If structDecl is a forward declaration, this loop won't do |
| 1148 | // anything except look at designated initializers; That's okay, |
| 1149 | // because an error should get printed out elsewhere. It might be |
| 1150 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1151 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1152 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1153 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1154 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1155 | while (Index < IList->getNumInits()) { |
| 1156 | Expr *Init = IList->getInit(Index); |
| 1157 | |
| 1158 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1159 | // If we're not the subobject that matches up with the '{' for |
| 1160 | // the designator, we shouldn't be handling the |
| 1161 | // designator. Return immediately. |
| 1162 | if (!SubobjectIsDesignatorContext) |
| 1163 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1164 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1165 | // Handle this designated initializer. Field will be updated to |
| 1166 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1167 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1168 | DeclType, &Field, 0, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1169 | StructuredList, StructuredIndex, |
| 1170 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1171 | hadError = true; |
| 1172 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1173 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Disable check for missing fields when designators are used. |
| 1176 | // This matches gcc behaviour. |
| 1177 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1178 | continue; |
| 1179 | } |
| 1180 | |
| 1181 | if (Field == FieldEnd) { |
| 1182 | // We've run out of fields. We're done. |
| 1183 | break; |
| 1184 | } |
| 1185 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1186 | // We've already initialized a member of a union. We're done. |
| 1187 | if (InitializedSomething && DeclType->isUnionType()) |
| 1188 | break; |
| 1189 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1190 | // If we've hit the flexible array member at the end, we're done. |
| 1191 | if (Field->getType()->isIncompleteArrayType()) |
| 1192 | break; |
| 1193 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1194 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1195 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1196 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1197 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1198 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1200 | // Make sure we can use this declaration. |
| 1201 | if (SemaRef.DiagnoseUseOfDecl(*Field, |
| 1202 | IList->getInit(Index)->getLocStart())) { |
| 1203 | ++Index; |
| 1204 | ++Field; |
| 1205 | hadError = true; |
| 1206 | continue; |
| 1207 | } |
| 1208 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1209 | InitializedEntity MemberEntity = |
| 1210 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1211 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1212 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1213 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1214 | |
| 1215 | if (DeclType->isUnionType()) { |
| 1216 | // Initialize the first field within the union. |
| 1217 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1218 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1219 | |
| 1220 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1221 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1222 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1223 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1224 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1225 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1226 | // It is possible we have one or more unnamed bitfields remaining. |
| 1227 | // Find first (if any) named field and emit warning. |
| 1228 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1229 | it != end; ++it) { |
| 1230 | if (!it->isUnnamedBitfield()) { |
| 1231 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1232 | diag::warn_missing_field_initializers) << it->getName(); |
| 1233 | break; |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1239 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1240 | return; |
| 1241 | |
| 1242 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1243 | if (!TopLevelObject && |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1244 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1245 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1247 | diag::err_flexible_array_init_nonempty) |
| 1248 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1249 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1250 | << *Field; |
| 1251 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1252 | ++Index; |
| 1253 | return; |
| 1254 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1256 | diag::ext_flexible_array_init) |
| 1257 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1258 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1259 | << *Field; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1262 | InitializedEntity MemberEntity = |
| 1263 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1264 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1265 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1266 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1267 | StructuredList, StructuredIndex); |
| 1268 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1269 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1270 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1271 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1272 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1273 | /// \brief Expand a field designator that refers to a member of an |
| 1274 | /// anonymous struct or union into a series of field designators that |
| 1275 | /// refers to the field within the appropriate subobject. |
| 1276 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1277 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | DesignatedInitExpr *DIE, |
| 1279 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1280 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1281 | typedef DesignatedInitExpr::Designator Designator; |
| 1282 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1283 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1284 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1285 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1286 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1287 | if (PI + 1 == PE) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1289 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1290 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1291 | else |
| 1292 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1293 | SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1294 | assert(isa<FieldDecl>(*PI)); |
| 1295 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | // Expand the current designator into the set of replacement |
| 1299 | // designators, so we have a full subobject path down to where the |
| 1300 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1301 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1302 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1303 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1305 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1306 | /// corresponds to FieldName. |
| 1307 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1308 | IdentifierInfo *FieldName) { |
| 1309 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1310 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1311 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1312 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1313 | return IF; |
| 1314 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1315 | } |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1316 | return 0; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1319 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1320 | /// |
| 1321 | /// Determines whether the designated initializer @p DIE, which |
| 1322 | /// resides at the given @p Index within the initializer list @p |
| 1323 | /// IList, is well-formed for a current object of type @p DeclType |
| 1324 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1326 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1327 | /// |
| 1328 | /// @param IList The initializer list in which this designated |
| 1329 | /// initializer occurs. |
| 1330 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1331 | /// @param DIE The designated initializer expression. |
| 1332 | /// |
| 1333 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1334 | /// |
| 1335 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1336 | /// into which the designation in @p DIE should refer. |
| 1337 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1338 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1339 | /// a field, this will be set to the field declaration corresponding |
| 1340 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1341 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1342 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1343 | /// DIE is an array designator or GNU array-range designator, this |
| 1344 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1345 | /// |
| 1346 | /// @param Index Index into @p IList where the designated initializer |
| 1347 | /// @p DIE occurs. |
| 1348 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1349 | /// @param StructuredList The initializer list expression that |
| 1350 | /// describes all of the subobject initializers in the order they'll |
| 1351 | /// actually be initialized. |
| 1352 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1353 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1355 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1356 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1357 | DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1358 | unsigned DesigIdx, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1359 | QualType &CurrentObjectType, |
| 1360 | RecordDecl::field_iterator *NextField, |
| 1361 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1362 | unsigned &Index, |
| 1363 | InitListExpr *StructuredList, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1364 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1365 | bool FinishSubobjectInit, |
| 1366 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1367 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1368 | // Check the actual initialization for the designated object type. |
| 1369 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1370 | |
| 1371 | // Temporarily remove the designator expression from the |
| 1372 | // initializer list that the child calls see, so that we don't try |
| 1373 | // to re-process the designator. |
| 1374 | unsigned OldIndex = Index; |
| 1375 | IList->setInit(OldIndex, DIE->getInit()); |
| 1376 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1377 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1378 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1379 | |
| 1380 | // Restore the designated initializer expression in the syntactic |
| 1381 | // form of the initializer list. |
| 1382 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1383 | DIE->setInit(IList->getInit(OldIndex)); |
| 1384 | IList->setInit(OldIndex, DIE); |
| 1385 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1386 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1389 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1391 | "Need a non-designated initializer list to start from"); |
| 1392 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1393 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1394 | // Determine the structural initializer list that corresponds to the |
| 1395 | // current subobject. |
| 1396 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1398 | StructuredList, StructuredIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1399 | SourceRange(D->getStartLocation(), |
| 1400 | DIE->getSourceRange().getEnd())); |
| 1401 | assert(StructuredList && "Expected a structured initializer list"); |
| 1402 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1403 | if (D->isFieldDesignator()) { |
| 1404 | // C99 6.7.8p7: |
| 1405 | // |
| 1406 | // If a designator has the form |
| 1407 | // |
| 1408 | // . identifier |
| 1409 | // |
| 1410 | // then the current object (defined below) shall have |
| 1411 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1413 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1414 | if (!RT) { |
| 1415 | SourceLocation Loc = D->getDotLoc(); |
| 1416 | if (Loc.isInvalid()) |
| 1417 | Loc = D->getFieldLoc(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1418 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1419 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1420 | ++Index; |
| 1421 | return true; |
| 1422 | } |
| 1423 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1424 | // Note: we perform a linear search of the fields here, despite |
| 1425 | // the fact that we have a faster lookup method, because we always |
| 1426 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1427 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1428 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1429 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1431 | Field = RT->getDecl()->field_begin(), |
| 1432 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1433 | for (; Field != FieldEnd; ++Field) { |
| 1434 | if (Field->isUnnamedBitfield()) |
| 1435 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1436 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1437 | // If we find a field representing an anonymous field, look in the |
| 1438 | // IndirectFieldDecl that follow for the designated initializer. |
| 1439 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1440 | if (IndirectFieldDecl *IF = |
| 1441 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1442 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1443 | D = DIE->getDesignator(DesigIdx); |
| 1444 | break; |
| 1445 | } |
| 1446 | } |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1447 | if (KnownField && KnownField == *Field) |
| 1448 | break; |
| 1449 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1450 | break; |
| 1451 | |
| 1452 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1455 | if (Field == FieldEnd) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1456 | // There was no normal field in the struct with the designated |
| 1457 | // name. Perform another lookup for this name, which may find |
| 1458 | // something that we can't designate (e.g., a member function), |
| 1459 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1461 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1462 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1463 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1464 | // Name lookup didn't find anything. Determine whether this |
| 1465 | // was a typo for another field name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1466 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1467 | Sema::LookupMemberName); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1468 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1469 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1470 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1471 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1472 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1473 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1474 | ->Equals(RT->getDecl())) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1475 | std::string CorrectedStr( |
| 1476 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1477 | std::string CorrectedQuotedStr( |
| 1478 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1479 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1480 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1481 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1482 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1483 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1484 | diag::note_previous_decl) << CorrectedQuotedStr; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1485 | } else { |
| 1486 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1487 | << FieldName << CurrentObjectType; |
| 1488 | ++Index; |
| 1489 | return true; |
| 1490 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1491 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1493 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1494 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1495 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1496 | << FieldName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1498 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1499 | ++Index; |
| 1500 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1501 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1502 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1503 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1504 | // The replacement field comes from typo correction; find it |
| 1505 | // in the list of fields. |
| 1506 | FieldIndex = 0; |
| 1507 | Field = RT->getDecl()->field_begin(); |
| 1508 | for (; Field != FieldEnd; ++Field) { |
| 1509 | if (Field->isUnnamedBitfield()) |
| 1510 | continue; |
| 1511 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1512 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1513 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1514 | break; |
| 1515 | |
| 1516 | ++FieldIndex; |
| 1517 | } |
| 1518 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1519 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1520 | |
| 1521 | // All of the fields of a union are located at the same place in |
| 1522 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1523 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1524 | FieldIndex = 0; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1525 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1526 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1527 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1528 | // Make sure we can use this declaration. |
| 1529 | if (SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc())) { |
| 1530 | ++Index; |
| 1531 | return true; |
| 1532 | } |
| 1533 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1534 | // Update the designator with the field declaration. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1535 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1537 | // Make sure that our non-designated initializer list has space |
| 1538 | // for a subobject corresponding to this field. |
| 1539 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1540 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1541 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1542 | // This designator names a flexible array member. |
| 1543 | if (Field->getType()->isIncompleteArrayType()) { |
| 1544 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1545 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1546 | // We can't designate an object within the flexible array |
| 1547 | // member (because GCC doesn't allow it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1549 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1551 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1553 | DIE->getSourceRange().getEnd()); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1554 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1555 | << *Field; |
| 1556 | Invalid = true; |
| 1557 | } |
| 1558 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1559 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1560 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1561 | // The initializer is not an initializer list. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1562 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1563 | diag::err_flexible_array_init_needs_braces) |
| 1564 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1565 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1566 | << *Field; |
| 1567 | Invalid = true; |
| 1568 | } |
| 1569 | |
| 1570 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1572 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1574 | diag::err_flexible_array_init_nonempty) |
| 1575 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1576 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1577 | << *Field; |
| 1578 | Invalid = true; |
| 1579 | } |
| 1580 | |
| 1581 | if (Invalid) { |
| 1582 | ++Index; |
| 1583 | return true; |
| 1584 | } |
| 1585 | |
| 1586 | // Initialize the array. |
| 1587 | bool prevHadError = hadError; |
| 1588 | unsigned newStructuredIndex = FieldIndex; |
| 1589 | unsigned OldIndex = Index; |
| 1590 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1591 | |
| 1592 | InitializedEntity MemberEntity = |
| 1593 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1594 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1595 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1596 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1597 | IList->setInit(OldIndex, DIE); |
| 1598 | if (hadError && !prevHadError) { |
| 1599 | ++Field; |
| 1600 | ++FieldIndex; |
| 1601 | if (NextField) |
| 1602 | *NextField = Field; |
| 1603 | StructuredIndex = FieldIndex; |
| 1604 | return true; |
| 1605 | } |
| 1606 | } else { |
| 1607 | // Recurse to check later designated subobjects. |
| 1608 | QualType FieldType = (*Field)->getType(); |
| 1609 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1610 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1611 | InitializedEntity MemberEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1612 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1613 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1614 | FieldType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1615 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1616 | true, false)) |
| 1617 | return true; |
| 1618 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1619 | |
| 1620 | // Find the position of the next field to be initialized in this |
| 1621 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1622 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1623 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1624 | |
| 1625 | // If this the first designator, our caller will continue checking |
| 1626 | // the rest of this struct/class/union subobject. |
| 1627 | if (IsFirstDesignator) { |
| 1628 | if (NextField) |
| 1629 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1630 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1631 | return false; |
| 1632 | } |
| 1633 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1634 | if (!FinishSubobjectInit) |
| 1635 | return false; |
| 1636 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1637 | // We've already initialized something in the union; we're done. |
| 1638 | if (RT->getDecl()->isUnion()) |
| 1639 | return hadError; |
| 1640 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1641 | // Check the remaining fields within this class/struct/union subobject. |
| 1642 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1643 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1644 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1645 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1646 | return hadError && !prevHadError; |
| 1647 | } |
| 1648 | |
| 1649 | // C99 6.7.8p6: |
| 1650 | // |
| 1651 | // If a designator has the form |
| 1652 | // |
| 1653 | // [ constant-expression ] |
| 1654 | // |
| 1655 | // then the current object (defined below) shall have array |
| 1656 | // type and the expression shall be an integer constant |
| 1657 | // expression. If the array is of unknown size, any |
| 1658 | // nonnegative value is valid. |
| 1659 | // |
| 1660 | // Additionally, cope with the GNU extension that permits |
| 1661 | // designators of the form |
| 1662 | // |
| 1663 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1664 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1665 | if (!AT) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1666 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1667 | << CurrentObjectType; |
| 1668 | ++Index; |
| 1669 | return true; |
| 1670 | } |
| 1671 | |
| 1672 | Expr *IndexExpr = 0; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1673 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1674 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1675 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1676 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1677 | DesignatedEndIndex = DesignatedStartIndex; |
| 1678 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1679 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1680 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | DesignatedStartIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1682 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | DesignatedEndIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1684 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1685 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1686 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1687 | // Codegen can't handle evaluating array range designators that have side |
| 1688 | // effects, because we replicate the AST value for each initialized element. |
| 1689 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1690 | // elements with something that has a side effect, so codegen can emit an |
| 1691 | // "error unsupported" error instead of miscompiling the app. |
| 1692 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1693 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1694 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1697 | if (isa<ConstantArrayType>(AT)) { |
| 1698 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1699 | DesignatedStartIndex |
| 1700 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1701 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1702 | DesignatedEndIndex |
| 1703 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1704 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1705 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1706 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1707 | diag::err_array_designator_too_large) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1708 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1709 | << IndexExpr->getSourceRange(); |
| 1710 | ++Index; |
| 1711 | return true; |
| 1712 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1713 | } else { |
| 1714 | // Make sure the bit-widths and signedness match. |
| 1715 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1716 | DesignatedEndIndex |
| 1717 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1718 | else if (DesignatedStartIndex.getBitWidth() < |
| 1719 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1720 | DesignatedStartIndex |
| 1721 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1722 | DesignatedStartIndex.setIsUnsigned(true); |
| 1723 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1724 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1726 | // Make sure that our non-designated initializer list has space |
| 1727 | // for a subobject corresponding to this array element. |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1728 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1730 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1731 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1732 | // Repeatedly perform subobject initializations in the range |
| 1733 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1735 | // Move to the next designator |
| 1736 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1737 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1738 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1739 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1740 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1741 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1742 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1743 | // Recurse to check later designated subobjects. |
| 1744 | QualType ElementType = AT->getElementType(); |
| 1745 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1746 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1747 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1748 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1749 | ElementType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1750 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1751 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1752 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1753 | return true; |
| 1754 | |
| 1755 | // Move to the next index in the array that we'll be initializing. |
| 1756 | ++DesignatedStartIndex; |
| 1757 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1758 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1759 | |
| 1760 | // If this the first designator, our caller will continue checking |
| 1761 | // the rest of this array subobject. |
| 1762 | if (IsFirstDesignator) { |
| 1763 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1764 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1765 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1766 | return false; |
| 1767 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1768 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1769 | if (!FinishSubobjectInit) |
| 1770 | return false; |
| 1771 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1772 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1773 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1774 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1775 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1776 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1780 | // Get the structured initializer list for a subobject of type |
| 1781 | // @p CurrentObjectType. |
| 1782 | InitListExpr * |
| 1783 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1784 | QualType CurrentObjectType, |
| 1785 | InitListExpr *StructuredList, |
| 1786 | unsigned StructuredIndex, |
| 1787 | SourceRange InitRange) { |
| 1788 | Expr *ExistingInit = 0; |
| 1789 | if (!StructuredList) |
| 1790 | ExistingInit = SyntacticToSemantic[IList]; |
| 1791 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1792 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1794 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1795 | return Result; |
| 1796 | |
| 1797 | if (ExistingInit) { |
| 1798 | // We are creating an initializer list that initializes the |
| 1799 | // subobjects of the current object, but there was already an |
| 1800 | // initialization that completely initialized the current |
| 1801 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1803 | // struct X { int a, b; }; |
| 1804 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1806 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1807 | // designated initializer re-initializes the whole |
| 1808 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1810 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1811 | << InitRange; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1813 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1814 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1815 | << ExistingInit->getSourceRange(); |
| 1816 | } |
| 1817 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1819 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1820 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1821 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1823 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1825 | // Pre-allocate storage for the structured initializer list. |
| 1826 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1827 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1828 | bool GotNumInits = false; |
| 1829 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1830 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1831 | GotNumInits = true; |
| 1832 | } else if (Index < IList->getNumInits()) { |
| 1833 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1834 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1835 | GotNumInits = true; |
| 1836 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1840 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1841 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1842 | NumElements = CAType->getSize().getZExtValue(); |
| 1843 | // Simple heuristic so that we don't allocate a very large |
| 1844 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1845 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1846 | NumElements = 0; |
| 1847 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1848 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1849 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1850 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1851 | RecordDecl *RDecl = RType->getDecl(); |
| 1852 | if (RDecl->isUnion()) |
| 1853 | NumElements = 1; |
| 1854 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1856 | RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1859 | if (NumElements < NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1860 | NumElements = IList->getNumInits(); |
| 1861 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1862 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1863 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1864 | // Link this new initializer list into the structured initializer |
| 1865 | // lists. |
| 1866 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1867 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1868 | else { |
| 1869 | Result->setSyntacticForm(IList); |
| 1870 | SyntacticToSemantic[IList] = Result; |
| 1871 | } |
| 1872 | |
| 1873 | return Result; |
| 1874 | } |
| 1875 | |
| 1876 | /// Update the initializer at index @p StructuredIndex within the |
| 1877 | /// structured initializer list to the value @p expr. |
| 1878 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1879 | unsigned &StructuredIndex, |
| 1880 | Expr *expr) { |
| 1881 | // No structured initializer list to update |
| 1882 | if (!StructuredList) |
| 1883 | return; |
| 1884 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1885 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1886 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1887 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1889 | diag::warn_initializer_overrides) |
| 1890 | << expr->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1891 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1892 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1893 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1894 | << PrevInit->getSourceRange(); |
| 1895 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1897 | ++StructuredIndex; |
| 1898 | } |
| 1899 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1900 | /// Check that the given Index expression is a valid array designator |
| 1901 | /// value. This is essentailly just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1902 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1903 | /// and produces a reasonable diagnostic if there is a |
| 1904 | /// failure. Returns true if there was an error, false otherwise. If |
| 1905 | /// everything went okay, Value will receive the value of the constant |
| 1906 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | static bool |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1908 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1909 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1910 | |
| 1911 | // Make sure this is an integer constant expression. |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1912 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1913 | return true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1914 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1915 | if (Value.isSigned() && Value.isNegative()) |
| 1916 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1917 | << Value.toString(10) << Index->getSourceRange(); |
| 1918 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1919 | Value.setIsUnsigned(true); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1920 | return false; |
| 1921 | } |
| 1922 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1923 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1924 | SourceLocation Loc, |
| 1925 | bool GNUSyntax, |
| 1926 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1927 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1928 | |
| 1929 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1930 | SmallVector<ASTDesignator, 32> Designators; |
| 1931 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1932 | |
| 1933 | // Build designators and check array designator expressions. |
| 1934 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1935 | const Designator &D = Desig.getDesignator(Idx); |
| 1936 | switch (D.getKind()) { |
| 1937 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1939 | D.getFieldLoc())); |
| 1940 | break; |
| 1941 | |
| 1942 | case Designator::ArrayDesignator: { |
| 1943 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1944 | llvm::APSInt IndexValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1945 | if (!Index->isTypeDependent() && |
| 1946 | !Index->isValueDependent() && |
| 1947 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1948 | Invalid = true; |
| 1949 | else { |
| 1950 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1952 | D.getRBracketLoc())); |
| 1953 | InitExpressions.push_back(Index); |
| 1954 | } |
| 1955 | break; |
| 1956 | } |
| 1957 | |
| 1958 | case Designator::ArrayRangeDesignator: { |
| 1959 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1960 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1961 | llvm::APSInt StartValue; |
| 1962 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1963 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1964 | StartIndex->isValueDependent(); |
| 1965 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1966 | EndIndex->isValueDependent(); |
| 1967 | if ((!StartDependent && |
| 1968 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1969 | (!EndDependent && |
| 1970 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1971 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1972 | else { |
| 1973 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1974 | if (StartDependent || EndDependent) { |
| 1975 | // Nothing to compute. |
| 1976 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1977 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1978 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1979 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1981 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1982 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1984 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1985 | Invalid = true; |
| 1986 | } else { |
| 1987 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1989 | D.getEllipsisLoc(), |
| 1990 | D.getRBracketLoc())); |
| 1991 | InitExpressions.push_back(StartIndex); |
| 1992 | InitExpressions.push_back(EndIndex); |
| 1993 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1994 | } |
| 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | if (Invalid || Init.isInvalid()) |
| 2001 | return ExprError(); |
| 2002 | |
| 2003 | // Clear out the expressions within the designation. |
| 2004 | Desig.ClearExprs(*this); |
| 2005 | |
| 2006 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2007 | = DesignatedInitExpr::Create(Context, |
| 2008 | Designators.data(), Designators.size(), |
| 2009 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2010 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2012 | if (getLangOptions().CPlusPlus) |
Eli Friedman | ea7b85b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2013 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2014 | << DIE->getSourceRange(); |
| 2015 | else if (!getLangOptions().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2016 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2017 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2018 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2019 | return Owned(DIE); |
| 2020 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2021 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2022 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 2023 | InitListExpr *&InitList, QualType &DeclType) { |
| 2024 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2025 | if (!CheckInitList.HadError()) |
| 2026 | InitList = CheckInitList.getFullyStructuredList(); |
| 2027 | |
| 2028 | return CheckInitList.HadError(); |
| 2029 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2031 | //===----------------------------------------------------------------------===// |
| 2032 | // Initialization entity |
| 2033 | //===----------------------------------------------------------------------===// |
| 2034 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2035 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2036 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2037 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2038 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2039 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2040 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2041 | Type = AT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2042 | } else { |
| 2043 | Kind = EK_VectorElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2044 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2045 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2048 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2049 | CXXBaseSpecifier *Base, |
| 2050 | bool IsInheritedVirtualBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2051 | { |
| 2052 | InitializedEntity Result; |
| 2053 | Result.Kind = EK_Base; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2054 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2055 | if (IsInheritedVirtualBase) |
| 2056 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2058 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2059 | return Result; |
| 2060 | } |
| 2061 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2062 | DeclarationName InitializedEntity::getName() const { |
| 2063 | switch (getKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2064 | case EK_Parameter: { |
| 2065 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2066 | return (D ? D->getDeclName() : DeclarationName()); |
| 2067 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2068 | |
| 2069 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2070 | case EK_Member: |
| 2071 | return VariableOrMember->getDeclName(); |
| 2072 | |
| 2073 | case EK_Result: |
| 2074 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2075 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2076 | case EK_Temporary: |
| 2077 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2078 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2079 | case EK_ArrayElement: |
| 2080 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2081 | case EK_BlockElement: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2082 | return DeclarationName(); |
| 2083 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2084 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2085 | // Silence GCC warning |
| 2086 | return DeclarationName(); |
| 2087 | } |
| 2088 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2089 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2090 | switch (getKind()) { |
| 2091 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2092 | case EK_Member: |
| 2093 | return VariableOrMember; |
| 2094 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2095 | case EK_Parameter: |
| 2096 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2097 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2098 | case EK_Result: |
| 2099 | case EK_Exception: |
| 2100 | case EK_New: |
| 2101 | case EK_Temporary: |
| 2102 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2103 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2104 | case EK_ArrayElement: |
| 2105 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2106 | case EK_BlockElement: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2107 | return 0; |
| 2108 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2109 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2110 | // Silence GCC warning |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2114 | bool InitializedEntity::allowsNRVO() const { |
| 2115 | switch (getKind()) { |
| 2116 | case EK_Result: |
| 2117 | case EK_Exception: |
| 2118 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2120 | case EK_Variable: |
| 2121 | case EK_Parameter: |
| 2122 | case EK_Member: |
| 2123 | case EK_New: |
| 2124 | case EK_Temporary: |
| 2125 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2126 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2127 | case EK_ArrayElement: |
| 2128 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2129 | case EK_BlockElement: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2130 | break; |
| 2131 | } |
| 2132 | |
| 2133 | return false; |
| 2134 | } |
| 2135 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2136 | //===----------------------------------------------------------------------===// |
| 2137 | // Initialization sequence |
| 2138 | //===----------------------------------------------------------------------===// |
| 2139 | |
| 2140 | void InitializationSequence::Step::Destroy() { |
| 2141 | switch (Kind) { |
| 2142 | case SK_ResolveAddressOfOverloadedFunction: |
| 2143 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2144 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2145 | case SK_CastDerivedToBaseLValue: |
| 2146 | case SK_BindReference: |
| 2147 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2148 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2149 | case SK_UserConversion: |
| 2150 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2151 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2152 | case SK_QualificationConversionLValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2153 | case SK_ListInitialization: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2154 | case SK_ConstructorInitialization: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2155 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2156 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2157 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2158 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2159 | case SK_ArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2160 | case SK_PassByIndirectCopyRestore: |
| 2161 | case SK_PassByIndirectRestore: |
| 2162 | case SK_ProduceObjCObject: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2163 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2165 | case SK_ConversionSequence: |
| 2166 | delete ICS; |
| 2167 | } |
| 2168 | } |
| 2169 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2170 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2171 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2175 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2176 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2178 | switch (getFailureKind()) { |
| 2179 | case FK_TooManyInitsForReference: |
| 2180 | case FK_ArrayNeedsInitList: |
| 2181 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2182 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2183 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2184 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2185 | case FK_RValueReferenceBindingToLValue: |
| 2186 | case FK_ReferenceInitDropsQualifiers: |
| 2187 | case FK_ReferenceInitFailed: |
| 2188 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2189 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2190 | case FK_TooManyInitsForScalar: |
| 2191 | case FK_ReferenceBindingToInitList: |
| 2192 | case FK_InitListBadDestinationType: |
| 2193 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2194 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2195 | case FK_ArrayTypeMismatch: |
| 2196 | case FK_NonConstantArrayInit: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2197 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2199 | case FK_ReferenceInitOverloadFailed: |
| 2200 | case FK_UserConversionOverloadFailed: |
| 2201 | case FK_ConstructorOverloadFailed: |
| 2202 | return FailedOverloadResult == OR_Ambiguous; |
| 2203 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2205 | return false; |
| 2206 | } |
| 2207 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2208 | bool InitializationSequence::isConstructorInitialization() const { |
| 2209 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2210 | } |
| 2211 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2212 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2213 | const Expr *Initializer, |
| 2214 | bool *isInitializerConstant, |
| 2215 | APValue *ConstantValue) const { |
| 2216 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2217 | return false; |
| 2218 | |
| 2219 | const Step &LastStep = Steps.back(); |
| 2220 | if (LastStep.Kind != SK_ConversionSequence) |
| 2221 | return false; |
| 2222 | |
| 2223 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2224 | const StandardConversionSequence *SCS = NULL; |
| 2225 | switch (ICS.getKind()) { |
| 2226 | case ImplicitConversionSequence::StandardConversion: |
| 2227 | SCS = &ICS.Standard; |
| 2228 | break; |
| 2229 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2230 | SCS = &ICS.UserDefined.After; |
| 2231 | break; |
| 2232 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2233 | case ImplicitConversionSequence::EllipsisConversion: |
| 2234 | case ImplicitConversionSequence::BadConversion: |
| 2235 | return false; |
| 2236 | } |
| 2237 | |
| 2238 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2239 | // [dcl.init.list]p7: |
| 2240 | // |
| 2241 | // A narrowing conversion is an implicit conversion ... |
| 2242 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2243 | QualType FromType = SCS->getToType(0); |
| 2244 | QualType ToType = SCS->getToType(1); |
| 2245 | switch (PossibleNarrowing) { |
| 2246 | // * from a floating-point type to an integer type, or |
| 2247 | // |
| 2248 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2249 | // type, except where the source is a constant expression and the actual |
| 2250 | // value after conversion will fit into the target type and will produce |
| 2251 | // the original value when converted back to the original type, or |
| 2252 | case ICK_Floating_Integral: |
| 2253 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2254 | *isInitializerConstant = false; |
| 2255 | return true; |
| 2256 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2257 | llvm::APSInt IntConstantValue; |
| 2258 | if (Initializer && |
| 2259 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2260 | // Convert the integer to the floating type. |
| 2261 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2262 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2263 | llvm::APFloat::rmNearestTiesToEven); |
| 2264 | // And back. |
| 2265 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2266 | bool ignored; |
| 2267 | Result.convertToInteger(ConvertedValue, |
| 2268 | llvm::APFloat::rmTowardZero, &ignored); |
| 2269 | // If the resulting value is different, this was a narrowing conversion. |
| 2270 | if (IntConstantValue != ConvertedValue) { |
| 2271 | *isInitializerConstant = true; |
| 2272 | *ConstantValue = APValue(IntConstantValue); |
| 2273 | return true; |
| 2274 | } |
| 2275 | } else { |
| 2276 | // Variables are always narrowings. |
| 2277 | *isInitializerConstant = false; |
| 2278 | return true; |
| 2279 | } |
| 2280 | } |
| 2281 | return false; |
| 2282 | |
| 2283 | // * from long double to double or float, or from double to float, except |
| 2284 | // where the source is a constant expression and the actual value after |
| 2285 | // conversion is within the range of values that can be represented (even |
| 2286 | // if it cannot be represented exactly), or |
| 2287 | case ICK_Floating_Conversion: |
| 2288 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2289 | // FromType is larger than ToType. |
| 2290 | Expr::EvalResult InitializerValue; |
| 2291 | // FIXME: Check whether Initializer is a constant expression according |
| 2292 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2293 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2294 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2295 | // Constant! (Except for FIXME above.) |
| 2296 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2297 | // Convert the source value into the target type. |
| 2298 | bool ignored; |
| 2299 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2300 | Ctx.getFloatTypeSemantics(ToType), |
| 2301 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2302 | // If there was no overflow, the source value is within the range of |
| 2303 | // values that can be represented. |
| 2304 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2305 | *isInitializerConstant = true; |
| 2306 | *ConstantValue = InitializerValue.Val; |
| 2307 | return true; |
| 2308 | } |
| 2309 | } else { |
| 2310 | *isInitializerConstant = false; |
| 2311 | return true; |
| 2312 | } |
| 2313 | } |
| 2314 | return false; |
| 2315 | |
| 2316 | // * from an integer type or unscoped enumeration type to an integer type |
| 2317 | // that cannot represent all the values of the original type, except where |
| 2318 | // the source is a constant expression and the actual value after |
| 2319 | // conversion will fit into the target type and will produce the original |
| 2320 | // value when converted back to the original type. |
Jeffrey Yasskin | 94f8c77 | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2321 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2322 | case ICK_Integral_Conversion: { |
| 2323 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2324 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2325 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2326 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2327 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2328 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2329 | |
| 2330 | if (FromWidth > ToWidth || |
| 2331 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2332 | // Not all values of FromType can be represented in ToType. |
| 2333 | llvm::APSInt InitializerValue; |
| 2334 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2335 | *isInitializerConstant = true; |
| 2336 | *ConstantValue = APValue(InitializerValue); |
| 2337 | |
| 2338 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2339 | // signed vs. unsigned comparisons. |
| 2340 | InitializerValue = InitializerValue.extend( |
| 2341 | InitializerValue.getBitWidth() + 1); |
| 2342 | // Convert the initializer to and from the target width and signed-ness. |
| 2343 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2344 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2345 | ConvertedValue.setIsSigned(ToSigned); |
| 2346 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2347 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2348 | // If the result is different, this was a narrowing conversion. |
| 2349 | return ConvertedValue != InitializerValue; |
| 2350 | } else { |
| 2351 | // Variables are always narrowings. |
| 2352 | *isInitializerConstant = false; |
| 2353 | return true; |
| 2354 | } |
| 2355 | } |
| 2356 | return false; |
| 2357 | } |
| 2358 | |
| 2359 | default: |
| 2360 | // Other kinds of conversions are not narrowings. |
| 2361 | return false; |
| 2362 | } |
| 2363 | } |
| 2364 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2365 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2366 | FunctionDecl *Function, |
| 2367 | DeclAccessPair Found) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2368 | Step S; |
| 2369 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2370 | S.Type = Function->getType(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2371 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2372 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2373 | Steps.push_back(S); |
| 2374 | } |
| 2375 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2376 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2377 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2378 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2379 | switch (VK) { |
| 2380 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2381 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2382 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2383 | default: llvm_unreachable("No such category"); |
| 2384 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2385 | S.Type = BaseType; |
| 2386 | Steps.push_back(S); |
| 2387 | } |
| 2388 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2389 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2390 | bool BindingTemporary) { |
| 2391 | Step S; |
| 2392 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2393 | S.Type = T; |
| 2394 | Steps.push_back(S); |
| 2395 | } |
| 2396 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2397 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2398 | Step S; |
| 2399 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2400 | S.Type = T; |
| 2401 | Steps.push_back(S); |
| 2402 | } |
| 2403 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2404 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2405 | DeclAccessPair FoundDecl, |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2406 | QualType T) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2407 | Step S; |
| 2408 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2409 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2410 | S.Function.Function = Function; |
| 2411 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2412 | Steps.push_back(S); |
| 2413 | } |
| 2414 | |
| 2415 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2416 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2417 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2418 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2419 | switch (VK) { |
| 2420 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2421 | S.Kind = SK_QualificationConversionRValue; |
| 2422 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2423 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2424 | S.Kind = SK_QualificationConversionXValue; |
| 2425 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2426 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2427 | S.Kind = SK_QualificationConversionLValue; |
| 2428 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2429 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2430 | S.Type = Ty; |
| 2431 | Steps.push_back(S); |
| 2432 | } |
| 2433 | |
| 2434 | void InitializationSequence::AddConversionSequenceStep( |
| 2435 | const ImplicitConversionSequence &ICS, |
| 2436 | QualType T) { |
| 2437 | Step S; |
| 2438 | S.Kind = SK_ConversionSequence; |
| 2439 | S.Type = T; |
| 2440 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2441 | Steps.push_back(S); |
| 2442 | } |
| 2443 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2444 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2445 | Step S; |
| 2446 | S.Kind = SK_ListInitialization; |
| 2447 | S.Type = T; |
| 2448 | Steps.push_back(S); |
| 2449 | } |
| 2450 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2451 | void |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2452 | InitializationSequence::AddConstructorInitializationStep( |
| 2453 | CXXConstructorDecl *Constructor, |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2454 | AccessSpecifier Access, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2455 | QualType T) { |
| 2456 | Step S; |
| 2457 | S.Kind = SK_ConstructorInitialization; |
| 2458 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2459 | S.Function.Function = Constructor; |
| 2460 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2461 | Steps.push_back(S); |
| 2462 | } |
| 2463 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2464 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2465 | Step S; |
| 2466 | S.Kind = SK_ZeroInitialization; |
| 2467 | S.Type = T; |
| 2468 | Steps.push_back(S); |
| 2469 | } |
| 2470 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2471 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2472 | Step S; |
| 2473 | S.Kind = SK_CAssignment; |
| 2474 | S.Type = T; |
| 2475 | Steps.push_back(S); |
| 2476 | } |
| 2477 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2478 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2479 | Step S; |
| 2480 | S.Kind = SK_StringInit; |
| 2481 | S.Type = T; |
| 2482 | Steps.push_back(S); |
| 2483 | } |
| 2484 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2485 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2486 | Step S; |
| 2487 | S.Kind = SK_ObjCObjectConversion; |
| 2488 | S.Type = T; |
| 2489 | Steps.push_back(S); |
| 2490 | } |
| 2491 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2492 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2493 | Step S; |
| 2494 | S.Kind = SK_ArrayInit; |
| 2495 | S.Type = T; |
| 2496 | Steps.push_back(S); |
| 2497 | } |
| 2498 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2499 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2500 | bool shouldCopy) { |
| 2501 | Step s; |
| 2502 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2503 | : SK_PassByIndirectRestore); |
| 2504 | s.Type = type; |
| 2505 | Steps.push_back(s); |
| 2506 | } |
| 2507 | |
| 2508 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2509 | Step S; |
| 2510 | S.Kind = SK_ProduceObjCObject; |
| 2511 | S.Type = T; |
| 2512 | Steps.push_back(S); |
| 2513 | } |
| 2514 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2515 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2516 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2517 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2518 | this->Failure = Failure; |
| 2519 | this->FailedOverloadResult = Result; |
| 2520 | } |
| 2521 | |
| 2522 | //===----------------------------------------------------------------------===// |
| 2523 | // Attempt initialization |
| 2524 | //===----------------------------------------------------------------------===// |
| 2525 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2526 | static void MaybeProduceObjCObject(Sema &S, |
| 2527 | InitializationSequence &Sequence, |
| 2528 | const InitializedEntity &Entity) { |
| 2529 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2530 | |
| 2531 | /// When initializing a parameter, produce the value if it's marked |
| 2532 | /// __attribute__((ns_consumed)). |
| 2533 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2534 | if (!Entity.isParameterConsumed()) |
| 2535 | return; |
| 2536 | |
| 2537 | assert(Entity.getType()->isObjCRetainableType() && |
| 2538 | "consuming an object of unretainable type?"); |
| 2539 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2540 | |
| 2541 | /// When initializing a return value, if the return type is a |
| 2542 | /// retainable type, then returns need to immediately retain the |
| 2543 | /// object. If an autorelease is required, it will be done at the |
| 2544 | /// last instant. |
| 2545 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2546 | if (!Entity.getType()->isObjCRetainableType()) |
| 2547 | return; |
| 2548 | |
| 2549 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2550 | } |
| 2551 | } |
| 2552 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2553 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2554 | static void TryListInitialization(Sema &S, |
| 2555 | const InitializedEntity &Entity, |
| 2556 | const InitializationKind &Kind, |
| 2557 | InitListExpr *InitList, |
| 2558 | InitializationSequence &Sequence) { |
| 2559 | // FIXME: We only perform rudimentary checking of list |
| 2560 | // initializations at this point, then assume that any list |
| 2561 | // initialization of an array, aggregate, or scalar will be |
| 2562 | // well-formed. When we actually "perform" list initialization, we'll |
| 2563 | // do all of the necessary checking. C++0x initializer lists will |
| 2564 | // force us to perform more checking here. |
| 2565 | |
| 2566 | QualType DestType = Entity.getType(); |
| 2567 | |
| 2568 | // C++ [dcl.init]p13: |
| 2569 | // If T is a scalar type, then a declaration of the form |
| 2570 | // |
| 2571 | // T x = { a }; |
| 2572 | // |
| 2573 | // is equivalent to |
| 2574 | // |
| 2575 | // T x = a; |
| 2576 | if (DestType->isScalarType()) { |
| 2577 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2578 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | // Assume scalar initialization from a single value works. |
| 2583 | } else if (DestType->isAggregateType()) { |
| 2584 | // Assume aggregate initialization works. |
| 2585 | } else if (DestType->isVectorType()) { |
| 2586 | // Assume vector initialization works. |
| 2587 | } else if (DestType->isReferenceType()) { |
| 2588 | // FIXME: C++0x defines behavior for this. |
| 2589 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2590 | return; |
| 2591 | } else if (DestType->isRecordType()) { |
| 2592 | // FIXME: C++0x defines behavior for this |
| 2593 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2594 | } |
| 2595 | |
| 2596 | // Add a general "list initialization" step. |
| 2597 | Sequence.AddListInitializationStep(DestType); |
| 2598 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2599 | |
| 2600 | /// \brief Try a reference initialization that involves calling a conversion |
| 2601 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2602 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2603 | const InitializedEntity &Entity, |
| 2604 | const InitializationKind &Kind, |
| 2605 | Expr *Initializer, |
| 2606 | bool AllowRValues, |
| 2607 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2608 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2609 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2610 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2611 | QualType cv2T2 = Initializer->getType(); |
| 2612 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2613 | |
| 2614 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2615 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2616 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2617 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2618 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2619 | ObjCConversion, |
| 2620 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2621 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2622 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2623 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2624 | (void)ObjCLifetimeConversion; |
| 2625 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2626 | // Build the candidate set directly in the initialization sequence |
| 2627 | // structure, so that it will persist if we fail. |
| 2628 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2629 | CandidateSet.clear(); |
| 2630 | |
| 2631 | // Determine whether we are allowed to call explicit constructors or |
| 2632 | // explicit conversion operators. |
| 2633 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2634 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2635 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2636 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2637 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2638 | // The type we're converting to is a class type. Enumerate its constructors |
| 2639 | // to see if there is a suitable conversion. |
| 2640 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2641 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2642 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2643 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2644 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2645 | NamedDecl *D = *Con; |
| 2646 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2647 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2648 | // Find the constructor (which may be a template). |
| 2649 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2650 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2651 | if (ConstructorTmpl) |
| 2652 | Constructor = cast<CXXConstructorDecl>( |
| 2653 | ConstructorTmpl->getTemplatedDecl()); |
| 2654 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2655 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2656 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2657 | if (!Constructor->isInvalidDecl() && |
| 2658 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2659 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2660 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2661 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2662 | &Initializer, 1, CandidateSet, |
| 2663 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2664 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2665 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2666 | &Initializer, 1, CandidateSet, |
| 2667 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2669 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2670 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2671 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2672 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2674 | const RecordType *T2RecordType = 0; |
| 2675 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2676 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2677 | // The type we're converting from is a class type, enumerate its conversion |
| 2678 | // functions. |
| 2679 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2680 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2681 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2682 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2683 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2684 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2685 | NamedDecl *D = *I; |
| 2686 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2687 | if (isa<UsingShadowDecl>(D)) |
| 2688 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2689 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2690 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2691 | CXXConversionDecl *Conv; |
| 2692 | if (ConvTemplate) |
| 2693 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2694 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2695 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2697 | // If the conversion function doesn't return a reference type, |
| 2698 | // it can't be considered for this conversion unless we're allowed to |
| 2699 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2700 | // FIXME: Do we need to make sure that we only consider conversion |
| 2701 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2702 | // break recursion. |
| 2703 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2704 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2705 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2706 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2707 | ActingDC, Initializer, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2708 | DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2709 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2710 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2711 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2712 | } |
| 2713 | } |
| 2714 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2715 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2716 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2717 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2718 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2719 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2720 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2721 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2722 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2723 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2724 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2726 | FunctionDecl *Function = Best->Function; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2727 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2728 | // This is the overload that will actually be used for the initialization, so |
| 2729 | // mark it as used. |
| 2730 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2731 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2732 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2733 | if (isa<CXXConversionDecl>(Function)) |
| 2734 | T2 = Function->getResultType(); |
| 2735 | else |
| 2736 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2737 | |
| 2738 | // Add the user-defined conversion step. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2739 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2740 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2741 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2743 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2744 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2745 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2746 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2747 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2748 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2749 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2750 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2751 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2752 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2753 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2754 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2755 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2756 | NewDerivedToBase, NewObjCConversion, |
| 2757 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2758 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2759 | // If the type we've converted to is not reference-related to the |
| 2760 | // type we're looking for, then there is another conversion step |
| 2761 | // we need to perform to produce a temporary of the right type |
| 2762 | // that we'll be binding to. |
| 2763 | ImplicitConversionSequence ICS; |
| 2764 | ICS.setStandard(); |
| 2765 | ICS.Standard = Best->FinalConversion; |
| 2766 | T2 = ICS.Standard.getToType(2); |
| 2767 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2768 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2769 | Sequence.AddDerivedToBaseCastStep( |
| 2770 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2771 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2772 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2773 | else if (NewObjCConversion) |
| 2774 | Sequence.AddObjCObjectConversionStep( |
| 2775 | S.Context.getQualifiedType(T1, |
| 2776 | T2.getNonReferenceType().getQualifiers())); |
| 2777 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2778 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2779 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2780 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2781 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2782 | return OR_Success; |
| 2783 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2784 | |
| 2785 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2786 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2787 | const InitializedEntity &Entity, |
| 2788 | const InitializationKind &Kind, |
| 2789 | Expr *Initializer, |
| 2790 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2791 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2792 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2793 | Qualifiers T1Quals; |
| 2794 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2795 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2796 | Qualifiers T2Quals; |
| 2797 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2798 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2800 | // If the initializer is the address of an overloaded function, try |
| 2801 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2802 | // type of the resulting function. |
| 2803 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2804 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2806 | T1, |
| 2807 | false, |
| 2808 | Found)) { |
| 2809 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2810 | cv2T2 = Fn->getType(); |
| 2811 | T2 = cv2T2.getUnqualifiedType(); |
| 2812 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2813 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2814 | return; |
| 2815 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2816 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2817 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2818 | // Compute some basic properties of the types and the initializer. |
| 2819 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2820 | bool isRValueRef = !isLValueRef; |
| 2821 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2822 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2823 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2824 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2825 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2826 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2827 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2828 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2829 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2830 | // 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] | 2831 | // "cv2 T2" as follows: |
| 2832 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2833 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2834 | // expression |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2835 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2836 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2837 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2838 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2839 | bool T1Function = T1->isFunctionType(); |
| 2840 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2841 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2842 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2843 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2844 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2845 | // - 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] | 2846 | // reference-compatible with "cv2 T2," or |
| 2847 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2848 | // 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] | 2849 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2850 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2851 | // to decide whether we're actually binding to a temporary created from |
| 2852 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2853 | if (DerivedToBase) |
| 2854 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2856 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2857 | else if (ObjCConversion) |
| 2858 | Sequence.AddObjCObjectConversionStep( |
| 2859 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2860 | |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2861 | if (T1Quals != T2Quals) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2862 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2863 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2864 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2865 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2866 | return; |
| 2867 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2868 | |
| 2869 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2870 | // reference-related to T2, and can be implicitly converted to an |
| 2871 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2872 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2873 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2874 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2875 | // If we have an rvalue ref to function type here, the rhs must be |
| 2876 | // an rvalue. |
| 2877 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2878 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2879 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2880 | Initializer, |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2881 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2882 | Sequence); |
| 2883 | if (ConvOvlResult == OR_Success) |
| 2884 | return; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2885 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2886 | Sequence.SetOverloadFailure( |
| 2887 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2888 | ConvOvlResult); |
| 2889 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2890 | } |
| 2891 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2892 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2893 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2894 | // 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] | 2895 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2896 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2897 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2898 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2899 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2900 | Sequence.SetOverloadFailure( |
| 2901 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2902 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2903 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2904 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2905 | ? (RefRelationship == Sema::Ref_Related |
| 2906 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2907 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2908 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2909 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2910 | return; |
| 2911 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2913 | // - If the initializer expression |
| 2914 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2915 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 2916 | // Note: functions are handled below. |
| 2917 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2918 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2919 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2920 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2921 | (InitCategory.isXValue() || |
| 2922 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 2923 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 2924 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 2925 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2926 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2927 | // compiler the freedom to perform a copy here or bind to the |
| 2928 | // object, while C++0x requires that we bind directly to the |
| 2929 | // object. Hence, we always bind to the object without making an |
| 2930 | // extra copy. However, in C++03 requires that we check for the |
| 2931 | // presence of a suitable copy constructor: |
| 2932 | // |
| 2933 | // The constructor that would be used to make the copy shall |
| 2934 | // be callable whether or not the copy is actually done. |
Francois Pichet | 687aaf0 | 2010-12-31 10:43:42 +0000 | [diff] [blame] | 2935 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2936 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2937 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2939 | if (DerivedToBase) |
| 2940 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 2941 | ValueKind); |
| 2942 | else if (ObjCConversion) |
| 2943 | Sequence.AddObjCObjectConversionStep( |
| 2944 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2945 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2946 | if (T1Quals != T2Quals) |
| 2947 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2948 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2949 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2950 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2951 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2952 | |
| 2953 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2954 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2955 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 2956 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2957 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2958 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2959 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2960 | Kind, Initializer, |
| 2961 | /*AllowRValues=*/true, |
| 2962 | Sequence); |
| 2963 | if (ConvOvlResult) |
| 2964 | Sequence.SetOverloadFailure( |
| 2965 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2966 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2967 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2968 | return; |
| 2969 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2970 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2971 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2972 | return; |
| 2973 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2974 | |
| 2975 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2976 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2978 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2979 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2980 | // Determine whether we are allowed to call explicit constructors or |
| 2981 | // explicit conversion operators. |
| 2982 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2983 | |
| 2984 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 2985 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2986 | ImplicitConversionSequence ICS |
| 2987 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2988 | /*SuppressUserConversions*/ false, |
| 2989 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2990 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2991 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 2992 | /*AllowObjCWritebackConversion=*/false); |
| 2993 | |
| 2994 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2995 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2996 | // this into an overloading ambiguity diagnostic. However, we need |
| 2997 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2998 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2999 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3000 | Sequence.SetOverloadFailure( |
| 3001 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3002 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3003 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3004 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3005 | else |
| 3006 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3007 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3008 | } else { |
| 3009 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3013 | // same cv-qualification as, or greater cv-qualification |
| 3014 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3015 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3016 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3017 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3018 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3019 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3020 | return; |
| 3021 | } |
| 3022 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3023 | // [...] 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] | 3024 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3025 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3026 | InitCategory.isLValue()) { |
| 3027 | Sequence.SetFailed( |
| 3028 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3029 | return; |
| 3030 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3031 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3032 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3033 | return; |
| 3034 | } |
| 3035 | |
| 3036 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3037 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3038 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3039 | const InitializedEntity &Entity, |
| 3040 | const InitializationKind &Kind, |
| 3041 | Expr *Initializer, |
| 3042 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3043 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3046 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3047 | /// enumerates the constructors of the initialized entity and performs overload |
| 3048 | /// resolution to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3049 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3050 | const InitializedEntity &Entity, |
| 3051 | const InitializationKind &Kind, |
| 3052 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3053 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3054 | InitializationSequence &Sequence) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3055 | // Build the candidate set directly in the initialization sequence |
| 3056 | // structure, so that it will persist if we fail. |
| 3057 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3058 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3059 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3060 | // Determine whether we are allowed to call explicit constructors or |
| 3061 | // explicit conversion operators. |
| 3062 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3063 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3064 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3065 | |
| 3066 | // The type we're constructing needs to be complete. |
| 3067 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3068 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3069 | return; |
| 3070 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3071 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3072 | // The type we're converting to is a class type. Enumerate its constructors |
| 3073 | // to see if one is suitable. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3074 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3076 | CXXRecordDecl *DestRecordDecl |
| 3077 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3078 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3079 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3080 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3081 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3082 | NamedDecl *D = *Con; |
| 3083 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3084 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3086 | // Find the constructor (which may be a template). |
| 3087 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3088 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3089 | if (ConstructorTmpl) |
| 3090 | Constructor = cast<CXXConstructorDecl>( |
| 3091 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3092 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3093 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3094 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3095 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3096 | // suppress user-defined conversions on the arguments. |
| 3097 | // FIXME: Move constructors? |
| 3098 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3099 | Constructor->isCopyConstructor()) |
| 3100 | SuppressUserConversions = true; |
| 3101 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3103 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3104 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3105 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3106 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3107 | /*ExplicitArgs*/ 0, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3108 | Args, NumArgs, CandidateSet, |
| 3109 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3110 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3111 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3112 | Args, NumArgs, CandidateSet, |
| 3113 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3114 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3117 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3118 | |
| 3119 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3120 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3121 | if (OverloadingResult Result |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3122 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3123 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3124 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3125 | Result); |
| 3126 | return; |
| 3127 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3128 | |
| 3129 | // C++0x [dcl.init]p6: |
| 3130 | // If a program calls for the default initialization of an object |
| 3131 | // of a const-qualified type T, T shall be a class type with a |
| 3132 | // user-provided default constructor. |
| 3133 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3134 | Entity.getType().isConstQualified() && |
| 3135 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3136 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3137 | return; |
| 3138 | } |
| 3139 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3140 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3141 | // subsumed by the initialization. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3142 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3143 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3144 | Best->FoundDecl.getAccess(), |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3145 | DestType); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3148 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3149 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3150 | const InitializedEntity &Entity, |
| 3151 | const InitializationKind &Kind, |
| 3152 | InitializationSequence &Sequence) { |
| 3153 | // C++ [dcl.init]p5: |
| 3154 | // |
| 3155 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3156 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3157 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3158 | // -- if T is an array type, then each element is value-initialized; |
| 3159 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3160 | T = AT->getElementType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3162 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3163 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3164 | // -- if T is a class type (clause 9) with a user-declared |
| 3165 | // constructor (12.1), then the default constructor for T is |
| 3166 | // called (and the initialization is ill-formed if T has no |
| 3167 | // accessible default constructor); |
| 3168 | // |
| 3169 | // FIXME: we really want to refer to a single subobject of the array, |
| 3170 | // but Entity doesn't have a way to capture that (yet). |
| 3171 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3172 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3173 | |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3174 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3175 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3176 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3177 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3178 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3179 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3180 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3181 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3182 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3186 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3189 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3190 | static void TryDefaultInitialization(Sema &S, |
| 3191 | const InitializedEntity &Entity, |
| 3192 | const InitializationKind &Kind, |
| 3193 | InitializationSequence &Sequence) { |
| 3194 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3195 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3196 | // C++ [dcl.init]p6: |
| 3197 | // To default-initialize an object of type T means: |
| 3198 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3199 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3200 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3201 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3202 | // constructor for T is called (and the initialization is ill-formed if |
| 3203 | // T has no accessible default constructor); |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3204 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3205 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3206 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3207 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3208 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3209 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3210 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3211 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3212 | // 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] | 3213 | // default constructor. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3214 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3215 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3216 | return; |
| 3217 | } |
| 3218 | |
| 3219 | // If the destination type has a lifetime property, zero-initialize it. |
| 3220 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3221 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3222 | return; |
| 3223 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3226 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3227 | /// which enumerates all conversion functions and performs overload resolution |
| 3228 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3229 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3230 | const InitializedEntity &Entity, |
| 3231 | const InitializationKind &Kind, |
| 3232 | Expr *Initializer, |
| 3233 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3234 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3235 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3236 | QualType SourceType = Initializer->getType(); |
| 3237 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3238 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3240 | // Build the candidate set directly in the initialization sequence |
| 3241 | // structure, so that it will persist if we fail. |
| 3242 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3243 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3245 | // Determine whether we are allowed to call explicit constructors or |
| 3246 | // explicit conversion operators. |
| 3247 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3248 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3249 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3250 | // The type we're converting to is a class type. Enumerate its constructors |
| 3251 | // to see if there is a suitable conversion. |
| 3252 | CXXRecordDecl *DestRecordDecl |
| 3253 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3254 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3255 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3256 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3257 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3258 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3259 | Con != ConEnd; ++Con) { |
| 3260 | NamedDecl *D = *Con; |
| 3261 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3262 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3263 | // Find the constructor (which may be a template). |
| 3264 | CXXConstructorDecl *Constructor = 0; |
| 3265 | FunctionTemplateDecl *ConstructorTmpl |
| 3266 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3267 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3268 | Constructor = cast<CXXConstructorDecl>( |
| 3269 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3270 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3271 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3272 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3273 | if (!Constructor->isInvalidDecl() && |
| 3274 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3275 | if (ConstructorTmpl) |
| 3276 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3277 | /*ExplicitArgs*/ 0, |
| 3278 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3279 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3280 | else |
| 3281 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3282 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3283 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3284 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3285 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3286 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3287 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3288 | |
| 3289 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3290 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3291 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3292 | // The type we're converting from is a class type, enumerate its conversion |
| 3293 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3294 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3295 | // We can only enumerate the conversion functions for a complete type; if |
| 3296 | // the type isn't complete, simply skip this step. |
| 3297 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3298 | CXXRecordDecl *SourceRecordDecl |
| 3299 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3301 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3302 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3303 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3304 | E = Conversions->end(); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3305 | I != E; ++I) { |
| 3306 | NamedDecl *D = *I; |
| 3307 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3308 | if (isa<UsingShadowDecl>(D)) |
| 3309 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3310 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3311 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3312 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3313 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3314 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3315 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3316 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3317 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3318 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3319 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3320 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3321 | ActingDC, Initializer, DestType, |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3322 | CandidateSet); |
| 3323 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3324 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3325 | Initializer, DestType, CandidateSet); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3326 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3330 | |
| 3331 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3332 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3333 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3334 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3335 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3336 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3337 | Result); |
| 3338 | return; |
| 3339 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3341 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3342 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3343 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3344 | if (isa<CXXConstructorDecl>(Function)) { |
| 3345 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3346 | // subsumed by the initialization. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3347 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3348 | return; |
| 3349 | } |
| 3350 | |
| 3351 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3352 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3353 | if (ConvType->getAs<RecordType>()) { |
| 3354 | // If we're converting to a class type, there may be an copy if |
| 3355 | // the resulting temporary object (possible to create an object of |
| 3356 | // a base class type). That copy is not a separate conversion, so |
| 3357 | // we just make a note of the actual destination type (possibly a |
| 3358 | // base class of the type returned by the conversion function) and |
| 3359 | // let the user-defined conversion step handle the conversion. |
| 3360 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3361 | return; |
| 3362 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3363 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3364 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3365 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3366 | // If the conversion following the call to the conversion function |
| 3367 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3368 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3369 | Best->FinalConversion.Third) { |
| 3370 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3371 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3372 | ICS.Standard = Best->FinalConversion; |
| 3373 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3374 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3377 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3378 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3379 | |
| 3380 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3381 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3382 | bool isAddressOf) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3383 | // Skip parens. |
| 3384 | e = e->IgnoreParens(); |
| 3385 | |
| 3386 | // Skip address-of nodes. |
| 3387 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3388 | if (op->getOpcode() == UO_AddrOf) |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3389 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3390 | |
| 3391 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3392 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3393 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3394 | case CK_Dependent: |
| 3395 | case CK_BitCast: |
| 3396 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3397 | case CK_NoOp: |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3398 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3399 | |
| 3400 | case CK_ArrayToPointerDecay: |
| 3401 | return IIK_nonscalar; |
| 3402 | |
| 3403 | case CK_NullToPointer: |
| 3404 | return IIK_okay; |
| 3405 | |
| 3406 | default: |
| 3407 | break; |
| 3408 | } |
| 3409 | |
| 3410 | // 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] | 3411 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3412 | if (!isAddressOf) return IIK_nonlocal; |
| 3413 | |
| 3414 | VarDecl *var; |
| 3415 | if (isa<DeclRefExpr>(e)) { |
| 3416 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3417 | if (!var) return IIK_nonlocal; |
| 3418 | } else { |
| 3419 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3420 | } |
| 3421 | |
| 3422 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3423 | |
| 3424 | // If we have a conditional operator, check both sides. |
| 3425 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3426 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3427 | return iik; |
| 3428 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3429 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3430 | |
| 3431 | // These are never scalar. |
| 3432 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3433 | return IIK_nonscalar; |
| 3434 | |
| 3435 | // Otherwise, it needs to be a null pointer constant. |
| 3436 | } else { |
| 3437 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3438 | ? IIK_okay : IIK_nonlocal); |
| 3439 | } |
| 3440 | |
| 3441 | return IIK_nonlocal; |
| 3442 | } |
| 3443 | |
| 3444 | /// Check whether the given expression is a valid operand for an |
| 3445 | /// indirect copy/restore. |
| 3446 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3447 | assert(src->isRValue()); |
| 3448 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3449 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3450 | if (iik == IIK_okay) return; |
| 3451 | |
| 3452 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3453 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3454 | << src->getSourceRange(); |
| 3455 | } |
| 3456 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3457 | /// \brief Determine whether we have compatible array types for the |
| 3458 | /// purposes of GNU by-copy array initialization. |
| 3459 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3460 | const ArrayType *Dest, |
| 3461 | const ArrayType *Source) { |
| 3462 | // If the source and destination array types are equivalent, we're |
| 3463 | // done. |
| 3464 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3465 | return true; |
| 3466 | |
| 3467 | // Make sure that the element types are the same. |
| 3468 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3469 | return false; |
| 3470 | |
| 3471 | // The only mismatch we allow is when the destination is an |
| 3472 | // incomplete array type and the source is a constant array type. |
| 3473 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3474 | } |
| 3475 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3476 | static bool tryObjCWritebackConversion(Sema &S, |
| 3477 | InitializationSequence &Sequence, |
| 3478 | const InitializedEntity &Entity, |
| 3479 | Expr *Initializer) { |
| 3480 | bool ArrayDecay = false; |
| 3481 | QualType ArgType = Initializer->getType(); |
| 3482 | QualType ArgPointee; |
| 3483 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3484 | ArrayDecay = true; |
| 3485 | ArgPointee = ArgArrayType->getElementType(); |
| 3486 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3487 | } |
| 3488 | |
| 3489 | // Handle write-back conversion. |
| 3490 | QualType ConvertedArgType; |
| 3491 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3492 | ConvertedArgType)) |
| 3493 | return false; |
| 3494 | |
| 3495 | // We should copy unless we're passing to an argument explicitly |
| 3496 | // marked 'out'. |
| 3497 | bool ShouldCopy = true; |
| 3498 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3499 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3500 | |
| 3501 | // Do we need an lvalue conversion? |
| 3502 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3503 | ImplicitConversionSequence ICS; |
| 3504 | ICS.setStandard(); |
| 3505 | ICS.Standard.setAsIdentityConversion(); |
| 3506 | |
| 3507 | QualType ResultType; |
| 3508 | if (ArrayDecay) { |
| 3509 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3510 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3511 | } else { |
| 3512 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3513 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3514 | } |
| 3515 | |
| 3516 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3517 | } |
| 3518 | |
| 3519 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3520 | return true; |
| 3521 | } |
| 3522 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3523 | InitializationSequence::InitializationSequence(Sema &S, |
| 3524 | const InitializedEntity &Entity, |
| 3525 | const InitializationKind &Kind, |
| 3526 | Expr **Args, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3527 | unsigned NumArgs) |
| 3528 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3529 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3530 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3531 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3532 | // The semantics of initializers are as follows. The destination type is |
| 3533 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3534 | // 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] | 3535 | // 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] | 3536 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3537 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3538 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3539 | if (DestType->isDependentType() || |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3540 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3541 | SequenceKind = DependentSequence; |
| 3542 | return; |
| 3543 | } |
| 3544 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3545 | // Almost everything is a normal sequence. |
| 3546 | setSequenceKind(NormalSequence); |
| 3547 | |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3548 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3549 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3550 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3551 | if (Result.isInvalid()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3552 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3553 | return; |
| 3554 | } |
| 3555 | Args[I] = Result.take(); |
| 3556 | } |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3558 | QualType SourceType; |
| 3559 | Expr *Initializer = 0; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3560 | if (NumArgs == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3561 | Initializer = Args[0]; |
| 3562 | if (!isa<InitListExpr>(Initializer)) |
| 3563 | SourceType = Initializer->getType(); |
| 3564 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3565 | |
| 3566 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3567 | // list-initialized (8.5.4). |
| 3568 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3569 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3570 | return; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3571 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3572 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3573 | // - If the destination type is a reference type, see 8.5.3. |
| 3574 | if (DestType->isReferenceType()) { |
| 3575 | // C++0x [dcl.init.ref]p1: |
| 3576 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3577 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3578 | // by an object that can be converted into a T. |
| 3579 | // (Therefore, multiple arguments are not permitted.) |
| 3580 | if (NumArgs != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3581 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3582 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3583 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3584 | return; |
| 3585 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3587 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3588 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3589 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3590 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3591 | return; |
| 3592 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3593 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3594 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3595 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3596 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3597 | return; |
| 3598 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3599 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3600 | // - If the destination type is an array of characters, an array of |
| 3601 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3602 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3603 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3604 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3605 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3606 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3607 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3608 | return; |
| 3609 | } |
| 3610 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3611 | // Note: as an GNU C extension, we allow initialization of an |
| 3612 | // array from a compound literal that creates an array of the same |
| 3613 | // type, so long as the initializer has no side effects. |
| 3614 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3615 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3616 | Initializer->getType()->isArrayType()) { |
| 3617 | const ArrayType *SourceAT |
| 3618 | = Context.getAsArrayType(Initializer->getType()); |
| 3619 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3620 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3621 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3622 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3623 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3624 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3625 | } |
| 3626 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3627 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3628 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3629 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3630 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3631 | return; |
| 3632 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3633 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3634 | // Determine whether we should consider writeback conversions for |
| 3635 | // Objective-C ARC. |
| 3636 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3637 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3638 | |
| 3639 | // We're at the end of the line for C: it's either a write-back conversion |
| 3640 | // 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] | 3641 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3642 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3643 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3644 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3645 | return; |
| 3646 | } |
| 3647 | |
| 3648 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3649 | AddCAssignmentStep(DestType); |
| 3650 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3651 | return; |
| 3652 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3653 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3654 | assert(S.getLangOptions().CPlusPlus); |
| 3655 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3656 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3657 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3658 | // - If the initialization is direct-initialization, or if it is |
| 3659 | // copy-initialization where the cv-unqualified version of the |
| 3660 | // 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] | 3661 | // class of the destination, constructors are considered. [...] |
| 3662 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3663 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3664 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3665 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3666 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3667 | Entity.getType(), *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3668 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3669 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3670 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3671 | // used) to a derived class thereof are enumerated as described in |
| 3672 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3673 | // (13.3). |
| 3674 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3675 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3676 | return; |
| 3677 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3679 | if (NumArgs > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3680 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3681 | return; |
| 3682 | } |
| 3683 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3684 | |
| 3685 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3686 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3687 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3688 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3689 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3690 | return; |
| 3691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3692 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3693 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3694 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3695 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3696 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3697 | // destination type; no user-defined conversions are considered. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3698 | |
| 3699 | ImplicitConversionSequence ICS |
| 3700 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3701 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3702 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3703 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3704 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3705 | allowObjCWritebackConversion); |
| 3706 | |
| 3707 | if (ICS.isStandard() && |
| 3708 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3709 | // Objective-C ARC writeback conversion. |
| 3710 | |
| 3711 | // We should copy unless we're passing to an argument explicitly |
| 3712 | // marked 'out'. |
| 3713 | bool ShouldCopy = true; |
| 3714 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3715 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3716 | |
| 3717 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3718 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3719 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3720 | ImplicitConversionSequence LvalueICS; |
| 3721 | LvalueICS.setStandard(); |
| 3722 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3723 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3724 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3725 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3726 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3727 | |
| 3728 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3729 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3730 | DeclAccessPair dap; |
| 3731 | if (Initializer->getType() == Context.OverloadTy && |
| 3732 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3733 | , DestType, false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3734 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3735 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3736 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3737 | } else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3738 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3739 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3740 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3741 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3745 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3746 | StepEnd = Steps.end(); |
| 3747 | Step != StepEnd; ++Step) |
| 3748 | Step->Destroy(); |
| 3749 | } |
| 3750 | |
| 3751 | //===----------------------------------------------------------------------===// |
| 3752 | // Perform initialization |
| 3753 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | static Sema::AssignmentAction |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3755 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3756 | switch(Entity.getKind()) { |
| 3757 | case InitializedEntity::EK_Variable: |
| 3758 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3759 | case InitializedEntity::EK_Exception: |
| 3760 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3761 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3762 | return Sema::AA_Initializing; |
| 3763 | |
| 3764 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3765 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3766 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3767 | return Sema::AA_Sending; |
| 3768 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3769 | return Sema::AA_Passing; |
| 3770 | |
| 3771 | case InitializedEntity::EK_Result: |
| 3772 | return Sema::AA_Returning; |
| 3773 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3774 | case InitializedEntity::EK_Temporary: |
| 3775 | // FIXME: Can we tell apart casting vs. converting? |
| 3776 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3778 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3779 | case InitializedEntity::EK_ArrayElement: |
| 3780 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3781 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3782 | return Sema::AA_Initializing; |
| 3783 | } |
| 3784 | |
| 3785 | return Sema::AA_Converting; |
| 3786 | } |
| 3787 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3788 | /// \brief Whether we should binding a created object as a temporary when |
| 3789 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3790 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3791 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3792 | case InitializedEntity::EK_ArrayElement: |
| 3793 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3794 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3795 | case InitializedEntity::EK_New: |
| 3796 | case InitializedEntity::EK_Variable: |
| 3797 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3798 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3799 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3800 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3801 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3802 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3803 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3804 | case InitializedEntity::EK_Parameter: |
| 3805 | case InitializedEntity::EK_Temporary: |
| 3806 | return true; |
| 3807 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3808 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3809 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3810 | } |
| 3811 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3812 | /// \brief Whether the given entity, when initialized with an object |
| 3813 | /// created for that initialization, requires destruction. |
| 3814 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3815 | switch (Entity.getKind()) { |
| 3816 | case InitializedEntity::EK_Member: |
| 3817 | case InitializedEntity::EK_Result: |
| 3818 | case InitializedEntity::EK_New: |
| 3819 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3820 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3821 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3822 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3823 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3824 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3825 | case InitializedEntity::EK_Variable: |
| 3826 | case InitializedEntity::EK_Parameter: |
| 3827 | case InitializedEntity::EK_Temporary: |
| 3828 | case InitializedEntity::EK_ArrayElement: |
| 3829 | case InitializedEntity::EK_Exception: |
| 3830 | return true; |
| 3831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3832 | |
| 3833 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3836 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3837 | /// provided by the given initializer by calling the appropriate copy |
| 3838 | /// constructor. |
| 3839 | /// |
| 3840 | /// \param S The Sema object used for type-checking. |
| 3841 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3842 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3843 | /// the type of the initializer expression or a superclass thereof. |
| 3844 | /// |
| 3845 | /// \param Enter The entity being initialized. |
| 3846 | /// |
| 3847 | /// \param CurInit The initializer expression. |
| 3848 | /// |
| 3849 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3850 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3851 | /// an rvalue. |
| 3852 | /// |
| 3853 | /// \returns An expression that copies the initializer expression into |
| 3854 | /// a temporary object, or an error expression if a copy could not be |
| 3855 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3856 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3857 | QualType T, |
| 3858 | const InitializedEntity &Entity, |
| 3859 | ExprResult CurInit, |
| 3860 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3861 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3862 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3863 | CXXRecordDecl *Class = 0; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3864 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3865 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3866 | if (!Class) |
| 3867 | return move(CurInit); |
| 3868 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3869 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3870 | // When certain criteria are met, an implementation is allowed to |
| 3871 | // omit the copy/move construction of a class object, even if the |
| 3872 | // copy/move constructor and/or destructor for the object have |
| 3873 | // side effects. [...] |
| 3874 | // - when a temporary class object that has not been bound to a |
| 3875 | // reference (12.2) would be copied/moved to a class object |
| 3876 | // with the same cv-unqualified type, the copy/move operation |
| 3877 | // can be omitted by constructing the temporary object |
| 3878 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3880 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3881 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3882 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3883 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3884 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3885 | SourceLocation Loc; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3886 | switch (Entity.getKind()) { |
| 3887 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3888 | Loc = Entity.getReturnLoc(); |
| 3889 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3890 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3891 | case InitializedEntity::EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3892 | Loc = Entity.getThrowLoc(); |
| 3893 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3894 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3895 | case InitializedEntity::EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3896 | Loc = Entity.getDecl()->getLocation(); |
| 3897 | break; |
| 3898 | |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3899 | case InitializedEntity::EK_ArrayElement: |
| 3900 | case InitializedEntity::EK_Member: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3901 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3902 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3903 | case InitializedEntity::EK_New: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3904 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3905 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3906 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3907 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3908 | Loc = CurInitExpr->getLocStart(); |
| 3909 | break; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3910 | } |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3911 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3912 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3913 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3914 | return move(CurInit); |
| 3915 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3916 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3917 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3918 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3919 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3920 | Con != ConEnd; ++Con) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3921 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3922 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3923 | // initialization is direct-initialization. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3924 | CXXConstructorDecl *Constructor = 0; |
| 3925 | |
| 3926 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3927 | // Handle copy/moveconstructors, only. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3928 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3929 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3930 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3931 | continue; |
| 3932 | |
| 3933 | DeclAccessPair FoundDecl |
| 3934 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3935 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3936 | &CurInitExpr, 1, CandidateSet); |
| 3937 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3938 | } |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3939 | |
| 3940 | // Handle constructor templates. |
| 3941 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3942 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3943 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3945 | Constructor = cast<CXXConstructorDecl>( |
| 3946 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3947 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3948 | continue; |
| 3949 | |
| 3950 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3951 | // candidates? |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3952 | DeclAccessPair FoundDecl |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3953 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3954 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3955 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3956 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3957 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3958 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3959 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3960 | case OR_Success: |
| 3961 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3963 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3964 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3965 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 3966 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3967 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3968 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3969 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3970 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3971 | return ExprError(); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3972 | return move(CurInit); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3973 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3974 | case OR_Ambiguous: |
| 3975 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3976 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3977 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3978 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3979 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3980 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3981 | case OR_Deleted: |
| 3982 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3983 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3984 | << CurInitExpr->getSourceRange(); |
| 3985 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3986 | << 1 << Best->Function->isDeleted(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3987 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3990 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3991 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3992 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3993 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3994 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3995 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3996 | |
| 3997 | if (IsExtraneousCopy) { |
| 3998 | // If this is a totally extraneous copy for C++03 reference |
| 3999 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4000 | // expression. We don't generate an (elided) copy operation here |
| 4001 | // because doing so would require us to pass down a flag to avoid |
| 4002 | // infinite recursion, where each step adds another extraneous, |
| 4003 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4004 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4005 | // Instantiate the default arguments of any extra parameters in |
| 4006 | // the selected copy constructor, as if we were going to create a |
| 4007 | // proper call to the copy constructor. |
| 4008 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4009 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4010 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4011 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4012 | break; |
| 4013 | |
| 4014 | // Build the default argument expression; we don't actually care |
| 4015 | // if this succeeds or not, because this routine will complain |
| 4016 | // if there was a problem. |
| 4017 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4018 | } |
| 4019 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4020 | return S.Owned(CurInitExpr); |
| 4021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4022 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4023 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4024 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4025 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4026 | // constructor call (we might have derived-to-base conversions, or |
| 4027 | // the copy constructor may have default arguments). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4028 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4029 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4030 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4031 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4032 | // Actually perform the constructor call. |
| 4033 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4034 | move_arg(ConstructorArgs), |
| 4035 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4036 | CXXConstructExpr::CK_Complete, |
| 4037 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4039 | // If we're supposed to bind temporaries, do so. |
| 4040 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4041 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4042 | return move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4043 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4045 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4046 | const InitializedEntity &Entity) { |
| 4047 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4048 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4049 | return; |
| 4050 | |
| 4051 | if (Entity.getDecl()->getDeclName()) |
| 4052 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4053 | << Entity.getDecl()->getDeclName(); |
| 4054 | else |
| 4055 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4056 | } |
| 4057 | } |
| 4058 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4059 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4060 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4061 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4062 | } |
| 4063 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4065 | InitializationSequence::Perform(Sema &S, |
| 4066 | const InitializedEntity &Entity, |
| 4067 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4068 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4069 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4070 | if (Failed()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4071 | unsigned NumArgs = Args.size(); |
| 4072 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4073 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4074 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4075 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4076 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4077 | // If the declaration is a non-dependent, incomplete array type |
| 4078 | // that has an initializer, then its type will be completed once |
| 4079 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4080 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4081 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4082 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4083 | if (const IncompleteArrayType *ArrayT |
| 4084 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4085 | // FIXME: We don't currently have the ability to accurately |
| 4086 | // compute the length of an initializer list without |
| 4087 | // performing full type-checking of the initializer list |
| 4088 | // (since we have to determine where braces are implicitly |
| 4089 | // introduced and such). So, we fall back to making the array |
| 4090 | // type a dependently-sized array type with no specified |
| 4091 | // bound. |
| 4092 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4093 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4095 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4096 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4097 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4098 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4099 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4100 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4101 | Brackets = ArrayLoc->getBracketsRange(); |
| 4102 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | *ResultType |
| 4106 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4107 | /*NumElts=*/0, |
| 4108 | ArrayT->getSizeModifier(), |
| 4109 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4110 | Brackets); |
| 4111 | } |
| 4112 | |
| 4113 | } |
| 4114 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4115 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4116 | Kind.isExplicitCast()); |
| 4117 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4120 | // No steps means no initialization. |
| 4121 | if (Steps.empty()) |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4122 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4123 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4124 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4125 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4126 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4127 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4128 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4129 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4130 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4131 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4132 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4133 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4134 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4135 | // grab the only argument out the Args and place it into the "current" |
| 4136 | // initializer. |
| 4137 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4138 | case SK_ResolveAddressOfOverloadedFunction: |
| 4139 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4140 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4141 | case SK_CastDerivedToBaseLValue: |
| 4142 | case SK_BindReference: |
| 4143 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4144 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4145 | case SK_UserConversion: |
| 4146 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4147 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4148 | case SK_QualificationConversionRValue: |
| 4149 | case SK_ConversionSequence: |
| 4150 | case SK_ListInitialization: |
| 4151 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4152 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4153 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4154 | case SK_ArrayInit: |
| 4155 | case SK_PassByIndirectCopyRestore: |
| 4156 | case SK_PassByIndirectRestore: |
| 4157 | case SK_ProduceObjCObject: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4158 | assert(Args.size() == 1); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4159 | CurInit = Args.get()[0]; |
| 4160 | if (!CurInit.get()) return ExprError(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4161 | |
| 4162 | // Read from a property when initializing something with it. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4163 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4164 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4165 | if (CurInit.isInvalid()) |
| 4166 | return ExprError(); |
| 4167 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4168 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4169 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4170 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4171 | case SK_ConstructorInitialization: |
| 4172 | case SK_ZeroInitialization: |
| 4173 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4174 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | |
| 4176 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4177 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4178 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4179 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4180 | Step != StepEnd; ++Step) { |
| 4181 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4182 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4184 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4185 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4186 | switch (Step->Kind) { |
| 4187 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4188 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4189 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4190 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4191 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4192 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4193 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4194 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4195 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4196 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4197 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4198 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4199 | case SK_CastDerivedToBaseLValue: { |
| 4200 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4201 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4202 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4203 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4204 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4205 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4206 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4207 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4208 | CurInit.get()->getLocStart(), |
| 4209 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4210 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4211 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4212 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4213 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4214 | QualType T = SourceType; |
| 4215 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4216 | T = Pointer->getPointeeType(); |
| 4217 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4218 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4219 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4220 | } |
| 4221 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4222 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4223 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4224 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4225 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4226 | VK_XValue : |
| 4227 | VK_RValue); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4228 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4229 | Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4230 | CK_DerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4231 | CurInit.get(), |
| 4232 | &BasePath, VK)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4233 | break; |
| 4234 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4235 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4236 | case SK_BindReference: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4237 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4238 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4239 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4240 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4241 | << BitField->getDeclName() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4242 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4243 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4244 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4245 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4246 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4247 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4248 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4249 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4250 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4251 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4252 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4253 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4254 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4256 | // Reference binding does not have any corresponding ASTs. |
| 4257 | |
| 4258 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4259 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4260 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4261 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4262 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4264 | case SK_BindReferenceToTemporary: |
| 4265 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4266 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4267 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4268 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4269 | // Materialize the temporary into memory. |
Douglas Gregor | 2fa40a3 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4270 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4271 | Entity.getType().getNonReferenceType(), |
| 4272 | CurInit.get(), |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4273 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4274 | |
| 4275 | // If we're binding to an Objective-C object that has lifetime, we |
| 4276 | // need cleanups. |
| 4277 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4278 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4279 | S.ExprNeedsCleanups = true; |
| 4280 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4281 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4283 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4285 | /*IsExtraneousCopy=*/true); |
| 4286 | break; |
| 4287 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4288 | case SK_UserConversion: { |
| 4289 | // We have a user-defined conversion that invokes either a constructor |
| 4290 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4291 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4292 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4293 | FunctionDecl *Fn = Step->Function.Function; |
| 4294 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4295 | bool CreatedObject = false; |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4296 | bool IsLvalue = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4297 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4298 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4299 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4300 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4301 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4303 | // Determine the arguments required to actually perform the constructor |
| 4304 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4305 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4306 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4307 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4308 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4309 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4310 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4311 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4312 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4313 | move_arg(ConstructorArgs), |
| 4314 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4315 | CXXConstructExpr::CK_Complete, |
| 4316 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4317 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4318 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4319 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4320 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4321 | FoundFn.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4322 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4324 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4325 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4326 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4327 | S.IsDerivedFrom(SourceType, Class)) |
| 4328 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4329 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4330 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4331 | } else { |
| 4332 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4333 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4334 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4335 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4336 | FoundFn); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4337 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4338 | |
| 4339 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4340 | // derived-to-base conversion? I believe the answer is "no", because |
| 4341 | // 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] | 4342 | ExprResult CurInitExprRes = |
| 4343 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4344 | FoundFn, Conversion); |
| 4345 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4346 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4347 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4348 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4349 | // Build the actual call to the conversion function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4350 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4351 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4352 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4353 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4354 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4356 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4357 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4358 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4359 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4360 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4361 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4362 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4363 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4364 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4366 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4367 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4368 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4369 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4370 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4371 | } |
| 4372 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4374 | // FIXME: xvalues |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4375 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4376 | CurInit.get()->getType(), |
| 4377 | CastKind, CurInit.get(), 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4378 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4379 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4380 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4381 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4382 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4383 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4384 | break; |
| 4385 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4386 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4387 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4388 | case SK_QualificationConversionXValue: |
| 4389 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4390 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4391 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4392 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4393 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4394 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4395 | VK_XValue : |
| 4396 | VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4397 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4398 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4401 | case SK_ConversionSequence: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4402 | Sema::CheckedConversionKind CCK |
| 4403 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4404 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4405 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4406 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4407 | ExprResult CurInitExprRes = |
| 4408 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4409 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4410 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4411 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4412 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4413 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4414 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4415 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4416 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4417 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4418 | QualType Ty = Step->Type; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 4419 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4420 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4421 | |
| 4422 | CurInit.release(); |
| 4423 | CurInit = S.Owned(InitList); |
| 4424 | break; |
| 4425 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4426 | |
| 4427 | case SK_ConstructorInitialization: { |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4428 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4429 | CXXConstructorDecl *Constructor |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4430 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4431 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4432 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4433 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | da2da9c | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4434 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4435 | ? Kind.getEqualLoc() |
| 4436 | : Kind.getLocation(); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4437 | |
| 4438 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4439 | // Force even a trivial, implicit default constructor to be |
| 4440 | // semantically checked. We do this explicitly because we don't build |
| 4441 | // the definition for completely trivial constructors. |
| 4442 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4443 | assert(ClassDecl && "No parent class for constructor."); |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4444 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4445 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4446 | !Constructor->isUsed(false)) |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4447 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4448 | } |
| 4449 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4450 | // Determine the arguments required to actually perform the constructor |
| 4451 | // call. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4453 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4454 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4455 | |
| 4456 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4457 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4458 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4459 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4460 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4461 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4462 | unsigned NumExprs = ConstructorArgs.size(); |
| 4463 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 3fd2a55 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4464 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4465 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4467 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4468 | if (!TSInfo) |
| 4469 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4471 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4472 | Constructor, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4473 | TSInfo, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | Exprs, |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4475 | NumExprs, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4476 | Kind.getParenRange(), |
Douglas Gregor | 199db36 | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4477 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4478 | } else { |
| 4479 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4480 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4482 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4483 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4484 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4485 | CXXConstructExpr::CK_NonVirtualBase; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4486 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4487 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4488 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4489 | |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4490 | // Only get the parenthesis range if it is a direct construction. |
| 4491 | SourceRange parenRange = |
| 4492 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4493 | Kind.getParenRange() : SourceRange(); |
| 4494 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4495 | // If the entity allows NRVO, mark the construction as elidable |
| 4496 | // unconditionally. |
| 4497 | if (Entity.allowsNRVO()) |
| 4498 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4499 | Constructor, /*Elidable=*/true, |
| 4500 | move_arg(ConstructorArgs), |
| 4501 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4502 | ConstructKind, |
| 4503 | parenRange); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4504 | else |
| 4505 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4506 | Constructor, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4507 | move_arg(ConstructorArgs), |
| 4508 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4509 | ConstructKind, |
| 4510 | parenRange); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4511 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4512 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4513 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4514 | |
| 4515 | // Only check access if all of that succeeded. |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4516 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4517 | Step->Function.FoundDecl.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4518 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4520 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4521 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4523 | break; |
| 4524 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4525 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4526 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4527 | step_iterator NextStep = Step; |
| 4528 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4529 | if (NextStep != StepEnd && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4530 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4531 | // The need for zero-initialization is recorded directly into |
| 4532 | // the call to the object's constructor within the next step. |
| 4533 | ConstructorInitRequiresZeroInit = true; |
| 4534 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4535 | S.getLangOptions().CPlusPlus && |
| 4536 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4537 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4538 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4539 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4540 | Kind.getRange().getBegin()); |
| 4541 | |
| 4542 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4543 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4544 | TSInfo, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4545 | Kind.getRange().getEnd())); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4546 | } else { |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4547 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4548 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4549 | break; |
| 4550 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4551 | |
| 4552 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4553 | QualType SourceType = CurInit.get()->getType(); |
| 4554 | ExprResult Result = move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4555 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4556 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4557 | if (Result.isInvalid()) |
| 4558 | return ExprError(); |
| 4559 | CurInit = move(Result); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4560 | |
| 4561 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4562 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4563 | if (ConvTy != Sema::Compatible && |
| 4564 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4565 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4566 | == Sema::Compatible) |
| 4567 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4568 | if (CurInitExprRes.isInvalid()) |
| 4569 | return ExprError(); |
| 4570 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4571 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4572 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4573 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4574 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4575 | CurInit.get(), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4576 | getAssignmentAction(Entity), |
| 4577 | &Complained)) { |
| 4578 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4579 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4580 | } else if (Complained) |
| 4581 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4582 | break; |
| 4583 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4584 | |
| 4585 | case SK_StringInit: { |
| 4586 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4587 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4588 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4589 | break; |
| 4590 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4591 | |
| 4592 | case SK_ObjCObjectConversion: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4593 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4594 | CK_ObjCObjectLValueCast, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4595 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4596 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4597 | |
| 4598 | case SK_ArrayInit: |
| 4599 | // Okay: we checked everything before creating this step. Note that |
| 4600 | // this is a GNU extension. |
| 4601 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4602 | << Step->Type << CurInit.get()->getType() |
| 4603 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4604 | |
| 4605 | // If the destination type is an incomplete array type, update the |
| 4606 | // type accordingly. |
| 4607 | if (ResultType) { |
| 4608 | if (const IncompleteArrayType *IncompleteDest |
| 4609 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4610 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4611 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4612 | *ResultType = S.Context.getConstantArrayType( |
| 4613 | IncompleteDest->getElementType(), |
| 4614 | ConstantSource->getSize(), |
| 4615 | ArrayType::Normal, 0); |
| 4616 | } |
| 4617 | } |
| 4618 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4619 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4620 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4621 | case SK_PassByIndirectCopyRestore: |
| 4622 | case SK_PassByIndirectRestore: |
| 4623 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4624 | CurInit = S.Owned(new (S.Context) |
| 4625 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4626 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4627 | break; |
| 4628 | |
| 4629 | case SK_ProduceObjCObject: |
| 4630 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 4631 | CK_ObjCProduceObject, |
| 4632 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4633 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4634 | } |
| 4635 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4636 | |
| 4637 | // Diagnose non-fatal problems with the completed initialization. |
| 4638 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4639 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4640 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4641 | cast<FieldDecl>(Entity.getDecl()), |
| 4642 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4643 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4644 | return move(CurInit); |
| 4645 | } |
| 4646 | |
| 4647 | //===----------------------------------------------------------------------===// |
| 4648 | // Diagnose initialization failures |
| 4649 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4650 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4651 | const InitializedEntity &Entity, |
| 4652 | const InitializationKind &Kind, |
| 4653 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4654 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4655 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4656 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4657 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4658 | switch (Failure) { |
| 4659 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4660 | // FIXME: Customize for the initialized entity? |
| 4661 | if (NumArgs == 0) |
| 4662 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4663 | << DestType.getNonReferenceType(); |
| 4664 | else // FIXME: diagnostic below could be better! |
| 4665 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4666 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4667 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4668 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4669 | case FK_ArrayNeedsInitList: |
| 4670 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4671 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4672 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4673 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4675 | case FK_ArrayTypeMismatch: |
| 4676 | case FK_NonConstantArrayInit: |
| 4677 | S.Diag(Kind.getLocation(), |
| 4678 | (Failure == FK_ArrayTypeMismatch |
| 4679 | ? diag::err_array_init_different_type |
| 4680 | : diag::err_array_init_non_constant_array)) |
| 4681 | << DestType.getNonReferenceType() |
| 4682 | << Args[0]->getType() |
| 4683 | << Args[0]->getSourceRange(); |
| 4684 | break; |
| 4685 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4686 | case FK_AddressOfOverloadFailed: { |
| 4687 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4689 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4690 | true, |
| 4691 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4692 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4693 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4694 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4695 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4696 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4697 | switch (FailedOverloadResult) { |
| 4698 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4699 | if (Failure == FK_UserConversionOverloadFailed) |
| 4700 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4701 | << Args[0]->getType() << DestType |
| 4702 | << Args[0]->getSourceRange(); |
| 4703 | else |
| 4704 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4705 | << DestType << Args[0]->getType() |
| 4706 | << Args[0]->getSourceRange(); |
| 4707 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4708 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4709 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4711 | case OR_No_Viable_Function: |
| 4712 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4713 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4714 | << Args[0]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4715 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4716 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4718 | case OR_Deleted: { |
| 4719 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4720 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4721 | << Args[0]->getSourceRange(); |
| 4722 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4723 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4724 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4725 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4726 | if (Ovl == OR_Deleted) { |
| 4727 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4728 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4729 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4730 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4731 | } |
| 4732 | break; |
| 4733 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4734 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4735 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4736 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4737 | break; |
| 4738 | } |
| 4739 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4741 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4742 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4743 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4744 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4745 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4746 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4747 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4748 | << DestType.getNonReferenceType() |
| 4749 | << Args[0]->getType() |
| 4750 | << Args[0]->getSourceRange(); |
| 4751 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4752 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4753 | case FK_RValueReferenceBindingToLValue: |
| 4754 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4755 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4756 | << Args[0]->getSourceRange(); |
| 4757 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4758 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4759 | case FK_ReferenceInitDropsQualifiers: |
| 4760 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4761 | << DestType.getNonReferenceType() |
| 4762 | << Args[0]->getType() |
| 4763 | << Args[0]->getSourceRange(); |
| 4764 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4765 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4766 | case FK_ReferenceInitFailed: |
| 4767 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4768 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4769 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4770 | << Args[0]->getType() |
| 4771 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4772 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4773 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4774 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4775 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4776 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4777 | case FK_ConversionFailed: { |
| 4778 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4779 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4780 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4781 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4782 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4783 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4784 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4785 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4786 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4787 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4788 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4789 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4790 | |
| 4791 | case FK_ConversionFromPropertyFailed: |
| 4792 | // No-op. This error has already been reported. |
| 4793 | break; |
| 4794 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4795 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4796 | SourceRange R; |
| 4797 | |
| 4798 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4799 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4800 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4801 | else |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4802 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4804 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4805 | if (Kind.isCStyleOrFunctionalCast()) |
| 4806 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4807 | << R; |
| 4808 | else |
| 4809 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4810 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4811 | break; |
| 4812 | } |
| 4813 | |
| 4814 | case FK_ReferenceBindingToInitList: |
| 4815 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4816 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4817 | break; |
| 4818 | |
| 4819 | case FK_InitListBadDestinationType: |
| 4820 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4821 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4822 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4824 | case FK_ConstructorOverloadFailed: { |
| 4825 | SourceRange ArgsRange; |
| 4826 | if (NumArgs) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4827 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4828 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4829 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4830 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4831 | // bad. |
| 4832 | switch (FailedOverloadResult) { |
| 4833 | case OR_Ambiguous: |
| 4834 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4835 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4836 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4837 | Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4838 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4840 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4841 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4842 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4843 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4844 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4845 | // This is implicit default initialization of a member or |
| 4846 | // base within a constructor. If no viable function was |
| 4847 | // found, notify the user that she needs to explicitly |
| 4848 | // initialize this base/member. |
| 4849 | CXXConstructorDecl *Constructor |
| 4850 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4851 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4852 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4853 | << Constructor->isImplicit() |
| 4854 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4855 | << /*base=*/0 |
| 4856 | << Entity.getType(); |
| 4857 | |
| 4858 | RecordDecl *BaseDecl |
| 4859 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4860 | ->getDecl(); |
| 4861 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4862 | << S.Context.getTagDeclType(BaseDecl); |
| 4863 | } else { |
| 4864 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4865 | << Constructor->isImplicit() |
| 4866 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4867 | << /*member=*/1 |
| 4868 | << Entity.getName(); |
| 4869 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4870 | |
| 4871 | if (const RecordType *Record |
| 4872 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4874 | diag::note_previous_decl) |
| 4875 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4876 | } |
| 4877 | break; |
| 4878 | } |
| 4879 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4880 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4881 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4882 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4883 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4884 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4885 | case OR_Deleted: { |
| 4886 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4887 | << true << DestType << ArgsRange; |
| 4888 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4889 | OverloadingResult Ovl |
| 4890 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4891 | if (Ovl == OR_Deleted) { |
| 4892 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4893 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4894 | } else { |
| 4895 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4896 | } |
| 4897 | break; |
| 4898 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4899 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4900 | case OR_Success: |
| 4901 | llvm_unreachable("Conversion did not fail!"); |
| 4902 | break; |
| 4903 | } |
| 4904 | break; |
| 4905 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4906 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4907 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4908 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4909 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4910 | // This is implicit default-initialization of a const member in |
| 4911 | // a constructor. Complain that it needs to be explicitly |
| 4912 | // initialized. |
| 4913 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4914 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4915 | << Constructor->isImplicit() |
| 4916 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4917 | << /*const=*/1 |
| 4918 | << Entity.getName(); |
| 4919 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4920 | << Entity.getName(); |
| 4921 | } else { |
| 4922 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4923 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4924 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4925 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4926 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4927 | case FK_Incomplete: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4928 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4929 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4933 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4934 | return true; |
| 4935 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4936 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4937 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4938 | switch (SequenceKind) { |
| 4939 | case FailedSequence: { |
| 4940 | OS << "Failed sequence: "; |
| 4941 | switch (Failure) { |
| 4942 | case FK_TooManyInitsForReference: |
| 4943 | OS << "too many initializers for reference"; |
| 4944 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4945 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4946 | case FK_ArrayNeedsInitList: |
| 4947 | OS << "array requires initializer list"; |
| 4948 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4949 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4950 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4951 | OS << "array requires initializer list or string literal"; |
| 4952 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4953 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4954 | case FK_ArrayTypeMismatch: |
| 4955 | OS << "array type mismatch"; |
| 4956 | break; |
| 4957 | |
| 4958 | case FK_NonConstantArrayInit: |
| 4959 | OS << "non-constant array initializer"; |
| 4960 | break; |
| 4961 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4962 | case FK_AddressOfOverloadFailed: |
| 4963 | OS << "address of overloaded function failed"; |
| 4964 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4966 | case FK_ReferenceInitOverloadFailed: |
| 4967 | OS << "overload resolution for reference initialization failed"; |
| 4968 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4969 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4970 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4971 | OS << "non-const lvalue reference bound to temporary"; |
| 4972 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4973 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4974 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4975 | OS << "non-const lvalue reference bound to unrelated type"; |
| 4976 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4977 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4978 | case FK_RValueReferenceBindingToLValue: |
| 4979 | OS << "rvalue reference bound to an lvalue"; |
| 4980 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4981 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4982 | case FK_ReferenceInitDropsQualifiers: |
| 4983 | OS << "reference initialization drops qualifiers"; |
| 4984 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4985 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4986 | case FK_ReferenceInitFailed: |
| 4987 | OS << "reference initialization failed"; |
| 4988 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4989 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4990 | case FK_ConversionFailed: |
| 4991 | OS << "conversion failed"; |
| 4992 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4993 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4994 | case FK_ConversionFromPropertyFailed: |
| 4995 | OS << "conversion from property failed"; |
| 4996 | break; |
| 4997 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4998 | case FK_TooManyInitsForScalar: |
| 4999 | OS << "too many initializers for scalar"; |
| 5000 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5001 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5002 | case FK_ReferenceBindingToInitList: |
| 5003 | OS << "referencing binding to initializer list"; |
| 5004 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5005 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5006 | case FK_InitListBadDestinationType: |
| 5007 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5008 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5009 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5010 | case FK_UserConversionOverloadFailed: |
| 5011 | OS << "overloading failed for user-defined conversion"; |
| 5012 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5013 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5014 | case FK_ConstructorOverloadFailed: |
| 5015 | OS << "constructor overloading failed"; |
| 5016 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5017 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5018 | case FK_DefaultInitOfConst: |
| 5019 | OS << "default initialization of a const variable"; |
| 5020 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5021 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5022 | case FK_Incomplete: |
| 5023 | OS << "initialization of incomplete type"; |
| 5024 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5025 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5026 | OS << '\n'; |
| 5027 | return; |
| 5028 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5029 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5030 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5031 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5032 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5033 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5034 | case NormalSequence: |
| 5035 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5036 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5037 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5038 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5039 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5040 | if (S != step_begin()) { |
| 5041 | OS << " -> "; |
| 5042 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5043 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5044 | switch (S->Kind) { |
| 5045 | case SK_ResolveAddressOfOverloadedFunction: |
| 5046 | OS << "resolve address of overloaded function"; |
| 5047 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5049 | case SK_CastDerivedToBaseRValue: |
| 5050 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5051 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5052 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5053 | case SK_CastDerivedToBaseXValue: |
| 5054 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5055 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5056 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5057 | case SK_CastDerivedToBaseLValue: |
| 5058 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5059 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5060 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5061 | case SK_BindReference: |
| 5062 | OS << "bind reference to lvalue"; |
| 5063 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5064 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5065 | case SK_BindReferenceToTemporary: |
| 5066 | OS << "bind reference to a temporary"; |
| 5067 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5068 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5069 | case SK_ExtraneousCopyToTemporary: |
| 5070 | OS << "extraneous C++03 copy to temporary"; |
| 5071 | break; |
| 5072 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5073 | case SK_UserConversion: |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 5074 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5075 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5076 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5077 | case SK_QualificationConversionRValue: |
| 5078 | OS << "qualification conversion (rvalue)"; |
| 5079 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5080 | case SK_QualificationConversionXValue: |
| 5081 | OS << "qualification conversion (xvalue)"; |
| 5082 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5083 | case SK_QualificationConversionLValue: |
| 5084 | OS << "qualification conversion (lvalue)"; |
| 5085 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5086 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5087 | case SK_ConversionSequence: |
| 5088 | OS << "implicit conversion sequence ("; |
| 5089 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5090 | OS << ")"; |
| 5091 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5092 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5093 | case SK_ListInitialization: |
| 5094 | OS << "list initialization"; |
| 5095 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5096 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5097 | case SK_ConstructorInitialization: |
| 5098 | OS << "constructor initialization"; |
| 5099 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5101 | case SK_ZeroInitialization: |
| 5102 | OS << "zero initialization"; |
| 5103 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5104 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5105 | case SK_CAssignment: |
| 5106 | OS << "C assignment"; |
| 5107 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5108 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5109 | case SK_StringInit: |
| 5110 | OS << "string initialization"; |
| 5111 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5112 | |
| 5113 | case SK_ObjCObjectConversion: |
| 5114 | OS << "Objective-C object conversion"; |
| 5115 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5116 | |
| 5117 | case SK_ArrayInit: |
| 5118 | OS << "array initialization"; |
| 5119 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5120 | |
| 5121 | case SK_PassByIndirectCopyRestore: |
| 5122 | OS << "pass by indirect copy and restore"; |
| 5123 | break; |
| 5124 | |
| 5125 | case SK_PassByIndirectRestore: |
| 5126 | OS << "pass by indirect restore"; |
| 5127 | break; |
| 5128 | |
| 5129 | case SK_ProduceObjCObject: |
| 5130 | OS << "Objective-C object retension"; |
| 5131 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5132 | } |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | void InitializationSequence::dump() const { |
| 5137 | dump(llvm::errs()); |
| 5138 | } |
| 5139 | |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5140 | static void DiagnoseNarrowingInInitList( |
| 5141 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5142 | bool Constant, const APValue &ConstantValue) { |
| 5143 | if (Constant) { |
| 5144 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 4c72694 | 2011-08-18 00:04:08 +0000 | [diff] [blame] | 5145 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5146 | ? diag::err_init_list_constant_narrowing |
| 5147 | : diag::warn_init_list_constant_narrowing) |
| 5148 | << InitE->getSourceRange() |
| 5149 | << ConstantValue |
| 5150 | << EntityType; |
| 5151 | } else |
| 5152 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 4c72694 | 2011-08-18 00:04:08 +0000 | [diff] [blame] | 5153 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5154 | ? diag::err_init_list_variable_narrowing |
| 5155 | : diag::warn_init_list_variable_narrowing) |
| 5156 | << InitE->getSourceRange() |
| 5157 | << InitE->getType() |
| 5158 | << EntityType; |
| 5159 | |
| 5160 | llvm::SmallString<128> StaticCast; |
| 5161 | llvm::raw_svector_ostream OS(StaticCast); |
| 5162 | OS << "static_cast<"; |
| 5163 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5164 | // It's important to use the typedef's name if there is one so that the |
| 5165 | // fixit doesn't break code using types like int64_t. |
| 5166 | // |
| 5167 | // FIXME: This will break if the typedef requires qualification. But |
| 5168 | // getQualifiedNameAsString() includes non-machine-parsable components. |
| 5169 | OS << TT->getDecl(); |
| 5170 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5171 | OS << BT->getName(S.getLangOptions()); |
| 5172 | else { |
| 5173 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5174 | // with a broken cast. |
| 5175 | return; |
| 5176 | } |
| 5177 | OS << ">("; |
| 5178 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5179 | << InitE->getSourceRange() |
| 5180 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5181 | << FixItHint::CreateInsertion( |
| 5182 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5183 | } |
| 5184 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5185 | //===----------------------------------------------------------------------===// |
| 5186 | // Initialization helper functions |
| 5187 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5188 | bool |
| 5189 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5190 | ExprResult Init) { |
| 5191 | if (Init.isInvalid()) |
| 5192 | return false; |
| 5193 | |
| 5194 | Expr *InitE = Init.get(); |
| 5195 | assert(InitE && "No initialization expression"); |
| 5196 | |
| 5197 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5198 | SourceLocation()); |
| 5199 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5200 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5203 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5204 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5205 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5206 | ExprResult Init, |
| 5207 | bool TopLevelOfInitList) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5208 | if (Init.isInvalid()) |
| 5209 | return ExprError(); |
| 5210 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5211 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5212 | assert(InitE && "No initialization expression?"); |
| 5213 | |
| 5214 | if (EqualLoc.isInvalid()) |
| 5215 | EqualLoc = InitE->getLocStart(); |
| 5216 | |
| 5217 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5218 | EqualLoc); |
| 5219 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5220 | Init.release(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5221 | |
| 5222 | bool Constant = false; |
| 5223 | APValue Result; |
| 5224 | if (TopLevelOfInitList && |
| 5225 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5226 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5227 | Constant, Result); |
| 5228 | } |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5229 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5230 | } |