Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Designator.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 25 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 26 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Sema Initialization Checking |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 32 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 33 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 34 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 35 | return 0; |
| 36 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 37 | // See if this is a string literal or @encode. |
| 38 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // Handle @encode, which is a narrow string. |
| 41 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 42 | return Init; |
| 43 | |
| 44 | // Otherwise we can only handle string literals. |
| 45 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 012b339 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 46 | if (SL == 0) return 0; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 47 | |
| 48 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 49 | // char array can be initialized with a narrow string. |
| 50 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
| 51 | if (!SL->isWide()) |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 52 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 53 | |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 54 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 55 | // correction from DR343): "An array with element type compatible with a |
| 56 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 57 | // string literal, optionally enclosed in braces." |
| 58 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 59 | ElemTy.getUnqualifiedType())) |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 60 | return Init; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 65 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 66 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 67 | if (!arrayType) return 0; |
| 68 | |
| 69 | return IsStringInit(init, arrayType, Context); |
| 70 | } |
| 71 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 72 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 73 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 74 | // Get the length of the string as parsed. |
| 75 | uint64_t StrLength = |
| 76 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 77 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 79 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | // 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] | 81 | // being initialized to a string literal. |
| 82 | llvm::APSInt ConstVal(32); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 83 | ConstVal = StrLength; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 84 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 85 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 86 | ConstVal, |
| 87 | ArrayType::Normal, 0); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 88 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 89 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 91 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 93 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 94 | // the size may be smaller or larger than the string we are initializing. |
| 95 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 96 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 97 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 98 | // For Pascal strings it's OK to strip off the terminating null character, |
| 99 | // so the example below is valid: |
| 100 | // |
| 101 | // unsigned char a[2] = "\pa"; |
| 102 | if (SL->isPascal()) |
| 103 | StrLength--; |
| 104 | } |
| 105 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 106 | // [dcl.init.string]p2 |
| 107 | if (StrLength > CAT->getSize().getZExtValue()) |
| 108 | S.Diag(Str->getSourceRange().getBegin(), |
| 109 | diag::err_initializer_string_for_char_array_too_long) |
| 110 | << Str->getSourceRange(); |
| 111 | } else { |
| 112 | // C99 6.7.8p14. |
| 113 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 114 | S.Diag(Str->getSourceRange().getBegin(), |
| 115 | diag::warn_initializer_string_for_char_array_too_long) |
| 116 | << Str->getSourceRange(); |
| 117 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 119 | // Set the type to the actual size that we are initializing. If we have |
| 120 | // something like: |
| 121 | // char x[1] = "foo"; |
| 122 | // then this will set the string literal's type to char[1]. |
| 123 | Str->setType(DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 126 | //===----------------------------------------------------------------------===// |
| 127 | // Semantic checking for initializer lists. |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 130 | /// @brief Semantic checking for initializer lists. |
| 131 | /// |
| 132 | /// The InitListChecker class contains a set of routines that each |
| 133 | /// handle the initialization of a certain kind of entity, e.g., |
| 134 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 135 | /// InitListChecker itself performs a recursive walk of the subobject |
| 136 | /// structure of the type to be initialized, while stepping through |
| 137 | /// the initializer list one element at a time. The IList and Index |
| 138 | /// parameters to each of the Check* routines contain the active |
| 139 | /// (syntactic) initializer list and the index into that initializer |
| 140 | /// list that represents the current initializer. Each routine is |
| 141 | /// responsible for moving that Index forward as it consumes elements. |
| 142 | /// |
| 143 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 144 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 145 | /// initializer list and the index into that initializer list where we |
| 146 | /// are copying initializers as we map them over to the semantic |
| 147 | /// list. Once we have completed our recursive walk of the subobject |
| 148 | /// structure, we will have constructed a full semantic initializer |
| 149 | /// list. |
| 150 | /// |
| 151 | /// C99 designators cause changes in the initializer list traversal, |
| 152 | /// because they make the initialization "jump" into a specific |
| 153 | /// subobject and then continue the initialization from that |
| 154 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 155 | /// designated subobject and manages backing out the recursion to |
| 156 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 157 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 158 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 159 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 160 | bool hadError; |
| 161 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 162 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 164 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 165 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 166 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 167 | unsigned &StructuredIndex, |
| 168 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 169 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 170 | InitListExpr *IList, QualType &T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 171 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 172 | unsigned &StructuredIndex, |
| 173 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 174 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 175 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 177 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 178 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 179 | unsigned &StructuredIndex, |
| 180 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 181 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 182 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 183 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 184 | InitListExpr *StructuredList, |
| 185 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 186 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 187 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 188 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 189 | InitListExpr *StructuredList, |
| 190 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 191 | void CheckReferenceType(const InitializedEntity &Entity, |
| 192 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 193 | unsigned &Index, |
| 194 | InitListExpr *StructuredList, |
| 195 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 196 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 197 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 198 | InitListExpr *StructuredList, |
| 199 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 200 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 201 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 203 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 204 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 205 | unsigned &StructuredIndex, |
| 206 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 207 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 208 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 210 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 211 | InitListExpr *StructuredList, |
| 212 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 213 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 214 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 215 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 217 | RecordDecl::field_iterator *NextField, |
| 218 | llvm::APSInt *NextElementIndex, |
| 219 | unsigned &Index, |
| 220 | InitListExpr *StructuredList, |
| 221 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 222 | bool FinishSubobjectInit, |
| 223 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 224 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 225 | QualType CurrentObjectType, |
| 226 | InitListExpr *StructuredList, |
| 227 | unsigned StructuredIndex, |
| 228 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 229 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 230 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 231 | Expr *expr); |
| 232 | int numArrayElements(QualType DeclType); |
| 233 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 234 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 235 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 236 | const InitializedEntity &ParentEntity, |
| 237 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 238 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 239 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 241 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 242 | InitListExpr *IL, QualType &T); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 243 | bool HadError() { return hadError; } |
| 244 | |
| 245 | // @brief Retrieves the fully-structured initializer list used for |
| 246 | // semantic analysis and code generation. |
| 247 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 248 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 249 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 251 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 252 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 253 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 254 | bool &RequiresSecondPass) { |
| 255 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 256 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 257 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 258 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 259 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 260 | // FIXME: We probably don't need to handle references |
| 261 | // specially here, since value-initialization of references is |
| 262 | // handled in InitializationSequence. |
| 263 | if (Field->getType()->isReferenceType()) { |
| 264 | // C++ [dcl.init.aggr]p9: |
| 265 | // If an incomplete or empty initializer-list leaves a |
| 266 | // member of reference type uninitialized, the program is |
| 267 | // ill-formed. |
| 268 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 269 | << Field->getType() |
| 270 | << ILE->getSyntacticForm()->getSourceRange(); |
| 271 | SemaRef.Diag(Field->getLocation(), |
| 272 | diag::note_uninit_reference_member); |
| 273 | hadError = true; |
| 274 | return; |
| 275 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 277 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 278 | true); |
| 279 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 280 | if (!InitSeq) { |
| 281 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 282 | hadError = true; |
| 283 | return; |
| 284 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 285 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 286 | ExprResult MemberInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 287 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 288 | if (MemberInit.isInvalid()) { |
| 289 | hadError = true; |
| 290 | return; |
| 291 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 292 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 293 | if (hadError) { |
| 294 | // Do nothing |
| 295 | } else if (Init < NumInits) { |
| 296 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 297 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 298 | // Value-initialization requires a constructor call, so |
| 299 | // extend the initializer list to include the constructor |
| 300 | // call and make a note that we'll need to take another pass |
| 301 | // through the initializer list. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 302 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 303 | RequiresSecondPass = true; |
| 304 | } |
| 305 | } else if (InitListExpr *InnerILE |
| 306 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 307 | FillInValueInitializations(MemberEntity, InnerILE, |
| 308 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 311 | /// Recursively replaces NULL values within the given initializer list |
| 312 | /// with expressions that perform value-initialization of the |
| 313 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 314 | void |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 315 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 316 | InitListExpr *ILE, |
| 317 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 319 | "Should not have void type"); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 320 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 321 | if (ILE->getSyntacticForm()) |
| 322 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 324 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 325 | if (RType->getDecl()->isUnion() && |
| 326 | ILE->getInitializedFieldInUnion()) |
| 327 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 328 | Entity, ILE, RequiresSecondPass); |
| 329 | else { |
| 330 | unsigned Init = 0; |
| 331 | for (RecordDecl::field_iterator |
| 332 | Field = RType->getDecl()->field_begin(), |
| 333 | FieldEnd = RType->getDecl()->field_end(); |
| 334 | Field != FieldEnd; ++Field) { |
| 335 | if (Field->isUnnamedBitfield()) |
| 336 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 337 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 338 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 339 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 340 | |
| 341 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 342 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 343 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 345 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 347 | // Only look at the first initialization of a union. |
| 348 | if (RType->getDecl()->isUnion()) |
| 349 | break; |
| 350 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 355 | |
| 356 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 358 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 359 | unsigned NumInits = ILE->getNumInits(); |
| 360 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 361 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 362 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 363 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 364 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 365 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 366 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 367 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 368 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 369 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 370 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 371 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 373 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 375 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 376 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 377 | if (hadError) |
| 378 | return; |
| 379 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 380 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 381 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 382 | ElementEntity.setElementIndex(Init); |
| 383 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 384 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 385 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 386 | true); |
| 387 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 388 | if (!InitSeq) { |
| 389 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 390 | hadError = true; |
| 391 | return; |
| 392 | } |
| 393 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 394 | ExprResult ElementInit |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 395 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 396 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 397 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (hadError) { |
| 402 | // Do nothing |
| 403 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 404 | // For arrays, just set the expression used for value-initialization |
| 405 | // of the "holes" in the array. |
| 406 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 407 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 408 | else |
| 409 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 410 | } else { |
| 411 | // For arrays, just set the expression used for value-initialization |
| 412 | // of the rest of elements and exit. |
| 413 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 414 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 415 | return; |
| 416 | } |
| 417 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 418 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 419 | // Value-initialization requires a constructor call, so |
| 420 | // extend the initializer list to include the constructor |
| 421 | // call and make a note that we'll need to take another pass |
| 422 | // through the initializer list. |
| 423 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 424 | RequiresSecondPass = true; |
| 425 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 426 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 427 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 428 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 429 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 433 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 434 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 435 | InitListExpr *IL, QualType &T) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 436 | : SemaRef(S) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 437 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 438 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 439 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 440 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | FullyStructuredList |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 442 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 443 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 444 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 445 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 446 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 447 | if (!hadError) { |
| 448 | bool RequiresSecondPass = false; |
| 449 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 450 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 451 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 452 | RequiresSecondPass); |
| 453 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 457 | // FIXME: use a proper constant |
| 458 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 459 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 460 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 461 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 462 | } |
| 463 | return maxElements; |
| 464 | } |
| 465 | |
| 466 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 467 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 468 | int InitializableMembers = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 470 | Field = structDecl->field_begin(), |
| 471 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 472 | Field != FieldEnd; ++Field) { |
| 473 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 474 | ++InitializableMembers; |
| 475 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 476 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 477 | return std::min(InitializableMembers, 1); |
| 478 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 481 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 482 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 483 | QualType T, unsigned &Index, |
| 484 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 485 | unsigned &StructuredIndex, |
| 486 | bool TopLevelObject) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 487 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 489 | if (T->isArrayType()) |
| 490 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 491 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 492 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 493 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 494 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 495 | else |
| 496 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 497 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 498 | if (maxElements == 0) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 499 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 500 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 501 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 502 | hadError = true; |
| 503 | return; |
| 504 | } |
| 505 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 506 | // Build a structured initializer list corresponding to this subobject. |
| 507 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 509 | StructuredIndex, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 510 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 511 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 512 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 514 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 515 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 516 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 517 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | StructuredSubobjectInitList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 519 | StructuredSubobjectInitIndex, |
| 520 | TopLevelObject); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 521 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 522 | StructuredSubobjectInitList->setType(T); |
| 523 | |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 524 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 525 | // range corresponds with the end of the last initializer it used. |
| 526 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | SourceLocation EndLoc |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 528 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 529 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 530 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 531 | |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 532 | // Warn about missing braces. |
| 533 | if (T->isArrayType() || T->isRecordType()) { |
Tanya Lattner | 5cbff48 | 2010-03-07 04:40:06 +0000 | [diff] [blame] | 534 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
| 535 | diag::warn_missing_braces) |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 536 | << StructuredSubobjectInitList->getSourceRange() |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 537 | << FixItHint::CreateInsertion(StructuredSubobjectInitList->getLocStart(), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 538 | "{") |
| 539 | << FixItHint::CreateInsertion(SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 540 | StructuredSubobjectInitList->getLocEnd()), |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 541 | "}"); |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 542 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 545 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 546 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 547 | unsigned &Index, |
| 548 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 549 | unsigned &StructuredIndex, |
| 550 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 551 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 552 | SyntacticToSemantic[IList] = StructuredList; |
| 553 | StructuredList->setSyntacticForm(IList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 554 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 555 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 556 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 557 | IList->setType(ExprTy); |
| 558 | StructuredList->setType(ExprTy); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 559 | if (hadError) |
| 560 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 561 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 562 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 563 | // We have leftover initializers |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 564 | if (StructuredIndex == 1 && |
| 565 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 566 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 567 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 568 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 569 | hadError = true; |
| 570 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 571 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 572 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 573 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 574 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 575 | // Don't complain for incomplete types, since we'll get an error |
| 576 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 577 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 579 | CurrentObjectType->isArrayType()? 0 : |
| 580 | CurrentObjectType->isVectorType()? 1 : |
| 581 | CurrentObjectType->isScalarType()? 2 : |
| 582 | CurrentObjectType->isUnionType()? 3 : |
| 583 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 584 | |
| 585 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 586 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 587 | DK = diag::err_excess_initializers; |
| 588 | hadError = true; |
| 589 | } |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 590 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 591 | DK = diag::err_excess_initializers; |
| 592 | hadError = true; |
| 593 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 594 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 595 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 596 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 599 | |
Eli Friedman | 0b4af8f | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 600 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 601 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 602 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 603 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 604 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 607 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 608 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 610 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 611 | unsigned &Index, |
| 612 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 613 | unsigned &StructuredIndex, |
| 614 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 615 | if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 616 | CheckScalarType(Entity, IList, DeclType, Index, |
| 617 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 618 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 619 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 620 | StructuredList, StructuredIndex); |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 621 | } else if (DeclType->isAggregateType()) { |
| 622 | if (DeclType->isRecordType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 623 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 624 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 625 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 626 | StructuredList, StructuredIndex, |
| 627 | TopLevelObject); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 628 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 629 | llvm::APSInt Zero( |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 630 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 631 | false); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 632 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 633 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 634 | StructuredList, StructuredIndex); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 635 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 636 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 637 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 638 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 639 | ++Index; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 640 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 641 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 642 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 643 | } else if (DeclType->isRecordType()) { |
| 644 | // C++ [dcl.init]p14: |
| 645 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 646 | // is a brace-enclosed list, see 8.5.1. |
| 647 | // |
| 648 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 649 | // we have an initializer list and a destination type that is not |
| 650 | // an aggregate. |
| 651 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 652 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 653 | << DeclType << IList->getSourceRange(); |
| 654 | hadError = true; |
| 655 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 656 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 657 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 658 | } else if (DeclType->isObjCObjectType()) { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 659 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 660 | << DeclType; |
| 661 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 662 | } else { |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 663 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 664 | << DeclType; |
| 665 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 669 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 670 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 672 | unsigned &Index, |
| 673 | InitListExpr *StructuredList, |
| 674 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 675 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 676 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 677 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 678 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | InitListExpr *newStructuredList |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 680 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 681 | StructuredList, StructuredIndex, |
| 682 | SubInitList->getSourceRange()); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 683 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 684 | newStructuredList, newStructuredIndex); |
| 685 | ++StructuredIndex; |
| 686 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 687 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 688 | } else if (ElemType->isScalarType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 689 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 690 | StructuredList, StructuredIndex); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 691 | } else if (ElemType->isReferenceType()) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 692 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 693 | StructuredList, StructuredIndex); |
| 694 | } |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 695 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 696 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 697 | // arrayType can be incomplete if we're initializing a flexible |
| 698 | // array member. There's nothing we can do with the completed |
| 699 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 700 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 701 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
| 702 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 703 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 704 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 705 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 706 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 707 | |
| 708 | // Fall through for subaggregate initialization. |
| 709 | |
| 710 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 711 | // C++ [dcl.init.aggr]p12: |
| 712 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 713 | // initializing the aggregate member with an initializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 714 | // an initializer-list. If the initializer can initialize a |
| 715 | // member, the member is initialized. [...] |
| 716 | |
| 717 | // FIXME: Better EqualLoc? |
| 718 | InitializationKind Kind = |
| 719 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 720 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 721 | |
| 722 | if (Seq) { |
| 723 | ExprResult Result = |
| 724 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 725 | if (Result.isInvalid()) |
| 726 | hadError = true; |
| 727 | |
| 728 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 729 | Result.takeAs<Expr>()); |
| 730 | ++Index; |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | // Fall through for subaggregate initialization |
| 735 | } else { |
| 736 | // C99 6.7.8p13: |
| 737 | // |
| 738 | // The initializer for a structure or union object that has |
| 739 | // automatic storage duration shall be either an initializer |
| 740 | // list as described below, or a single expression that has |
| 741 | // compatible structure or union type. In the latter case, the |
| 742 | // initial value of the object, including unnamed members, is |
| 743 | // that of the expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 744 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 745 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 746 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 747 | == Sema::Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 748 | if (ExprRes.isInvalid()) |
| 749 | hadError = true; |
| 750 | else { |
| 751 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 752 | if (ExprRes.isInvalid()) |
| 753 | hadError = true; |
| 754 | } |
| 755 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 756 | ExprRes.takeAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 757 | ++Index; |
| 758 | return; |
| 759 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 760 | ExprRes.release(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 761 | // Fall through for subaggregate initialization |
| 762 | } |
| 763 | |
| 764 | // C++ [dcl.init.aggr]p12: |
| 765 | // |
| 766 | // [...] Otherwise, if the member is itself a non-empty |
| 767 | // subaggregate, brace elision is assumed and the initializer is |
| 768 | // considered for the initialization of the first member of |
| 769 | // the subaggregate. |
| 770 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
| 771 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 772 | StructuredIndex); |
| 773 | ++StructuredIndex; |
| 774 | } else { |
| 775 | // We cannot initialize this element, so let |
| 776 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 777 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 778 | SemaRef.Owned(expr)); |
| 779 | hadError = true; |
| 780 | ++Index; |
| 781 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 782 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 785 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 786 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 787 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 788 | InitListExpr *StructuredList, |
| 789 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 790 | if (Index >= IList->getNumInits()) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 791 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 792 | << IList->getSourceRange(); |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 793 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 794 | ++Index; |
| 795 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 796 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 797 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 798 | |
| 799 | Expr *expr = IList->getInit(Index); |
| 800 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
| 801 | SemaRef.Diag(SubIList->getLocStart(), |
| 802 | diag::warn_many_braces_around_scalar_init) |
| 803 | << SubIList->getSourceRange(); |
| 804 | |
| 805 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 806 | StructuredIndex); |
| 807 | return; |
| 808 | } else if (isa<DesignatedInitExpr>(expr)) { |
| 809 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 810 | diag::err_designator_for_scalar_init) |
| 811 | << DeclType << expr->getSourceRange(); |
| 812 | hadError = true; |
| 813 | ++Index; |
| 814 | ++StructuredIndex; |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | ExprResult Result = |
| 819 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 820 | SemaRef.Owned(expr)); |
| 821 | |
| 822 | Expr *ResultExpr = 0; |
| 823 | |
| 824 | if (Result.isInvalid()) |
| 825 | hadError = true; // types weren't compatible. |
| 826 | else { |
| 827 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 828 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 829 | if (ResultExpr != expr) { |
| 830 | // The type was promoted, update initializer list. |
| 831 | IList->setInit(Index, ResultExpr); |
| 832 | } |
| 833 | } |
| 834 | if (hadError) |
| 835 | ++StructuredIndex; |
| 836 | else |
| 837 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 838 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 841 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 842 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 843 | unsigned &Index, |
| 844 | InitListExpr *StructuredList, |
| 845 | unsigned &StructuredIndex) { |
| 846 | if (Index < IList->getNumInits()) { |
| 847 | Expr *expr = IList->getInit(Index); |
| 848 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 849 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 850 | << DeclType << IList->getSourceRange(); |
| 851 | hadError = true; |
| 852 | ++Index; |
| 853 | ++StructuredIndex; |
| 854 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 856 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 857 | ExprResult Result = |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 858 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 859 | SemaRef.Owned(expr)); |
| 860 | |
| 861 | if (Result.isInvalid()) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 862 | hadError = true; |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 863 | |
| 864 | expr = Result.takeAs<Expr>(); |
| 865 | IList->setInit(Index, expr); |
| 866 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 867 | if (hadError) |
| 868 | ++StructuredIndex; |
| 869 | else |
| 870 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 871 | ++Index; |
| 872 | } else { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 873 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 874 | // general, it would be useful to pass location information down the stack, |
| 875 | // so that we know the location (or decl) of the "current object" being |
| 876 | // initialized. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 878 | diag::err_init_reference_member_uninitialized) |
| 879 | << DeclType |
| 880 | << IList->getSourceRange(); |
| 881 | hadError = true; |
| 882 | ++Index; |
| 883 | ++StructuredIndex; |
| 884 | return; |
| 885 | } |
| 886 | } |
| 887 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 888 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 889 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 890 | unsigned &Index, |
| 891 | InitListExpr *StructuredList, |
| 892 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 893 | if (Index >= IList->getNumInits()) |
| 894 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 896 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 897 | unsigned maxElements = VT->getNumElements(); |
| 898 | unsigned numEltsInit = 0; |
| 899 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 900 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 901 | if (!SemaRef.getLangOptions().OpenCL) { |
| 902 | // If the initializing element is a vector, try to copy-initialize |
| 903 | // instead of breaking it apart (which is doomed to failure anyway). |
| 904 | Expr *Init = IList->getInit(Index); |
| 905 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
| 906 | ExprResult Result = |
| 907 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
| 908 | SemaRef.Owned(Init)); |
| 909 | |
| 910 | Expr *ResultExpr = 0; |
| 911 | if (Result.isInvalid()) |
| 912 | hadError = true; // types weren't compatible. |
| 913 | else { |
| 914 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 915 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 916 | if (ResultExpr != Init) { |
| 917 | // The type was promoted, update initializer list. |
| 918 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 919 | } |
| 920 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 921 | if (hadError) |
| 922 | ++StructuredIndex; |
| 923 | else |
| 924 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 925 | ++Index; |
| 926 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 927 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 929 | InitializedEntity ElementEntity = |
| 930 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 931 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 932 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 933 | // Don't attempt to go past the end of the init list |
| 934 | if (Index >= IList->getNumInits()) |
| 935 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 936 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 937 | ElementEntity.setElementIndex(Index); |
| 938 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 939 | StructuredList, StructuredIndex); |
| 940 | } |
| 941 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 942 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 943 | |
| 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 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 948 | for (unsigned i = 0; i < maxElements; ++i) { |
| 949 | // Don't attempt to go past the end of the init list |
| 950 | if (Index >= IList->getNumInits()) |
| 951 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 952 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 953 | ElementEntity.setElementIndex(Index); |
| 954 | |
| 955 | QualType IType = IList->getInit(Index)->getType(); |
| 956 | if (!IType->isVectorType()) { |
| 957 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 958 | StructuredList, StructuredIndex); |
| 959 | ++numEltsInit; |
| 960 | } else { |
| 961 | QualType VecType; |
| 962 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 963 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 964 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 965 | if (IType->isExtVectorType()) |
| 966 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 967 | else |
| 968 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 969 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 970 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 971 | StructuredList, StructuredIndex); |
| 972 | numEltsInit += numIElts; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | // OpenCL requires all elements to be initialized. |
| 977 | if (numEltsInit != maxElements) |
| 978 | if (SemaRef.getLangOptions().OpenCL) |
| 979 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 980 | diag::err_vector_incorrect_num_initializers) |
| 981 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 984 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 985 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 986 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 988 | unsigned &Index, |
| 989 | InitListExpr *StructuredList, |
| 990 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 991 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 992 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 993 | // Check for the special-case of initializing an array with a string. |
| 994 | if (Index < IList->getNumInits()) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 995 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 996 | SemaRef.Context)) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 997 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 998 | // We place the string literal directly into the resulting |
| 999 | // initializer list. This is the only place where the structure |
| 1000 | // of the structured initializer list doesn't match exactly, |
| 1001 | // because doing so would involve allocating one character |
| 1002 | // constant for each string. |
Chris Lattner | edbf3ba | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 1003 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1004 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1005 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1006 | return; |
| 1007 | } |
| 1008 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1009 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1010 | // Check for VLAs; in standard C it would be possible to check this |
| 1011 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1012 | // them in all sorts of strange places). |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1013 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1014 | diag::err_variable_object_no_init) |
| 1015 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1016 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1017 | ++Index; |
| 1018 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1022 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1023 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1024 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1025 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1026 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1027 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1028 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1029 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1030 | maxElementsKnown = true; |
| 1031 | } |
| 1032 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1033 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1034 | while (Index < IList->getNumInits()) { |
| 1035 | Expr *Init = IList->getInit(Index); |
| 1036 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1037 | // If we're not the subobject that matches up with the '{' for |
| 1038 | // the designator, we shouldn't be handling the |
| 1039 | // designator. Return immediately. |
| 1040 | if (!SubobjectIsDesignatorContext) |
| 1041 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1042 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1043 | // Handle this designated initializer. elementIndex will be |
| 1044 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1045 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1046 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1047 | StructuredList, StructuredIndex, true, |
| 1048 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1049 | hadError = true; |
| 1050 | continue; |
| 1051 | } |
| 1052 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1053 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1054 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1055 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1056 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1057 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1059 | // If the array is of incomplete type, keep track of the number of |
| 1060 | // elements in the initializer. |
| 1061 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1062 | maxElements = elementIndex; |
| 1063 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1064 | continue; |
| 1065 | } |
| 1066 | |
| 1067 | // If we know the maximum number of elements, and we've already |
| 1068 | // hit it, stop consuming elements in the initializer list. |
| 1069 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1070 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1071 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1072 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1073 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1074 | Entity); |
| 1075 | // Check this element. |
| 1076 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1077 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1078 | ++elementIndex; |
| 1079 | |
| 1080 | // If the array is of incomplete type, keep track of the number of |
| 1081 | // elements in the initializer. |
| 1082 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1083 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1084 | } |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1085 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1086 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1087 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1088 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1089 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1090 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1091 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1092 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1093 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1094 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1095 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1097 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1101 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1102 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1104 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1106 | unsigned &Index, |
| 1107 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1108 | unsigned &StructuredIndex, |
| 1109 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1110 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1112 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1113 | // confusion, we forgo checking the intializer for the entire record. |
| 1114 | if (structDecl->isInvalidDecl()) { |
| 1115 | hadError = true; |
| 1116 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1120 | // Value-initialize the first named member of the union. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1121 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1122 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1123 | Field != FieldEnd; ++Field) { |
| 1124 | if (Field->getDeclName()) { |
| 1125 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1126 | break; |
| 1127 | } |
| 1128 | } |
| 1129 | return; |
| 1130 | } |
| 1131 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1132 | // If structDecl is a forward declaration, this loop won't do |
| 1133 | // anything except look at designated initializers; That's okay, |
| 1134 | // because an error should get printed out elsewhere. It might be |
| 1135 | // worthwhile to skip over the rest of the initializer, though. |
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 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1138 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1139 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1140 | while (Index < IList->getNumInits()) { |
| 1141 | Expr *Init = IList->getInit(Index); |
| 1142 | |
| 1143 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1144 | // If we're not the subobject that matches up with the '{' for |
| 1145 | // the designator, we shouldn't be handling the |
| 1146 | // designator. Return immediately. |
| 1147 | if (!SubobjectIsDesignatorContext) |
| 1148 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1150 | // Handle this designated initializer. Field will be updated to |
| 1151 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1152 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1153 | DeclType, &Field, 0, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1154 | StructuredList, StructuredIndex, |
| 1155 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1156 | hadError = true; |
| 1157 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1158 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1159 | |
| 1160 | // Disable check for missing fields when designators are used. |
| 1161 | // This matches gcc behaviour. |
| 1162 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1163 | continue; |
| 1164 | } |
| 1165 | |
| 1166 | if (Field == FieldEnd) { |
| 1167 | // We've run out of fields. We're done. |
| 1168 | break; |
| 1169 | } |
| 1170 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1171 | // We've already initialized a member of a union. We're done. |
| 1172 | if (InitializedSomething && DeclType->isUnionType()) |
| 1173 | break; |
| 1174 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1175 | // If we've hit the flexible array member at the end, we're done. |
| 1176 | if (Field->getType()->isIncompleteArrayType()) |
| 1177 | break; |
| 1178 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1179 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1180 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1181 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1182 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1183 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1184 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1185 | // Make sure we can use this declaration. |
| 1186 | if (SemaRef.DiagnoseUseOfDecl(*Field, |
| 1187 | IList->getInit(Index)->getLocStart())) { |
| 1188 | ++Index; |
| 1189 | ++Field; |
| 1190 | hadError = true; |
| 1191 | continue; |
| 1192 | } |
| 1193 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1194 | InitializedEntity MemberEntity = |
| 1195 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1196 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1197 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1198 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (DeclType->isUnionType()) { |
| 1201 | // Initialize the first field within the union. |
| 1202 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1203 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1204 | |
| 1205 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1206 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1207 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1208 | // Emit warnings for missing struct field initializers. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1209 | if (InitializedSomething && CheckForMissingFields && Field != FieldEnd && |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1210 | !Field->getType()->isIncompleteArrayType() && !DeclType->isUnionType()) { |
| 1211 | // It is possible we have one or more unnamed bitfields remaining. |
| 1212 | // Find first (if any) named field and emit warning. |
| 1213 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1214 | it != end; ++it) { |
| 1215 | if (!it->isUnnamedBitfield()) { |
| 1216 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1217 | diag::warn_missing_field_initializers) << it->getName(); |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1224 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1225 | return; |
| 1226 | |
| 1227 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | if (!TopLevelObject && |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1229 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1230 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1232 | diag::err_flexible_array_init_nonempty) |
| 1233 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1234 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1235 | << *Field; |
| 1236 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1237 | ++Index; |
| 1238 | return; |
| 1239 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1241 | diag::ext_flexible_array_init) |
| 1242 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1243 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1244 | << *Field; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1247 | InitializedEntity MemberEntity = |
| 1248 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1249 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1250 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1251 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1252 | StructuredList, StructuredIndex); |
| 1253 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1254 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1255 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1256 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1257 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1258 | /// \brief Expand a field designator that refers to a member of an |
| 1259 | /// anonymous struct or union into a series of field designators that |
| 1260 | /// refers to the field within the appropriate subobject. |
| 1261 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1262 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | DesignatedInitExpr *DIE, |
| 1264 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1265 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1266 | typedef DesignatedInitExpr::Designator Designator; |
| 1267 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1268 | // Build the replacement designators. |
| 1269 | llvm::SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1270 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1271 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1272 | if (PI + 1 == PE) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1274 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1275 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1276 | else |
| 1277 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1278 | SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1279 | assert(isa<FieldDecl>(*PI)); |
| 1280 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | // Expand the current designator into the set of replacement |
| 1284 | // designators, so we have a full subobject path down to where the |
| 1285 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1286 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1287 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1288 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1290 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1291 | /// corresponds to FieldName. |
| 1292 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1293 | IdentifierInfo *FieldName) { |
| 1294 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1295 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1296 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1297 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1298 | return IF; |
| 1299 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1300 | } |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1301 | return 0; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1304 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1305 | /// |
| 1306 | /// Determines whether the designated initializer @p DIE, which |
| 1307 | /// resides at the given @p Index within the initializer list @p |
| 1308 | /// IList, is well-formed for a current object of type @p DeclType |
| 1309 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1311 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1312 | /// |
| 1313 | /// @param IList The initializer list in which this designated |
| 1314 | /// initializer occurs. |
| 1315 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1316 | /// @param DIE The designated initializer expression. |
| 1317 | /// |
| 1318 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1319 | /// |
| 1320 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1321 | /// into which the designation in @p DIE should refer. |
| 1322 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1323 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1324 | /// a field, this will be set to the field declaration corresponding |
| 1325 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1326 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1327 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1328 | /// DIE is an array designator or GNU array-range designator, this |
| 1329 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1330 | /// |
| 1331 | /// @param Index Index into @p IList where the designated initializer |
| 1332 | /// @p DIE occurs. |
| 1333 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1334 | /// @param StructuredList The initializer list expression that |
| 1335 | /// describes all of the subobject initializers in the order they'll |
| 1336 | /// actually be initialized. |
| 1337 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1338 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1339 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1340 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1341 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1343 | unsigned DesigIdx, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1344 | QualType &CurrentObjectType, |
| 1345 | RecordDecl::field_iterator *NextField, |
| 1346 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1347 | unsigned &Index, |
| 1348 | InitListExpr *StructuredList, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1349 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1350 | bool FinishSubobjectInit, |
| 1351 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1352 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1353 | // Check the actual initialization for the designated object type. |
| 1354 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Temporarily remove the designator expression from the |
| 1357 | // initializer list that the child calls see, so that we don't try |
| 1358 | // to re-process the designator. |
| 1359 | unsigned OldIndex = Index; |
| 1360 | IList->setInit(OldIndex, DIE->getInit()); |
| 1361 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1362 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1363 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1364 | |
| 1365 | // Restore the designated initializer expression in the syntactic |
| 1366 | // form of the initializer list. |
| 1367 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1368 | DIE->setInit(IList->getInit(OldIndex)); |
| 1369 | IList->setInit(OldIndex, DIE); |
| 1370 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1371 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1374 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1376 | "Need a non-designated initializer list to start from"); |
| 1377 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1378 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1379 | // Determine the structural initializer list that corresponds to the |
| 1380 | // current subobject. |
| 1381 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1383 | StructuredList, StructuredIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1384 | SourceRange(D->getStartLocation(), |
| 1385 | DIE->getSourceRange().getEnd())); |
| 1386 | assert(StructuredList && "Expected a structured initializer list"); |
| 1387 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1388 | if (D->isFieldDesignator()) { |
| 1389 | // C99 6.7.8p7: |
| 1390 | // |
| 1391 | // If a designator has the form |
| 1392 | // |
| 1393 | // . identifier |
| 1394 | // |
| 1395 | // then the current object (defined below) shall have |
| 1396 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1398 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1399 | if (!RT) { |
| 1400 | SourceLocation Loc = D->getDotLoc(); |
| 1401 | if (Loc.isInvalid()) |
| 1402 | Loc = D->getFieldLoc(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1403 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1404 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1405 | ++Index; |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1409 | // Note: we perform a linear search of the fields here, despite |
| 1410 | // the fact that we have a faster lookup method, because we always |
| 1411 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1412 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1413 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1414 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1416 | Field = RT->getDecl()->field_begin(), |
| 1417 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1418 | for (; Field != FieldEnd; ++Field) { |
| 1419 | if (Field->isUnnamedBitfield()) |
| 1420 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1421 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1422 | // If we find a field representing an anonymous field, look in the |
| 1423 | // IndirectFieldDecl that follow for the designated initializer. |
| 1424 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1425 | if (IndirectFieldDecl *IF = |
| 1426 | FindIndirectFieldDesignator(*Field, FieldName)) { |
| 1427 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1428 | D = DIE->getDesignator(DesigIdx); |
| 1429 | break; |
| 1430 | } |
| 1431 | } |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1432 | if (KnownField && KnownField == *Field) |
| 1433 | break; |
| 1434 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1435 | break; |
| 1436 | |
| 1437 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1440 | if (Field == FieldEnd) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1441 | // There was no normal field in the struct with the designated |
| 1442 | // name. Perform another lookup for this name, which may find |
| 1443 | // something that we can't designate (e.g., a member function), |
| 1444 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1446 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1447 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1448 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1449 | // Name lookup didn't find anything. Determine whether this |
| 1450 | // was a typo for another field name. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1451 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1452 | Sema::LookupMemberName); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1453 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1454 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1455 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1456 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1457 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1458 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1459 | ->Equals(RT->getDecl())) { |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1460 | std::string CorrectedStr( |
| 1461 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1462 | std::string CorrectedQuotedStr( |
| 1463 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1464 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1465 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1466 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1467 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1468 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1469 | diag::note_previous_decl) << CorrectedQuotedStr; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1470 | } else { |
| 1471 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1472 | << FieldName << CurrentObjectType; |
| 1473 | ++Index; |
| 1474 | return true; |
| 1475 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1476 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1477 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1478 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1479 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1480 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1481 | << FieldName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1483 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1484 | ++Index; |
| 1485 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1486 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1487 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1488 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1489 | // The replacement field comes from typo correction; find it |
| 1490 | // in the list of fields. |
| 1491 | FieldIndex = 0; |
| 1492 | Field = RT->getDecl()->field_begin(); |
| 1493 | for (; Field != FieldEnd; ++Field) { |
| 1494 | if (Field->isUnnamedBitfield()) |
| 1495 | continue; |
| 1496 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1497 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1498 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1499 | break; |
| 1500 | |
| 1501 | ++FieldIndex; |
| 1502 | } |
| 1503 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1504 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1505 | |
| 1506 | // All of the fields of a union are located at the same place in |
| 1507 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1508 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1509 | FieldIndex = 0; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1510 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1511 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1512 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1513 | // Make sure we can use this declaration. |
| 1514 | if (SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc())) { |
| 1515 | ++Index; |
| 1516 | return true; |
| 1517 | } |
| 1518 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1519 | // Update the designator with the field declaration. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1520 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1522 | // Make sure that our non-designated initializer list has space |
| 1523 | // for a subobject corresponding to this field. |
| 1524 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1525 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1526 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1527 | // This designator names a flexible array member. |
| 1528 | if (Field->getType()->isIncompleteArrayType()) { |
| 1529 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1530 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1531 | // We can't designate an object within the flexible array |
| 1532 | // member (because GCC doesn't allow it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1534 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1536 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1538 | DIE->getSourceRange().getEnd()); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1539 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1540 | << *Field; |
| 1541 | Invalid = true; |
| 1542 | } |
| 1543 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1544 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1545 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1546 | // The initializer is not an initializer list. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1547 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1548 | diag::err_flexible_array_init_needs_braces) |
| 1549 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1550 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1551 | << *Field; |
| 1552 | Invalid = true; |
| 1553 | } |
| 1554 | |
| 1555 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1557 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1559 | diag::err_flexible_array_init_nonempty) |
| 1560 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1561 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1562 | << *Field; |
| 1563 | Invalid = true; |
| 1564 | } |
| 1565 | |
| 1566 | if (Invalid) { |
| 1567 | ++Index; |
| 1568 | return true; |
| 1569 | } |
| 1570 | |
| 1571 | // Initialize the array. |
| 1572 | bool prevHadError = hadError; |
| 1573 | unsigned newStructuredIndex = FieldIndex; |
| 1574 | unsigned OldIndex = Index; |
| 1575 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1576 | |
| 1577 | InitializedEntity MemberEntity = |
| 1578 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1579 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1580 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1581 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1582 | IList->setInit(OldIndex, DIE); |
| 1583 | if (hadError && !prevHadError) { |
| 1584 | ++Field; |
| 1585 | ++FieldIndex; |
| 1586 | if (NextField) |
| 1587 | *NextField = Field; |
| 1588 | StructuredIndex = FieldIndex; |
| 1589 | return true; |
| 1590 | } |
| 1591 | } else { |
| 1592 | // Recurse to check later designated subobjects. |
| 1593 | QualType FieldType = (*Field)->getType(); |
| 1594 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1595 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1596 | InitializedEntity MemberEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1597 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1598 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1599 | FieldType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1600 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1601 | true, false)) |
| 1602 | return true; |
| 1603 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1604 | |
| 1605 | // Find the position of the next field to be initialized in this |
| 1606 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1607 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1608 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1609 | |
| 1610 | // If this the first designator, our caller will continue checking |
| 1611 | // the rest of this struct/class/union subobject. |
| 1612 | if (IsFirstDesignator) { |
| 1613 | if (NextField) |
| 1614 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1615 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1616 | return false; |
| 1617 | } |
| 1618 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1619 | if (!FinishSubobjectInit) |
| 1620 | return false; |
| 1621 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1622 | // We've already initialized something in the union; we're done. |
| 1623 | if (RT->getDecl()->isUnion()) |
| 1624 | return hadError; |
| 1625 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1626 | // Check the remaining fields within this class/struct/union subobject. |
| 1627 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1628 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1629 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1630 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1631 | return hadError && !prevHadError; |
| 1632 | } |
| 1633 | |
| 1634 | // C99 6.7.8p6: |
| 1635 | // |
| 1636 | // If a designator has the form |
| 1637 | // |
| 1638 | // [ constant-expression ] |
| 1639 | // |
| 1640 | // then the current object (defined below) shall have array |
| 1641 | // type and the expression shall be an integer constant |
| 1642 | // expression. If the array is of unknown size, any |
| 1643 | // nonnegative value is valid. |
| 1644 | // |
| 1645 | // Additionally, cope with the GNU extension that permits |
| 1646 | // designators of the form |
| 1647 | // |
| 1648 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1649 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1650 | if (!AT) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1651 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1652 | << CurrentObjectType; |
| 1653 | ++Index; |
| 1654 | return true; |
| 1655 | } |
| 1656 | |
| 1657 | Expr *IndexExpr = 0; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1658 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1659 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1660 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1661 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1662 | DesignatedEndIndex = DesignatedStartIndex; |
| 1663 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1664 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1665 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | DesignatedStartIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1667 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | DesignatedEndIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1669 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1670 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1672 | // Codegen can't handle evaluating array range designators that have side |
| 1673 | // effects, because we replicate the AST value for each initialized element. |
| 1674 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1675 | // elements with something that has a side effect, so codegen can emit an |
| 1676 | // "error unsupported" error instead of miscompiling the app. |
| 1677 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
| 1678 | DIE->getInit()->HasSideEffects(SemaRef.Context)) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1679 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1682 | if (isa<ConstantArrayType>(AT)) { |
| 1683 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1684 | DesignatedStartIndex |
| 1685 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1686 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1687 | DesignatedEndIndex |
| 1688 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1689 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1690 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1691 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1692 | diag::err_array_designator_too_large) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1693 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1694 | << IndexExpr->getSourceRange(); |
| 1695 | ++Index; |
| 1696 | return true; |
| 1697 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1698 | } else { |
| 1699 | // Make sure the bit-widths and signedness match. |
| 1700 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1701 | DesignatedEndIndex |
| 1702 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1703 | else if (DesignatedStartIndex.getBitWidth() < |
| 1704 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1705 | DesignatedStartIndex |
| 1706 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1707 | DesignatedStartIndex.setIsUnsigned(true); |
| 1708 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1709 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1711 | // Make sure that our non-designated initializer list has space |
| 1712 | // for a subobject corresponding to this array element. |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1713 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1715 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1716 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1717 | // Repeatedly perform subobject initializations in the range |
| 1718 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1719 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1720 | // Move to the next designator |
| 1721 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1722 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1723 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1724 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1725 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1727 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1728 | // Recurse to check later designated subobjects. |
| 1729 | QualType ElementType = AT->getElementType(); |
| 1730 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1731 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1732 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1733 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1734 | ElementType, 0, 0, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1735 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1736 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1737 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1738 | return true; |
| 1739 | |
| 1740 | // Move to the next index in the array that we'll be initializing. |
| 1741 | ++DesignatedStartIndex; |
| 1742 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1743 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1744 | |
| 1745 | // If this the first designator, our caller will continue checking |
| 1746 | // the rest of this array subobject. |
| 1747 | if (IsFirstDesignator) { |
| 1748 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1749 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1750 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1754 | if (!FinishSubobjectInit) |
| 1755 | return false; |
| 1756 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1757 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1758 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1759 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1760 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1761 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1765 | // Get the structured initializer list for a subobject of type |
| 1766 | // @p CurrentObjectType. |
| 1767 | InitListExpr * |
| 1768 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1769 | QualType CurrentObjectType, |
| 1770 | InitListExpr *StructuredList, |
| 1771 | unsigned StructuredIndex, |
| 1772 | SourceRange InitRange) { |
| 1773 | Expr *ExistingInit = 0; |
| 1774 | if (!StructuredList) |
| 1775 | ExistingInit = SyntacticToSemantic[IList]; |
| 1776 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1777 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1779 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1780 | return Result; |
| 1781 | |
| 1782 | if (ExistingInit) { |
| 1783 | // We are creating an initializer list that initializes the |
| 1784 | // subobjects of the current object, but there was already an |
| 1785 | // initialization that completely initialized the current |
| 1786 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1788 | // struct X { int a, b; }; |
| 1789 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1791 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1792 | // designated initializer re-initializes the whole |
| 1793 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1795 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1796 | << InitRange; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1798 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1799 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1800 | << ExistingInit->getSourceRange(); |
| 1801 | } |
| 1802 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1804 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 1805 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1806 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 1808 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1810 | // Pre-allocate storage for the structured initializer list. |
| 1811 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1812 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1813 | bool GotNumInits = false; |
| 1814 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1815 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1816 | GotNumInits = true; |
| 1817 | } else if (Index < IList->getNumInits()) { |
| 1818 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1819 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1820 | GotNumInits = true; |
| 1821 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1825 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1826 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1827 | NumElements = CAType->getSize().getZExtValue(); |
| 1828 | // Simple heuristic so that we don't allocate a very large |
| 1829 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 1830 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1831 | NumElements = 0; |
| 1832 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1833 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1834 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1835 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1836 | RecordDecl *RDecl = RType->getDecl(); |
| 1837 | if (RDecl->isUnion()) |
| 1838 | NumElements = 1; |
| 1839 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1841 | RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1844 | if (NumElements < NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1845 | NumElements = IList->getNumInits(); |
| 1846 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1847 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1848 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1849 | // Link this new initializer list into the structured initializer |
| 1850 | // lists. |
| 1851 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1852 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1853 | else { |
| 1854 | Result->setSyntacticForm(IList); |
| 1855 | SyntacticToSemantic[IList] = Result; |
| 1856 | } |
| 1857 | |
| 1858 | return Result; |
| 1859 | } |
| 1860 | |
| 1861 | /// Update the initializer at index @p StructuredIndex within the |
| 1862 | /// structured initializer list to the value @p expr. |
| 1863 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1864 | unsigned &StructuredIndex, |
| 1865 | Expr *expr) { |
| 1866 | // No structured initializer list to update |
| 1867 | if (!StructuredList) |
| 1868 | return; |
| 1869 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1870 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 1871 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1872 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1874 | diag::warn_initializer_overrides) |
| 1875 | << expr->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1877 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1878 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1879 | << PrevInit->getSourceRange(); |
| 1880 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1882 | ++StructuredIndex; |
| 1883 | } |
| 1884 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1885 | /// Check that the given Index expression is a valid array designator |
| 1886 | /// value. This is essentailly just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1887 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1888 | /// and produces a reasonable diagnostic if there is a |
| 1889 | /// failure. Returns true if there was an error, false otherwise. If |
| 1890 | /// everything went okay, Value will receive the value of the constant |
| 1891 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | static bool |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1893 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1894 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1895 | |
| 1896 | // Make sure this is an integer constant expression. |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1897 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1898 | return true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1899 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1900 | if (Value.isSigned() && Value.isNegative()) |
| 1901 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1902 | << Value.toString(10) << Index->getSourceRange(); |
| 1903 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1904 | Value.setIsUnsigned(true); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1905 | return false; |
| 1906 | } |
| 1907 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1908 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 1909 | SourceLocation Loc, |
| 1910 | bool GNUSyntax, |
| 1911 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1912 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1913 | |
| 1914 | bool Invalid = false; |
| 1915 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1916 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1917 | |
| 1918 | // Build designators and check array designator expressions. |
| 1919 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1920 | const Designator &D = Desig.getDesignator(Idx); |
| 1921 | switch (D.getKind()) { |
| 1922 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1924 | D.getFieldLoc())); |
| 1925 | break; |
| 1926 | |
| 1927 | case Designator::ArrayDesignator: { |
| 1928 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1929 | llvm::APSInt IndexValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1930 | if (!Index->isTypeDependent() && |
| 1931 | !Index->isValueDependent() && |
| 1932 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1933 | Invalid = true; |
| 1934 | else { |
| 1935 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1937 | D.getRBracketLoc())); |
| 1938 | InitExpressions.push_back(Index); |
| 1939 | } |
| 1940 | break; |
| 1941 | } |
| 1942 | |
| 1943 | case Designator::ArrayRangeDesignator: { |
| 1944 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1945 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1946 | llvm::APSInt StartValue; |
| 1947 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1948 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1949 | StartIndex->isValueDependent(); |
| 1950 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1951 | EndIndex->isValueDependent(); |
| 1952 | if ((!StartDependent && |
| 1953 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1954 | (!EndDependent && |
| 1955 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1956 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1957 | else { |
| 1958 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1959 | if (StartDependent || EndDependent) { |
| 1960 | // Nothing to compute. |
| 1961 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1962 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1963 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1964 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1965 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1966 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1967 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1969 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1970 | Invalid = true; |
| 1971 | } else { |
| 1972 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1974 | D.getEllipsisLoc(), |
| 1975 | D.getRBracketLoc())); |
| 1976 | InitExpressions.push_back(StartIndex); |
| 1977 | InitExpressions.push_back(EndIndex); |
| 1978 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1979 | } |
| 1980 | break; |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | if (Invalid || Init.isInvalid()) |
| 1986 | return ExprError(); |
| 1987 | |
| 1988 | // Clear out the expressions within the designation. |
| 1989 | Desig.ClearExprs(*this); |
| 1990 | |
| 1991 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1992 | = DesignatedInitExpr::Create(Context, |
| 1993 | Designators.data(), Designators.size(), |
| 1994 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1995 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1996 | |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 1997 | if (getLangOptions().CPlusPlus) |
Eli Friedman | ea7b85b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 1998 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 1999 | << DIE->getSourceRange(); |
| 2000 | else if (!getLangOptions().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2001 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2002 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2003 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2004 | return Owned(DIE); |
| 2005 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2006 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2007 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 2008 | InitListExpr *&InitList, QualType &DeclType) { |
| 2009 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2010 | if (!CheckInitList.HadError()) |
| 2011 | InitList = CheckInitList.getFullyStructuredList(); |
| 2012 | |
| 2013 | return CheckInitList.HadError(); |
| 2014 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2016 | //===----------------------------------------------------------------------===// |
| 2017 | // Initialization entity |
| 2018 | //===----------------------------------------------------------------------===// |
| 2019 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2020 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2021 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2022 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2023 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2024 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2025 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2026 | Type = AT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2027 | } else { |
| 2028 | Kind = EK_VectorElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2029 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2030 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2033 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2034 | CXXBaseSpecifier *Base, |
| 2035 | bool IsInheritedVirtualBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2036 | { |
| 2037 | InitializedEntity Result; |
| 2038 | Result.Kind = EK_Base; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2039 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2040 | if (IsInheritedVirtualBase) |
| 2041 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2043 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2044 | return Result; |
| 2045 | } |
| 2046 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2047 | DeclarationName InitializedEntity::getName() const { |
| 2048 | switch (getKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2049 | case EK_Parameter: { |
| 2050 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2051 | return (D ? D->getDeclName() : DeclarationName()); |
| 2052 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2053 | |
| 2054 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2055 | case EK_Member: |
| 2056 | return VariableOrMember->getDeclName(); |
| 2057 | |
| 2058 | case EK_Result: |
| 2059 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2060 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2061 | case EK_Temporary: |
| 2062 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2063 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2064 | case EK_ArrayElement: |
| 2065 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2066 | case EK_BlockElement: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2067 | return DeclarationName(); |
| 2068 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2069 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2070 | // Silence GCC warning |
| 2071 | return DeclarationName(); |
| 2072 | } |
| 2073 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2074 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2075 | switch (getKind()) { |
| 2076 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2077 | case EK_Member: |
| 2078 | return VariableOrMember; |
| 2079 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2080 | case EK_Parameter: |
| 2081 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2082 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2083 | case EK_Result: |
| 2084 | case EK_Exception: |
| 2085 | case EK_New: |
| 2086 | case EK_Temporary: |
| 2087 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2088 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2089 | case EK_ArrayElement: |
| 2090 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2091 | case EK_BlockElement: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2092 | return 0; |
| 2093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2094 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2095 | // Silence GCC warning |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2099 | bool InitializedEntity::allowsNRVO() const { |
| 2100 | switch (getKind()) { |
| 2101 | case EK_Result: |
| 2102 | case EK_Exception: |
| 2103 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2104 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2105 | case EK_Variable: |
| 2106 | case EK_Parameter: |
| 2107 | case EK_Member: |
| 2108 | case EK_New: |
| 2109 | case EK_Temporary: |
| 2110 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2111 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2112 | case EK_ArrayElement: |
| 2113 | case EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2114 | case EK_BlockElement: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2115 | break; |
| 2116 | } |
| 2117 | |
| 2118 | return false; |
| 2119 | } |
| 2120 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2121 | //===----------------------------------------------------------------------===// |
| 2122 | // Initialization sequence |
| 2123 | //===----------------------------------------------------------------------===// |
| 2124 | |
| 2125 | void InitializationSequence::Step::Destroy() { |
| 2126 | switch (Kind) { |
| 2127 | case SK_ResolveAddressOfOverloadedFunction: |
| 2128 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2129 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2130 | case SK_CastDerivedToBaseLValue: |
| 2131 | case SK_BindReference: |
| 2132 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2133 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2134 | case SK_UserConversion: |
| 2135 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2136 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2137 | case SK_QualificationConversionLValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2138 | case SK_ListInitialization: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2139 | case SK_ConstructorInitialization: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2140 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2141 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2142 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2143 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2144 | case SK_ArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2145 | case SK_PassByIndirectCopyRestore: |
| 2146 | case SK_PassByIndirectRestore: |
| 2147 | case SK_ProduceObjCObject: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2148 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2149 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2150 | case SK_ConversionSequence: |
| 2151 | delete ICS; |
| 2152 | } |
| 2153 | } |
| 2154 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2155 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2156 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2160 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2161 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2163 | switch (getFailureKind()) { |
| 2164 | case FK_TooManyInitsForReference: |
| 2165 | case FK_ArrayNeedsInitList: |
| 2166 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2167 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2168 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2169 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2170 | case FK_RValueReferenceBindingToLValue: |
| 2171 | case FK_ReferenceInitDropsQualifiers: |
| 2172 | case FK_ReferenceInitFailed: |
| 2173 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2174 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2175 | case FK_TooManyInitsForScalar: |
| 2176 | case FK_ReferenceBindingToInitList: |
| 2177 | case FK_InitListBadDestinationType: |
| 2178 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2179 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2180 | case FK_ArrayTypeMismatch: |
| 2181 | case FK_NonConstantArrayInit: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2182 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2183 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2184 | case FK_ReferenceInitOverloadFailed: |
| 2185 | case FK_UserConversionOverloadFailed: |
| 2186 | case FK_ConstructorOverloadFailed: |
| 2187 | return FailedOverloadResult == OR_Ambiguous; |
| 2188 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2189 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2190 | return false; |
| 2191 | } |
| 2192 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2193 | bool InitializationSequence::isConstructorInitialization() const { |
| 2194 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2195 | } |
| 2196 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2197 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2198 | FunctionDecl *Function, |
| 2199 | DeclAccessPair Found) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2200 | Step S; |
| 2201 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2202 | S.Type = Function->getType(); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2203 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2204 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2205 | Steps.push_back(S); |
| 2206 | } |
| 2207 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2208 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2209 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2210 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2211 | switch (VK) { |
| 2212 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2213 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2214 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2215 | default: llvm_unreachable("No such category"); |
| 2216 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2217 | S.Type = BaseType; |
| 2218 | Steps.push_back(S); |
| 2219 | } |
| 2220 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2221 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2222 | bool BindingTemporary) { |
| 2223 | Step S; |
| 2224 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2225 | S.Type = T; |
| 2226 | Steps.push_back(S); |
| 2227 | } |
| 2228 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2229 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2230 | Step S; |
| 2231 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2232 | S.Type = T; |
| 2233 | Steps.push_back(S); |
| 2234 | } |
| 2235 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2236 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2237 | DeclAccessPair FoundDecl, |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2238 | QualType T) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2239 | Step S; |
| 2240 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2241 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2242 | S.Function.Function = Function; |
| 2243 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2244 | Steps.push_back(S); |
| 2245 | } |
| 2246 | |
| 2247 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2248 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2249 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2250 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2251 | switch (VK) { |
| 2252 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2253 | S.Kind = SK_QualificationConversionRValue; |
| 2254 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2255 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2256 | S.Kind = SK_QualificationConversionXValue; |
| 2257 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2258 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2259 | S.Kind = SK_QualificationConversionLValue; |
| 2260 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2261 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2262 | S.Type = Ty; |
| 2263 | Steps.push_back(S); |
| 2264 | } |
| 2265 | |
| 2266 | void InitializationSequence::AddConversionSequenceStep( |
| 2267 | const ImplicitConversionSequence &ICS, |
| 2268 | QualType T) { |
| 2269 | Step S; |
| 2270 | S.Kind = SK_ConversionSequence; |
| 2271 | S.Type = T; |
| 2272 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2273 | Steps.push_back(S); |
| 2274 | } |
| 2275 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2276 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2277 | Step S; |
| 2278 | S.Kind = SK_ListInitialization; |
| 2279 | S.Type = T; |
| 2280 | Steps.push_back(S); |
| 2281 | } |
| 2282 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2283 | void |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2284 | InitializationSequence::AddConstructorInitializationStep( |
| 2285 | CXXConstructorDecl *Constructor, |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2286 | AccessSpecifier Access, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2287 | QualType T) { |
| 2288 | Step S; |
| 2289 | S.Kind = SK_ConstructorInitialization; |
| 2290 | S.Type = T; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2291 | S.Function.Function = Constructor; |
| 2292 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2293 | Steps.push_back(S); |
| 2294 | } |
| 2295 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2296 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2297 | Step S; |
| 2298 | S.Kind = SK_ZeroInitialization; |
| 2299 | S.Type = T; |
| 2300 | Steps.push_back(S); |
| 2301 | } |
| 2302 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2303 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2304 | Step S; |
| 2305 | S.Kind = SK_CAssignment; |
| 2306 | S.Type = T; |
| 2307 | Steps.push_back(S); |
| 2308 | } |
| 2309 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2310 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2311 | Step S; |
| 2312 | S.Kind = SK_StringInit; |
| 2313 | S.Type = T; |
| 2314 | Steps.push_back(S); |
| 2315 | } |
| 2316 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2317 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2318 | Step S; |
| 2319 | S.Kind = SK_ObjCObjectConversion; |
| 2320 | S.Type = T; |
| 2321 | Steps.push_back(S); |
| 2322 | } |
| 2323 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2324 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2325 | Step S; |
| 2326 | S.Kind = SK_ArrayInit; |
| 2327 | S.Type = T; |
| 2328 | Steps.push_back(S); |
| 2329 | } |
| 2330 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2331 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2332 | bool shouldCopy) { |
| 2333 | Step s; |
| 2334 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2335 | : SK_PassByIndirectRestore); |
| 2336 | s.Type = type; |
| 2337 | Steps.push_back(s); |
| 2338 | } |
| 2339 | |
| 2340 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2341 | Step S; |
| 2342 | S.Kind = SK_ProduceObjCObject; |
| 2343 | S.Type = T; |
| 2344 | Steps.push_back(S); |
| 2345 | } |
| 2346 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2347 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2348 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2349 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2350 | this->Failure = Failure; |
| 2351 | this->FailedOverloadResult = Result; |
| 2352 | } |
| 2353 | |
| 2354 | //===----------------------------------------------------------------------===// |
| 2355 | // Attempt initialization |
| 2356 | //===----------------------------------------------------------------------===// |
| 2357 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2358 | static void MaybeProduceObjCObject(Sema &S, |
| 2359 | InitializationSequence &Sequence, |
| 2360 | const InitializedEntity &Entity) { |
| 2361 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2362 | |
| 2363 | /// When initializing a parameter, produce the value if it's marked |
| 2364 | /// __attribute__((ns_consumed)). |
| 2365 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2366 | if (!Entity.isParameterConsumed()) |
| 2367 | return; |
| 2368 | |
| 2369 | assert(Entity.getType()->isObjCRetainableType() && |
| 2370 | "consuming an object of unretainable type?"); |
| 2371 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2372 | |
| 2373 | /// When initializing a return value, if the return type is a |
| 2374 | /// retainable type, then returns need to immediately retain the |
| 2375 | /// object. If an autorelease is required, it will be done at the |
| 2376 | /// last instant. |
| 2377 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2378 | if (!Entity.getType()->isObjCRetainableType()) |
| 2379 | return; |
| 2380 | |
| 2381 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2382 | } |
| 2383 | } |
| 2384 | |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 2385 | static void SelectInitialization(Sema &S, const InitializedEntity &Entity, |
| 2386 | const InitializationKind &Kind, |
| 2387 | Expr **Args, unsigned NumArgs, |
| 2388 | InitializationSequence &Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2389 | |
| 2390 | /// \brief Try a reference initialization that involves calling a conversion |
| 2391 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2392 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2393 | const InitializedEntity &Entity, |
| 2394 | const InitializationKind &Kind, |
| 2395 | Expr *Initializer, |
| 2396 | bool AllowRValues, |
| 2397 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2398 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2399 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2400 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2401 | QualType cv2T2 = Initializer->getType(); |
| 2402 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2403 | |
| 2404 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2405 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2406 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2407 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2408 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2409 | ObjCConversion, |
| 2410 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2411 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2412 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2413 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2414 | (void)ObjCLifetimeConversion; |
| 2415 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2416 | // Build the candidate set directly in the initialization sequence |
| 2417 | // structure, so that it will persist if we fail. |
| 2418 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2419 | CandidateSet.clear(); |
| 2420 | |
| 2421 | // Determine whether we are allowed to call explicit constructors or |
| 2422 | // explicit conversion operators. |
| 2423 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2424 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2425 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2426 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2427 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2428 | // The type we're converting to is a class type. Enumerate its constructors |
| 2429 | // to see if there is a suitable conversion. |
| 2430 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2431 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2432 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2433 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2434 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2435 | NamedDecl *D = *Con; |
| 2436 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2437 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2438 | // Find the constructor (which may be a template). |
| 2439 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2440 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2441 | if (ConstructorTmpl) |
| 2442 | Constructor = cast<CXXConstructorDecl>( |
| 2443 | ConstructorTmpl->getTemplatedDecl()); |
| 2444 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2445 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2446 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2447 | if (!Constructor->isInvalidDecl() && |
| 2448 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2449 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2450 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2451 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2452 | &Initializer, 1, CandidateSet, |
| 2453 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2454 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2455 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2456 | &Initializer, 1, CandidateSet, |
| 2457 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2458 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2459 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2460 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2461 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2462 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2463 | |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2464 | const RecordType *T2RecordType = 0; |
| 2465 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2466 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2467 | // The type we're converting from is a class type, enumerate its conversion |
| 2468 | // functions. |
| 2469 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2470 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2471 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2472 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2473 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2474 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2475 | NamedDecl *D = *I; |
| 2476 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2477 | if (isa<UsingShadowDecl>(D)) |
| 2478 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2479 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2480 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2481 | CXXConversionDecl *Conv; |
| 2482 | if (ConvTemplate) |
| 2483 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2484 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2485 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2487 | // If the conversion function doesn't return a reference type, |
| 2488 | // it can't be considered for this conversion unless we're allowed to |
| 2489 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2490 | // FIXME: Do we need to make sure that we only consider conversion |
| 2491 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2492 | // break recursion. |
| 2493 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2494 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2495 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2496 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2497 | ActingDC, Initializer, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2498 | DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2499 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2500 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | d412fe5 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2501 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2505 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2506 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2508 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2509 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2511 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2512 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2513 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2514 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2515 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2516 | FunctionDecl *Function = Best->Function; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2517 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2518 | // This is the overload that will actually be used for the initialization, so |
| 2519 | // mark it as used. |
| 2520 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2521 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2522 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2523 | if (isa<CXXConversionDecl>(Function)) |
| 2524 | T2 = Function->getResultType(); |
| 2525 | else |
| 2526 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2527 | |
| 2528 | // Add the user-defined conversion step. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2529 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2530 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2531 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2532 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2533 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2534 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2535 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2536 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2537 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2538 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2539 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2540 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2541 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2542 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2543 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2544 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2545 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2546 | NewDerivedToBase, NewObjCConversion, |
| 2547 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2548 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2549 | // If the type we've converted to is not reference-related to the |
| 2550 | // type we're looking for, then there is another conversion step |
| 2551 | // we need to perform to produce a temporary of the right type |
| 2552 | // that we'll be binding to. |
| 2553 | ImplicitConversionSequence ICS; |
| 2554 | ICS.setStandard(); |
| 2555 | ICS.Standard = Best->FinalConversion; |
| 2556 | T2 = ICS.Standard.getToType(2); |
| 2557 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 2558 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2559 | Sequence.AddDerivedToBaseCastStep( |
| 2560 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2561 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2562 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2563 | else if (NewObjCConversion) |
| 2564 | Sequence.AddObjCObjectConversionStep( |
| 2565 | S.Context.getQualifiedType(T1, |
| 2566 | T2.getNonReferenceType().getQualifiers())); |
| 2567 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2568 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2569 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2570 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2571 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2572 | return OR_Success; |
| 2573 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2574 | |
| 2575 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 2576 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2577 | const InitializedEntity &Entity, |
| 2578 | const InitializationKind &Kind, |
| 2579 | Expr *Initializer, |
| 2580 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2581 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2582 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2583 | Qualifiers T1Quals; |
| 2584 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2585 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2586 | Qualifiers T2Quals; |
| 2587 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2588 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2589 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2590 | // If the initializer is the address of an overloaded function, try |
| 2591 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2592 | // type of the resulting function. |
| 2593 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2594 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2595 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2596 | T1, |
| 2597 | false, |
| 2598 | Found)) { |
| 2599 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 2600 | cv2T2 = Fn->getType(); |
| 2601 | T2 = cv2T2.getUnqualifiedType(); |
| 2602 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2603 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2604 | return; |
| 2605 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2606 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2607 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2608 | // Compute some basic properties of the types and the initializer. |
| 2609 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2610 | bool isRValueRef = !isLValueRef; |
| 2611 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2612 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2613 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2614 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2615 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2616 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2617 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2618 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2619 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2620 | // 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] | 2621 | // "cv2 T2" as follows: |
| 2622 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2623 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2624 | // expression |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2625 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 2626 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 2627 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2628 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2629 | bool T1Function = T1->isFunctionType(); |
| 2630 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2631 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2632 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2633 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2634 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2635 | // - 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] | 2636 | // reference-compatible with "cv2 T2," or |
| 2637 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2638 | // 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] | 2639 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2640 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2641 | // to decide whether we're actually binding to a temporary created from |
| 2642 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2643 | if (DerivedToBase) |
| 2644 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2645 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2646 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2647 | else if (ObjCConversion) |
| 2648 | Sequence.AddObjCObjectConversionStep( |
| 2649 | S.Context.getQualifiedType(T1, T2Quals)); |
| 2650 | |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2651 | if (T1Quals != T2Quals) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2652 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2653 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2654 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2655 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2656 | return; |
| 2657 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2658 | |
| 2659 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2660 | // reference-related to T2, and can be implicitly converted to an |
| 2661 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2662 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2663 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2664 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2665 | // If we have an rvalue ref to function type here, the rhs must be |
| 2666 | // an rvalue. |
| 2667 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 2668 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2669 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2670 | Initializer, |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2671 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2672 | Sequence); |
| 2673 | if (ConvOvlResult == OR_Success) |
| 2674 | return; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2675 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2676 | Sequence.SetOverloadFailure( |
| 2677 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2678 | ConvOvlResult); |
| 2679 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2680 | } |
| 2681 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2682 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2683 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2684 | // 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] | 2685 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2686 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2687 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2688 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2689 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2690 | Sequence.SetOverloadFailure( |
| 2691 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2692 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2693 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2694 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2695 | ? (RefRelationship == Sema::Ref_Related |
| 2696 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2697 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2698 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2699 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2700 | return; |
| 2701 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2703 | // - If the initializer expression |
| 2704 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 2705 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 2706 | // Note: functions are handled below. |
| 2707 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2708 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2709 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2710 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2711 | (InitCategory.isXValue() || |
| 2712 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 2713 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 2714 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 2715 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2716 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 2717 | // compiler the freedom to perform a copy here or bind to the |
| 2718 | // object, while C++0x requires that we bind directly to the |
| 2719 | // object. Hence, we always bind to the object without making an |
| 2720 | // extra copy. However, in C++03 requires that we check for the |
| 2721 | // presence of a suitable copy constructor: |
| 2722 | // |
| 2723 | // The constructor that would be used to make the copy shall |
| 2724 | // be callable whether or not the copy is actually done. |
Francois Pichet | 687aaf0 | 2010-12-31 10:43:42 +0000 | [diff] [blame] | 2725 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().Microsoft) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2726 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2727 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2728 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2729 | if (DerivedToBase) |
| 2730 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 2731 | ValueKind); |
| 2732 | else if (ObjCConversion) |
| 2733 | Sequence.AddObjCObjectConversionStep( |
| 2734 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2736 | if (T1Quals != T2Quals) |
| 2737 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2738 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2739 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2740 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2741 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | |
| 2743 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2744 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2745 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 2746 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 2747 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2748 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2749 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2750 | Kind, Initializer, |
| 2751 | /*AllowRValues=*/true, |
| 2752 | Sequence); |
| 2753 | if (ConvOvlResult) |
| 2754 | Sequence.SetOverloadFailure( |
| 2755 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2756 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2758 | return; |
| 2759 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2760 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2761 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2762 | return; |
| 2763 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2764 | |
| 2765 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2766 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2767 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2768 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2770 | // Determine whether we are allowed to call explicit constructors or |
| 2771 | // explicit conversion operators. |
| 2772 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2773 | |
| 2774 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 2775 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2776 | ImplicitConversionSequence ICS |
| 2777 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 2778 | /*SuppressUserConversions*/ false, |
| 2779 | AllowExplicit, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2780 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2781 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 2782 | /*AllowObjCWritebackConversion=*/false); |
| 2783 | |
| 2784 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2785 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2786 | // this into an overloading ambiguity diagnostic. However, we need |
| 2787 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2788 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2789 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2790 | Sequence.SetOverloadFailure( |
| 2791 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2792 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 2793 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 2794 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2795 | else |
| 2796 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2797 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2798 | } else { |
| 2799 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 2803 | // same cv-qualification as, or greater cv-qualification |
| 2804 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2805 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 2806 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2807 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2808 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2809 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2810 | return; |
| 2811 | } |
| 2812 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | // [...] 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] | 2814 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2815 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 2816 | InitCategory.isLValue()) { |
| 2817 | Sequence.SetFailed( |
| 2818 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 2819 | return; |
| 2820 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2821 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2822 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2823 | return; |
| 2824 | } |
| 2825 | |
| 2826 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2827 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 2828 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2829 | const InitializedEntity &Entity, |
| 2830 | const InitializationKind &Kind, |
| 2831 | Expr *Initializer, |
| 2832 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2833 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2836 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2837 | /// enumerates the constructors of the initialized entity and performs overload |
| 2838 | /// resolution to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2839 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2840 | const InitializedEntity &Entity, |
| 2841 | const InitializationKind &Kind, |
| 2842 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2843 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2844 | InitializationSequence &Sequence) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2845 | // Build the candidate set directly in the initialization sequence |
| 2846 | // structure, so that it will persist if we fail. |
| 2847 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2848 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2849 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2850 | // Determine whether we are allowed to call explicit constructors or |
| 2851 | // explicit conversion operators. |
| 2852 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 2853 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2854 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2855 | |
| 2856 | // The type we're constructing needs to be complete. |
| 2857 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2858 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 2859 | return; |
| 2860 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2861 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2862 | // The type we're converting to is a class type. Enumerate its constructors |
| 2863 | // to see if one is suitable. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2864 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2865 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2866 | CXXRecordDecl *DestRecordDecl |
| 2867 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2868 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2869 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2870 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2871 | Con != ConEnd; ++Con) { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2872 | NamedDecl *D = *Con; |
| 2873 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2874 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2875 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2876 | // Find the constructor (which may be a template). |
| 2877 | CXXConstructorDecl *Constructor = 0; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2878 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2879 | if (ConstructorTmpl) |
| 2880 | Constructor = cast<CXXConstructorDecl>( |
| 2881 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2882 | else { |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2883 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2884 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2885 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2886 | // suppress user-defined conversions on the arguments. |
| 2887 | // FIXME: Move constructors? |
| 2888 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 2889 | Constructor->isCopyConstructor()) |
| 2890 | SuppressUserConversions = true; |
| 2891 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2892 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2893 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2894 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2895 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2896 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2897 | /*ExplicitArgs*/ 0, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2898 | Args, NumArgs, CandidateSet, |
| 2899 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2900 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2901 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | c779e99 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 2902 | Args, NumArgs, CandidateSet, |
| 2903 | SuppressUserConversions); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2904 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2907 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2908 | |
| 2909 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2910 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2911 | if (OverloadingResult Result |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2912 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2913 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2914 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2915 | Result); |
| 2916 | return; |
| 2917 | } |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2918 | |
| 2919 | // C++0x [dcl.init]p6: |
| 2920 | // If a program calls for the default initialization of an object |
| 2921 | // of a const-qualified type T, T shall be a class type with a |
| 2922 | // user-provided default constructor. |
| 2923 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2924 | Entity.getType().isConstQualified() && |
| 2925 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2926 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2927 | return; |
| 2928 | } |
| 2929 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2930 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2931 | // subsumed by the initialization. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2932 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2933 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2934 | Best->FoundDecl.getAccess(), |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2935 | DestType); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2938 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2939 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2940 | const InitializedEntity &Entity, |
| 2941 | const InitializationKind &Kind, |
| 2942 | InitializationSequence &Sequence) { |
| 2943 | // C++ [dcl.init]p5: |
| 2944 | // |
| 2945 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2946 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2947 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2948 | // -- if T is an array type, then each element is value-initialized; |
| 2949 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 2950 | T = AT->getElementType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2951 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2952 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2953 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2954 | // -- if T is a class type (clause 9) with a user-declared |
| 2955 | // constructor (12.1), then the default constructor for T is |
| 2956 | // called (and the initialization is ill-formed if T has no |
| 2957 | // accessible default constructor); |
| 2958 | // |
| 2959 | // FIXME: we really want to refer to a single subobject of the array, |
| 2960 | // but Entity doesn't have a way to capture that (yet). |
| 2961 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 2962 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2963 | |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2964 | // -- if T is a (possibly cv-qualified) non-union class type |
| 2965 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 2966 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2967 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2968 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2969 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2970 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2971 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2972 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2976 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2979 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 2980 | static void TryDefaultInitialization(Sema &S, |
| 2981 | const InitializedEntity &Entity, |
| 2982 | const InitializationKind &Kind, |
| 2983 | InitializationSequence &Sequence) { |
| 2984 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2985 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2986 | // C++ [dcl.init]p6: |
| 2987 | // To default-initialize an object of type T means: |
| 2988 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2989 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 2990 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2991 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 2992 | // constructor for T is called (and the initialization is ill-formed if |
| 2993 | // T has no accessible default constructor); |
Douglas Gregor | e656562 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 2994 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 2995 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 2996 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2997 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2998 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2999 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3000 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3001 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3002 | // 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] | 3003 | // default constructor. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3004 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3005 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3006 | return; |
| 3007 | } |
| 3008 | |
| 3009 | // If the destination type has a lifetime property, zero-initialize it. |
| 3010 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3011 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3012 | return; |
| 3013 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3016 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3017 | /// which enumerates all conversion functions and performs overload resolution |
| 3018 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3019 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3020 | const InitializedEntity &Entity, |
| 3021 | const InitializationKind &Kind, |
| 3022 | Expr *Initializer, |
| 3023 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3024 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3025 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3026 | QualType SourceType = Initializer->getType(); |
| 3027 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3028 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3029 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3030 | // Build the candidate set directly in the initialization sequence |
| 3031 | // structure, so that it will persist if we fail. |
| 3032 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3033 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3034 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3035 | // Determine whether we are allowed to call explicit constructors or |
| 3036 | // explicit conversion operators. |
| 3037 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3038 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3039 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3040 | // The type we're converting to is a class type. Enumerate its constructors |
| 3041 | // to see if there is a suitable conversion. |
| 3042 | CXXRecordDecl *DestRecordDecl |
| 3043 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3045 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3046 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3047 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3048 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3049 | Con != ConEnd; ++Con) { |
| 3050 | NamedDecl *D = *Con; |
| 3051 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3053 | // Find the constructor (which may be a template). |
| 3054 | CXXConstructorDecl *Constructor = 0; |
| 3055 | FunctionTemplateDecl *ConstructorTmpl |
| 3056 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3057 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3058 | Constructor = cast<CXXConstructorDecl>( |
| 3059 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3060 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3061 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3062 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3063 | if (!Constructor->isInvalidDecl() && |
| 3064 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3065 | if (ConstructorTmpl) |
| 3066 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3067 | /*ExplicitArgs*/ 0, |
| 3068 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3069 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3070 | else |
| 3071 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3072 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3073 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3074 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3075 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3076 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3077 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3078 | |
| 3079 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3080 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3081 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3082 | // The type we're converting from is a class type, enumerate its conversion |
| 3083 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3084 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3085 | // We can only enumerate the conversion functions for a complete type; if |
| 3086 | // the type isn't complete, simply skip this step. |
| 3087 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3088 | CXXRecordDecl *SourceRecordDecl |
| 3089 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3090 | |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3091 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3092 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3093 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3094 | E = Conversions->end(); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3095 | I != E; ++I) { |
| 3096 | NamedDecl *D = *I; |
| 3097 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3098 | if (isa<UsingShadowDecl>(D)) |
| 3099 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3100 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3101 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3102 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3103 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3104 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3105 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3106 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3108 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3109 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3110 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3111 | ActingDC, Initializer, DestType, |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3112 | CandidateSet); |
| 3113 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3114 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3115 | Initializer, DestType, CandidateSet); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3116 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3120 | |
| 3121 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3122 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3123 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3124 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3125 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3126 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3127 | Result); |
| 3128 | return; |
| 3129 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3130 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3131 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3132 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3133 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3134 | if (isa<CXXConstructorDecl>(Function)) { |
| 3135 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3136 | // subsumed by the initialization. |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3137 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3138 | return; |
| 3139 | } |
| 3140 | |
| 3141 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3142 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3143 | if (ConvType->getAs<RecordType>()) { |
| 3144 | // If we're converting to a class type, there may be an copy if |
| 3145 | // the resulting temporary object (possible to create an object of |
| 3146 | // a base class type). That copy is not a separate conversion, so |
| 3147 | // we just make a note of the actual destination type (possibly a |
| 3148 | // base class of the type returned by the conversion function) and |
| 3149 | // let the user-defined conversion step handle the conversion. |
| 3150 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3151 | return; |
| 3152 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3153 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3154 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3156 | // If the conversion following the call to the conversion function |
| 3157 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3158 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3159 | Best->FinalConversion.Third) { |
| 3160 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3161 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3162 | ICS.Standard = Best->FinalConversion; |
| 3163 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3164 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3167 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3168 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3169 | |
| 3170 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3171 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3172 | bool isAddressOf) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3173 | // Skip parens. |
| 3174 | e = e->IgnoreParens(); |
| 3175 | |
| 3176 | // Skip address-of nodes. |
| 3177 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3178 | if (op->getOpcode() == UO_AddrOf) |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3179 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3180 | |
| 3181 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3182 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3183 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3184 | case CK_Dependent: |
| 3185 | case CK_BitCast: |
| 3186 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3187 | case CK_NoOp: |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3188 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3189 | |
| 3190 | case CK_ArrayToPointerDecay: |
| 3191 | return IIK_nonscalar; |
| 3192 | |
| 3193 | case CK_NullToPointer: |
| 3194 | return IIK_okay; |
| 3195 | |
| 3196 | default: |
| 3197 | break; |
| 3198 | } |
| 3199 | |
| 3200 | // 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] | 3201 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3202 | if (!isAddressOf) return IIK_nonlocal; |
| 3203 | |
| 3204 | VarDecl *var; |
| 3205 | if (isa<DeclRefExpr>(e)) { |
| 3206 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3207 | if (!var) return IIK_nonlocal; |
| 3208 | } else { |
| 3209 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3210 | } |
| 3211 | |
| 3212 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3213 | |
| 3214 | // If we have a conditional operator, check both sides. |
| 3215 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3216 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3217 | return iik; |
| 3218 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3219 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3220 | |
| 3221 | // These are never scalar. |
| 3222 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3223 | return IIK_nonscalar; |
| 3224 | |
| 3225 | // Otherwise, it needs to be a null pointer constant. |
| 3226 | } else { |
| 3227 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3228 | ? IIK_okay : IIK_nonlocal); |
| 3229 | } |
| 3230 | |
| 3231 | return IIK_nonlocal; |
| 3232 | } |
| 3233 | |
| 3234 | /// Check whether the given expression is a valid operand for an |
| 3235 | /// indirect copy/restore. |
| 3236 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3237 | assert(src->isRValue()); |
| 3238 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3239 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3240 | if (iik == IIK_okay) return; |
| 3241 | |
| 3242 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3243 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3244 | << src->getSourceRange(); |
| 3245 | } |
| 3246 | |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3247 | static bool hasDefaultConstructor(Sema &S, CXXRecordDecl *decl) { |
| 3248 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 3249 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(decl); |
| 3250 | Con != ConEnd; ++Con) { |
| 3251 | // FIXME: A constructor template can be a default constructor, but we don't |
| 3252 | // handle this in other places as well. |
| 3253 | if (isa<FunctionTemplateDecl>(*Con)) |
| 3254 | continue; |
| 3255 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 3256 | if (Constructor->isDefaultConstructor()) |
| 3257 | return true; |
| 3258 | } |
| 3259 | return false; |
| 3260 | } |
| 3261 | |
| 3262 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3263 | static void TryListInitialization(Sema &S, |
| 3264 | const InitializedEntity &Entity, |
| 3265 | const InitializationKind &Kind, |
| 3266 | InitListExpr *InitList, |
| 3267 | InitializationSequence &Sequence) { |
| 3268 | QualType DestType = Entity.getType(); |
| 3269 | |
| 3270 | // If we're not in C++ mode, defer everything to the init list checker. |
| 3271 | if (!S.getLangOptions().CPlusPlus) { |
| 3272 | Sequence.AddListInitializationStep(DestType); |
| 3273 | return; |
| 3274 | } |
| 3275 | |
| 3276 | // Early error return for some C++11 features when we're in 98 mode. |
| 3277 | if (!S.getLangOptions().CPlusPlus0x) { |
| 3278 | if (DestType->isReferenceType()) { |
| 3279 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3280 | return; |
| 3281 | } |
| 3282 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
| 3283 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 3284 | return; |
| 3285 | } |
| 3286 | } |
| 3287 | |
| 3288 | // If we have a reference and not exactly one initializer (see below), unwrap |
| 3289 | // the reference. |
| 3290 | bool wasReference = false; |
| 3291 | if (InitList->getNumInits() != 1) { |
| 3292 | if (const ReferenceType *ref = DestType->getAs<ReferenceType>()) { |
| 3293 | wasReference = true; |
| 3294 | DestType = ref->getPointeeType(); |
| 3295 | } |
| 3296 | } |
| 3297 | // Create an object that automatically adds a ref binding step on successful |
| 3298 | // return. |
| 3299 | class AddRefBinding { |
| 3300 | InitializationSequence &Sequence; |
| 3301 | bool Bind; |
| 3302 | QualType RefType; |
| 3303 | public: |
| 3304 | AddRefBinding(InitializationSequence &sequence, bool bind, QualType refType) |
| 3305 | : Sequence(sequence), Bind(bind), RefType(refType) {} |
| 3306 | ~AddRefBinding() { |
| 3307 | if (Bind && Sequence) { |
| 3308 | Sequence.AddReferenceBindingStep(RefType, /*temporary*/true); |
| 3309 | } |
| 3310 | } |
| 3311 | } addRefBinding(Sequence, wasReference, Entity.getType()); |
| 3312 | |
| 3313 | // C++11 [dcl.init.list]p3: |
| 3314 | // List-initialization of an object or reference of type T is defined as |
| 3315 | // follows: |
| 3316 | // |
| 3317 | // - If the initializer list has no elements and T is a class type with |
| 3318 | // a default constructor, the object is value-initialized. |
| 3319 | // |
| 3320 | // See DR990. This case is handled specially because if we let it get to |
| 3321 | // overload resolution, std::initializer_list constructors would be chosen |
| 3322 | // over the default constructor. When there's more than one initlist ctor, |
| 3323 | // this would actually be ambiguous and fail. |
| 3324 | |
| 3325 | const RecordType *recordType = DestType->getAs<RecordType>(); |
| 3326 | CXXRecordDecl *recordDecl = recordType ? |
| 3327 | dyn_cast<CXXRecordDecl>(recordType->getDecl()) : 0; |
| 3328 | if (recordDecl && InitList->getNumInits() == 0 && |
| 3329 | hasDefaultConstructor(S, recordDecl)) { |
| 3330 | TryValueInitialization(S, Entity, Kind, Sequence); |
| 3331 | return; |
| 3332 | } |
| 3333 | |
| 3334 | // - Otherwise, if T is an aggregate, aggregate initialization is |
| 3335 | // performed. |
| 3336 | // |
| 3337 | // Aggregate initialization is the most complicated part. We delegate to |
| 3338 | // an InitListChecker to build a representation of what's happening. |
| 3339 | // We also treat vector types the same as aggregates. |
| 3340 | if (DestType->isAggregateType() || DestType->isVectorType()) { |
| 3341 | // FIXME: Deeper analysis necessary. |
| 3342 | Sequence.AddListInitializationStep(DestType); |
| 3343 | return; |
| 3344 | } |
| 3345 | |
| 3346 | // - Otherwise, if T is a specialization of std::initializer_list<E>, an |
| 3347 | // initializer_list object is constructed as described below and used |
| 3348 | // to initialize the object according to the rules for initialization |
| 3349 | // of an object from a class of the same type. |
| 3350 | // |
| 3351 | // FIXME: Implement this case. |
| 3352 | |
| 3353 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3354 | // applicable constructors are enumerated and the best one is chosen |
| 3355 | // through overload resolution. |
| 3356 | if (recordDecl) { |
| 3357 | // FIXME: initializer_list constructors are applicable. |
| 3358 | TryConstructorInitialization(S, Entity, Kind, InitList->getInits(), |
| 3359 | InitList->getNumInits(), DestType, Sequence); |
| 3360 | return; |
| 3361 | } |
| 3362 | |
| 3363 | // At this point, there is most likely a defect in the standard. The next |
| 3364 | // bullet grabs all reference targets and creates temporaries from the init |
| 3365 | // list. However, this means that code such as this doesn't work: |
| 3366 | // int i; |
| 3367 | // int &ri { i }; // error: non-const lvalue ref cannot bind to temporary. |
| 3368 | // This is rather startling, since this code works: |
| 3369 | // int &si ( i ); |
| 3370 | // |
| 3371 | // DR934 (CD2 status) tried to address the problem by making the bullet about |
| 3372 | // references be only about references to class types, letting references to |
| 3373 | // other things fall through. This means the above works, but this still |
| 3374 | // doesn't: |
| 3375 | // string s; |
| 3376 | // string &rs { s }; // cannot bind to temporary |
| 3377 | // string &ss ( s ); // fine |
| 3378 | // And this works, but has different semantics: |
| 3379 | // const string &cs { s }; // binds to temporary copy |
| 3380 | // const string &ds ( s ); // binds directly to s |
| 3381 | // Also, the wording change from that DR somehow got lost in the FDIS. |
| 3382 | // |
| 3383 | // DR1095 (FDIS status) again discovered the problem, but didn't actually |
| 3384 | // fix it. |
| 3385 | // |
| 3386 | // GCC implements it this way. We swap the next two bullets instead, thus |
| 3387 | // always letting a reference bind to the single element of an initializer |
| 3388 | // list, and constructing a temporary only if the isn't exactly one element. |
| 3389 | // So in our order, the next bullet is: |
| 3390 | // |
| 3391 | // - Otherwise, if the initializer list has a single element, the object |
| 3392 | // or reference is initialized from that element; |
| 3393 | if (InitList->getNumInits() == 1) { |
| 3394 | SelectInitialization(S, Entity, Kind, InitList->getInits(), |
| 3395 | InitList->getNumInits(), Sequence); |
| 3396 | // Adjust the type of the whole init list to be the same as that of the |
| 3397 | // single initializer. |
| 3398 | InitList->setType(InitList->getInits()[0]->getType()); |
| 3399 | return; |
| 3400 | } |
| 3401 | |
| 3402 | // - Otherwise, if T is a reference type, a prvalue temporary of the type |
| 3403 | // referenced by T is list-initialized, and the reference is bound to |
| 3404 | // that temporary. |
| 3405 | // |
| 3406 | // We implement this by unwrapping references at the start of the function |
| 3407 | // and adding a reference binding step at the bottom. |
| 3408 | |
| 3409 | // - Otherwise, if the initializer list has no elements, the object is |
| 3410 | // value-initialized. |
| 3411 | if (InitList->getNumInits() == 0) { |
| 3412 | TryValueInitialization(S, Entity, Kind, Sequence); |
| 3413 | return; |
| 3414 | } |
| 3415 | |
| 3416 | // - Otherwise, the program is ill-formed. |
| 3417 | // |
| 3418 | // The only way to get here ought to be for scalar types with > 1 inits. |
| 3419 | assert(DestType->isScalarType() && "Something strange is list-initialized."); |
| 3420 | assert(InitList->getNumInits() > 1 && "Strange number of initializers."); |
| 3421 | |
| 3422 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3423 | return; |
| 3424 | } |
| 3425 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3426 | /// \brief Determine whether we have compatible array types for the |
| 3427 | /// purposes of GNU by-copy array initialization. |
| 3428 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3429 | const ArrayType *Dest, |
| 3430 | const ArrayType *Source) { |
| 3431 | // If the source and destination array types are equivalent, we're |
| 3432 | // done. |
| 3433 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3434 | return true; |
| 3435 | |
| 3436 | // Make sure that the element types are the same. |
| 3437 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3438 | return false; |
| 3439 | |
| 3440 | // The only mismatch we allow is when the destination is an |
| 3441 | // incomplete array type and the source is a constant array type. |
| 3442 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3443 | } |
| 3444 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3445 | static bool tryObjCWritebackConversion(Sema &S, |
| 3446 | InitializationSequence &Sequence, |
| 3447 | const InitializedEntity &Entity, |
| 3448 | Expr *Initializer) { |
| 3449 | bool ArrayDecay = false; |
| 3450 | QualType ArgType = Initializer->getType(); |
| 3451 | QualType ArgPointee; |
| 3452 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3453 | ArrayDecay = true; |
| 3454 | ArgPointee = ArgArrayType->getElementType(); |
| 3455 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3456 | } |
| 3457 | |
| 3458 | // Handle write-back conversion. |
| 3459 | QualType ConvertedArgType; |
| 3460 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3461 | ConvertedArgType)) |
| 3462 | return false; |
| 3463 | |
| 3464 | // We should copy unless we're passing to an argument explicitly |
| 3465 | // marked 'out'. |
| 3466 | bool ShouldCopy = true; |
| 3467 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3468 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3469 | |
| 3470 | // Do we need an lvalue conversion? |
| 3471 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3472 | ImplicitConversionSequence ICS; |
| 3473 | ICS.setStandard(); |
| 3474 | ICS.Standard.setAsIdentityConversion(); |
| 3475 | |
| 3476 | QualType ResultType; |
| 3477 | if (ArrayDecay) { |
| 3478 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3479 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3480 | } else { |
| 3481 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3482 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3483 | } |
| 3484 | |
| 3485 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3486 | } |
| 3487 | |
| 3488 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3489 | return true; |
| 3490 | } |
| 3491 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3492 | InitializationSequence::InitializationSequence(Sema &S, |
| 3493 | const InitializedEntity &Entity, |
| 3494 | const InitializationKind &Kind, |
| 3495 | Expr **Args, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3496 | unsigned NumArgs) |
| 3497 | : FailedCandidateSet(Kind.getLocation()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3499 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3500 | // The semantics of initializers are as follows. The destination type is |
| 3501 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3502 | // 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] | 3503 | // 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] | 3504 | // parenthesized list of expressions. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3505 | |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3506 | if (Entity.getType()->isDependentType() || |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3507 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3508 | SequenceKind = DependentSequence; |
| 3509 | return; |
| 3510 | } |
| 3511 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3512 | // Almost everything is a normal sequence. |
| 3513 | setSequenceKind(NormalSequence); |
| 3514 | |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3515 | SelectInitialization(S, Entity, Kind, Args, NumArgs, *this); |
| 3516 | } |
| 3517 | |
| 3518 | static void SelectInitialization(Sema &S, const InitializedEntity &Entity, |
| 3519 | const InitializationKind &Kind, |
| 3520 | Expr **Args, unsigned NumArgs, |
| 3521 | InitializationSequence &Sequence) { |
| 3522 | ASTContext &Context = S.Context; |
| 3523 | QualType DestType = Entity.getType(); |
| 3524 | |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3525 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3526 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3527 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3528 | if (Result.isInvalid()) { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3529 | Sequence.SetFailed( |
| 3530 | InitializationSequence::FK_ConversionFromPropertyFailed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3531 | return; |
| 3532 | } |
| 3533 | Args[I] = Result.take(); |
| 3534 | } |
John McCall | ed75c09 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3535 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3536 | QualType SourceType; |
| 3537 | Expr *Initializer = 0; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3538 | if (NumArgs == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3539 | Initializer = Args[0]; |
| 3540 | if (!isa<InitListExpr>(Initializer)) |
| 3541 | SourceType = Initializer->getType(); |
| 3542 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3543 | |
| 3544 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3545 | // list-initialized (8.5.4). |
| 3546 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3547 | TryListInitialization(S, Entity, Kind, InitList, Sequence); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3548 | return; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3549 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3551 | // - If the destination type is a reference type, see 8.5.3. |
| 3552 | if (DestType->isReferenceType()) { |
| 3553 | // C++0x [dcl.init.ref]p1: |
| 3554 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3555 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3556 | // by an object that can be converted into a T. |
| 3557 | // (Therefore, multiple arguments are not permitted.) |
| 3558 | if (NumArgs != 1) |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3559 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3560 | else |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3561 | TryReferenceInitialization(S, Entity, Kind, Args[0], Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3562 | return; |
| 3563 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3565 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3566 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3567 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3568 | TryValueInitialization(S, Entity, Kind, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3569 | return; |
| 3570 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3571 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3572 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3573 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3574 | TryDefaultInitialization(S, Entity, Kind, Sequence); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3575 | return; |
| 3576 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3577 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3578 | // - If the destination type is an array of characters, an array of |
| 3579 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3580 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3582 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3583 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3584 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3585 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, Sequence); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3586 | return; |
| 3587 | } |
| 3588 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3589 | // Note: as an GNU C extension, we allow initialization of an |
| 3590 | // array from a compound literal that creates an array of the same |
| 3591 | // type, so long as the initializer has no side effects. |
| 3592 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3593 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3594 | Initializer->getType()->isArrayType()) { |
| 3595 | const ArrayType *SourceAT |
| 3596 | = Context.getAsArrayType(Initializer->getType()); |
| 3597 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3598 | Sequence.SetFailed(InitializationSequence::FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3599 | else if (Initializer->HasSideEffects(S.Context)) |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3600 | Sequence.SetFailed(InitializationSequence::FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3601 | else { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3602 | Sequence.AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3603 | } |
| 3604 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3605 | Sequence.SetFailed( |
| 3606 | InitializationSequence::FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3607 | else |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3608 | Sequence.SetFailed(InitializationSequence::FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3609 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3610 | return; |
| 3611 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3612 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3613 | // Determine whether we should consider writeback conversions for |
| 3614 | // Objective-C ARC. |
| 3615 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3616 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3617 | |
| 3618 | // We're at the end of the line for C: it's either a write-back conversion |
| 3619 | // 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] | 3620 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3621 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3622 | if (allowObjCWritebackConversion && |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3623 | tryObjCWritebackConversion(S, Sequence, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3624 | return; |
| 3625 | } |
| 3626 | |
| 3627 | // Handle initialization in C |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3628 | Sequence.AddCAssignmentStep(DestType); |
| 3629 | MaybeProduceObjCObject(S, Sequence, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3630 | return; |
| 3631 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3632 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3633 | assert(S.getLangOptions().CPlusPlus); |
| 3634 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3635 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3636 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3637 | // - If the initialization is direct-initialization, or if it is |
| 3638 | // copy-initialization where the cv-unqualified version of the |
| 3639 | // 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] | 3640 | // class of the destination, constructors are considered. [...] |
| 3641 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3642 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3643 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3644 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3645 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3646 | Entity.getType(), Sequence); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3647 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3648 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3649 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3650 | // used) to a derived class thereof are enumerated as described in |
| 3651 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3652 | // (13.3). |
| 3653 | else |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3654 | TryUserDefinedConversion(S, Entity, Kind, Initializer, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3655 | return; |
| 3656 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3657 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3658 | if (NumArgs > 1) { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3659 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3660 | return; |
| 3661 | } |
| 3662 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3663 | |
| 3664 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3665 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3666 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3667 | TryUserDefinedConversion(S, Entity, Kind, Initializer, Sequence); |
| 3668 | MaybeProduceObjCObject(S, Sequence, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3669 | return; |
| 3670 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3671 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3672 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3673 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3674 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3675 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3676 | // destination type; no user-defined conversions are considered. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3677 | |
| 3678 | ImplicitConversionSequence ICS |
| 3679 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3680 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3681 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3682 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3683 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3684 | allowObjCWritebackConversion); |
| 3685 | |
| 3686 | if (ICS.isStandard() && |
| 3687 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3688 | // Objective-C ARC writeback conversion. |
| 3689 | |
| 3690 | // We should copy unless we're passing to an argument explicitly |
| 3691 | // marked 'out'. |
| 3692 | bool ShouldCopy = true; |
| 3693 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3694 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3695 | |
| 3696 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3697 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3698 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3699 | ImplicitConversionSequence LvalueICS; |
| 3700 | LvalueICS.setStandard(); |
| 3701 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3702 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3703 | LvalueICS.Standard.First = ICS.Standard.First; |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3704 | Sequence.AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3705 | } |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3706 | |
| 3707 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3708 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3709 | DeclAccessPair dap; |
| 3710 | if (Initializer->getType() == Context.OverloadTy && |
| 3711 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3712 | , DestType, false, dap)) |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3713 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3714 | else |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3715 | Sequence.SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3716 | } else { |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3717 | Sequence.AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 3718 | |
Sebastian Redl | 9c61809 | 2011-07-14 19:08:10 +0000 | [diff] [blame] | 3719 | MaybeProduceObjCObject(S, Sequence, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3720 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | InitializationSequence::~InitializationSequence() { |
| 3724 | for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
| 3725 | StepEnd = Steps.end(); |
| 3726 | Step != StepEnd; ++Step) |
| 3727 | Step->Destroy(); |
| 3728 | } |
| 3729 | |
| 3730 | //===----------------------------------------------------------------------===// |
| 3731 | // Perform initialization |
| 3732 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3733 | static Sema::AssignmentAction |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3734 | getAssignmentAction(const InitializedEntity &Entity) { |
| 3735 | switch(Entity.getKind()) { |
| 3736 | case InitializedEntity::EK_Variable: |
| 3737 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 3738 | case InitializedEntity::EK_Exception: |
| 3739 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3740 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3741 | return Sema::AA_Initializing; |
| 3742 | |
| 3743 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3744 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 3745 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 3746 | return Sema::AA_Sending; |
| 3747 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3748 | return Sema::AA_Passing; |
| 3749 | |
| 3750 | case InitializedEntity::EK_Result: |
| 3751 | return Sema::AA_Returning; |
| 3752 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3753 | case InitializedEntity::EK_Temporary: |
| 3754 | // FIXME: Can we tell apart casting vs. converting? |
| 3755 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3757 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3758 | case InitializedEntity::EK_ArrayElement: |
| 3759 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3760 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3761 | return Sema::AA_Initializing; |
| 3762 | } |
| 3763 | |
| 3764 | return Sema::AA_Converting; |
| 3765 | } |
| 3766 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3767 | /// \brief Whether we should binding a created object as a temporary when |
| 3768 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3769 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3770 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3771 | case InitializedEntity::EK_ArrayElement: |
| 3772 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3773 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3774 | case InitializedEntity::EK_New: |
| 3775 | case InitializedEntity::EK_Variable: |
| 3776 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3777 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3778 | case InitializedEntity::EK_VectorElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 3779 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3780 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3781 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3782 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3783 | case InitializedEntity::EK_Parameter: |
| 3784 | case InitializedEntity::EK_Temporary: |
| 3785 | return true; |
| 3786 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3787 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3788 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3789 | } |
| 3790 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3791 | /// \brief Whether the given entity, when initialized with an object |
| 3792 | /// created for that initialization, requires destruction. |
| 3793 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 3794 | switch (Entity.getKind()) { |
| 3795 | case InitializedEntity::EK_Member: |
| 3796 | case InitializedEntity::EK_Result: |
| 3797 | case InitializedEntity::EK_New: |
| 3798 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3799 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3800 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3801 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3802 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3803 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3804 | case InitializedEntity::EK_Variable: |
| 3805 | case InitializedEntity::EK_Parameter: |
| 3806 | case InitializedEntity::EK_Temporary: |
| 3807 | case InitializedEntity::EK_ArrayElement: |
| 3808 | case InitializedEntity::EK_Exception: |
| 3809 | return true; |
| 3810 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3811 | |
| 3812 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3815 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 3816 | /// provided by the given initializer by calling the appropriate copy |
| 3817 | /// constructor. |
| 3818 | /// |
| 3819 | /// \param S The Sema object used for type-checking. |
| 3820 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3821 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3822 | /// the type of the initializer expression or a superclass thereof. |
| 3823 | /// |
| 3824 | /// \param Enter The entity being initialized. |
| 3825 | /// |
| 3826 | /// \param CurInit The initializer expression. |
| 3827 | /// |
| 3828 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 3829 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 3830 | /// an rvalue. |
| 3831 | /// |
| 3832 | /// \returns An expression that copies the initializer expression into |
| 3833 | /// a temporary object, or an error expression if a copy could not be |
| 3834 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3835 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3836 | QualType T, |
| 3837 | const InitializedEntity &Entity, |
| 3838 | ExprResult CurInit, |
| 3839 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3840 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3841 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3842 | CXXRecordDecl *Class = 0; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3843 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3844 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3845 | if (!Class) |
| 3846 | return move(CurInit); |
| 3847 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 3848 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3849 | // When certain criteria are met, an implementation is allowed to |
| 3850 | // omit the copy/move construction of a class object, even if the |
| 3851 | // copy/move constructor and/or destructor for the object have |
| 3852 | // side effects. [...] |
| 3853 | // - when a temporary class object that has not been bound to a |
| 3854 | // reference (12.2) would be copied/moved to a class object |
| 3855 | // with the same cv-unqualified type, the copy/move operation |
| 3856 | // can be omitted by constructing the temporary object |
| 3857 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3858 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3859 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3860 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3861 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 3862 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 3863 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3864 | SourceLocation Loc; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3865 | switch (Entity.getKind()) { |
| 3866 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3867 | Loc = Entity.getReturnLoc(); |
| 3868 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3870 | case InitializedEntity::EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3871 | Loc = Entity.getThrowLoc(); |
| 3872 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3873 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3874 | case InitializedEntity::EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3875 | Loc = Entity.getDecl()->getLocation(); |
| 3876 | break; |
| 3877 | |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3878 | case InitializedEntity::EK_ArrayElement: |
| 3879 | case InitializedEntity::EK_Member: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3880 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3881 | case InitializedEntity::EK_Temporary: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3882 | case InitializedEntity::EK_New: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3883 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 3884 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3885 | case InitializedEntity::EK_VectorElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 3886 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3887 | Loc = CurInitExpr->getLocStart(); |
| 3888 | break; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3889 | } |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3890 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3891 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 3892 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 3893 | return move(CurInit); |
| 3894 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3895 | // Perform overload resolution using the class's copy/move constructors. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3896 | DeclContext::lookup_iterator Con, ConEnd; |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3897 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 52b7282 | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3898 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3899 | Con != ConEnd; ++Con) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3900 | // Only consider copy/move constructors and constructor templates. Per |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3901 | // C++0x [dcl.init]p16, second bullet to class types, this |
| 3902 | // initialization is direct-initialization. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3903 | CXXConstructorDecl *Constructor = 0; |
| 3904 | |
| 3905 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3906 | // Handle copy/moveconstructors, only. |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3907 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 3908 | !Constructor->isCopyOrMoveConstructor() || |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3909 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3910 | continue; |
| 3911 | |
| 3912 | DeclAccessPair FoundDecl |
| 3913 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 3914 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3915 | &CurInitExpr, 1, CandidateSet); |
| 3916 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | } |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3918 | |
| 3919 | // Handle constructor templates. |
| 3920 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 3921 | if (ConstructorTmpl->isInvalidDecl()) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3922 | continue; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3923 | |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3924 | Constructor = cast<CXXConstructorDecl>( |
| 3925 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | cbd0710 | 2010-11-12 03:34:06 +0000 | [diff] [blame] | 3926 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3927 | continue; |
| 3928 | |
| 3929 | // FIXME: Do we need to limit this to copy-constructor-like |
| 3930 | // candidates? |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3931 | DeclAccessPair FoundDecl |
Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 3932 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 3933 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 3934 | &CurInitExpr, 1, CandidateSet, true); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3935 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3936 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3937 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3938 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3939 | case OR_Success: |
| 3940 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3941 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3942 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3943 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 3944 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 3945 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3946 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3947 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3948 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3949 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3950 | return ExprError(); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3951 | return move(CurInit); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3952 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3953 | case OR_Ambiguous: |
| 3954 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3955 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3956 | << CurInitExpr->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3957 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3958 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3959 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3960 | case OR_Deleted: |
| 3961 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3962 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3963 | << CurInitExpr->getSourceRange(); |
| 3964 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3965 | << 1 << Best->Function->isDeleted(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3966 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3969 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3970 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3971 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3972 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 3973 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 3974 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3975 | |
| 3976 | if (IsExtraneousCopy) { |
| 3977 | // If this is a totally extraneous copy for C++03 reference |
| 3978 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3979 | // expression. We don't generate an (elided) copy operation here |
| 3980 | // because doing so would require us to pass down a flag to avoid |
| 3981 | // infinite recursion, where each step adds another extraneous, |
| 3982 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 3984 | // Instantiate the default arguments of any extra parameters in |
| 3985 | // the selected copy constructor, as if we were going to create a |
| 3986 | // proper call to the copy constructor. |
| 3987 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 3988 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 3989 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 3990 | S.PDiag(diag::err_call_incomplete_argument))) |
| 3991 | break; |
| 3992 | |
| 3993 | // Build the default argument expression; we don't actually care |
| 3994 | // if this succeeds or not, because this routine will complain |
| 3995 | // if there was a problem. |
| 3996 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 3997 | } |
| 3998 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3999 | return S.Owned(CurInitExpr); |
| 4000 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4001 | |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4002 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4003 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4004 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4005 | // constructor call (we might have derived-to-base conversions, or |
| 4006 | // the copy constructor may have default arguments). |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4007 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4008 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4009 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4011 | // Actually perform the constructor call. |
| 4012 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4013 | move_arg(ConstructorArgs), |
| 4014 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4015 | CXXConstructExpr::CK_Complete, |
| 4016 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4018 | // If we're supposed to bind temporaries, do so. |
| 4019 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4020 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4021 | return move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4022 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4023 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4024 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4025 | const InitializedEntity &Entity) { |
| 4026 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4027 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4028 | return; |
| 4029 | |
| 4030 | if (Entity.getDecl()->getDeclName()) |
| 4031 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4032 | << Entity.getDecl()->getDeclName(); |
| 4033 | else |
| 4034 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4035 | } |
| 4036 | } |
| 4037 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4038 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4039 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4040 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4041 | } |
| 4042 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4043 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4044 | InitializationSequence::Perform(Sema &S, |
| 4045 | const InitializedEntity &Entity, |
| 4046 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4047 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4048 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4049 | if (Failed()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4050 | unsigned NumArgs = Args.size(); |
| 4051 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4052 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4053 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4054 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4055 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4056 | // If the declaration is a non-dependent, incomplete array type |
| 4057 | // that has an initializer, then its type will be completed once |
| 4058 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4059 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4060 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4061 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4062 | if (const IncompleteArrayType *ArrayT |
| 4063 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4064 | // FIXME: We don't currently have the ability to accurately |
| 4065 | // compute the length of an initializer list without |
| 4066 | // performing full type-checking of the initializer list |
| 4067 | // (since we have to determine where braces are implicitly |
| 4068 | // introduced and such). So, we fall back to making the array |
| 4069 | // type a dependently-sized array type with no specified |
| 4070 | // bound. |
| 4071 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4072 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4073 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4074 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4075 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4076 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4077 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4078 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4079 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4080 | Brackets = ArrayLoc->getBracketsRange(); |
| 4081 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | *ResultType |
| 4085 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4086 | /*NumElts=*/0, |
| 4087 | ArrayT->getSizeModifier(), |
| 4088 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4089 | Brackets); |
| 4090 | } |
| 4091 | |
| 4092 | } |
| 4093 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4094 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4095 | Kind.isExplicitCast()); |
| 4096 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4099 | // No steps means no initialization. |
| 4100 | if (Steps.empty()) |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4101 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4102 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4103 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4104 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4105 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4106 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4107 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4108 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4109 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4110 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4111 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4112 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4113 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4114 | // grab the only argument out the Args and place it into the "current" |
| 4115 | // initializer. |
| 4116 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4117 | case SK_ResolveAddressOfOverloadedFunction: |
| 4118 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4119 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4120 | case SK_CastDerivedToBaseLValue: |
| 4121 | case SK_BindReference: |
| 4122 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4123 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4124 | case SK_UserConversion: |
| 4125 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4126 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4127 | case SK_QualificationConversionRValue: |
| 4128 | case SK_ConversionSequence: |
| 4129 | case SK_ListInitialization: |
| 4130 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4131 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4132 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4133 | case SK_ArrayInit: |
| 4134 | case SK_PassByIndirectCopyRestore: |
| 4135 | case SK_PassByIndirectRestore: |
| 4136 | case SK_ProduceObjCObject: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4137 | assert(Args.size() == 1); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4138 | CurInit = Args.get()[0]; |
| 4139 | if (!CurInit.get()) return ExprError(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4140 | |
| 4141 | // Read from a property when initializing something with it. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4142 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4143 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4144 | if (CurInit.isInvalid()) |
| 4145 | return ExprError(); |
| 4146 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4147 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4148 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4149 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4150 | case SK_ConstructorInitialization: |
| 4151 | case SK_ZeroInitialization: |
| 4152 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4153 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4154 | |
| 4155 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4156 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4157 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4158 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4159 | Step != StepEnd; ++Step) { |
| 4160 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4161 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4162 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4163 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4164 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4165 | switch (Step->Kind) { |
| 4166 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4168 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4169 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4170 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4171 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4172 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4173 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4174 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4176 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4177 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4178 | case SK_CastDerivedToBaseLValue: { |
| 4179 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4180 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4182 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4184 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4185 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4186 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4187 | CurInit.get()->getLocStart(), |
| 4188 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4189 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4190 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4191 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4192 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4193 | QualType T = SourceType; |
| 4194 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4195 | T = Pointer->getPointeeType(); |
| 4196 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4197 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4198 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4199 | } |
| 4200 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4201 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4202 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4203 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4204 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4205 | VK_XValue : |
| 4206 | VK_RValue); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4207 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4208 | Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4209 | CK_DerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4210 | CurInit.get(), |
| 4211 | &BasePath, VK)); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4212 | break; |
| 4213 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4214 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4215 | case SK_BindReference: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4216 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4217 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4218 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4219 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4220 | << BitField->getDeclName() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4221 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4222 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4223 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4224 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4225 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4226 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4227 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4228 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4229 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4230 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4231 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4232 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4233 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4234 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4235 | // Reference binding does not have any corresponding ASTs. |
| 4236 | |
| 4237 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4238 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4239 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4241 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4242 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4243 | case SK_BindReferenceToTemporary: |
| 4244 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4245 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4246 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4247 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4248 | // Materialize the temporary into memory. |
Douglas Gregor | 2fa40a3 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4249 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4250 | Entity.getType().getNonReferenceType(), |
| 4251 | CurInit.get(), |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4252 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4253 | |
| 4254 | // If we're binding to an Objective-C object that has lifetime, we |
| 4255 | // need cleanups. |
| 4256 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4257 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4258 | S.ExprNeedsCleanups = true; |
| 4259 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4260 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4261 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4262 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4264 | /*IsExtraneousCopy=*/true); |
| 4265 | break; |
| 4266 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4267 | case SK_UserConversion: { |
| 4268 | // We have a user-defined conversion that invokes either a constructor |
| 4269 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4270 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4271 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4272 | FunctionDecl *Fn = Step->Function.Function; |
| 4273 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4274 | bool CreatedObject = false; |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4275 | bool IsLvalue = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4276 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4277 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4278 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4279 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4280 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4282 | // Determine the arguments required to actually perform the constructor |
| 4283 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4284 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4285 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4286 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4287 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4288 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4290 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4292 | move_arg(ConstructorArgs), |
| 4293 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4294 | CXXConstructExpr::CK_Complete, |
| 4295 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4296 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4297 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4298 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4299 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4300 | FoundFn.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4301 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4302 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4303 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4304 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4305 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4306 | S.IsDerivedFrom(SourceType, Class)) |
| 4307 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4309 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4310 | } else { |
| 4311 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4312 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | 031296e | 2010-03-25 00:20:38 +0000 | [diff] [blame] | 4313 | IsLvalue = Conversion->getResultType()->isLValueReferenceType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4314 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4315 | FoundFn); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4316 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | |
| 4318 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4319 | // derived-to-base conversion? I believe the answer is "no", because |
| 4320 | // 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] | 4321 | ExprResult CurInitExprRes = |
| 4322 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4323 | FoundFn, Conversion); |
| 4324 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4325 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4326 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4327 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4328 | // Build the actual call to the conversion function. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4329 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4330 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4331 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4332 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4333 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4334 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4335 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4336 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4337 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4338 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4339 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4340 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4341 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4342 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4343 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4344 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4345 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4346 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4347 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4348 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4349 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4350 | } |
| 4351 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4352 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4353 | // FIXME: xvalues |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4354 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4355 | CurInit.get()->getType(), |
| 4356 | CastKind, CurInit.get(), 0, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4357 | IsLvalue ? VK_LValue : VK_RValue)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4358 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4359 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4360 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4361 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4362 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4363 | break; |
| 4364 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4365 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4366 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4367 | case SK_QualificationConversionXValue: |
| 4368 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4369 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4370 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4371 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4372 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4373 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4374 | VK_XValue : |
| 4375 | VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4376 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4377 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4380 | case SK_ConversionSequence: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4381 | Sema::CheckedConversionKind CCK |
| 4382 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4383 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4384 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4385 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4386 | ExprResult CurInitExprRes = |
| 4387 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4388 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4389 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4390 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4391 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4392 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4393 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4394 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4395 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4396 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4397 | QualType Ty = Step->Type; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 4398 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4399 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4400 | |
| 4401 | CurInit.release(); |
| 4402 | CurInit = S.Owned(InitList); |
| 4403 | break; |
| 4404 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4405 | |
| 4406 | case SK_ConstructorInitialization: { |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4407 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4408 | CXXConstructorDecl *Constructor |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4409 | = cast<CXXConstructorDecl>(Step->Function.Function); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4410 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4411 | // Build a call to the selected constructor. |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4412 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | da2da9c | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4413 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4414 | ? Kind.getEqualLoc() |
| 4415 | : Kind.getLocation(); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4416 | |
| 4417 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4418 | // Force even a trivial, implicit default constructor to be |
| 4419 | // semantically checked. We do this explicitly because we don't build |
| 4420 | // the definition for completely trivial constructors. |
| 4421 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4422 | assert(ClassDecl && "No parent class for constructor."); |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4423 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4424 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4425 | !Constructor->isUsed(false)) |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4426 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4427 | } |
| 4428 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4429 | // Determine the arguments required to actually perform the constructor |
| 4430 | // call. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4431 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4432 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4433 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | |
| 4435 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4436 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4437 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4438 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4439 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4440 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4441 | unsigned NumExprs = ConstructorArgs.size(); |
| 4442 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 3fd2a55 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4443 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4444 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4446 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4447 | if (!TSInfo) |
| 4448 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4450 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4451 | Constructor, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4452 | TSInfo, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4453 | Exprs, |
Douglas Gregor | 9bc6b7f | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4454 | NumExprs, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4455 | Kind.getParenRange(), |
Douglas Gregor | 199db36 | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4456 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4457 | } else { |
| 4458 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4459 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4460 | |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4461 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4462 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4463 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4464 | CXXConstructExpr::CK_NonVirtualBase; |
Alexis Hunt | 271c368 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4465 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4466 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4467 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4469 | // Only get the parenthesis range if it is a direct construction. |
| 4470 | SourceRange parenRange = |
| 4471 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4472 | Kind.getParenRange() : SourceRange(); |
| 4473 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4474 | // If the entity allows NRVO, mark the construction as elidable |
| 4475 | // unconditionally. |
| 4476 | if (Entity.allowsNRVO()) |
| 4477 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4478 | Constructor, /*Elidable=*/true, |
| 4479 | move_arg(ConstructorArgs), |
| 4480 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4481 | ConstructKind, |
| 4482 | parenRange); |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4483 | else |
| 4484 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4485 | Constructor, |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4486 | move_arg(ConstructorArgs), |
| 4487 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4488 | ConstructKind, |
| 4489 | parenRange); |
Anders Carlsson | bcc066b | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4490 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4491 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4492 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4493 | |
| 4494 | // Only check access if all of that succeeded. |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4495 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4496 | Step->Function.FoundDecl.getAccess()); |
John McCall | 4fa0d5f | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4497 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4498 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4499 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4500 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4502 | break; |
| 4503 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4504 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4505 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4506 | step_iterator NextStep = Step; |
| 4507 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | if (NextStep != StepEnd && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4509 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4510 | // The need for zero-initialization is recorded directly into |
| 4511 | // the call to the object's constructor within the next step. |
| 4512 | ConstructorInitRequiresZeroInit = true; |
| 4513 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4514 | S.getLangOptions().CPlusPlus && |
| 4515 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4516 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4517 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4518 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4519 | Kind.getRange().getBegin()); |
| 4520 | |
| 4521 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4522 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4523 | TSInfo, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4524 | Kind.getRange().getEnd())); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4525 | } else { |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4526 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4527 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4528 | break; |
| 4529 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4530 | |
| 4531 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4532 | QualType SourceType = CurInit.get()->getType(); |
| 4533 | ExprResult Result = move(CurInit); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4534 | Sema::AssignConvertType ConvTy = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4535 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4536 | if (Result.isInvalid()) |
| 4537 | return ExprError(); |
| 4538 | CurInit = move(Result); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4539 | |
| 4540 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4541 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4542 | if (ConvTy != Sema::Compatible && |
| 4543 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4544 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4545 | == Sema::Compatible) |
| 4546 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4547 | if (CurInitExprRes.isInvalid()) |
| 4548 | return ExprError(); |
| 4549 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4550 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4551 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4552 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4553 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4554 | CurInit.get(), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4555 | getAssignmentAction(Entity), |
| 4556 | &Complained)) { |
| 4557 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4558 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4559 | } else if (Complained) |
| 4560 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4561 | break; |
| 4562 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4563 | |
| 4564 | case SK_StringInit: { |
| 4565 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4566 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4567 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4568 | break; |
| 4569 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4570 | |
| 4571 | case SK_ObjCObjectConversion: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4572 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4573 | CK_ObjCObjectLValueCast, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4574 | S.CastCategory(CurInit.get())); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4575 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4576 | |
| 4577 | case SK_ArrayInit: |
| 4578 | // Okay: we checked everything before creating this step. Note that |
| 4579 | // this is a GNU extension. |
| 4580 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4581 | << Step->Type << CurInit.get()->getType() |
| 4582 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4583 | |
| 4584 | // If the destination type is an incomplete array type, update the |
| 4585 | // type accordingly. |
| 4586 | if (ResultType) { |
| 4587 | if (const IncompleteArrayType *IncompleteDest |
| 4588 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4589 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4590 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4591 | *ResultType = S.Context.getConstantArrayType( |
| 4592 | IncompleteDest->getElementType(), |
| 4593 | ConstantSource->getSize(), |
| 4594 | ArrayType::Normal, 0); |
| 4595 | } |
| 4596 | } |
| 4597 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4598 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4599 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4600 | case SK_PassByIndirectCopyRestore: |
| 4601 | case SK_PassByIndirectRestore: |
| 4602 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4603 | CurInit = S.Owned(new (S.Context) |
| 4604 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4605 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4606 | break; |
| 4607 | |
| 4608 | case SK_ProduceObjCObject: |
| 4609 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 4610 | CK_ObjCProduceObject, |
| 4611 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4612 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4613 | } |
| 4614 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4615 | |
| 4616 | // Diagnose non-fatal problems with the completed initialization. |
| 4617 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4618 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4619 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4620 | cast<FieldDecl>(Entity.getDecl()), |
| 4621 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4622 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4623 | return move(CurInit); |
| 4624 | } |
| 4625 | |
| 4626 | //===----------------------------------------------------------------------===// |
| 4627 | // Diagnose initialization failures |
| 4628 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4629 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4630 | const InitializedEntity &Entity, |
| 4631 | const InitializationKind &Kind, |
| 4632 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4633 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4634 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4636 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4637 | switch (Failure) { |
| 4638 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4639 | // FIXME: Customize for the initialized entity? |
| 4640 | if (NumArgs == 0) |
| 4641 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 4642 | << DestType.getNonReferenceType(); |
| 4643 | else // FIXME: diagnostic below could be better! |
| 4644 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 4645 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4646 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4647 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4648 | case FK_ArrayNeedsInitList: |
| 4649 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4650 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 4651 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 4652 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4654 | case FK_ArrayTypeMismatch: |
| 4655 | case FK_NonConstantArrayInit: |
| 4656 | S.Diag(Kind.getLocation(), |
| 4657 | (Failure == FK_ArrayTypeMismatch |
| 4658 | ? diag::err_array_init_different_type |
| 4659 | : diag::err_array_init_non_constant_array)) |
| 4660 | << DestType.getNonReferenceType() |
| 4661 | << Args[0]->getType() |
| 4662 | << Args[0]->getSourceRange(); |
| 4663 | break; |
| 4664 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4665 | case FK_AddressOfOverloadFailed: { |
| 4666 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4667 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4668 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4669 | true, |
| 4670 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4671 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4672 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4674 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4675 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4676 | switch (FailedOverloadResult) { |
| 4677 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4678 | if (Failure == FK_UserConversionOverloadFailed) |
| 4679 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 4680 | << Args[0]->getType() << DestType |
| 4681 | << Args[0]->getSourceRange(); |
| 4682 | else |
| 4683 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 4684 | << DestType << Args[0]->getType() |
| 4685 | << Args[0]->getSourceRange(); |
| 4686 | |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4687 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4688 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4689 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4690 | case OR_No_Viable_Function: |
| 4691 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 4692 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4693 | << Args[0]->getSourceRange(); |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4694 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4695 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4696 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4697 | case OR_Deleted: { |
| 4698 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 4699 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 4700 | << Args[0]->getSourceRange(); |
| 4701 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4702 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4703 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 4704 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4705 | if (Ovl == OR_Deleted) { |
| 4706 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4707 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4708 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4709 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4710 | } |
| 4711 | break; |
| 4712 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4714 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 4715 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4716 | break; |
| 4717 | } |
| 4718 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4719 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4720 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4721 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4723 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 4724 | ? diag::err_lvalue_reference_bind_to_temporary |
| 4725 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 4726 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4727 | << DestType.getNonReferenceType() |
| 4728 | << Args[0]->getType() |
| 4729 | << Args[0]->getSourceRange(); |
| 4730 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4732 | case FK_RValueReferenceBindingToLValue: |
| 4733 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 4734 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4735 | << Args[0]->getSourceRange(); |
| 4736 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4737 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4738 | case FK_ReferenceInitDropsQualifiers: |
| 4739 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 4740 | << DestType.getNonReferenceType() |
| 4741 | << Args[0]->getType() |
| 4742 | << Args[0]->getSourceRange(); |
| 4743 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4744 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4745 | case FK_ReferenceInitFailed: |
| 4746 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 4747 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4748 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4749 | << Args[0]->getType() |
| 4750 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4751 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4752 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4753 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4754 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4755 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4756 | case FK_ConversionFailed: { |
| 4757 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4758 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 4759 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4760 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 4761 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4762 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4763 | << Args[0]->getSourceRange(); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4764 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 4765 | Args[0]->getType()->isObjCObjectPointerType()) |
| 4766 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4767 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4768 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4769 | |
| 4770 | case FK_ConversionFromPropertyFailed: |
| 4771 | // No-op. This error has already been reported. |
| 4772 | break; |
| 4773 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4774 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4775 | SourceRange R; |
| 4776 | |
| 4777 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4778 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4779 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4780 | else |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4781 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4782 | |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 4783 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 4784 | if (Kind.isCStyleOrFunctionalCast()) |
| 4785 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 4786 | << R; |
| 4787 | else |
| 4788 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 4789 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4790 | break; |
| 4791 | } |
| 4792 | |
| 4793 | case FK_ReferenceBindingToInitList: |
| 4794 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 4795 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 4796 | break; |
| 4797 | |
| 4798 | case FK_InitListBadDestinationType: |
| 4799 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 4800 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 4801 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4802 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4803 | case FK_ConstructorOverloadFailed: { |
| 4804 | SourceRange ArgsRange; |
| 4805 | if (NumArgs) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4807 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4808 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4809 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 4810 | // bad. |
| 4811 | switch (FailedOverloadResult) { |
| 4812 | case OR_Ambiguous: |
| 4813 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 4814 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4815 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 4816 | Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4817 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4818 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4819 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4820 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 4821 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 4822 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 4823 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4824 | // This is implicit default initialization of a member or |
| 4825 | // base within a constructor. If no viable function was |
| 4826 | // found, notify the user that she needs to explicitly |
| 4827 | // initialize this base/member. |
| 4828 | CXXConstructorDecl *Constructor |
| 4829 | = cast<CXXConstructorDecl>(S.CurContext); |
| 4830 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4831 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4832 | << Constructor->isImplicit() |
| 4833 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4834 | << /*base=*/0 |
| 4835 | << Entity.getType(); |
| 4836 | |
| 4837 | RecordDecl *BaseDecl |
| 4838 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 4839 | ->getDecl(); |
| 4840 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 4841 | << S.Context.getTagDeclType(BaseDecl); |
| 4842 | } else { |
| 4843 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 4844 | << Constructor->isImplicit() |
| 4845 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4846 | << /*member=*/1 |
| 4847 | << Entity.getName(); |
| 4848 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 4849 | |
| 4850 | if (const RecordType *Record |
| 4851 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4852 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4853 | diag::note_previous_decl) |
| 4854 | << S.Context.getTagDeclType(Record->getDecl()); |
| 4855 | } |
| 4856 | break; |
| 4857 | } |
| 4858 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4859 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 4860 | << DestType << ArgsRange; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4861 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4862 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4863 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4864 | case OR_Deleted: { |
| 4865 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 4866 | << true << DestType << ArgsRange; |
| 4867 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4868 | OverloadingResult Ovl |
| 4869 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4870 | if (Ovl == OR_Deleted) { |
| 4871 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4872 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4873 | } else { |
| 4874 | llvm_unreachable("Inconsistent overload resolution?"); |
| 4875 | } |
| 4876 | break; |
| 4877 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4879 | case OR_Success: |
| 4880 | llvm_unreachable("Conversion did not fail!"); |
| 4881 | break; |
| 4882 | } |
| 4883 | break; |
| 4884 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4885 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4886 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4887 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4888 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 4889 | // This is implicit default-initialization of a const member in |
| 4890 | // a constructor. Complain that it needs to be explicitly |
| 4891 | // initialized. |
| 4892 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 4893 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 4894 | << Constructor->isImplicit() |
| 4895 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 4896 | << /*const=*/1 |
| 4897 | << Entity.getName(); |
| 4898 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 4899 | << Entity.getName(); |
| 4900 | } else { |
| 4901 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 4902 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 4903 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4904 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4906 | case FK_Incomplete: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4907 | S.RequireCompleteType(Kind.getLocation(), DestType, |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 4908 | diag::err_init_incomplete_type); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4909 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4910 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4911 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4912 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4913 | return true; |
| 4914 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4915 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4916 | void InitializationSequence::dump(llvm::raw_ostream &OS) const { |
| 4917 | switch (SequenceKind) { |
| 4918 | case FailedSequence: { |
| 4919 | OS << "Failed sequence: "; |
| 4920 | switch (Failure) { |
| 4921 | case FK_TooManyInitsForReference: |
| 4922 | OS << "too many initializers for reference"; |
| 4923 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4925 | case FK_ArrayNeedsInitList: |
| 4926 | OS << "array requires initializer list"; |
| 4927 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4928 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4929 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 4930 | OS << "array requires initializer list or string literal"; |
| 4931 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4933 | case FK_ArrayTypeMismatch: |
| 4934 | OS << "array type mismatch"; |
| 4935 | break; |
| 4936 | |
| 4937 | case FK_NonConstantArrayInit: |
| 4938 | OS << "non-constant array initializer"; |
| 4939 | break; |
| 4940 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4941 | case FK_AddressOfOverloadFailed: |
| 4942 | OS << "address of overloaded function failed"; |
| 4943 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4944 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4945 | case FK_ReferenceInitOverloadFailed: |
| 4946 | OS << "overload resolution for reference initialization failed"; |
| 4947 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4948 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4949 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 4950 | OS << "non-const lvalue reference bound to temporary"; |
| 4951 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4952 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4953 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 4954 | OS << "non-const lvalue reference bound to unrelated type"; |
| 4955 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4957 | case FK_RValueReferenceBindingToLValue: |
| 4958 | OS << "rvalue reference bound to an lvalue"; |
| 4959 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4961 | case FK_ReferenceInitDropsQualifiers: |
| 4962 | OS << "reference initialization drops qualifiers"; |
| 4963 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4964 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4965 | case FK_ReferenceInitFailed: |
| 4966 | OS << "reference initialization failed"; |
| 4967 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4968 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4969 | case FK_ConversionFailed: |
| 4970 | OS << "conversion failed"; |
| 4971 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4972 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4973 | case FK_ConversionFromPropertyFailed: |
| 4974 | OS << "conversion from property failed"; |
| 4975 | break; |
| 4976 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4977 | case FK_TooManyInitsForScalar: |
| 4978 | OS << "too many initializers for scalar"; |
| 4979 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4980 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4981 | case FK_ReferenceBindingToInitList: |
| 4982 | OS << "referencing binding to initializer list"; |
| 4983 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4984 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4985 | case FK_InitListBadDestinationType: |
| 4986 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 4987 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4988 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4989 | case FK_UserConversionOverloadFailed: |
| 4990 | OS << "overloading failed for user-defined conversion"; |
| 4991 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4992 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4993 | case FK_ConstructorOverloadFailed: |
| 4994 | OS << "constructor overloading failed"; |
| 4995 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4996 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 4997 | case FK_DefaultInitOfConst: |
| 4998 | OS << "default initialization of a const variable"; |
| 4999 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5000 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5001 | case FK_Incomplete: |
| 5002 | OS << "initialization of incomplete type"; |
| 5003 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5005 | OS << '\n'; |
| 5006 | return; |
| 5007 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5008 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5009 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5010 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5011 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5013 | case NormalSequence: |
| 5014 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5015 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5016 | } |
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 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5019 | if (S != step_begin()) { |
| 5020 | OS << " -> "; |
| 5021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5023 | switch (S->Kind) { |
| 5024 | case SK_ResolveAddressOfOverloadedFunction: |
| 5025 | OS << "resolve address of overloaded function"; |
| 5026 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5027 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5028 | case SK_CastDerivedToBaseRValue: |
| 5029 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5030 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5032 | case SK_CastDerivedToBaseXValue: |
| 5033 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5034 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5035 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5036 | case SK_CastDerivedToBaseLValue: |
| 5037 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5038 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5039 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5040 | case SK_BindReference: |
| 5041 | OS << "bind reference to lvalue"; |
| 5042 | break; |
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 | case SK_BindReferenceToTemporary: |
| 5045 | OS << "bind reference to a temporary"; |
| 5046 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5047 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5048 | case SK_ExtraneousCopyToTemporary: |
| 5049 | OS << "extraneous C++03 copy to temporary"; |
| 5050 | break; |
| 5051 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5052 | case SK_UserConversion: |
Benjamin Kramer | b11416d | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 5053 | OS << "user-defined conversion via " << S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5054 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5056 | case SK_QualificationConversionRValue: |
| 5057 | OS << "qualification conversion (rvalue)"; |
| 5058 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5059 | case SK_QualificationConversionXValue: |
| 5060 | OS << "qualification conversion (xvalue)"; |
| 5061 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5062 | case SK_QualificationConversionLValue: |
| 5063 | OS << "qualification conversion (lvalue)"; |
| 5064 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5065 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5066 | case SK_ConversionSequence: |
| 5067 | OS << "implicit conversion sequence ("; |
| 5068 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5069 | OS << ")"; |
| 5070 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5071 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5072 | case SK_ListInitialization: |
| 5073 | OS << "list initialization"; |
| 5074 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5075 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5076 | case SK_ConstructorInitialization: |
| 5077 | OS << "constructor initialization"; |
| 5078 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5079 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5080 | case SK_ZeroInitialization: |
| 5081 | OS << "zero initialization"; |
| 5082 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5083 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5084 | case SK_CAssignment: |
| 5085 | OS << "C assignment"; |
| 5086 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5087 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5088 | case SK_StringInit: |
| 5089 | OS << "string initialization"; |
| 5090 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5091 | |
| 5092 | case SK_ObjCObjectConversion: |
| 5093 | OS << "Objective-C object conversion"; |
| 5094 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5095 | |
| 5096 | case SK_ArrayInit: |
| 5097 | OS << "array initialization"; |
| 5098 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5099 | |
| 5100 | case SK_PassByIndirectCopyRestore: |
| 5101 | OS << "pass by indirect copy and restore"; |
| 5102 | break; |
| 5103 | |
| 5104 | case SK_PassByIndirectRestore: |
| 5105 | OS << "pass by indirect restore"; |
| 5106 | break; |
| 5107 | |
| 5108 | case SK_ProduceObjCObject: |
| 5109 | OS << "Objective-C object retension"; |
| 5110 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5111 | } |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | void InitializationSequence::dump() const { |
| 5116 | dump(llvm::errs()); |
| 5117 | } |
| 5118 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5119 | //===----------------------------------------------------------------------===// |
| 5120 | // Initialization helper functions |
| 5121 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5122 | bool |
| 5123 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5124 | ExprResult Init) { |
| 5125 | if (Init.isInvalid()) |
| 5126 | return false; |
| 5127 | |
| 5128 | Expr *InitE = Init.get(); |
| 5129 | assert(InitE && "No initialization expression"); |
| 5130 | |
| 5131 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5132 | SourceLocation()); |
| 5133 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5134 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5137 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5138 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5139 | SourceLocation EqualLoc, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5140 | ExprResult Init) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5141 | if (Init.isInvalid()) |
| 5142 | return ExprError(); |
| 5143 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5144 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5145 | assert(InitE && "No initialization expression?"); |
| 5146 | |
| 5147 | if (EqualLoc.isInvalid()) |
| 5148 | EqualLoc = InitE->getLocStart(); |
| 5149 | |
| 5150 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5151 | EqualLoc); |
| 5152 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5153 | Init.release(); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5154 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5155 | } |