 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
79.6% |
1105 / 1388 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
90.1% |
1251 / 1388 |
| |
|
Line Coverage: |
88.0% |
1406 / 1597 |
| |
 |
|
 |
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 : //
10 : // This file implements semantic analysis for initializers. The main entry
11 : // point is Sema::CheckInitList(), but all of the work is performed
12 : // within the InitListChecker class.
13 : //
14 : // This file also implements Sema::CheckInitializerTypes.
15 : //
16 : //===----------------------------------------------------------------------===//
17 :
18 : #include "SemaInit.h"
19 : #include "Lookup.h"
20 : #include "Sema.h"
21 : #include "clang/Parse/Designator.h"
22 : #include "clang/AST/ASTContext.h"
23 : #include "clang/AST/ExprCXX.h"
24 : #include "clang/AST/ExprObjC.h"
25 : #include "clang/AST/TypeLoc.h"
26 : #include "llvm/Support/ErrorHandling.h"
27 : #include <map>
28 : using namespace clang;
29 :
30 : //===----------------------------------------------------------------------===//
31 : // Sema Initialization Checking
32 : //===----------------------------------------------------------------------===//
33 :
34 20907: static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) {
35 20907: const ArrayType *AT = Context.getAsArrayType(DeclType);
20415: branch 0 taken
492: branch 1 taken
36 20907: if (!AT) return 0;
37 :
167: branch 1 taken
325: branch 2 taken
7: branch 4 taken
160: branch 5 taken
7: branch 6 taken
485: branch 7 taken
38 492: if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
39 7: return 0;
40 :
41 : // See if this is a string literal or @encode.
42 485: Init = Init->IgnoreParens();
43 :
44 : // Handle @encode, which is a narrow string.
4: branch 1 taken
481: branch 2 taken
4: branch 6 taken
0: branch 7 not taken
4: branch 8 taken
481: branch 9 taken
45 485: if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
46 4: return Init;
47 :
48 : // Otherwise we can only handle string literals.
49 481: StringLiteral *SL = dyn_cast<StringLiteral>(Init);
396: branch 0 taken
85: branch 1 taken
50 481: if (SL == 0) return 0;
51 :
52 85: QualType ElemTy = Context.getCanonicalType(AT->getElementType());
53 : // char array can be initialized with a narrow string.
54 : // Only allow char x[] = "foo"; not char x[] = L"foo";
78: branch 1 taken
7: branch 2 taken
55 85: if (!SL->isWide())
67: branch 2 taken
11: branch 3 taken
56 78: return ElemTy->isCharType() ? Init : 0;
57 :
58 : // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
59 : // correction from DR343): "An array with element type compatible with a
60 : // qualified or unqualified version of wchar_t may be initialized by a wide
61 : // string literal, optionally enclosed in braces."
5: branch 3 taken
2: branch 4 taken
62 7: if (Context.typesAreCompatible(Context.getWCharType(),
63 : ElemTy.getUnqualifiedType()))
64 5: return Init;
65 :
66 2: return 0;
67 : }
68 :
69 74: static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) {
70 : // Get the length of the string as parsed.
71 : uint64_t StrLength =
72 74: cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
73 :
74 :
75 74: const ArrayType *AT = S.Context.getAsArrayType(DeclT);
29: branch 1 taken
45: branch 2 taken
76 74: if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
77 : // C99 6.7.8p14. We have an array of character type with unknown size
78 : // being initialized to a string literal.
79 29: llvm::APSInt ConstVal(32);
80 29: ConstVal = StrLength;
81 : // Return a new array type (C99 6.7.8p22).
82 : DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
83 : ConstVal,
84 29: ArrayType::Normal, 0);
85 29: return;
86 : }
87 :
88 45: const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
89 :
90 : // C99 6.7.8p14. We have an array of character type with known size. However,
91 : // the size may be smaller or larger than the string we are initializing.
92 : // FIXME: Avoid truncation for 64-bit length strings.
4: branch 2 taken
41: branch 3 taken
93 45: if (StrLength-1 > CAT->getSize().getZExtValue())
94 : S.Diag(Str->getSourceRange().getBegin(),
95 : diag::warn_initializer_string_for_char_array_too_long)
96 4: << Str->getSourceRange();
97 :
98 : // Set the type to the actual size that we are initializing. If we have
99 : // something like:
100 : // char x[1] = "foo";
101 : // then this will set the string literal's type to char[1].
102 45: Str->setType(DeclT);
103 : }
104 :
105 : //===----------------------------------------------------------------------===//
106 : // Semantic checking for initializer lists.
107 : //===----------------------------------------------------------------------===//
108 :
109 : /// @brief Semantic checking for initializer lists.
110 : ///
111 : /// The InitListChecker class contains a set of routines that each
112 : /// handle the initialization of a certain kind of entity, e.g.,
113 : /// arrays, vectors, struct/union types, scalars, etc. The
114 : /// InitListChecker itself performs a recursive walk of the subobject
115 : /// structure of the type to be initialized, while stepping through
116 : /// the initializer list one element at a time. The IList and Index
117 : /// parameters to each of the Check* routines contain the active
118 : /// (syntactic) initializer list and the index into that initializer
119 : /// list that represents the current initializer. Each routine is
120 : /// responsible for moving that Index forward as it consumes elements.
121 : ///
122 : /// Each Check* routine also has a StructuredList/StructuredIndex
123 : /// arguments, which contains the current the "structured" (semantic)
124 : /// initializer list and the index into that initializer list where we
125 : /// are copying initializers as we map them over to the semantic
126 : /// list. Once we have completed our recursive walk of the subobject
127 : /// structure, we will have constructed a full semantic initializer
128 : /// list.
129 : ///
130 : /// C99 designators cause changes in the initializer list traversal,
131 : /// because they make the initialization "jump" into a specific
132 : /// subobject and then continue the initialization from that
133 : /// point. CheckDesignatedInitializer() recursively steps into the
134 : /// designated subobject and manages backing out the recursion to
135 : /// initialize the subobjects after the one designated.
136 : namespace {
137 747: class InitListChecker {
138 : Sema &SemaRef;
139 : bool hadError;
140 : std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic;
141 : InitListExpr *FullyStructuredList;
142 :
143 : void CheckImplicitInitList(const InitializedEntity &Entity,
144 : InitListExpr *ParentIList, QualType T,
145 : unsigned &Index, InitListExpr *StructuredList,
146 : unsigned &StructuredIndex,
147 : bool TopLevelObject = false);
148 : void CheckExplicitInitList(const InitializedEntity &Entity,
149 : InitListExpr *IList, QualType &T,
150 : unsigned &Index, InitListExpr *StructuredList,
151 : unsigned &StructuredIndex,
152 : bool TopLevelObject = false);
153 : void CheckListElementTypes(const InitializedEntity &Entity,
154 : InitListExpr *IList, QualType &DeclType,
155 : bool SubobjectIsDesignatorContext,
156 : unsigned &Index,
157 : InitListExpr *StructuredList,
158 : unsigned &StructuredIndex,
159 : bool TopLevelObject = false);
160 : void CheckSubElementType(const InitializedEntity &Entity,
161 : InitListExpr *IList, QualType ElemType,
162 : unsigned &Index,
163 : InitListExpr *StructuredList,
164 : unsigned &StructuredIndex);
165 : void CheckScalarType(const InitializedEntity &Entity,
166 : InitListExpr *IList, QualType DeclType,
167 : unsigned &Index,
168 : InitListExpr *StructuredList,
169 : unsigned &StructuredIndex);
170 : void CheckReferenceType(const InitializedEntity &Entity,
171 : InitListExpr *IList, QualType DeclType,
172 : unsigned &Index,
173 : InitListExpr *StructuredList,
174 : unsigned &StructuredIndex);
175 : void CheckVectorType(const InitializedEntity &Entity,
176 : InitListExpr *IList, QualType DeclType, unsigned &Index,
177 : InitListExpr *StructuredList,
178 : unsigned &StructuredIndex);
179 : void CheckStructUnionTypes(const InitializedEntity &Entity,
180 : InitListExpr *IList, QualType DeclType,
181 : RecordDecl::field_iterator Field,
182 : bool SubobjectIsDesignatorContext, unsigned &Index,
183 : InitListExpr *StructuredList,
184 : unsigned &StructuredIndex,
185 : bool TopLevelObject = false);
186 : void CheckArrayType(const InitializedEntity &Entity,
187 : InitListExpr *IList, QualType &DeclType,
188 : llvm::APSInt elementIndex,
189 : bool SubobjectIsDesignatorContext, unsigned &Index,
190 : InitListExpr *StructuredList,
191 : unsigned &StructuredIndex);
192 : bool CheckDesignatedInitializer(const InitializedEntity &Entity,
193 : InitListExpr *IList, DesignatedInitExpr *DIE,
194 : unsigned DesigIdx,
195 : QualType &CurrentObjectType,
196 : RecordDecl::field_iterator *NextField,
197 : llvm::APSInt *NextElementIndex,
198 : unsigned &Index,
199 : InitListExpr *StructuredList,
200 : unsigned &StructuredIndex,
201 : bool FinishSubobjectInit,
202 : bool TopLevelObject);
203 : InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
204 : QualType CurrentObjectType,
205 : InitListExpr *StructuredList,
206 : unsigned StructuredIndex,
207 : SourceRange InitRange);
208 : void UpdateStructuredListElement(InitListExpr *StructuredList,
209 : unsigned &StructuredIndex,
210 : Expr *expr);
211 : int numArrayElements(QualType DeclType);
212 : int numStructUnionElements(QualType DeclType);
213 :
214 : void FillInValueInitForField(unsigned Init, FieldDecl *Field,
215 : const InitializedEntity &ParentEntity,
216 : InitListExpr *ILE, bool &RequiresSecondPass);
217 : void FillInValueInitializations(const InitializedEntity &Entity,
218 : InitListExpr *ILE, bool &RequiresSecondPass);
219 : public:
220 : InitListChecker(Sema &S, const InitializedEntity &Entity,
221 : InitListExpr *IL, QualType &T);
222 1494: bool HadError() { return hadError; }
223 :
224 : // @brief Retrieves the fully-structured initializer list used for
225 : // semantic analysis and code generation.
226 702: InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
227 : };
228 : } // end anonymous namespace
229 :
230 : void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field,
231 : const InitializedEntity &ParentEntity,
232 : InitListExpr *ILE,
233 635: bool &RequiresSecondPass) {
234 635: SourceLocation Loc = ILE->getSourceRange().getBegin();
235 635: unsigned NumInits = ILE->getNumInits();
236 : InitializedEntity MemberEntity
237 635: = InitializedEntity::InitializeMember(Field, &ParentEntity);
591: branch 0 taken
44: branch 1 taken
36: branch 3 taken
555: branch 4 taken
80: branch 5 taken
555: branch 6 taken
238 635: if (Init >= NumInits || !ILE->getInit(Init)) {
239 : // FIXME: We probably don't need to handle references
240 : // specially here, since value-initialization of references is
241 : // handled in InitializationSequence.
1: branch 3 taken
79: branch 4 taken
242 80: if (Field->getType()->isReferenceType()) {
243 : // C++ [dcl.init.aggr]p9:
244 : // If an incomplete or empty initializer-list leaves a
245 : // member of reference type uninitialized, the program is
246 : // ill-formed.
247 : SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
248 : << Field->getType()
249 1: << ILE->getSyntacticForm()->getSourceRange();
250 : SemaRef.Diag(Field->getLocation(),
251 1: diag::note_uninit_reference_member);
252 1: hadError = true;
253 1: return;
254 : }
255 :
256 : InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
257 79: true);
258 79: InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0);
2: branch 1 taken
77: branch 2 taken
259 79: if (!InitSeq) {
260 2: InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0);
261 2: hadError = true;
262 2: return;
263 : }
264 :
265 : Sema::OwningExprResult MemberInit
266 : = InitSeq.Perform(SemaRef, MemberEntity, Kind,
267 77: Sema::MultiExprArg(SemaRef, 0, 0));
0: branch 1 not taken
77: branch 2 taken
268 77: if (MemberInit.isInvalid()) {
269 0: hadError = true;
270 : return;
271 : }
272 :
77: branch 0 taken
0: branch 1 not taken
273 77: if (hadError) {
274 : // Do nothing
36: branch 0 taken
41: branch 1 taken
275 77: } else if (Init < NumInits) {
276 36: ILE->setInit(Init, MemberInit.takeAs<Expr>());
2: branch 1 taken
39: branch 2 taken
277 41: } else if (InitSeq.getKind()
278 : == InitializationSequence::ConstructorInitialization) {
279 : // Value-initialization requires a constructor call, so
280 : // extend the initializer list to include the constructor
281 : // call and make a note that we'll need to take another pass
282 : // through the initializer list.
283 2: ILE->updateInit(Init, MemberInit.takeAs<Expr>());
284 2: RequiresSecondPass = true;
77: branch 1 taken
0: branch 2 not taken
77: branch 4 taken
2: branch 5 taken
285 77: }
93: branch 0 taken
462: branch 1 taken
286 555: } else if (InitListExpr *InnerILE
287 555: = dyn_cast<InitListExpr>(ILE->getInit(Init)))
288 : FillInValueInitializations(MemberEntity, InnerILE,
289 93: RequiresSecondPass);
290 : }
291 :
292 : /// Recursively replaces NULL values within the given initializer list
293 : /// with expressions that perform value-initialization of the
294 : /// appropriate type.
295 : void
296 : InitListChecker::FillInValueInitializations(const InitializedEntity &Entity,
297 : InitListExpr *ILE,
298 954: bool &RequiresSecondPass) {
299 : assert((ILE->getType() != SemaRef.Context.VoidTy) &&
954: branch 3 taken
0: branch 4 not taken
300 954: "Should not have void type");
301 954: SourceLocation Loc = ILE->getSourceRange().getBegin();
834: branch 1 taken
120: branch 2 taken
302 954: if (ILE->getSyntacticForm())
303 834: Loc = ILE->getSyntacticForm()->getSourceRange().getBegin();
304 :
356: branch 3 taken
598: branch 4 taken
305 954: if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
32: branch 2 taken
324: branch 3 taken
31: branch 5 taken
1: branch 6 taken
31: branch 7 taken
325: branch 8 taken
306 356: if (RType->getDecl()->isUnion() &&
307 : ILE->getInitializedFieldInUnion())
308 : FillInValueInitForField(0, ILE->getInitializedFieldInUnion(),
309 31: Entity, ILE, RequiresSecondPass);
310 : else {
311 325: unsigned Init = 0;
611: branch 2 taken
322: branch 3 taken
312 933: for (RecordDecl::field_iterator
313 325: Field = RType->getDecl()->field_begin(),
314 325: FieldEnd = RType->getDecl()->field_end();
315 : Field != FieldEnd; ++Field) {
7: branch 2 taken
604: branch 3 taken
316 611: if (Field->isUnnamedBitfield())
317 7: continue;
318 :
0: branch 0 not taken
604: branch 1 taken
319 604: if (hadError)
320 0: return;
321 :
322 604: FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass);
3: branch 0 taken
601: branch 1 taken
323 604: if (hadError)
324 3: return;
325 :
326 601: ++Init;
327 :
328 : // Only look at the first initialization of a union.
0: branch 2 not taken
601: branch 3 taken
329 601: if (RType->getDecl()->isUnion())
330 0: break;
331 : }
332 : }
333 :
334 353: return;
335 : }
336 :
337 598: QualType ElementType;
338 :
339 598: InitializedEntity ElementEntity = Entity;
340 598: unsigned NumInits = ILE->getNumInits();
341 598: unsigned NumElements = NumInits;
362: branch 2 taken
236: branch 3 taken
342 598: if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
343 362: ElementType = AType->getElementType();
362: branch 1 taken
0: branch 2 not taken
344 362: if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
345 362: NumElements = CAType->getSize().getZExtValue();
346 : ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
347 362: 0, Entity);
218: branch 3 taken
18: branch 4 taken
348 236: } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
349 218: ElementType = VType->getElementType();
350 218: NumElements = VType->getNumElements();
351 : ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
352 218: 0, Entity);
353 : } else
354 18: ElementType = ILE->getType();
355 :
356 :
2793: branch 0 taken
596: branch 1 taken
357 3389: for (unsigned Init = 0; Init != NumElements; ++Init) {
1: branch 0 taken
2792: branch 1 taken
358 2793: if (hadError)
359 1: return;
360 :
912: branch 1 taken
1880: branch 2 taken
898: branch 4 taken
14: branch 5 taken
2778: branch 6 taken
14: branch 7 taken
361 2792: if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
362 : ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
363 2778: ElementEntity.setElementIndex(Init);
364 :
1946: branch 0 taken
846: branch 1 taken
64: branch 3 taken
1882: branch 4 taken
910: branch 5 taken
1882: branch 6 taken
365 2792: if (Init >= NumInits || !ILE->getInit(Init)) {
366 : InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
367 910: true);
368 910: InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0);
1: branch 1 taken
909: branch 2 taken
369 910: if (!InitSeq) {
370 1: InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0);
371 1: hadError = true;
372 1: return;
373 : }
374 :
375 : Sema::OwningExprResult ElementInit
376 : = InitSeq.Perform(SemaRef, ElementEntity, Kind,
377 909: Sema::MultiExprArg(SemaRef, 0, 0));
0: branch 1 not taken
909: branch 2 taken
378 909: if (ElementInit.isInvalid()) {
379 0: hadError = true;
380 : return;
381 : }
382 :
909: branch 0 taken
0: branch 1 not taken
383 909: if (hadError) {
384 : // Do nothing
64: branch 0 taken
845: branch 1 taken
385 909: } else if (Init < NumInits) {
386 64: ILE->setInit(Init, ElementInit.takeAs<Expr>());
2: branch 1 taken
843: branch 2 taken
387 845: } else if (InitSeq.getKind()
388 : == InitializationSequence::ConstructorInitialization) {
389 : // Value-initialization requires a constructor call, so
390 : // extend the initializer list to include the constructor
391 : // call and make a note that we'll need to take another pass
392 : // through the initializer list.
393 2: ILE->updateInit(Init, ElementInit.takeAs<Expr>());
394 2: RequiresSecondPass = true;
909: branch 1 taken
0: branch 2 not taken
909: branch 4 taken
1: branch 5 taken
395 909: }
151: branch 0 taken
1731: branch 1 taken
396 1882: } else if (InitListExpr *InnerILE
397 1882: = dyn_cast<InitListExpr>(ILE->getInit(Init)))
398 151: FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass);
399 : }
400 : }
401 :
402 :
403 : InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
404 747: InitListExpr *IL, QualType &T)
405 747: : SemaRef(S) {
406 747: hadError = false;
407 :
408 747: unsigned newIndex = 0;
409 747: unsigned newStructuredIndex = 0;
410 : FullyStructuredList
411 747: = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
412 : CheckExplicitInitList(Entity, IL, T, newIndex,
413 : FullyStructuredList, newStructuredIndex,
414 747: /*TopLevelObject=*/true);
415 :
706: branch 0 taken
41: branch 1 taken
41: branch 2 taken
41: branch 3 taken
416 747: if (!hadError) {
417 706: bool RequiresSecondPass = false;
418 706: FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass);
4: branch 0 taken
702: branch 1 taken
4: branch 2 taken
0: branch 3 not taken
0: branch 4 not taken
0: branch 5 not taken
0: branch 6 not taken
0: branch 7 not taken
419 706: if (RequiresSecondPass && !hadError)
420 : FillInValueInitializations(Entity, FullyStructuredList,
421 4: RequiresSecondPass);
422 : }
423 747: }
424 :
425 47: int InitListChecker::numArrayElements(QualType DeclType) {
426 : // FIXME: use a proper constant
427 47: int maxElements = 0x7FFFFFFF;
41: branch 0 taken
6: branch 1 taken
428 47: if (const ConstantArrayType *CAT =
429 47: SemaRef.Context.getAsConstantArrayType(DeclType)) {
430 41: maxElements = static_cast<int>(CAT->getSize().getZExtValue());
431 : }
432 47: return maxElements;
433 : }
434 :
435 30: int InitListChecker::numStructUnionElements(QualType DeclType) {
436 30: RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
437 30: int InitializableMembers = 0;
52: branch 2 taken
30: branch 3 taken
438 82: for (RecordDecl::field_iterator
439 30: Field = structDecl->field_begin(),
440 30: FieldEnd = structDecl->field_end();
441 : Field != FieldEnd; ++Field) {
4: branch 2 taken
48: branch 3 taken
0: branch 6 not taken
4: branch 7 taken
48: branch 8 taken
4: branch 9 taken
442 52: if ((*Field)->getIdentifier() || !(*Field)->isBitField())
443 48: ++InitializableMembers;
444 : }
6: branch 1 taken
24: branch 2 taken
445 30: if (structDecl->isUnion())
446 6: return std::min(InitializableMembers, 1);
447 24: return InitializableMembers - structDecl->hasFlexibleArrayMember();
448 : }
449 :
450 : void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity,
451 : InitListExpr *ParentIList,
452 : QualType T, unsigned &Index,
453 : InitListExpr *StructuredList,
454 : unsigned &StructuredIndex,
455 83: bool TopLevelObject) {
456 83: int maxElements = 0;
457 :
47: branch 2 taken
36: branch 3 taken
458 83: if (T->isArrayType())
459 47: maxElements = numArrayElements(T);
12: branch 2 taken
24: branch 3 taken
6: branch 6 taken
6: branch 7 taken
30: branch 8 taken
6: branch 9 taken
460 36: else if (T->isStructureType() || T->isUnionType())
461 30: maxElements = numStructUnionElements(T);
6: branch 2 taken
0: branch 3 not taken
462 6: else if (T->isVectorType())
463 6: maxElements = T->getAs<VectorType>()->getNumElements();
464 : else
465 0: assert(0 && "CheckImplicitInitList(): Illegal type");
466 :
3: branch 0 taken
80: branch 1 taken
467 83: if (maxElements == 0) {
468 : SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
469 3: diag::err_implicit_empty_initializer);
470 3: ++Index;
471 3: hadError = true;
472 3: return;
473 : }
474 :
475 : // Build a structured initializer list corresponding to this subobject.
476 : InitListExpr *StructuredSubobjectInitList
477 : = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
478 : StructuredIndex,
479 : SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(),
480 80: ParentIList->getSourceRange().getEnd()));
481 80: unsigned StructuredSubobjectInitIndex = 0;
482 :
483 : // Check the element types and build the structural subobject.
484 80: unsigned StartIndex = Index;
485 : CheckListElementTypes(Entity, ParentIList, T,
486 : /*SubobjectIsDesignatorContext=*/false, Index,
487 : StructuredSubobjectInitList,
488 : StructuredSubobjectInitIndex,
489 80: TopLevelObject);
80: branch 0 taken
0: branch 1 not taken
490 80: unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
491 80: StructuredSubobjectInitList->setType(T);
492 :
493 : // Update the structured sub-object initializer so that it's ending
494 : // range corresponds with the end of the last initializer it used.
80: branch 1 taken
0: branch 2 not taken
495 80: if (EndIndex < ParentIList->getNumInits()) {
496 : SourceLocation EndLoc
497 80: = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
498 80: StructuredSubobjectInitList->setRBraceLoc(EndLoc);
499 : }
500 : }
501 :
502 : void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
503 : InitListExpr *IList, QualType &T,
504 : unsigned &Index,
505 : InitListExpr *StructuredList,
506 : unsigned &StructuredIndex,
507 882: bool TopLevelObject) {
882: branch 1 taken
0: branch 2 not taken
508 882: assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
509 882: SyntacticToSemantic[IList] = StructuredList;
510 882: StructuredList->setSyntacticForm(IList);
511 : CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true,
512 882: Index, StructuredList, StructuredIndex, TopLevelObject);
513 882: IList->setType(T.getNonReferenceType());
514 882: StructuredList->setType(T.getNonReferenceType());
49: branch 0 taken
833: branch 1 taken
515 882: if (hadError)
516 49: return;
517 :
21: branch 1 taken
812: branch 2 taken
518 833: if (Index < IList->getNumInits()) {
519 : // We have leftover initializers
11: branch 0 taken
10: branch 1 taken
2: branch 4 taken
9: branch 5 taken
2: branch 6 taken
19: branch 7 taken
520 21: if (StructuredIndex == 1 &&
521 : IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
522 2: unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
0: branch 1 not taken
2: branch 2 taken
523 2: if (SemaRef.getLangOptions().CPlusPlus) {
524 0: DK = diag::err_excess_initializers_in_char_array_initializer;
525 0: hadError = true;
526 : }
527 : // Special-case
528 : SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
529 2: << IList->getInit(Index)->getSourceRange();
19: branch 2 taken
0: branch 3 not taken
530 19: } else if (!T->isIncompleteType()) {
531 : // Don't complain for incomplete types, since we'll get an error
532 : // elsewhere
533 19: QualType CurrentObjectType = StructuredList->getType();
534 : int initKind =
535 : CurrentObjectType->isArrayType()? 0 :
536 : CurrentObjectType->isVectorType()? 1 :
537 : CurrentObjectType->isScalarType()? 2 :
538 : CurrentObjectType->isUnionType()? 3 :
10: branch 2 taken
9: branch 3 taken
2: branch 6 taken
7: branch 7 taken
2: branch 10 taken
5: branch 11 taken
2: branch 14 taken
3: branch 15 taken
539 19: 4;
540 :
541 19: unsigned DK = diag::warn_excess_initializers;
3: branch 1 taken
16: branch 2 taken
542 19: if (SemaRef.getLangOptions().CPlusPlus) {
543 3: DK = diag::err_excess_initializers;
544 3: hadError = true;
545 : }
0: branch 1 not taken
19: branch 2 taken
19: branch 3 taken
19: branch 4 taken
0: branch 5 not taken
19: branch 6 taken
546 19: if (SemaRef.getLangOptions().OpenCL && initKind == 1) {
547 0: DK = diag::err_excess_initializers;
548 0: hadError = true;
549 : }
550 :
551 : SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
552 19: << initKind << IList->getInit(Index)->getSourceRange();
553 : }
554 : }
555 :
18: branch 2 taken
815: branch 3 taken
5: branch 4 taken
13: branch 5 taken
5: branch 6 taken
828: branch 7 taken
556 833: if (T->isScalarType() && !TopLevelObject)
557 : SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
558 : << IList->getSourceRange()
559 : << CodeModificationHint::CreateRemoval(IList->getLocStart())
560 5: << CodeModificationHint::CreateRemoval(IList->getLocEnd());
561 : }
562 :
563 : void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
564 : InitListExpr *IList,
565 : QualType &DeclType,
566 : bool SubobjectIsDesignatorContext,
567 : unsigned &Index,
568 : InitListExpr *StructuredList,
569 : unsigned &StructuredIndex,
570 962: bool TopLevelObject) {
21: branch 2 taken
941: branch 3 taken
571 962: if (DeclType->isScalarType()) {
572 : CheckScalarType(Entity, IList, DeclType, Index,
573 21: StructuredList, StructuredIndex);
219: branch 2 taken
722: branch 3 taken
574 941: } else if (DeclType->isVectorType()) {
575 : CheckVectorType(Entity, IList, DeclType, Index,
576 219: StructuredList, StructuredIndex);
718: branch 2 taken
4: branch 3 taken
577 722: } else if (DeclType->isAggregateType()) {
341: branch 2 taken
377: branch 3 taken
578 718: if (DeclType->isRecordType()) {
579 341: RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
580 : CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(),
581 : SubobjectIsDesignatorContext, Index,
582 : StructuredList, StructuredIndex,
583 341: TopLevelObject);
377: branch 2 taken
0: branch 3 not taken
584 377: } else if (DeclType->isArrayType()) {
585 : llvm::APSInt Zero(
586 : SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
587 377: false);
588 : CheckArrayType(Entity, IList, DeclType, Zero,
589 : SubobjectIsDesignatorContext, Index,
590 377: StructuredList, StructuredIndex);
591 : } else
592 0: assert(0 && "Aggregate that isn't a structure or array?!");
4: branch 2 taken
0: branch 3 not taken
1: branch 6 taken
3: branch 7 taken
1: branch 8 taken
3: branch 9 taken
593 4: } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
594 : // This type is invalid, issue a diagnostic.
595 1: ++Index;
596 : SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
597 1: << DeclType;
598 1: hadError = true;
2: branch 2 taken
1: branch 3 taken
599 3: } else if (DeclType->isRecordType()) {
600 : // C++ [dcl.init]p14:
601 : // [...] If the class is an aggregate (8.5.1), and the initializer
602 : // is a brace-enclosed list, see 8.5.1.
603 : //
604 : // Note: 8.5.1 is handled below; here, we diagnose the case where
605 : // we have an initializer list and a destination type that is not
606 : // an aggregate.
607 : // FIXME: In C++0x, this is yet another form of initialization.
608 : SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
609 2: << DeclType << IList->getSourceRange();
610 2: hadError = true;
1: branch 2 taken
0: branch 3 not taken
611 1: } else if (DeclType->isReferenceType()) {
612 : CheckReferenceType(Entity, IList, DeclType, Index,
613 1: StructuredList, StructuredIndex);
614 : } else {
615 : // In C, all types are either scalars or aggregates, but
616 : // additional handling is needed here for C++ (and possibly others?).
617 0: assert(0 && "Unsupported initializer type");
618 : }
619 962: }
620 :
621 : void InitListChecker::CheckSubElementType(const InitializedEntity &Entity,
622 : InitListExpr *IList,
623 : QualType ElemType,
624 : unsigned &Index,
625 : InitListExpr *StructuredList,
626 2410: unsigned &StructuredIndex) {
627 2410: Expr *expr = IList->getInit(Index);
135: branch 1 taken
2275: branch 2 taken
628 2410: if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
629 135: unsigned newIndex = 0;
630 135: unsigned newStructuredIndex = 0;
631 : InitListExpr *newStructuredList
632 : = getStructuredSubobjectInit(IList, Index, ElemType,
633 : StructuredList, StructuredIndex,
634 135: SubInitList->getSourceRange());
635 : CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex,
636 135: newStructuredList, newStructuredIndex);
637 135: ++StructuredIndex;
638 135: ++Index;
17: branch 1 taken
2258: branch 2 taken
639 2275: } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) {
640 17: CheckStringInit(Str, ElemType, SemaRef);
641 17: UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
642 17: ++Index;
2151: branch 2 taken
107: branch 3 taken
643 2258: } else if (ElemType->isScalarType()) {
644 : CheckScalarType(Entity, IList, ElemType, Index,
645 2151: StructuredList, StructuredIndex);
7: branch 2 taken
100: branch 3 taken
646 107: } else if (ElemType->isReferenceType()) {
647 : CheckReferenceType(Entity, IList, ElemType, Index,
648 7: StructuredList, StructuredIndex);
649 : } else {
21: branch 1 taken
79: branch 2 taken
650 100: if (SemaRef.getLangOptions().CPlusPlus) {
651 : // C++ [dcl.init.aggr]p12:
652 : // All implicit type conversions (clause 4) are considered when
653 : // initializing the aggregate member with an ini- tializer from
654 : // an initializer-list. If the initializer can initialize a
655 : // member, the member is initialized. [...]
656 :
657 : // FIXME: Better EqualLoc?
658 : InitializationKind Kind =
659 21: InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation());
660 21: InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1);
661 :
7: branch 1 taken
14: branch 2 taken
662 21: if (Seq) {
663 : Sema::OwningExprResult Result =
664 : Seq.Perform(SemaRef, Entity, Kind,
665 7: Sema::MultiExprArg(SemaRef, (void **)&expr, 1));
2: branch 1 taken
5: branch 2 taken
666 7: if (Result.isInvalid())
667 2: hadError = true;
668 :
669 : UpdateStructuredListElement(StructuredList, StructuredIndex,
670 7: Result.takeAs<Expr>());
671 7: ++Index;
672 7: return;
14: branch 1 taken
7: branch 2 taken
673 21: }
674 :
675 : // Fall through for subaggregate initialization
676 : } else {
677 : // C99 6.7.8p13:
678 : //
679 : // The initializer for a structure or union object that has
680 : // automatic storage duration shall be either an initializer
681 : // list as described below, or a single expression that has
682 : // compatible structure or union type. In the latter case, the
683 : // initial value of the object, including unnamed members, is
684 : // that of the expression.
48: branch 2 taken
31: branch 3 taken
13: branch 6 taken
35: branch 7 taken
14: branch 10 taken
30: branch 11 taken
14: branch 12 taken
65: branch 13 taken
685 79: if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
686 : SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) {
687 14: UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
688 14: ++Index;
689 14: return;
690 : }
691 :
692 : // Fall through for subaggregate initialization
693 : }
694 :
695 : // C++ [dcl.init.aggr]p12:
696 : //
697 : // [...] Otherwise, if the member is itself a non-empty
698 : // subaggregate, brace elision is assumed and the initializer is
699 : // considered for the initialization of the first member of
700 : // the subaggregate.
6: branch 2 taken
73: branch 3 taken
6: branch 6 taken
0: branch 7 not taken
79: branch 8 taken
0: branch 9 not taken
701 79: if (ElemType->isAggregateType() || ElemType->isVectorType()) {
702 : CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList,
703 79: StructuredIndex);
704 79: ++StructuredIndex;
705 : } else {
706 : // We cannot initialize this element, so let
707 : // PerformCopyInitialization produce the appropriate diagnostic.
708 : SemaRef.PerformCopyInitialization(Entity, SourceLocation(),
709 0: SemaRef.Owned(expr));
710 0: IList->setInit(Index, 0);
711 0: hadError = true;
712 0: ++Index;
713 0: ++StructuredIndex;
714 : }
715 : }
716 : }
717 :
718 : void InitListChecker::CheckScalarType(const InitializedEntity &Entity,
719 : InitListExpr *IList, QualType DeclType,
720 : unsigned &Index,
721 : InitListExpr *StructuredList,
722 2172: unsigned &StructuredIndex) {
2171: branch 1 taken
1: branch 2 taken
723 2172: if (Index < IList->getNumInits()) {
724 2171: Expr *expr = IList->getInit(Index);
1: branch 1 taken
2170: branch 2 taken
725 2171: if (isa<InitListExpr>(expr)) {
726 : SemaRef.Diag(IList->getLocStart(),
727 : diag::err_many_braces_around_scalar_init)
728 1: << IList->getSourceRange();
729 1: hadError = true;
730 1: ++Index;
731 1: ++StructuredIndex;
732 1: return;
1: branch 1 taken
2169: branch 2 taken
733 2170: } else if (isa<DesignatedInitExpr>(expr)) {
734 : SemaRef.Diag(expr->getSourceRange().getBegin(),
735 : diag::err_designator_for_scalar_init)
736 1: << DeclType << expr->getSourceRange();
737 1: hadError = true;
738 1: ++Index;
739 1: ++StructuredIndex;
740 1: return;
741 : }
742 :
743 : Sema::OwningExprResult Result =
744 : SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
745 2169: SemaRef.Owned(expr));
746 :
747 : Expr *ResultExpr;
748 :
6: branch 1 taken
2163: branch 2 taken
749 2169: if (Result.isInvalid())
750 6: hadError = true; // types weren't compatible.
751 : else {
752 2163: ResultExpr = Result.takeAs<Expr>();
753 :
526: branch 0 taken
1637: branch 1 taken
754 2163: if (ResultExpr != expr) {
755 : // The type was promoted, update initializer list.
756 526: IList->setInit(Index, ResultExpr);
757 : }
758 : }
17: branch 0 taken
2152: branch 1 taken
759 2169: if (hadError)
760 17: ++StructuredIndex;
761 : else
762 2152: UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
763 2169: ++Index;
764 : } else {
765 : SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer)
766 1: << IList->getSourceRange();
767 1: hadError = true;
768 1: ++Index;
769 1: ++StructuredIndex;
770 1: return;
771 : }
772 : }
773 :
774 : void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
775 : InitListExpr *IList, QualType DeclType,
776 : unsigned &Index,
777 : InitListExpr *StructuredList,
778 8: unsigned &StructuredIndex) {
8: branch 1 taken
0: branch 2 not taken
779 8: if (Index < IList->getNumInits()) {
780 8: Expr *expr = IList->getInit(Index);
0: branch 1 not taken
8: branch 2 taken
781 8: if (isa<InitListExpr>(expr)) {
782 : SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
783 0: << DeclType << IList->getSourceRange();
784 0: hadError = true;
785 0: ++Index;
786 0: ++StructuredIndex;
787 0: return;
788 : }
789 :
790 : Sema::OwningExprResult Result =
791 : SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
792 8: SemaRef.Owned(expr));
793 :
1: branch 1 taken
7: branch 2 taken
794 8: if (Result.isInvalid())
795 1: hadError = true;
796 :
797 8: expr = Result.takeAs<Expr>();
798 8: IList->setInit(Index, expr);
799 :
1: branch 0 taken
7: branch 1 taken
800 8: if (hadError)
801 1: ++StructuredIndex;
802 : else
803 7: UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
804 8: ++Index;
805 : } else {
806 : // FIXME: It would be wonderful if we could point at the actual member. In
807 : // general, it would be useful to pass location information down the stack,
808 : // so that we know the location (or decl) of the "current object" being
809 : // initialized.
810 : SemaRef.Diag(IList->getLocStart(),
811 : diag::err_init_reference_member_uninitialized)
812 : << DeclType
813 0: << IList->getSourceRange();
814 0: hadError = true;
815 0: ++Index;
816 0: ++StructuredIndex;
817 0: return;
818 : }
819 : }
820 :
821 : void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
822 : InitListExpr *IList, QualType DeclType,
823 : unsigned &Index,
824 : InitListExpr *StructuredList,
825 219: unsigned &StructuredIndex) {
219: branch 1 taken
0: branch 2 not taken
826 219: if (Index < IList->getNumInits()) {
827 219: const VectorType *VT = DeclType->getAs<VectorType>();
828 219: unsigned maxElements = VT->getNumElements();
829 219: unsigned numEltsInit = 0;
830 219: QualType elementType = VT->getElementType();
831 :
216: branch 1 taken
3: branch 2 taken
832 219: if (!SemaRef.getLangOptions().OpenCL) {
833 : InitializedEntity ElementEntity =
834 216: InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
835 :
871: branch 0 taken
201: branch 1 taken
836 1072: for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
837 : // Don't attempt to go past the end of the init list
15: branch 1 taken
856: branch 2 taken
838 871: if (Index >= IList->getNumInits())
839 15: break;
840 :
841 856: ElementEntity.setElementIndex(Index);
842 : CheckSubElementType(ElementEntity, IList, elementType, Index,
843 856: StructuredList, StructuredIndex);
844 : }
845 : } else {
846 : InitializedEntity ElementEntity =
847 3: InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
848 :
849 : // OpenCL initializers allows vectors to be constructed from vectors.
9: branch 0 taken
0: branch 1 not taken
850 9: for (unsigned i = 0; i < maxElements; ++i) {
851 : // Don't attempt to go past the end of the init list
3: branch 1 taken
6: branch 2 taken
852 9: if (Index >= IList->getNumInits())
853 3: break;
854 :
855 6: ElementEntity.setElementIndex(Index);
856 :
857 6: QualType IType = IList->getInit(Index)->getType();
0: branch 2 not taken
6: branch 3 taken
858 6: if (!IType->isVectorType()) {
859 : CheckSubElementType(ElementEntity, IList, elementType, Index,
860 0: StructuredList, StructuredIndex);
861 0: ++numEltsInit;
862 : } else {
863 6: const VectorType *IVT = IType->getAs<VectorType>();
864 6: unsigned numIElts = IVT->getNumElements();
865 : QualType VecType = SemaRef.Context.getExtVectorType(elementType,
866 6: numIElts);
867 : CheckSubElementType(ElementEntity, IList, VecType, Index,
868 6: StructuredList, StructuredIndex);
869 6: numEltsInit += numIElts;
870 : }
871 : }
872 : }
873 :
874 : // OpenCL & AltiVec require all elements to be initialized.
15: branch 0 taken
204: branch 1 taken
875 219: if (numEltsInit != maxElements)
15: branch 1 taken
0: branch 2 not taken
1: branch 4 taken
14: branch 5 taken
1: branch 6 taken
14: branch 7 taken
876 15: if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec)
877 : SemaRef.Diag(IList->getSourceRange().getBegin(),
878 : diag::err_vector_incorrect_num_initializers)
879 1: << (numEltsInit < maxElements) << maxElements << numEltsInit;
880 : }
881 219: }
882 :
883 : void InitListChecker::CheckArrayType(const InitializedEntity &Entity,
884 : InitListExpr *IList, QualType &DeclType,
885 : llvm::APSInt elementIndex,
886 : bool SubobjectIsDesignatorContext,
887 : unsigned &Index,
888 : InitListExpr *StructuredList,
889 384: unsigned &StructuredIndex) {
890 : // Check for the special-case of initializing an array with a string.
365: branch 1 taken
19: branch 2 taken
891 384: if (Index < IList->getNumInits()) {
11: branch 0 taken
354: branch 1 taken
892 365: if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType,
893 365: SemaRef.Context)) {
894 11: CheckStringInit(Str, DeclType, SemaRef);
895 : // We place the string literal directly into the resulting
896 : // initializer list. This is the only place where the structure
897 : // of the structured initializer list doesn't match exactly,
898 : // because doing so would involve allocating one character
899 : // constant for each string.
900 11: UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
901 11: StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
902 11: ++Index;
903 11: return;
904 : }
905 : }
4: branch 0 taken
369: branch 1 taken
906 373: if (const VariableArrayType *VAT =
907 373: SemaRef.Context.getAsVariableArrayType(DeclType)) {
908 : // Check for VLAs; in standard C it would be possible to check this
909 : // earlier, but I don't know where clang accepts VLAs (gcc accepts
910 : // them in all sorts of strange places).
911 : SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
912 : diag::err_variable_object_no_init)
913 4: << VAT->getSizeExpr()->getSourceRange();
914 4: hadError = true;
915 4: ++Index;
916 4: ++StructuredIndex;
917 4: return;
918 : }
919 :
920 : // We might know the maximum number of elements in advance.
921 : llvm::APSInt maxElements(elementIndex.getBitWidth(),
922 369: elementIndex.isUnsigned());
923 369: bool maxElementsKnown = false;
234: branch 0 taken
135: branch 1 taken
924 369: if (const ConstantArrayType *CAT =
925 369: SemaRef.Context.getAsConstantArrayType(DeclType)) {
926 234: maxElements = CAT->getSize();
927 234: elementIndex.extOrTrunc(maxElements.getBitWidth());
928 234: elementIndex.setIsUnsigned(maxElements.isUnsigned());
929 234: maxElementsKnown = true;
930 : }
931 :
932 : QualType elementType = SemaRef.Context.getAsArrayType(DeclType)
933 369: ->getElementType();
1046: branch 1 taken
334: branch 2 taken
934 1749: while (Index < IList->getNumInits()) {
935 1046: Expr *Init = IList->getInit(Index);
60: branch 1 taken
986: branch 2 taken
936 1046: if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
937 : // If we're not the subobject that matches up with the '{' for
938 : // the designator, we shouldn't be handling the
939 : // designator. Return immediately.
4: branch 0 taken
56: branch 1 taken
940 60: if (!SubobjectIsDesignatorContext)
941 4: return;
942 :
943 : // Handle this designated initializer. elementIndex will be
944 : // updated to be the next array element we'll initialize.
8: branch 1 taken
48: branch 2 taken
945 56: if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
946 : DeclType, 0, &elementIndex, Index,
947 : StructuredList, StructuredIndex, true,
948 : false)) {
949 8: hadError = true;
950 8: continue;
951 : }
952 :
0: branch 2 not taken
48: branch 3 taken
953 48: if (elementIndex.getBitWidth() > maxElements.getBitWidth())
954 0: maxElements.extend(elementIndex.getBitWidth());
20: branch 2 taken
28: branch 3 taken
955 48: else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
956 20: elementIndex.extend(maxElements.getBitWidth());
957 48: elementIndex.setIsUnsigned(maxElements.isUnsigned());
958 :
959 : // If the array is of incomplete type, keep track of the number of
960 : // elements in the initializer.
27: branch 0 taken
21: branch 1 taken
19: branch 3 taken
8: branch 4 taken
19: branch 5 taken
29: branch 6 taken
961 48: if (!maxElementsKnown && elementIndex > maxElements)
962 19: maxElements = elementIndex;
963 :
964 48: continue;
965 : }
966 :
967 : // If we know the maximum number of elements, and we've already
968 : // hit it, stop consuming elements in the initializer list.
599: branch 0 taken
387: branch 1 taken
31: branch 3 taken
568: branch 4 taken
31: branch 5 taken
955: branch 6 taken
969 986: if (maxElementsKnown && elementIndex == maxElements)
970 31: break;
971 :
972 : InitializedEntity ElementEntity =
973 : InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex,
974 955: Entity);
975 : // Check this element.
976 : CheckSubElementType(ElementEntity, IList, elementType, Index,
977 955: StructuredList, StructuredIndex);
978 955: ++elementIndex;
979 :
980 : // If the array is of incomplete type, keep track of the number of
981 : // elements in the initializer.
387: branch 0 taken
568: branch 1 taken
386: branch 3 taken
1: branch 4 taken
386: branch 5 taken
569: branch 6 taken
982 955: if (!maxElementsKnown && elementIndex > maxElements)
983 386: maxElements = elementIndex;
984 : }
349: branch 0 taken
16: branch 1 taken
130: branch 4 taken
219: branch 5 taken
130: branch 6 taken
235: branch 7 taken
985 365: if (!hadError && DeclType->isIncompleteArrayType()) {
986 : // If this is an incomplete array type, the actual type needs to
987 : // be calculated here.
988 130: llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
8: branch 1 taken
122: branch 2 taken
989 130: if (maxElements == Zero) {
990 : // Sizing an array implicitly to zero is not allowed by ISO C,
991 : // but is supported by GNU.
992 : SemaRef.Diag(IList->getLocStart(),
993 8: diag::ext_typecheck_zero_array_size);
994 : }
995 :
996 : DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
997 130: ArrayType::Normal, 0);
365: branch 1 taken
4: branch 2 taken
998 369: }
999 : }
1000 :
1001 : void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
1002 : InitListExpr *IList,
1003 : QualType DeclType,
1004 : RecordDecl::field_iterator Field,
1005 : bool SubobjectIsDesignatorContext,
1006 : unsigned &Index,
1007 : InitListExpr *StructuredList,
1008 : unsigned &StructuredIndex,
1009 383: bool TopLevelObject) {
1010 383: RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
1011 :
1012 : // If the record is invalid, some of it's members are invalid. To avoid
1013 : // confusion, we forgo checking the intializer for the entire record.
2: branch 1 taken
381: branch 2 taken
1014 383: if (structDecl->isInvalidDecl()) {
1015 2: hadError = true;
1016 2: return;
1017 : }
1018 :
32: branch 2 taken
349: branch 3 taken
4: branch 5 taken
28: branch 6 taken
4: branch 7 taken
377: branch 8 taken
1019 381: if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1020 : // Value-initialize the first named member of the union.
1021 4: RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
3: branch 3 taken
1: branch 4 taken
1022 4: for (RecordDecl::field_iterator FieldEnd = RD->field_end();
1023 : Field != FieldEnd; ++Field) {
3: branch 3 taken
0: branch 4 not taken
1024 3: if (Field->getDeclName()) {
1025 3: StructuredList->setInitializedFieldInUnion(*Field);
1026 3: break;
1027 : }
1028 : }
1029 4: return;
1030 : }
1031 :
1032 : // If structDecl is a forward declaration, this loop won't do
1033 : // anything except look at designated initializers; That's okay,
1034 : // because an error should get printed out elsewhere. It might be
1035 : // worthwhile to skip over the rest of the initializer, though.
1036 377: RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
1037 377: RecordDecl::field_iterator FieldEnd = RD->field_end();
1038 377: bool InitializedSomething = false;
605: branch 1 taken
312: branch 2 taken
1039 1294: while (Index < IList->getNumInits()) {
1040 605: Expr *Init = IList->getInit(Index);
1041 :
118: branch 1 taken
487: branch 2 taken
1042 605: if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
1043 : // If we're not the subobject that matches up with the '{' for
1044 : // the designator, we shouldn't be handling the
1045 : // designator. Return immediately.
17: branch 0 taken
101: branch 1 taken
1046 118: if (!SubobjectIsDesignatorContext)
1047 17: return;
1048 :
1049 : // Handle this designated initializer. Field will be updated to
1050 : // the next field that we'll be initializing.
7: branch 1 taken
94: branch 2 taken
1051 101: if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
1052 : DeclType, &Field, 0, Index,
1053 : StructuredList, StructuredIndex,
1054 : true, TopLevelObject))
1055 7: hadError = true;
1056 :
1057 101: InitializedSomething = true;
1058 101: continue;
1059 : }
1060 :
31: branch 1 taken
456: branch 2 taken
1061 487: if (Field == FieldEnd) {
1062 : // We've run out of fields. We're done.
1063 31: break;
1064 : }
1065 :
1066 : // We've already initialized a member of a union. We're done.
195: branch 0 taken
261: branch 1 taken
3: branch 4 taken
192: branch 5 taken
3: branch 6 taken
453: branch 7 taken
1067 456: if (InitializedSomething && DeclType->isUnionType())
1068 3: break;
1069 :
1070 : // If we've hit the flexible array member at the end, we're done.
14: branch 4 taken
439: branch 5 taken
1071 453: if (Field->getType()->isIncompleteArrayType())
1072 14: break;
1073 :
6: branch 2 taken
433: branch 3 taken
1074 439: if (Field->isUnnamedBitfield()) {
1075 : // Don't initialize unnamed bitfields, e.g. "int : 20;"
1076 6: ++Field;
1077 6: continue;
1078 : }
1079 :
1080 : InitializedEntity MemberEntity =
1081 433: InitializedEntity::InitializeMember(*Field, &Entity);
1082 : CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1083 433: StructuredList, StructuredIndex);
1084 433: InitializedSomething = true;
1085 :
17: branch 2 taken
416: branch 3 taken
1086 433: if (DeclType->isUnionType()) {
1087 : // Initialize the first field within the union.
1088 17: StructuredList->setInitializedFieldInUnion(*Field);
1089 : }
1090 :
1091 433: ++Field;
1092 : }
1093 :
77: branch 1 taken
283: branch 2 taken
14: branch 7 taken
63: branch 8 taken
0: branch 10 not taken
14: branch 11 taken
346: branch 12 taken
14: branch 13 taken
1094 360: if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
1095 : Index >= IList->getNumInits())
1096 346: return;
1097 :
1098 : // Handle GNU flexible array initializers.
3: branch 0 taken
11: branch 1 taken
3: branch 4 taken
0: branch 5 not taken
2: branch 9 taken
1: branch 10 taken
2: branch 11 taken
12: branch 12 taken
1099 14: if (!TopLevelObject &&
1100 : (!isa<InitListExpr>(IList->getInit(Index)) ||
1101 : cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) {
1102 : SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1103 : diag::err_flexible_array_init_nonempty)
1104 2: << IList->getInit(Index)->getSourceRange().getBegin();
1105 : SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1106 2: << *Field;
1107 2: hadError = true;
1108 2: ++Index;
1109 2: return;
1110 : } else {
1111 : SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(),
1112 : diag::ext_flexible_array_init)
1113 12: << IList->getInit(Index)->getSourceRange().getBegin();
1114 : SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1115 12: << *Field;
1116 : }
1117 :
1118 : InitializedEntity MemberEntity =
1119 12: InitializedEntity::InitializeMember(*Field, &Entity);
1120 :
8: branch 2 taken
4: branch 3 taken
1121 12: if (isa<InitListExpr>(IList->getInit(Index)))
1122 : CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1123 8: StructuredList, StructuredIndex);
1124 : else
1125 : CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
1126 4: StructuredList, StructuredIndex);
1127 : }
1128 :
1129 : /// \brief Expand a field designator that refers to a member of an
1130 : /// anonymous struct or union into a series of field designators that
1131 : /// refers to the field within the appropriate subobject.
1132 : ///
1133 : /// Field/FieldIndex will be updated to point to the (new)
1134 : /// currently-designated field.
1135 : static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
1136 : DesignatedInitExpr *DIE,
1137 : unsigned DesigIdx,
1138 : FieldDecl *Field,
1139 : RecordDecl::field_iterator &FieldIter,
1140 3: unsigned &FieldIndex) {
1141 : typedef DesignatedInitExpr::Designator Designator;
1142 :
1143 : // Build the path from the current object to the member of the
1144 : // anonymous struct/union (backwards).
1145 3: llvm::SmallVector<FieldDecl *, 4> Path;
1146 3: SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path);
1147 :
1148 : // Build the replacement designators.
1149 3: llvm::SmallVector<Designator, 4> Replacements;
9: branch 2 taken
3: branch 3 taken
1150 12: for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
1151 3: FI = Path.rbegin(), FIEnd = Path.rend();
1152 : FI != FIEnd; ++FI) {
3: branch 2 taken
6: branch 3 taken
1153 9: if (FI + 1 == FIEnd)
1154 : Replacements.push_back(Designator((IdentifierInfo *)0,
1155 : DIE->getDesignator(DesigIdx)->getDotLoc(),
1156 3: DIE->getDesignator(DesigIdx)->getFieldLoc()));
1157 : else
1158 : Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1159 6: SourceLocation()));
1160 9: Replacements.back().setField(*FI);
1161 : }
1162 :
1163 : // Expand the current designator into the set of replacement
1164 : // designators, so we have a full subobject path down to where the
1165 : // member of the anonymous struct/union is actually stored.
1166 : DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
1167 3: &Replacements[0] + Replacements.size());
1168 :
1169 : // Update FieldIter/FieldIndex;
1170 3: RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext());
1171 3: FieldIter = Record->field_begin();
1172 3: FieldIndex = 0;
3: branch 3 taken
0: branch 4 not taken
1173 3: for (RecordDecl::field_iterator FEnd = Record->field_end();
1174 : FieldIter != FEnd; ++FieldIter) {
0: branch 2 not taken
3: branch 3 taken
1175 3: if (FieldIter->isUnnamedBitfield())
1176 0: continue;
1177 :
0: branch 2 not taken
3: branch 3 taken
1178 3: if (*FieldIter == Path.back())
1179 : return;
1180 :
1181 0: ++FieldIndex;
1182 : }
1183 :
1184 0: assert(false && "Unable to find anonymous struct/union field");
1185 : }
1186 :
1187 : /// @brief Check the well-formedness of a C99 designated initializer.
1188 : ///
1189 : /// Determines whether the designated initializer @p DIE, which
1190 : /// resides at the given @p Index within the initializer list @p
1191 : /// IList, is well-formed for a current object of type @p DeclType
1192 : /// (C99 6.7.8). The actual subobject that this designator refers to
1193 : /// within the current subobject is returned in either
1194 : /// @p NextField or @p NextElementIndex (whichever is appropriate).
1195 : ///
1196 : /// @param IList The initializer list in which this designated
1197 : /// initializer occurs.
1198 : ///
1199 : /// @param DIE The designated initializer expression.
1200 : ///
1201 : /// @param DesigIdx The index of the current designator.
1202 : ///
1203 : /// @param DeclType The type of the "current object" (C99 6.7.8p17),
1204 : /// into which the designation in @p DIE should refer.
1205 : ///
1206 : /// @param NextField If non-NULL and the first designator in @p DIE is
1207 : /// a field, this will be set to the field declaration corresponding
1208 : /// to the field named by the designator.
1209 : ///
1210 : /// @param NextElementIndex If non-NULL and the first designator in @p
1211 : /// DIE is an array designator or GNU array-range designator, this
1212 : /// will be set to the last index initialized by this designator.
1213 : ///
1214 : /// @param Index Index into @p IList where the designated initializer
1215 : /// @p DIE occurs.
1216 : ///
1217 : /// @param StructuredList The initializer list expression that
1218 : /// describes all of the subobject initializers in the order they'll
1219 : /// actually be initialized.
1220 : ///
1221 : /// @returns true if there was an error, false otherwise.
1222 : bool
1223 : InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
1224 : InitListExpr *IList,
1225 : DesignatedInitExpr *DIE,
1226 : unsigned DesigIdx,
1227 : QualType &CurrentObjectType,
1228 : RecordDecl::field_iterator *NextField,
1229 : llvm::APSInt *NextElementIndex,
1230 : unsigned &Index,
1231 : InitListExpr *StructuredList,
1232 : unsigned &StructuredIndex,
1233 : bool FinishSubobjectInit,
1234 363: bool TopLevelObject) {
150: branch 1 taken
213: branch 2 taken
1235 363: if (DesigIdx == DIE->size()) {
1236 : // Check the actual initialization for the designated object type.
1237 150: bool prevHadError = hadError;
1238 :
1239 : // Temporarily remove the designator expression from the
1240 : // initializer list that the child calls see, so that we don't try
1241 : // to re-process the designator.
1242 150: unsigned OldIndex = Index;
1243 150: IList->setInit(OldIndex, DIE->getInit());
1244 :
1245 : CheckSubElementType(Entity, IList, CurrentObjectType, Index,
1246 150: StructuredList, StructuredIndex);
1247 :
1248 : // Restore the designated initializer expression in the syntactic
1249 : // form of the initializer list.
31: branch 2 taken
119: branch 3 taken
1250 150: if (IList->getInit(OldIndex) != DIE->getInit())
1251 31: DIE->setInit(IList->getInit(OldIndex));
1252 150: IList->setInit(OldIndex, DIE);
1253 :
14: branch 0 taken
136: branch 1 taken
1: branch 2 taken
13: branch 3 taken
1254 150: return hadError && !prevHadError;
1255 : }
1256 :
1257 213: bool IsFirstDesignator = (DesigIdx == 0);
1258 : assert((IsFirstDesignator || StructuredList) &&
56: branch 0 taken
157: branch 1 taken
0: branch 2 not taken
56: branch 3 taken
1259 213: "Need a non-designated initializer list to start from");
1260 :
1261 213: DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
1262 : // Determine the structural initializer list that corresponds to the
1263 : // current subobject.
1264 : StructuredList = IsFirstDesignator? SyntacticToSemantic[IList]
1265 : : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
1266 : StructuredList, StructuredIndex,
1267 : SourceRange(D->getStartLocation(),
157: branch 0 taken
56: branch 1 taken
1268 213: DIE->getSourceRange().getEnd()));
0: branch 0 not taken
213: branch 1 taken
1269 213: assert(StructuredList && "Expected a structured initializer list");
1270 :
150: branch 1 taken
63: branch 2 taken
1271 213: if (D->isFieldDesignator()) {
1272 : // C99 6.7.8p7:
1273 : //
1274 : // If a designator has the form
1275 : //
1276 : // . identifier
1277 : //
1278 : // then the current object (defined below) shall have
1279 : // structure or union type and the identifier shall be the
1280 : // name of a member of that type.
1281 150: const RecordType *RT = CurrentObjectType->getAs<RecordType>();
1: branch 0 taken
149: branch 1 taken
1282 150: if (!RT) {
1283 1: SourceLocation Loc = D->getDotLoc();
0: branch 1 not taken
1: branch 2 taken
1284 1: if (Loc.isInvalid())
1285 0: Loc = D->getFieldLoc();
1286 : SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1287 1: << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType;
1288 1: ++Index;
1289 1: return true;
1290 : }
1291 :
1292 : // Note: we perform a linear search of the fields here, despite
1293 : // the fact that we have a faster lookup method, because we always
1294 : // need to compute the field's index.
1295 149: FieldDecl *KnownField = D->getField();
1296 149: IdentifierInfo *FieldName = D->getFieldName();
1297 149: unsigned FieldIndex = 0;
1298 : RecordDecl::field_iterator
1299 149: Field = RT->getDecl()->field_begin(),
1300 149: FieldEnd = RT->getDecl()->field_end();
231: branch 2 taken
10: branch 3 taken
1301 241: for (; Field != FieldEnd; ++Field) {
0: branch 2 not taken
231: branch 3 taken
1302 231: if (Field->isUnnamedBitfield())
1303 0: continue;
1304 :
220: branch 1 taken
11: branch 2 taken
128: branch 5 taken
92: branch 6 taken
139: branch 7 taken
92: branch 8 taken
1305 231: if (KnownField == *Field || Field->getIdentifier() == FieldName)
1306 139: break;
1307 :
1308 92: ++FieldIndex;
1309 : }
1310 :
10: branch 1 taken
139: branch 2 taken
1311 149: if (Field == FieldEnd) {
1312 : // There was no normal field in the struct with the designated
1313 : // name. Perform another lookup for this name, which may find
1314 : // something that we can't designate (e.g., a member function),
1315 : // may find nothing, or may find a member of an anonymous
1316 : // struct/union.
1317 10: DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
1318 10: FieldDecl *ReplacementField = 0;
8: branch 0 taken
2: branch 1 taken
1319 10: if (Lookup.first == Lookup.second) {
1320 : // Name lookup didn't find anything. Determine whether this
1321 : // was a typo for another field name.
1322 : LookupResult R(SemaRef, FieldName, D->getFieldLoc(),
1323 8: Sema::LookupMemberName);
8: branch 1 taken
0: branch 2 not taken
4: branch 4 taken
4: branch 5 taken
4: branch 7 taken
0: branch 8 not taken
4: branch 10 taken
0: branch 11 not taken
4: branch 15 taken
0: branch 16 not taken
4: branch 17 taken
4: branch 18 taken
1324 8: if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl()) &&
1325 : (ReplacementField = R.getAsSingle<FieldDecl>()) &&
1326 : ReplacementField->getDeclContext()->getLookupContext()
1327 : ->Equals(RT->getDecl())) {
1328 : SemaRef.Diag(D->getFieldLoc(),
1329 : diag::err_field_designator_unknown_suggest)
1330 : << FieldName << CurrentObjectType << R.getLookupName()
1331 : << CodeModificationHint::CreateReplacement(D->getFieldLoc(),
1332 4: R.getLookupName().getAsString());
1333 : SemaRef.Diag(ReplacementField->getLocation(),
1334 : diag::note_previous_decl)
1335 4: << ReplacementField->getDeclName();
1336 : } else {
1337 : SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
1338 4: << FieldName << CurrentObjectType;
1339 4: ++Index;
1340 8: return true;
4: branch 1 taken
4: branch 2 taken
1341 8: }
2: branch 0 taken
0: branch 1 not taken
1342 2: } else if (!KnownField) {
1343 : // Determine whether we found a field at all.
1344 2: ReplacementField = dyn_cast<FieldDecl>(*Lookup.first);
1345 : }
1346 :
0: branch 0 not taken
6: branch 1 taken
1347 6: if (!ReplacementField) {
1348 : // Name lookup found something, but it wasn't a field.
1349 : SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
1350 0: << FieldName;
1351 : SemaRef.Diag((*Lookup.first)->getLocation(),
1352 0: diag::note_field_designator_found);
1353 0: ++Index;
1354 0: return true;
1355 : }
1356 :
6: branch 0 taken
0: branch 1 not taken
2: branch 5 taken
4: branch 6 taken
2: branch 7 taken
4: branch 8 taken
1357 6: if (!KnownField &&
1358 : cast<RecordDecl>((ReplacementField)->getDeclContext())
1359 : ->isAnonymousStructOrUnion()) {
1360 : // Handle an field designator that refers to a member of an
1361 : // anonymous struct or union.
1362 : ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx,
1363 : ReplacementField,
1364 2: Field, FieldIndex);
1365 2: D = DIE->getDesignator(DesigIdx);
4: branch 0 taken
0: branch 1 not taken
1366 4: } else if (!KnownField) {
1367 : // The replacement field comes from typo correction; find it
1368 : // in the list of fields.
1369 4: FieldIndex = 0;
1370 4: Field = RT->getDecl()->field_begin();
4: branch 2 taken
0: branch 3 not taken
1371 4: for (; Field != FieldEnd; ++Field) {
0: branch 2 not taken
4: branch 3 taken
1372 4: if (Field->isUnnamedBitfield())
1373 0: continue;
1374 :
0: branch 1 not taken
4: branch 2 taken
0: branch 6 not taken
0: branch 7 not taken
4: branch 8 taken
0: branch 9 not taken
1375 4: if (ReplacementField == *Field ||
1376 : Field->getIdentifier() == ReplacementField->getIdentifier())
1377 4: break;
1378 :
1379 0: ++FieldIndex;
1380 : }
1381 : }
128: branch 0 taken
11: branch 1 taken
1: branch 6 taken
127: branch 7 taken
1: branch 8 taken
138: branch 9 taken
1382 139: } else if (!KnownField &&
1383 : cast<RecordDecl>((*Field)->getDeclContext())
1384 : ->isAnonymousStructOrUnion()) {
1385 : ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field,
1386 1: Field, FieldIndex);
1387 1: D = DIE->getDesignator(DesigIdx);
1388 : }
1389 :
1390 : // All of the fields of a union are located at the same place in
1391 : // the initializer list.
16: branch 2 taken
129: branch 3 taken
1392 145: if (RT->getDecl()->isUnion()) {
1393 16: FieldIndex = 0;
1394 16: StructuredList->setInitializedFieldInUnion(*Field);
1395 : }
1396 :
1397 : // Update the designator with the field declaration.
1398 145: D->setField(*Field);
1399 :
1400 : // Make sure that our non-designated initializer list has space
1401 : // for a subobject corresponding to this field.
127: branch 1 taken
18: branch 2 taken
1402 145: if (FieldIndex >= StructuredList->getNumInits())
1403 127: StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
1404 :
1405 : // This designator names a flexible array member.
5: branch 4 taken
140: branch 5 taken
1406 145: if (Field->getType()->isIncompleteArrayType()) {
1407 5: bool Invalid = false;
1: branch 1 taken
4: branch 2 taken
1408 5: if ((DesigIdx + 1) != DIE->size()) {
1409 : // We can't designate an object within the flexible array
1410 : // member (because GCC doesn't allow it).
1411 : DesignatedInitExpr::Designator *NextD
1412 1: = DIE->getDesignator(DesigIdx + 1);
1413 : SemaRef.Diag(NextD->getStartLocation(),
1414 : diag::err_designator_into_flexible_array_member)
1415 : << SourceRange(NextD->getStartLocation(),
1416 1: DIE->getSourceRange().getEnd());
1417 : SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1418 1: << *Field;
1419 1: Invalid = true;
1420 : }
1421 :
5: branch 0 taken
0: branch 1 not taken
1: branch 4 taken
4: branch 5 taken
1: branch 6 taken
4: branch 7 taken
1422 5: if (!hadError && !isa<InitListExpr>(DIE->getInit())) {
1423 : // The initializer is not an initializer list.
1424 : SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(),
1425 : diag::err_flexible_array_init_needs_braces)
1426 1: << DIE->getInit()->getSourceRange();
1427 : SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1428 1: << *Field;
1429 1: Invalid = true;
1430 : }
1431 :
1432 : // Handle GNU flexible array initializers.
3: branch 0 taken
2: branch 1 taken
2: branch 2 taken
1: branch 3 taken
1: branch 7 taken
1: branch 8 taken
1: branch 9 taken
4: branch 10 taken
1433 5: if (!Invalid && !TopLevelObject &&
1434 : cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) {
1435 : SemaRef.Diag(DIE->getSourceRange().getBegin(),
1436 : diag::err_flexible_array_init_nonempty)
1437 1: << DIE->getSourceRange().getBegin();
1438 : SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1439 1: << *Field;
1440 1: Invalid = true;
1441 : }
1442 :
3: branch 0 taken
2: branch 1 taken
1443 5: if (Invalid) {
1444 3: ++Index;
1445 3: return true;
1446 : }
1447 :
1448 : // Initialize the array.
1449 2: bool prevHadError = hadError;
1450 2: unsigned newStructuredIndex = FieldIndex;
1451 2: unsigned OldIndex = Index;
1452 2: IList->setInit(Index, DIE->getInit());
1453 :
1454 : InitializedEntity MemberEntity =
1455 2: InitializedEntity::InitializeMember(*Field, &Entity);
1456 : CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1457 2: StructuredList, newStructuredIndex);
1458 :
1459 2: IList->setInit(OldIndex, DIE);
0: branch 0 not taken
2: branch 1 taken
2: branch 2 taken
2: branch 3 taken
1460 2: if (hadError && !prevHadError) {
1461 0: ++Field;
1462 0: ++FieldIndex;
0: branch 0 not taken
0: branch 1 not taken
1463 0: if (NextField)
1464 0: *NextField = Field;
1465 0: StructuredIndex = FieldIndex;
1466 0: return true;
1467 : }
1468 : } else {
1469 : // Recurse to check later designated subobjects.
1470 140: QualType FieldType = (*Field)->getType();
1471 140: unsigned newStructuredIndex = FieldIndex;
1472 :
1473 : InitializedEntity MemberEntity =
1474 140: InitializedEntity::InitializeMember(*Field, &Entity);
2: branch 1 taken
138: branch 2 taken
1475 140: if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1,
1476 : FieldType, 0, 0, Index,
1477 : StructuredList, newStructuredIndex,
1478 : true, false))
1479 2: return true;
1480 : }
1481 :
1482 : // Find the position of the next field to be initialized in this
1483 : // subobject.
1484 140: ++Field;
1485 140: ++FieldIndex;
1486 :
1487 : // If this the first designator, our caller will continue checking
1488 : // the rest of this struct/class/union subobject.
94: branch 0 taken
46: branch 1 taken
1489 140: if (IsFirstDesignator) {
94: branch 0 taken
0: branch 1 not taken
1490 94: if (NextField)
1491 94: *NextField = Field;
1492 94: StructuredIndex = FieldIndex;
1493 94: return false;
1494 : }
1495 :
1: branch 0 taken
45: branch 1 taken
1496 46: if (!FinishSubobjectInit)
1497 1: return false;
1498 :
1499 : // We've already initialized something in the union; we're done.
3: branch 2 taken
42: branch 3 taken
1500 45: if (RT->getDecl()->isUnion())
1501 3: return hadError;
1502 :
1503 : // Check the remaining fields within this class/struct/union subobject.
1504 42: bool prevHadError = hadError;
1505 :
1506 : CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index,
1507 42: StructuredList, FieldIndex);
2: branch 0 taken
40: branch 1 taken
0: branch 2 not taken
2: branch 3 taken
1508 42: return hadError && !prevHadError;
1509 : }
1510 :
1511 : // C99 6.7.8p6:
1512 : //
1513 : // If a designator has the form
1514 : //
1515 : // [ constant-expression ]
1516 : //
1517 : // then the current object (defined below) shall have array
1518 : // type and the expression shall be an integer constant
1519 : // expression. If the array is of unknown size, any
1520 : // nonnegative value is valid.
1521 : //
1522 : // Additionally, cope with the GNU extension that permits
1523 : // designators of the form
1524 : //
1525 : // [ constant-expression ... constant-expression ]
1526 63: const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
1: branch 0 taken
62: branch 1 taken
1527 63: if (!AT) {
1528 : SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
1529 1: << CurrentObjectType;
1530 1: ++Index;
1531 1: return true;
1532 : }
1533 :
1534 62: Expr *IndexExpr = 0;
1535 62: llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
56: branch 1 taken
6: branch 2 taken
1536 62: if (D->isArrayDesignator()) {
1537 56: IndexExpr = DIE->getArrayIndex(*D);
1538 56: DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context);
1539 56: DesignatedEndIndex = DesignatedStartIndex;
1540 : } else {
6: branch 1 taken
0: branch 2 not taken
1541 6: assert(D->isArrayRangeDesignator() && "Need array-range designator");
1542 :
1543 :
1544 : DesignatedStartIndex =
1545 6: DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context);
1546 : DesignatedEndIndex =
1547 6: DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context);
1548 6: IndexExpr = DIE->getArrayRangeEnd(*D);
1549 :
6: branch 2 taken
0: branch 3 not taken
1550 6: if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue())
1551 6: FullyStructuredList->sawArrayRangeDesignator();
1552 : }
1553 :
35: branch 1 taken
27: branch 2 taken
1554 62: if (isa<ConstantArrayType>(AT)) {
1555 35: llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
1556 35: DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
1557 35: DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
1558 35: DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
1559 35: DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
5: branch 1 taken
30: branch 2 taken
1560 35: if (DesignatedEndIndex >= MaxElements) {
1561 : SemaRef.Diag(IndexExpr->getSourceRange().getBegin(),
1562 : diag::err_array_designator_too_large)
1563 : << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
1564 5: << IndexExpr->getSourceRange();
1565 5: ++Index;
1566 5: return true;
30: branch 1 taken
5: branch 2 taken
1567 35: }
1568 : } else {
1569 : // Make sure the bit-widths and signedness match.
0: branch 2 not taken
27: branch 3 taken
1570 27: if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
1571 0: DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
1: branch 2 taken
26: branch 3 taken
1572 27: else if (DesignatedStartIndex.getBitWidth() <
1573 : DesignatedEndIndex.getBitWidth())
1574 1: DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
1575 27: DesignatedStartIndex.setIsUnsigned(true);
1576 27: DesignatedEndIndex.setIsUnsigned(true);
1577 : }
1578 :
1579 : // Make sure that our non-designated initializer list has space
1580 : // for a subobject corresponding to this array element.
47: branch 2 taken
10: branch 3 taken
1581 57: if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
1582 : StructuredList->resizeInits(SemaRef.Context,
1583 47: DesignatedEndIndex.getZExtValue() + 1);
1584 :
1585 : // Repeatedly perform subobject initializations in the range
1586 : // [DesignatedStartIndex, DesignatedEndIndex].
1587 :
1588 : // Move to the next designator
1589 57: unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
1590 57: unsigned OldIndex = Index;
1591 :
1592 : InitializedEntity ElementEntity =
1593 57: InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
1594 :
66: branch 1 taken
55: branch 2 taken
1595 178: while (DesignatedStartIndex <= DesignatedEndIndex) {
1596 : // Recurse to check later designated subobjects.
1597 66: QualType ElementType = AT->getElementType();
1598 66: Index = OldIndex;
1599 :
1600 66: ElementEntity.setElementIndex(ElementIndex);
2: branch 2 taken
64: branch 3 taken
1601 66: if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1,
1602 : ElementType, 0, 0, Index,
1603 : StructuredList, ElementIndex,
1604 : (DesignatedStartIndex == DesignatedEndIndex),
1605 : false))
1606 2: return true;
1607 :
1608 : // Move to the next index in the array that we'll be initializing.
1609 64: ++DesignatedStartIndex;
1610 64: ElementIndex = DesignatedStartIndex.getZExtValue();
1611 : }
1612 :
1613 : // If this the first designator, our caller will continue checking
1614 : // the rest of this array subobject.
48: branch 0 taken
7: branch 1 taken
1615 55: if (IsFirstDesignator) {
48: branch 0 taken
0: branch 1 not taken
1616 48: if (NextElementIndex)
1617 48: *NextElementIndex = DesignatedStartIndex;
1618 48: StructuredIndex = ElementIndex;
1619 48: return false;
1620 : }
1621 :
0: branch 0 not taken
7: branch 1 taken
1622 7: if (!FinishSubobjectInit)
1623 0: return false;
1624 :
1625 : // Check the remaining elements within this array subobject.
1626 7: bool prevHadError = hadError;
1627 : CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex,
1628 : /*SubobjectIsDesignatorContext=*/false, Index,
1629 7: StructuredList, ElementIndex);
0: branch 0 not taken
7: branch 1 taken
7: branch 2 taken
7: branch 3 taken
1630 7: return hadError && !prevHadError;
1631 : }
1632 :
1633 : // Get the structured initializer list for a subobject of type
1634 : // @p CurrentObjectType.
1635 : InitListExpr *
1636 : InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
1637 : QualType CurrentObjectType,
1638 : InitListExpr *StructuredList,
1639 : unsigned StructuredIndex,
1640 1018: SourceRange InitRange) {
1641 1018: Expr *ExistingInit = 0;
747: branch 0 taken
271: branch 1 taken
1642 1018: if (!StructuredList)
1643 747: ExistingInit = SyntacticToSemantic[IList];
80: branch 1 taken
191: branch 2 taken
1644 271: else if (StructuredIndex < StructuredList->getNumInits())
1645 80: ExistingInit = StructuredList->getInit(StructuredIndex);
1646 :
9: branch 1 taken
1009: branch 2 taken
1647 1018: if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
1648 9: return Result;
1649 :
2: branch 0 taken
1007: branch 1 taken
1650 1009: if (ExistingInit) {
1651 : // We are creating an initializer list that initializes the
1652 : // subobjects of the current object, but there was already an
1653 : // initialization that completely initialized the current
1654 : // subobject, e.g., by a compound literal:
1655 : //
1656 : // struct X { int a, b; };
1657 : // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
1658 : //
1659 : // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
1660 : // designated initializer re-initializes the whole
1661 : // subobject [0], overwriting previous initializers.
1662 : SemaRef.Diag(InitRange.getBegin(),
1663 : diag::warn_subobject_initializer_overrides)
1664 2: << InitRange;
1665 : SemaRef.Diag(ExistingInit->getSourceRange().getBegin(),
1666 : diag::note_previous_initializer)
1667 : << /*FIXME:has side effects=*/0
1668 2: << ExistingInit->getSourceRange();
1669 : }
1670 :
1671 : InitListExpr *Result
1672 : = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0,
1009: branch 3 taken
0: branch 4 not taken
1673 1009: InitRange.getEnd());
1674 :
1675 1009: Result->setType(CurrentObjectType.getNonReferenceType());
1676 :
1677 : // Pre-allocate storage for the structured initializer list.
1678 1009: unsigned NumElements = 0;
1679 1009: unsigned NumInits = 0;
747: branch 0 taken
262: branch 1 taken
1680 1009: if (!StructuredList)
1681 747: NumInits = IList->getNumInits();
262: branch 1 taken
0: branch 2 not taken
1682 262: else if (Index < IList->getNumInits()) {
133: branch 2 taken
129: branch 3 taken
1683 262: if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
1684 133: NumInits = SubList->getNumInits();
1685 : }
1686 :
382: branch 0 taken
627: branch 1 taken
1687 1009: if (const ArrayType *AType
1688 1009: = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
235: branch 1 taken
147: branch 2 taken
1689 382: if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
1690 235: NumElements = CAType->getSize().getZExtValue();
1691 : // Simple heuristic so that we don't allocate a very large
1692 : // initializer with many empty entries at the end.
181: branch 0 taken
54: branch 1 taken
84: branch 2 taken
97: branch 3 taken
1693 235: if (NumInits && NumElements > NumInits)
1694 84: NumElements = 0;
1695 : }
219: branch 2 taken
408: branch 3 taken
1696 627: } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
1697 219: NumElements = VType->getNumElements();
385: branch 2 taken
23: branch 3 taken
1698 408: else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
1699 385: RecordDecl *RDecl = RType->getDecl();
34: branch 1 taken
351: branch 2 taken
1700 385: if (RDecl->isUnion())
1701 34: NumElements = 1;
1702 : else
1703 : NumElements = std::distance(RDecl->field_begin(),
1704 351: RDecl->field_end());
1705 : }
1706 :
274: branch 0 taken
735: branch 1 taken
1707 1009: if (NumElements < NumInits)
1708 274: NumElements = IList->getNumInits();
1709 :
1710 1009: Result->reserveInits(NumElements);
1711 :
1712 : // Link this new initializer list into the structured initializer
1713 : // lists.
262: branch 0 taken
747: branch 1 taken
1714 1009: if (StructuredList)
1715 262: StructuredList->updateInit(StructuredIndex, Result);
1716 : else {
1717 747: Result->setSyntacticForm(IList);
1718 747: SyntacticToSemantic[IList] = Result;
1719 : }
1720 :
1721 1009: return Result;
1722 : }
1723 :
1724 : /// Update the initializer at index @p StructuredIndex within the
1725 : /// structured initializer list to the value @p expr.
1726 : void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
1727 : unsigned &StructuredIndex,
1728 2208: Expr *expr) {
1729 : // No structured initializer list to update
0: branch 0 not taken
2208: branch 1 taken
1730 2208: if (!StructuredList)
1731 0: return;
1732 :
3: branch 1 taken
2205: branch 2 taken
1733 2208: if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) {
1734 : // This initializer overwrites a previous initializer. Warn.
1735 : SemaRef.Diag(expr->getSourceRange().getBegin(),
1736 : diag::warn_initializer_overrides)
1737 3: << expr->getSourceRange();
1738 : SemaRef.Diag(PrevInit->getSourceRange().getBegin(),
1739 : diag::note_previous_initializer)
1740 : << /*FIXME:has side effects=*/0
1741 3: << PrevInit->getSourceRange();
1742 : }
1743 :
1744 2208: ++StructuredIndex;
1745 : }
1746 :
1747 : /// Check that the given Index expression is a valid array designator
1748 : /// value. This is essentailly just a wrapper around
1749 : /// VerifyIntegerConstantExpression that also checks for negative values
1750 : /// and produces a reasonable diagnostic if there is a
1751 : /// failure. Returns true if there was an error, false otherwise. If
1752 : /// everything went okay, Value will receive the value of the constant
1753 : /// expression.
1754 : static bool
1755 81: CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
1756 81: SourceLocation Loc = Index->getSourceRange().getBegin();
1757 :
1758 : // Make sure this is an integer constant expression.
0: branch 1 not taken
81: branch 2 taken
1759 81: if (S.VerifyIntegerConstantExpression(Index, &Value))
1760 0: return true;
1761 :
79: branch 1 taken
2: branch 2 taken
1: branch 4 taken
78: branch 5 taken
1: branch 6 taken
80: branch 7 taken
1762 81: if (Value.isSigned() && Value.isNegative())
1763 : return S.Diag(Loc, diag::err_array_designator_negative)
1764 1: << Value.toString(10) << Index->getSourceRange();
1765 :
1766 80: Value.setIsUnsigned(true);
1767 80: return false;
1768 : }
1769 :
1770 : Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
1771 : SourceLocation Loc,
1772 : bool GNUSyntax,
1773 168: OwningExprResult Init) {
1774 : typedef DesignatedInitExpr::Designator ASTDesignator;
1775 :
1776 168: bool Invalid = false;
1777 168: llvm::SmallVector<ASTDesignator, 32> Designators;
1778 168: llvm::SmallVector<Expr *, 32> InitExpressions;
1779 :
1780 : // Build designators and check array designator expressions.
221: branch 1 taken
168: branch 2 taken
1781 389: for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
1782 221: const Designator &D = Desig.getDesignator(Idx);
146: branch 1 taken
65: branch 2 taken
10: branch 3 taken
0: branch 4 not taken
1783 221: switch (D.getKind()) {
1784 : case Designator::FieldDesignator:
1785 : Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
1786 146: D.getFieldLoc()));
1787 146: break;
1788 :
1789 : case Designator::ArrayDesignator: {
1790 65: Expr *Index = static_cast<Expr *>(D.getArrayIndex());
1791 65: llvm::APSInt IndexValue;
65: branch 1 taken
0: branch 2 not taken
63: branch 4 taken
2: branch 5 taken
1: branch 7 taken
62: branch 8 taken
1: branch 9 taken
64: branch 10 taken
1792 65: if (!Index->isTypeDependent() &&
1793 : !Index->isValueDependent() &&
1794 : CheckArrayDesignatorExpr(*this, Index, IndexValue))
1795 1: Invalid = true;
1796 : else {
1797 : Designators.push_back(ASTDesignator(InitExpressions.size(),
1798 : D.getLBracketLoc(),
1799 64: D.getRBracketLoc()));
1800 64: InitExpressions.push_back(Index);
1801 : }
1802 65: break;
1803 : }
1804 :
1805 : case Designator::ArrayRangeDesignator: {
1806 10: Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
1807 10: Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
1808 10: llvm::APSInt StartValue;
1809 10: llvm::APSInt EndValue;
1810 : bool StartDependent = StartIndex->isTypeDependent() ||
10: branch 1 taken
0: branch 2 not taken
1: branch 4 taken
9: branch 5 taken
1811 10: StartIndex->isValueDependent();
1812 : bool EndDependent = EndIndex->isTypeDependent() ||
10: branch 1 taken
0: branch 2 not taken
1: branch 4 taken
9: branch 5 taken
1813 10: EndIndex->isValueDependent();
9: branch 0 taken
1: branch 1 taken
9: branch 3 taken
0: branch 4 not taken
9: branch 5 taken
1: branch 6 taken
0: branch 8 not taken
9: branch 9 taken
0: branch 10 not taken
10: branch 11 taken
1814 10: if ((!StartDependent &&
1815 : CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) ||
1816 : (!EndDependent &&
1817 : CheckArrayDesignatorExpr(*this, EndIndex, EndValue)))
1818 0: Invalid = true;
1819 : else {
1820 : // Make sure we're comparing values with the same bit width.
9: branch 0 taken
1: branch 1 taken
9: branch 2 taken
0: branch 3 not taken
1821 10: if (StartDependent || EndDependent) {
1822 : // Nothing to compute.
0: branch 2 not taken
9: branch 3 taken
1823 9: } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
1824 0: EndValue.extend(StartValue.getBitWidth());
1: branch 2 taken
8: branch 3 taken
1825 9: else if (StartValue.getBitWidth() < EndValue.getBitWidth())
1826 1: StartValue.extend(EndValue.getBitWidth());
1827 :
9: branch 0 taken
1: branch 1 taken
9: branch 2 taken
0: branch 3 not taken
1: branch 5 taken
8: branch 6 taken
1: branch 7 taken
9: branch 8 taken
1828 10: if (!StartDependent && !EndDependent && EndValue < StartValue) {
1829 : Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
1830 : << StartValue.toString(10) << EndValue.toString(10)
1831 1: << StartIndex->getSourceRange() << EndIndex->getSourceRange();
1832 1: Invalid = true;
1833 : } else {
1834 : Designators.push_back(ASTDesignator(InitExpressions.size(),
1835 : D.getLBracketLoc(),
1836 : D.getEllipsisLoc(),
1837 9: D.getRBracketLoc()));
1838 9: InitExpressions.push_back(StartIndex);
1839 9: InitExpressions.push_back(EndIndex);
1840 : }
1841 : }
1842 10: break;
1843 : }
1844 : }
1845 : }
1846 :
166: branch 0 taken
2: branch 1 taken
2: branch 3 taken
164: branch 4 taken
4: branch 5 taken
164: branch 6 taken
1847 168: if (Invalid || Init.isInvalid())
1848 4: return ExprError();
1849 :
1850 : // Clear out the expressions within the designation.
1851 164: Desig.ClearExprs(*this);
1852 :
1853 : DesignatedInitExpr *DIE
1854 : = DesignatedInitExpr::Create(Context,
1855 : Designators.data(), Designators.size(),
1856 : InitExpressions.data(), InitExpressions.size(),
1857 164: Loc, GNUSyntax, Init.takeAs<Expr>());
1858 164: return Owned(DIE);
1859 : }
1860 :
1861 : bool Sema::CheckInitList(const InitializedEntity &Entity,
1862 747: InitListExpr *&InitList, QualType &DeclType) {
1863 747: InitListChecker CheckInitList(*this, Entity, InitList, DeclType);
702: branch 1 taken
45: branch 2 taken
1864 747: if (!CheckInitList.HadError())
1865 702: InitList = CheckInitList.getFullyStructuredList();
1866 :
1867 747: return CheckInitList.HadError();
1868 : }
1869 :
1870 : //===----------------------------------------------------------------------===//
1871 : // Initialization entity
1872 : //===----------------------------------------------------------------------===//
1873 :
1874 : InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
1875 1811: const InitializedEntity &Parent)
1876 1811: : Parent(&Parent), Index(Index)
1877 : {
1374: branch 2 taken
437: branch 3 taken
0: branch 6 not taken
0: branch 7 not taken
1878 1811: if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) {
1879 1374: Kind = EK_ArrayElement;
1880 1374: Type = AT->getElementType();
1881 : } else {
1882 437: Kind = EK_VectorElement;
1883 437: Type = Parent.getType()->getAs<VectorType>()->getElementType();
1884 : }
1885 1811: }
1886 :
1887 : InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context,
1888 531: CXXBaseSpecifier *Base)
1889 : {
1890 531: InitializedEntity Result;
1891 531: Result.Kind = EK_Base;
1892 531: Result.Base = Base;
1893 531: Result.Type = Base->getType();
1894 : return Result;
1895 : }
1896 :
1897 6: DeclarationName InitializedEntity::getName() const {
0: branch 1 not taken
6: branch 2 taken
0: branch 3 not taken
0: branch 4 not taken
1898 6: switch (getKind()) {
1899 : case EK_Parameter:
0: branch 0 not taken
0: branch 1 not taken
1900 0: if (!VariableOrMember)
1901 0: return DeclarationName();
1902 : // Fall through
1903 :
1904 : case EK_Variable:
1905 : case EK_Member:
1906 6: return VariableOrMember->getDeclName();
1907 :
1908 : case EK_Result:
1909 : case EK_Exception:
1910 : case EK_New:
1911 : case EK_Temporary:
1912 : case EK_Base:
1913 : case EK_ArrayElement:
1914 : case EK_VectorElement:
1915 0: return DeclarationName();
1916 : }
1917 :
1918 : // Silence GCC warning
1919 0: return DeclarationName();
1920 : }
1921 :
1922 10707: DeclaratorDecl *InitializedEntity::getDecl() const {
10423: branch 1 taken
284: branch 2 taken
0: branch 3 not taken
1923 10707: switch (getKind()) {
1924 : case EK_Variable:
1925 : case EK_Parameter:
1926 : case EK_Member:
1927 10423: return VariableOrMember;
1928 :
1929 : case EK_Result:
1930 : case EK_Exception:
1931 : case EK_New:
1932 : case EK_Temporary:
1933 : case EK_Base:
1934 : case EK_ArrayElement:
1935 : case EK_VectorElement:
1936 284: return 0;
1937 : }
1938 :
1939 : // Silence GCC warning
1940 0: return 0;
1941 : }
1942 :
1943 : //===----------------------------------------------------------------------===//
1944 : // Initialization sequence
1945 : //===----------------------------------------------------------------------===//
1946 :
1947 23335: void InitializationSequence::Step::Destroy() {
19031: branch 0 taken
4304: branch 1 taken
0: branch 2 not taken
1948 23335: switch (Kind) {
1949 : case SK_ResolveAddressOfOverloadedFunction:
1950 : case SK_CastDerivedToBaseRValue:
1951 : case SK_CastDerivedToBaseLValue:
1952 : case SK_BindReference:
1953 : case SK_BindReferenceToTemporary:
1954 : case SK_UserConversion:
1955 : case SK_QualificationConversionRValue:
1956 : case SK_QualificationConversionLValue:
1957 : case SK_ListInitialization:
1958 : case SK_ConstructorInitialization:
1959 : case SK_ZeroInitialization:
1960 : case SK_CAssignment:
1961 : case SK_StringInit:
1962 19031: break;
1963 :
1964 : case SK_ConversionSequence:
4304: branch 0 taken
0: branch 1 not taken
1965 4304: delete ICS;
1966 : }
1967 23335: }
1968 :
1969 : void InitializationSequence::AddAddressOverloadResolutionStep(
1970 14: FunctionDecl *Function) {
1971 14: Step S;
1972 14: S.Kind = SK_ResolveAddressOfOverloadedFunction;
1973 14: S.Type = Function->getType();
1974 : // Access is currently ignored for these.
1975 14: S.Function = DeclAccessPair::make(Function, AccessSpecifier(0));
1976 14: Steps.push_back(S);
1977 14: }
1978 :
1979 : void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
1980 55: bool IsLValue) {
1981 55: Step S;
35: branch 0 taken
20: branch 1 taken
1982 55: S.Kind = IsLValue? SK_CastDerivedToBaseLValue : SK_CastDerivedToBaseRValue;
1983 55: S.Type = BaseType;
1984 55: Steps.push_back(S);
1985 55: }
1986 :
1987 : void InitializationSequence::AddReferenceBindingStep(QualType T,
1988 1314: bool BindingTemporary) {
1989 1314: Step S;
372: branch 0 taken
942: branch 1 taken
1990 1314: S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
1991 1314: S.Type = T;
1992 1314: Steps.push_back(S);
1993 1314: }
1994 :
1995 : void InitializationSequence::AddUserConversionStep(FunctionDecl *Function,
1996 : AccessSpecifier Access,
1997 506: QualType T) {
1998 506: Step S;
1999 506: S.Kind = SK_UserConversion;
2000 506: S.Type = T;
2001 506: S.Function = DeclAccessPair::make(Function, Access);
2002 506: Steps.push_back(S);
2003 506: }
2004 :
2005 : void InitializationSequence::AddQualificationConversionStep(QualType Ty,
2006 596: bool IsLValue) {
2007 596: Step S;
2008 : S.Kind = IsLValue? SK_QualificationConversionLValue
285: branch 0 taken
311: branch 1 taken
2009 596: : SK_QualificationConversionRValue;
2010 596: S.Type = Ty;
2011 596: Steps.push_back(S);
2012 596: }
2013 :
2014 : void InitializationSequence::AddConversionSequenceStep(
2015 : const ImplicitConversionSequence &ICS,
2016 4304: QualType T) {
2017 4304: Step S;
2018 4304: S.Kind = SK_ConversionSequence;
2019 4304: S.Type = T;
2020 4304: S.ICS = new ImplicitConversionSequence(ICS);
2021 4304: Steps.push_back(S);
2022 4304: }
2023 :
2024 752: void InitializationSequence::AddListInitializationStep(QualType T) {
2025 752: Step S;
2026 752: S.Kind = SK_ListInitialization;
2027 752: S.Type = T;
2028 752: Steps.push_back(S);
2029 752: }
2030 :
2031 : void
2032 : InitializationSequence::AddConstructorInitializationStep(
2033 : CXXConstructorDecl *Constructor,
2034 : AccessSpecifier Access,
2035 1408: QualType T) {
2036 1408: Step S;
2037 1408: S.Kind = SK_ConstructorInitialization;
2038 1408: S.Type = T;
2039 1408: S.Function = DeclAccessPair::make(Constructor, Access);
2040 1408: Steps.push_back(S);
2041 1408: }
2042 :
2043 1039: void InitializationSequence::AddZeroInitializationStep(QualType T) {
2044 1039: Step S;
2045 1039: S.Kind = SK_ZeroInitialization;
2046 1039: S.Type = T;
2047 1039: Steps.push_back(S);
2048 1039: }
2049 :
2050 13301: void InitializationSequence::AddCAssignmentStep(QualType T) {
2051 13301: Step S;
2052 13301: S.Kind = SK_CAssignment;
2053 13301: S.Type = T;
2054 13301: Steps.push_back(S);
2055 13301: }
2056 :
2057 46: void InitializationSequence::AddStringInitStep(QualType T) {
2058 46: Step S;
2059 46: S.Kind = SK_StringInit;
2060 46: S.Type = T;
2061 46: Steps.push_back(S);
2062 46: }
2063 :
2064 : void InitializationSequence::SetOverloadFailure(FailureKind Failure,
2065 90: OverloadingResult Result) {
2066 90: SequenceKind = FailedSequence;
2067 90: this->Failure = Failure;
2068 90: this->FailedOverloadResult = Result;
2069 90: }
2070 :
2071 : //===----------------------------------------------------------------------===//
2072 : // Attempt initialization
2073 : //===----------------------------------------------------------------------===//
2074 :
2075 : /// \brief Attempt list initialization (C++0x [dcl.init.list])
2076 : static void TryListInitialization(Sema &S,
2077 : const InitializedEntity &Entity,
2078 : const InitializationKind &Kind,
2079 : InitListExpr *InitList,
2080 753: InitializationSequence &Sequence) {
2081 : // FIXME: We only perform rudimentary checking of list
2082 : // initializations at this point, then assume that any list
2083 : // initialization of an array, aggregate, or scalar will be
2084 : // well-formed. We we actually "perform" list initialization, we'll
2085 : // do all of the necessary checking. C++0x initializer lists will
2086 : // force us to perform more checking here.
2087 753: Sequence.setSequenceKind(InitializationSequence::ListInitialization);
2088 :
2089 753: QualType DestType = Entity.getType();
2090 :
2091 : // C++ [dcl.init]p13:
2092 : // If T is a scalar type, then a declaration of the form
2093 : //
2094 : // T x = { a };
2095 : //
2096 : // is equivalent to
2097 : //
2098 : // T x = a;
15: branch 2 taken
738: branch 3 taken
2099 753: if (DestType->isScalarType()) {
1: branch 1 taken
14: branch 2 taken
0: branch 4 not taken
1: branch 5 taken
0: branch 6 not taken
15: branch 7 taken
2100 15: if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) {
2101 0: Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
2102 0: return;
2103 : }
2104 :
2105 : // Assume scalar initialization from a single value works.
219: branch 2 taken
519: branch 3 taken
2106 738: } else if (DestType->isAggregateType()) {
2107 : // Assume aggregate initialization works.
7: branch 2 taken
212: branch 3 taken
2108 219: } else if (DestType->isVectorType()) {
2109 : // Assume vector initialization works.
1: branch 2 taken
6: branch 3 taken
2110 7: } else if (DestType->isReferenceType()) {
2111 : // FIXME: C++0x defines behavior for this.
2112 1: Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
2113 1: return;
5: branch 2 taken
1: branch 3 taken
2114 6: } else if (DestType->isRecordType()) {
2115 : // FIXME: C++0x defines behavior for this
2116 5: Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType);
2117 : }
2118 :
2119 : // Add a general "list initialization" step.
2120 752: Sequence.AddListInitializationStep(DestType);
2121 : }
2122 :
2123 : /// \brief Try a reference initialization that involves calling a conversion
2124 : /// function.
2125 : ///
2126 : /// FIXME: look intos DRs 656, 896
2127 : static OverloadingResult TryRefInitWithConversionFunction(Sema &S,
2128 : const InitializedEntity &Entity,
2129 : const InitializationKind &Kind,
2130 : Expr *Initializer,
2131 : bool AllowRValues,
2132 94: InitializationSequence &Sequence) {
2133 94: QualType DestType = Entity.getType();
2134 94: QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
2135 94: QualType T1 = cv1T1.getUnqualifiedType();
2136 94: QualType cv2T2 = Initializer->getType();
2137 94: QualType T2 = cv2T2.getUnqualifiedType();
2138 :
2139 : bool DerivedToBase;
2140 : assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
2141 : T1, T2, DerivedToBase) &&
94: branch 2 taken
0: branch 3 not taken
2142 94: "Must have incompatible references when binding via conversion");
2143 : (void)DerivedToBase;
2144 :
2145 : // Build the candidate set directly in the initialization sequence
2146 : // structure, so that it will persist if we fail.
2147 94: OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2148 94: CandidateSet.clear();
2149 :
2150 : // Determine whether we are allowed to call explicit constructors or
2151 : // explicit conversion operators.
2152 94: bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
2153 :
2154 94: const RecordType *T1RecordType = 0;
33: branch 0 taken
61: branch 1 taken
33: branch 4 taken
0: branch 5 not taken
33: branch 6 taken
61: branch 7 taken
2155 127: if (AllowRValues && (T1RecordType = T1->getAs<RecordType>())) {
2156 : // The type we're converting to is a class type. Enumerate its constructors
2157 : // to see if there is a suitable conversion.
2158 33: CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
2159 :
2160 : DeclarationName ConstructorName
2161 : = S.Context.DeclarationNames.getCXXConstructorName(
2162 33: S.Context.getCanonicalType(T1).getUnqualifiedType());
2163 : DeclContext::lookup_iterator Con, ConEnd;
67: branch 3 taken
33: branch 4 taken
2164 100: for (llvm::tie(Con, ConEnd) = T1RecordDecl->lookup(ConstructorName);
2165 : Con != ConEnd; ++Con) {
2166 : // Find the constructor (which may be a template).
2167 67: CXXConstructorDecl *Constructor = 0;
2168 : FunctionTemplateDecl *ConstructorTmpl
2169 67: = dyn_cast<FunctionTemplateDecl>(*Con);
1: branch 0 taken
66: branch 1 taken
2170 67: if (ConstructorTmpl)
2171 : Constructor = cast<CXXConstructorDecl>(
2172 1: ConstructorTmpl->getTemplatedDecl());
2173 : else
2174 66: Constructor = cast<CXXConstructorDecl>(*Con);
2175 :
67: branch 1 taken
0: branch 2 not taken
39: branch 4 taken
28: branch 5 taken
39: branch 6 taken
28: branch 7 taken
2176 67: if (!Constructor->isInvalidDecl() &&
2177 : Constructor->isConvertingConstructor(AllowExplicit)) {
1: branch 0 taken
38: branch 1 taken
2178 39: if (ConstructorTmpl)
2179 : S.AddTemplateOverloadCandidate(ConstructorTmpl,
2180 : ConstructorTmpl->getAccess(),
2181 : /*ExplicitArgs*/ 0,
2182 1: &Initializer, 1, CandidateSet);
2183 : else
2184 : S.AddOverloadCandidate(Constructor, Constructor->getAccess(),
2185 38: &Initializer, 1, CandidateSet);
2186 : }
2187 : }
2188 : }
2189 :
94: branch 2 taken
0: branch 3 not taken
2190 94: if (const RecordType *T2RecordType = T2->getAs<RecordType>()) {
2191 : // The type we're converting from is a class type, enumerate its conversion
2192 : // functions.
2193 94: CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
2194 :
2195 : // Determine the type we are converting to. If we are allowed to
2196 : // convert to an rvalue, take the type that the destination type
2197 : // refers to.
33: branch 0 taken
61: branch 1 taken
2198 94: QualType ToType = AllowRValues? cv1T1 : DestType;
2199 :
2200 : const UnresolvedSetImpl *Conversions
2201 94: = T2RecordDecl->getVisibleConversionFunctions();
97: branch 3 taken
94: branch 4 taken
2202 285: for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
2203 94: E = Conversions->end(); I != E; ++I) {
2204 97: NamedDecl *D = *I;
2205 97: CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
0: branch 1 not taken
97: branch 2 taken
2206 97: if (isa<UsingShadowDecl>(D))
2207 0: D = cast<UsingShadowDecl>(D)->getTargetDecl();
2208 :
2209 97: FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
2210 : CXXConversionDecl *Conv;
5: branch 0 taken
92: branch 1 taken
2211 97: if (ConvTemplate)
2212 5: Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
2213 : else
2214 92: Conv = cast<CXXConversionDecl>(*I);
2215 :
2216 : // If the conversion function doesn't return a reference type,
2217 : // it can't be considered for this conversion unless we're allowed to
2218 : // consider rvalues.
2219 : // FIXME: Do we need to make sure that we only consider conversion
2220 : // candidates with reference-compatible results? That might be needed to
2221 : // break recursion.
96: branch 0 taken
1: branch 1 taken
94: branch 3 taken
2: branch 4 taken
63: branch 5 taken
32: branch 6 taken
24: branch 10 taken
39: branch 11 taken
56: branch 12 taken
41: branch 13 taken
2222 97: if ((AllowExplicit || !Conv->isExplicit()) &&
2223 : (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
2: branch 0 taken
54: branch 1 taken
2224 56: if (ConvTemplate)
2225 : S.AddTemplateConversionCandidate(ConvTemplate, I.getAccess(),
2226 : ActingDC, Initializer,
2227 2: ToType, CandidateSet);
2228 : else
2229 : S.AddConversionCandidate(Conv, I.getAccess(), ActingDC,
2230 54: Initializer, cv1T1, CandidateSet);
2231 : }
2232 : }
2233 : }
2234 :
2235 94: SourceLocation DeclLoc = Initializer->getLocStart();
2236 :
2237 : // Perform overload resolution. If it fails, return the failed result.
2238 : OverloadCandidateSet::iterator Best;
50: branch 0 taken
44: branch 1 taken
2239 94: if (OverloadingResult Result
2240 94: = S.BestViableFunction(CandidateSet, DeclLoc, Best))
2241 50: return Result;
2242 :
2243 44: FunctionDecl *Function = Best->Function;
2244 :
2245 : // Compute the returned type of the conversion.
41: branch 1 taken
3: branch 2 taken
2246 44: if (isa<CXXConversionDecl>(Function))
2247 41: T2 = Function->getResultType();
2248 : else
2249 3: T2 = cv1T1;
2250 :
2251 : // Add the user-defined conversion step.
2252 : Sequence.AddUserConversionStep(Function, Best->getAccess(),
2253 44: T2.getNonReferenceType());
2254 :
2255 : // Determine whether we need to perform derived-to-base or
2256 : // cv-qualification adjustments.
2257 44: bool NewDerivedToBase = false;
2258 : Sema::ReferenceCompareResult NewRefRelationship
2259 : = S.CompareReferenceRelationship(DeclLoc, T1, T2.getNonReferenceType(),
2260 44: NewDerivedToBase);
2261 : assert(NewRefRelationship != Sema::Ref_Incompatible &&
0: branch 0 not taken
44: branch 1 taken
2262 44: "Overload resolution picked a bad conversion function");
2263 : (void)NewRefRelationship;
5: branch 0 taken
39: branch 1 taken
2264 44: if (NewDerivedToBase)
2265 : Sequence.AddDerivedToBaseCastStep(
2266 : S.Context.getQualifiedType(T1,
2267 : T2.getNonReferenceType().getQualifiers()),
2268 5: /*isLValue=*/true);
2269 :
30: branch 4 taken
14: branch 5 taken
2270 44: if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
2271 30: Sequence.AddQualificationConversionStep(cv1T1, T2->isReferenceType());
2272 :
2273 44: Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
2274 44: return OR_Success;
2275 : }
2276 :
2277 : /// \brief Attempt reference initialization (C++0x [dcl.init.list])
2278 : static void TryReferenceInitialization(Sema &S,
2279 : const InitializedEntity &Entity,
2280 : const InitializationKind &Kind,
2281 : Expr *Initializer,
2282 1360: InitializationSequence &Sequence) {
2283 1360: Sequence.setSequenceKind(InitializationSequence::ReferenceBinding);
2284 :
2285 1360: QualType DestType = Entity.getType();
2286 1360: QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
2287 1360: Qualifiers T1Quals;
2288 1360: QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
2289 1360: QualType cv2T2 = Initializer->getType();
2290 1360: Qualifiers T2Quals;
2291 1360: QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
2292 1360: SourceLocation DeclLoc = Initializer->getLocStart();
2293 :
2294 : // If the initializer is the address of an overloaded function, try
2295 : // to resolve the overloaded function. If all goes well, T2 is the
2296 : // type of the resulting function.
14: branch 2 taken
1346: branch 3 taken
2297 1360: if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) {
2298 : FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer,
2299 : T1,
2300 14: false);
0: branch 0 not taken
14: branch 1 taken
2301 14: if (!Fn) {
2302 0: Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
2303 0: return;
2304 : }
2305 :
2306 14: Sequence.AddAddressOverloadResolutionStep(Fn);
2307 14: cv2T2 = Fn->getType();
2308 14: T2 = cv2T2.getUnqualifiedType();
2309 : }
2310 :
2311 : // FIXME: Rvalue references
2312 1360: bool ForceRValue = false;
2313 :
2314 : // Compute some basic properties of the types and the initializer.
2315 1360: bool isLValueRef = DestType->isLValueReferenceType();
2316 1360: bool isRValueRef = !isLValueRef;
2317 1360: bool DerivedToBase = false;
2318 : Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression :
0: branch 0 not taken
1360: branch 1 taken
2319 1360: Initializer->isLvalue(S.Context);
2320 : Sema::ReferenceCompareResult RefRelationship
2321 1360: = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase);
2322 :
2323 : // C++0x [dcl.init.ref]p5:
2324 : // A reference to type "cv1 T1" is initialized by an expression of type
2325 : // "cv2 T2" as follows:
2326 : //
2327 : // - If the reference is an lvalue reference and the initializer
2328 : // expression
2329 1360: OverloadingResult ConvOvlResult = OR_Success;
1347: branch 0 taken
13: branch 1 taken
2330 1360: if (isLValueRef) {
993: branch 0 taken
354: branch 1 taken
933: branch 2 taken
60: branch 3 taken
2331 1347: if (InitLvalue == Expr::LV_Valid &&
2332 : RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
2333 : // - is an lvalue (but is not a bit-field), and "cv1 T1" is
2334 : // reference-compatible with "cv2 T2," or
2335 : //
2336 : // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a
2337 : // bit-field when we're determining whether the reference initialization
2338 : // can occur. However, we do pay attention to whether it is a bit-field
2339 : // to decide whether we're actually binding to a temporary created from
2340 : // the bit-field.
30: branch 0 taken
903: branch 1 taken
2341 933: if (DerivedToBase)
2342 : Sequence.AddDerivedToBaseCastStep(
2343 : S.Context.getQualifiedType(T1, T2Quals),
2344 30: /*isLValue=*/true);
280: branch 1 taken
653: branch 2 taken
2345 933: if (T1Quals != T2Quals)
2346 280: Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/true);
2347 : bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() &&
382: branch 1 taken
551: branch 2 taken
379: branch 4 taken
3: branch 5 taken
376: branch 7 taken
3: branch 8 taken
3: branch 10 taken
373: branch 11 taken
2348 933: (Initializer->getBitField() || Initializer->refersToVectorElement());
2349 933: Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary);
2350 933: return;
2351 : }
2352 :
2353 : // - has a class type (i.e., T2 is a class type), where T1 is not
2354 : // reference-related to T2, and can be implicitly converted to an
2355 : // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
2356 : // with "cv3 T3" (this conversion is selected by enumerating the
2357 : // applicable conversion functions (13.3.1.6) and choosing the best
2358 : // one through overload resolution (13.3)),
90: branch 0 taken
324: branch 1 taken
61: branch 4 taken
29: branch 5 taken
61: branch 6 taken
353: branch 7 taken
2359 414: if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType()) {
2360 : ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind,
2361 : Initializer,
2362 : /*AllowRValues=*/false,
2363 61: Sequence);
14: branch 0 taken
47: branch 1 taken
2364 61: if (ConvOvlResult == OR_Success)
2365 14: return;
4: branch 0 taken
43: branch 1 taken
2366 47: if (ConvOvlResult != OR_No_Viable_Function) {
2367 : Sequence.SetOverloadFailure(
2368 : InitializationSequence::FK_ReferenceInitOverloadFailed,
2369 4: ConvOvlResult);
2370 : }
2371 : }
2372 : }
2373 :
2374 : // - Otherwise, the reference shall be an lvalue reference to a
2375 : // non-volatile const type (i.e., cv1 shall be const), or the reference
2376 : // shall be an rvalue reference and the initializer expression shall
2377 : // be an rvalue.
400: branch 0 taken
13: branch 1 taken
367: branch 3 taken
33: branch 4 taken
3: branch 6 taken
364: branch 7 taken
13: branch 8 taken
36: branch 9 taken
3: branch 10 taken
10: branch 11 taken
39: branch 12 taken
374: branch 13 taken
2378 413: if (!((isLValueRef && T1Quals.hasConst() && !T1Quals.hasVolatile()) ||
2379 : (isRValueRef && InitLvalue != Expr::LV_Valid))) {
9: branch 0 taken
30: branch 1 taken
2: branch 4 taken
7: branch 5 taken
2: branch 6 taken
37: branch 7 taken
2380 39: if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
2381 : Sequence.SetOverloadFailure(
2382 : InitializationSequence::FK_ReferenceInitOverloadFailed,
2383 2: ConvOvlResult);
34: branch 0 taken
3: branch 1 taken
2384 37: else if (isLValueRef)
2385 : Sequence.SetFailed(InitLvalue == Expr::LV_Valid
2386 : ? (RefRelationship == Sema::Ref_Related
2387 : ? InitializationSequence::FK_ReferenceInitDropsQualifiers
2388 : : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
24: branch 0 taken
10: branch 1 taken
6: branch 2 taken
18: branch 3 taken
2389 34: : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
2390 : else
2391 : Sequence.SetFailed(
2392 3: InitializationSequence::FK_RValueReferenceBindingToLValue);
2393 :
2394 39: return;
2395 : }
2396 :
2397 : // - If T1 and T2 are class types and
332: branch 2 taken
42: branch 3 taken
324: branch 6 taken
8: branch 7 taken
324: branch 8 taken
50: branch 9 taken
2398 374: if (T1->isRecordType() && T2->isRecordType()) {
2399 : // - the initializer expression is an rvalue and "cv1 T1" is
2400 : // reference-compatible with "cv2 T2", or
307: branch 0 taken
17: branch 1 taken
289: branch 2 taken
18: branch 3 taken
2401 324: if (InitLvalue != Expr::LV_Valid &&
2402 : RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) {
20: branch 0 taken
269: branch 1 taken
2403 289: if (DerivedToBase)
2404 : Sequence.AddDerivedToBaseCastStep(
2405 : S.Context.getQualifiedType(T1, T2Quals),
2406 20: /*isLValue=*/false);
286: branch 1 taken
3: branch 2 taken
2407 289: if (T1Quals != T2Quals)
2408 286: Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/false);
2409 289: Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
2410 289: return;
2411 : }
2412 :
2413 : // - T1 is not reference-related to T2 and the initializer expression
2414 : // can be implicitly converted to an rvalue of type "cv3 T3" (this
2415 : // conversion is selected by enumerating the applicable conversion
2416 : // functions (13.3.1.6) and choosing the best one through overload
2417 : // resolution (13.3)),
33: branch 0 taken
2: branch 1 taken
2418 35: if (RefRelationship == Sema::Ref_Incompatible) {
2419 : ConvOvlResult = TryRefInitWithConversionFunction(S, Entity,
2420 : Kind, Initializer,
2421 : /*AllowRValues=*/true,
2422 33: Sequence);
3: branch 0 taken
30: branch 1 taken
2423 33: if (ConvOvlResult)
2424 : Sequence.SetOverloadFailure(
2425 : InitializationSequence::FK_ReferenceInitOverloadFailed,
2426 3: ConvOvlResult);
2427 :
2428 33: return;
2429 : }
2430 :
2431 2: Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2432 2: return;
2433 : }
2434 :
2435 : // - If the initializer expression is an rvalue, with T2 an array type,
2436 : // and "cv1 T1" is reference-compatible with "cv2 T2," the reference
2437 : // is bound to the object represented by the rvalue (see 3.10).
2438 : // FIXME: How can an array type be reference-compatible with anything?
2439 : // Don't we mean the element types of T1 and T2?
2440 :
2441 : // - Otherwise, a temporary of type “cv1 T1” is created and initialized
2442 : // from the initializer expression using the rules for a non-reference
2443 : // copy initialization (8.5). The reference is then bound to the
2444 : // temporary. [...]
2445 : // Determine whether we are allowed to call explicit constructors or
2446 : // explicit conversion operators.
2447 50: bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct);
2448 : ImplicitConversionSequence ICS
2449 : = S.TryImplicitConversion(Initializer, cv1T1,
2450 : /*SuppressUserConversions=*/false, AllowExplicit,
2451 : /*ForceRValue=*/false,
2452 : /*FIXME:InOverloadResolution=*/false,
2453 50: /*UserCast=*/Kind.isExplicitCast());
2454 :
1: branch 1 taken
49: branch 2 taken
2455 50: if (ICS.isBad()) {
2456 : // FIXME: Use the conversion function set stored in ICS to turn
2457 : // this into an overloading ambiguity diagnostic. However, we need
2458 : // to keep that set as an OverloadCandidateSet rather than as some
2459 : // other kind of set.
0: branch 0 not taken
1: branch 1 taken
0: branch 4 not taken
0: branch 5 not taken
0: branch 6 not taken
1: branch 7 taken
2460 1: if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
2461 : Sequence.SetOverloadFailure(
2462 : InitializationSequence::FK_ReferenceInitOverloadFailed,
2463 0: ConvOvlResult);
2464 : else
2465 1: Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
2466 : return;
2467 : }
2468 :
2469 : // [...] If T1 is reference-related to T2, cv1 must be the
2470 : // same cv-qualification as, or greater cv-qualification
2471 : // than, cv2; otherwise, the program is ill-formed.
2472 49: unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
2473 49: unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
1: branch 0 taken
48: branch 1 taken
1: branch 2 taken
0: branch 3 not taken
2474 49: if (RefRelationship == Sema::Ref_Related &&
2475 : (T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
2476 1: Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
2477 : return;
2478 : }
2479 :
2480 : // Perform the actual conversion.
2481 48: Sequence.AddConversionSequenceStep(ICS, cv1T1);
2482 48: Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
2483 50: return;
2484 : }
2485 :
2486 : /// \brief Attempt character array initialization from a string literal
2487 : /// (C++ [dcl.init.string], C99 6.7.8).
2488 : static void TryStringLiteralInitialization(Sema &S,
2489 : const InitializedEntity &Entity,
2490 : const InitializationKind &Kind,
2491 : Expr *Initializer,
2492 46: InitializationSequence &Sequence) {
2493 46: Sequence.setSequenceKind(InitializationSequence::StringInit);
2494 46: Sequence.AddStringInitStep(Entity.getType());
2495 46: }
2496 :
2497 : /// \brief Attempt initialization by constructor (C++ [dcl.init]), which
2498 : /// enumerates the constructors of the initialized entity and performs overload
2499 : /// resolution to select the best.
2500 : static void TryConstructorInitialization(Sema &S,
2501 : const InitializedEntity &Entity,
2502 : const InitializationKind &Kind,
2503 : Expr **Args, unsigned NumArgs,
2504 : QualType DestType,
2505 1806: InitializationSequence &Sequence) {
359: branch 1 taken
1447: branch 2 taken
2506 1806: if (Kind.getKind() == InitializationKind::IK_Copy)
2507 359: Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
2508 : else
2509 1447: Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization);
2510 :
2511 : // Build the candidate set directly in the initialization sequence
2512 : // structure, so that it will persist if we fail.
2513 1806: OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2514 1806: CandidateSet.clear();
2515 :
2516 : // Determine whether we are allowed to call explicit constructors or
2517 : // explicit conversion operators.
2518 : bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct ||
2519 : Kind.getKind() == InitializationKind::IK_Value ||
1599: branch 1 taken
207: branch 2 taken
1520: branch 4 taken
79: branch 5 taken
1161: branch 7 taken
359: branch 8 taken
2520 1806: Kind.getKind() == InitializationKind::IK_Default);
2521 :
2522 : // The type we're converting to is a class type. Enumerate its constructors
2523 : // to see if one is suitable.
2524 1806: const RecordType *DestRecordType = DestType->getAs<RecordType>();
0: branch 0 not taken
1806: branch 1 taken
2525 1806: assert(DestRecordType && "Constructor initialization requires record type");
2526 : CXXRecordDecl *DestRecordDecl
2527 1806: = cast<CXXRecordDecl>(DestRecordType->getDecl());
2528 :
2529 : DeclarationName ConstructorName
2530 : = S.Context.DeclarationNames.getCXXConstructorName(
2531 1806: S.Context.getCanonicalType(DestType).getUnqualifiedType());
2532 : DeclContext::lookup_iterator Con, ConEnd;
3731: branch 3 taken
1806: branch 4 taken
2533 5537: for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName);
2534 : Con != ConEnd; ++Con) {
2535 : // Find the constructor (which may be a template).
2536 3731: CXXConstructorDecl *Constructor = 0;
2537 : FunctionTemplateDecl *ConstructorTmpl
2538 3731: = dyn_cast<FunctionTemplateDecl>(*Con);
25: branch 0 taken
3706: branch 1 taken
2539 3731: if (ConstructorTmpl)
2540 : Constructor = cast<CXXConstructorDecl>(
2541 25: ConstructorTmpl->getTemplatedDecl());
2542 : else
2543 3706: Constructor = cast<CXXConstructorDecl>(*Con);
2544 :
3729: branch 1 taken
2: branch 2 taken
738: branch 3 taken
2991: branch 4 taken
731: branch 6 taken
7: branch 7 taken
3722: branch 8 taken
9: branch 9 taken
2545 3731: if (!Constructor->isInvalidDecl() &&
2546 : (AllowExplicit || !Constructor->isExplicit())) {
25: branch 0 taken
3697: branch 1 taken
2547 3722: if (ConstructorTmpl)
2548 : S.AddTemplateOverloadCandidate(ConstructorTmpl,
2549 : ConstructorTmpl->getAccess(),
2550 : /*ExplicitArgs*/ 0,
2551 25: Args, NumArgs, CandidateSet);
2552 : else
2553 : S.AddOverloadCandidate(Constructor, Constructor->getAccess(),
2554 3697: Args, NumArgs, CandidateSet);
2555 : }
2556 : }
2557 :
2558 1806: SourceLocation DeclLoc = Kind.getLocation();
2559 :
2560 : // Perform overload resolution. If it fails, return the failed result.
2561 : OverloadCandidateSet::iterator Best;
46: branch 0 taken
1760: branch 1 taken
2562 1806: if (OverloadingResult Result
2563 1806: = S.BestViableFunction(CandidateSet, DeclLoc, Best)) {
2564 : Sequence.SetOverloadFailure(
2565 : InitializationSequence::FK_ConstructorOverloadFailed,
2566 46: Result);
2567 46: return;
2568 : }
2569 :
2570 : // C++0x [dcl.init]p6:
2571 : // If a program calls for the default initialization of an object
2572 : // of a const-qualified type T, T shall be a class type with a
2573 : // user-provided default constructor.
1146: branch 1 taken
614: branch 2 taken
5: branch 5 taken
1141: branch 6 taken
2: branch 9 taken
3: branch 10 taken
2: branch 11 taken
1758: branch 12 taken
2574 1760: if (Kind.getKind() == InitializationKind::IK_Default &&
2575 : Entity.getType().isConstQualified() &&
2576 : cast<CXXConstructorDecl>(Best->Function)->isImplicit()) {
2577 2: Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2578 2: return;
2579 : }
2580 :
2581 : // Add the constructor initialization step. Any cv-qualification conversion is
2582 : // subsumed by the initialization.
350: branch 1 taken
1408: branch 2 taken
2583 1758: if (Kind.getKind() == InitializationKind::IK_Copy) {
2584 350: Sequence.AddUserConversionStep(Best->Function, Best->getAccess(), DestType);
2585 : } else {
2586 : Sequence.AddConstructorInitializationStep(
2587 : cast<CXXConstructorDecl>(Best->Function),
2588 : Best->getAccess(),
2589 1408: DestType);
2590 : }
2591 : }
2592 :
2593 : /// \brief Attempt value initialization (C++ [dcl.init]p7).
2594 : static void TryValueInitialization(Sema &S,
2595 : const InitializedEntity &Entity,
2596 : const InitializationKind &Kind,
2597 1135: InitializationSequence &Sequence) {
2598 : // C++ [dcl.init]p5:
2599 : //
2600 : // To value-initialize an object of type T means:
2601 1135: QualType T = Entity.getType();
2602 :
2603 : // -- if T is an array type, then each element is value-initialized;
47: branch 1 taken
1135: branch 2 taken
2604 1229: while (const ArrayType *AT = S.Context.getAsArrayType(T))
2605 47: T = AT->getElementType();
2606 :
179: branch 2 taken
956: branch 3 taken
2607 1135: if (const RecordType *RT = T->getAs<RecordType>()) {
138: branch 2 taken
41: branch 3 taken
2608 179: if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
2609 : // -- if T is a class type (clause 9) with a user-declared
2610 : // constructor (12.1), then the default constructor for T is
2611 : // called (and the initialization is ill-formed if T has no
2612 : // accessible default constructor);
2613 : //
2614 : // FIXME: we really want to refer to a single subobject of the array,
2615 : // but Entity doesn't have a way to capture that (yet).
96: branch 1 taken
42: branch 2 taken
2616 138: if (ClassDecl->hasUserDeclaredConstructor())
2617 96: return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
2618 :
2619 : // -- if T is a (possibly cv-qualified) non-union class type
2620 : // without a user-provided constructor, then the object is
2621 : // zero-initialized and, if T’s implicitly-declared default
2622 : // constructor is non-trivial, that constructor is called.
33: branch 1 taken
9: branch 2 taken
33: branch 4 taken
0: branch 5 not taken
8: branch 7 taken
34: branch 8 taken
8: branch 9 taken
34: branch 10 taken
2623 42: if ((ClassDecl->getTagKind() == TagDecl::TK_class ||
2624 : ClassDecl->getTagKind() == TagDecl::TK_struct) &&
2625 : !ClassDecl->hasTrivialConstructor()) {
2626 8: Sequence.AddZeroInitializationStep(Entity.getType());
2627 8: return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
2628 : }
2629 : }
2630 : }
2631 :
2632 1031: Sequence.AddZeroInitializationStep(Entity.getType());
2633 1031: Sequence.setSequenceKind(InitializationSequence::ZeroInitialization);
2634 : }
2635 :
2636 : /// \brief Attempt default initialization (C++ [dcl.init]p6).
2637 : static void TryDefaultInitialization(Sema &S,
2638 : const InitializedEntity &Entity,
2639 : const InitializationKind &Kind,
2640 3681: InitializationSequence &Sequence) {
0: branch 1 not taken
3681: branch 2 taken
2641 3681: assert(Kind.getKind() == InitializationKind::IK_Default);
2642 :
2643 : // C++ [dcl.init]p6:
2644 : // To default-initialize an object of type T means:
2645 : // - if T is an array type, each element is default-initialized;
2646 3681: QualType DestType = Entity.getType();
689: branch 1 taken
3681: branch 2 taken
2647 5059: while (const ArrayType *Array = S.Context.getAsArrayType(DestType))
2648 689: DestType = Array->getElementType();
2649 :
2650 : // - if T is a (possibly cv-qualified) class type (Clause 9), the default
2651 : // constructor for T is called (and the initialization is ill-formed if
2652 : // T has no accessible default constructor);
1392: branch 2 taken
2289: branch 3 taken
1161: branch 5 taken
231: branch 6 taken
1161: branch 7 taken
2520: branch 8 taken
2653 3681: if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) {
2654 : return TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType,
2655 1161: Sequence);
2656 : }
2657 :
2658 : // - otherwise, no initialization is performed.
2659 2520: Sequence.setSequenceKind(InitializationSequence::NoInitialization);
2660 :
2661 : // If a program calls for the default initialization of an object of
2662 : // a const-qualified type T, T shall be a class type with a user-provided
2663 : // default constructor.
14: branch 1 taken
2506: branch 2 taken
2: branch 4 taken
12: branch 5 taken
2: branch 6 taken
2518: branch 7 taken
2664 2520: if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus)
2665 2: Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
2666 : }
2667 :
2668 : /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
2669 : /// which enumerates all conversion functions and performs overload resolution
2670 : /// to select the best.
2671 : static void TryUserDefinedConversion(Sema &S,
2672 : const InitializedEntity &Entity,
2673 : const InitializationKind &Kind,
2674 : Expr *Initializer,
2675 147: InitializationSequence &Sequence) {
2676 147: Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion);
2677 :
2678 147: QualType DestType = Entity.getType();
147: branch 2 taken
0: branch 3 not taken
2679 147: assert(!DestType->isReferenceType() && "References are handled elsewhere");
2680 147: QualType SourceType = Initializer->getType();
2681 : assert((DestType->isRecordType() || SourceType->isRecordType()) &&
39: branch 2 taken
108: branch 3 taken
39: branch 6 taken
0: branch 7 not taken
2682 186: "Must have a class type to perform a user-defined conversion");
2683 :
2684 : // Build the candidate set directly in the initialization sequence
2685 : // structure, so that it will persist if we fail.
2686 147: OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
2687 147: CandidateSet.clear();
2688 :
2689 : // Determine whether we are allowed to call explicit constructors or
2690 : // explicit conversion operators.
2691 147: bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct;
2692 :
108: branch 2 taken
39: branch 3 taken
2693 147: if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
2694 : // The type we're converting to is a class type. Enumerate its constructors
2695 : // to see if there is a suitable conversion.
2696 : CXXRecordDecl *DestRecordDecl
2697 108: = cast<CXXRecordDecl>(DestRecordType->getDecl());
2698 :
2699 : DeclarationName ConstructorName
2700 : = S.Context.DeclarationNames.getCXXConstructorName(
2701 108: S.Context.getCanonicalType(DestType).getUnqualifiedType());
2702 : DeclContext::lookup_iterator Con, ConEnd;
268: branch 3 taken
108: branch 4 taken
2703 376: for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName);
2704 : Con != ConEnd; ++Con) {
2705 : // Find the constructor (which may be a template).
2706 268: CXXConstructorDecl *Constructor = 0;
2707 : FunctionTemplateDecl *ConstructorTmpl
2708 268: = dyn_cast<FunctionTemplateDecl>(*Con);
20: branch 0 taken
248: branch 1 taken
2709 268: if (ConstructorTmpl)
2710 : Constructor = cast<CXXConstructorDecl>(
2711 20: ConstructorTmpl->getTemplatedDecl());
2712 : else
2713 248: Constructor = cast<CXXConstructorDecl>(*Con);
2714 :
267: branch 1 taken
1: branch 2 taken
197: branch 4 taken
70: branch 5 taken
197: branch 6 taken
71: branch 7 taken
2715 268: if (!Constructor->isInvalidDecl() &&
2716 : Constructor->isConvertingConstructor(AllowExplicit)) {
8: branch 0 taken
189: branch 1 taken
2717 197: if (ConstructorTmpl)
2718 : S.AddTemplateOverloadCandidate(ConstructorTmpl,
2719 : ConstructorTmpl->getAccess(),
2720 : /*ExplicitArgs*/ 0,
2721 8: &Initializer, 1, CandidateSet);
2722 : else
2723 : S.AddOverloadCandidate(Constructor, Constructor->getAccess(),
2724 189: &Initializer, 1, CandidateSet);
2725 : }
2726 : }
2727 : }
2728 :
2729 147: SourceLocation DeclLoc = Initializer->getLocStart();
2730 :
68: branch 2 taken
79: branch 3 taken
2731 147: if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
2732 : // The type we're converting from is a class type, enumerate its conversion
2733 : // functions.
2734 :
2735 : // We can only enumerate the conversion functions for a complete type; if
2736 : // the type isn't complete, simply skip this step.
67: branch 8 taken
1: branch 9 taken
2737 68: if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
2738 : CXXRecordDecl *SourceRecordDecl
2739 67: = cast<CXXRecordDecl>(SourceRecordType->getDecl());
2740 :
2741 : const UnresolvedSetImpl *Conversions
2742 67: = SourceRecordDecl->getVisibleConversionFunctions();
78: branch 3 taken
67: branch 4 taken
2743 212: for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
2744 67: E = Conversions->end();
2745 : I != E; ++I) {
2746 78: NamedDecl *D = *I;
2747 78: CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
0: branch 1 not taken
78: branch 2 taken
2748 78: if (isa<UsingShadowDecl>(D))
2749 0: D = cast<UsingShadowDecl>(D)->getTargetDecl();
2750 :
2751 78: FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
2752 : CXXConversionDecl *Conv;
11: branch 0 taken
67: branch 1 taken
2753 78: if (ConvTemplate)
2754 11: Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
2755 : else
2756 67: Conv = cast<CXXConversionDecl>(*I);
2757 :
77: branch 0 taken
1: branch 1 taken
75: branch 3 taken
2: branch 4 taken
76: branch 5 taken
2: branch 6 taken
2758 78: if (AllowExplicit || !Conv->isExplicit()) {
11: branch 0 taken
65: branch 1 taken
2759 76: if (ConvTemplate)
2760 : S.AddTemplateConversionCandidate(ConvTemplate, I.getAccess(),
2761 : ActingDC, Initializer, DestType,
2762 11: CandidateSet);
2763 : else
2764 : S.AddConversionCandidate(Conv, I.getAccess(), ActingDC,
2765 65: Initializer, DestType, CandidateSet);
2766 : }
2767 : }
2768 : }
2769 : }
2770 :
2771 : // Perform overload resolution. If it fails, return the failed result.
2772 : OverloadCandidateSet::iterator Best;
35: branch 0 taken
112: branch 1 taken
2773 147: if (OverloadingResult Result
2774 147: = S.BestViableFunction(CandidateSet, DeclLoc, Best)) {
2775 : Sequence.SetOverloadFailure(
2776 : InitializationSequence::FK_UserConversionOverloadFailed,
2777 35: Result);
2778 35: return;
2779 : }
2780 :
2781 112: FunctionDecl *Function = Best->Function;
2782 :
65: branch 1 taken
47: branch 2 taken
2783 112: if (isa<CXXConstructorDecl>(Function)) {
2784 : // Add the user-defined conversion step. Any cv-qualification conversion is
2785 : // subsumed by the initialization.
2786 65: Sequence.AddUserConversionStep(Function, Best->getAccess(), DestType);
2787 65: return;
2788 : }
2789 :
2790 : // Add the user-defined conversion step that calls the conversion function.
2791 47: QualType ConvType = Function->getResultType().getNonReferenceType();
2792 47: Sequence.AddUserConversionStep(Function, Best->getAccess(), ConvType);
2793 :
2794 : // If the conversion following the call to the conversion function is
2795 : // interesting, add it as a separate step.
46: branch 0 taken
1: branch 1 taken
38: branch 2 taken
8: branch 3 taken
1: branch 4 taken
37: branch 5 taken
2796 47: if (Best->FinalConversion.First || Best->FinalConversion.Second ||
2797 : Best->FinalConversion.Third) {
2798 10: ImplicitConversionSequence ICS;
2799 10: ICS.setStandard();
2800 10: ICS.Standard = Best->FinalConversion;
2801 10: Sequence.AddConversionSequenceStep(ICS, DestType);
2802 : }
2803 : }
2804 :
2805 : /// \brief Attempt an implicit conversion (C++ [conv]) converting from one
2806 : /// non-class type to another.
2807 : static void TryImplicitConversion(Sema &S,
2808 : const InitializedEntity &Entity,
2809 : const InitializationKind &Kind,
2810 : Expr *Initializer,
2811 4284: InitializationSequence &Sequence) {
2812 : ImplicitConversionSequence ICS
2813 : = S.TryImplicitConversion(Initializer, Entity.getType(),
2814 : /*SuppressUserConversions=*/true,
2815 : /*AllowExplicit=*/false,
2816 : /*ForceRValue=*/false,
2817 : /*FIXME:InOverloadResolution=*/false,
2818 4284: /*UserCast=*/Kind.isExplicitCast());
2819 :
38: branch 1 taken
4246: branch 2 taken
2820 4284: if (ICS.isBad()) {
2821 38: Sequence.SetFailed(InitializationSequence::FK_ConversionFailed);
2822 38: return;
2823 : }
2824 :
4246: branch 3 taken
38: branch 4 taken
2825 4246: Sequence.AddConversionSequenceStep(ICS, Entity.getType());
2826 : }
2827 :
2828 : InitializationSequence::InitializationSequence(Sema &S,
2829 : const InitializedEntity &Entity,
2830 : const InitializationKind &Kind,
2831 : Expr **Args,
2832 25385: unsigned NumArgs)
2833 25385: : FailedCandidateSet(Kind.getLocation()) {
2834 25385: ASTContext &Context = S.Context;
2835 :
2836 : // C++0x [dcl.init]p16:
2837 : // The semantics of initializers are as follows. The destination type is
2838 : // the type of the object or reference being initialized and the source
2839 : // type is the type of the initializer expression. The source type is not
2840 : // defined when the initializer is a braced-init-list or when it is a
2841 : // parenthesized list of expressions.
2842 25385: QualType DestType = Entity.getType();
2843 :
25286: branch 2 taken
99: branch 3 taken
20: branch 5 taken
25266: branch 6 taken
119: branch 7 taken
25266: branch 8 taken
0: branch 11 not taken
0: branch 12 not taken
0: branch 14 not taken
0: branch 15 not taken
0: branch 16 not taken
0: branch 17 not taken
2844 25385: if (DestType->isDependentType() ||
2845 : Expr::hasAnyTypeDependentArguments(Args, NumArgs)) {
2846 119: SequenceKind = DependentSequence;
2847 119: return;
2848 : }
2849 :
2850 25266: QualType SourceType;
2851 25266: Expr *Initializer = 0;
20369: branch 0 taken
4897: branch 1 taken
4897: branch 2 taken
4897: branch 3 taken
2852 25266: if (NumArgs == 1) {
2853 20369: Initializer = Args[0];
19616: branch 1 taken
753: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
2854 20369: if (!isa<InitListExpr>(Initializer))
2855 19616: SourceType = Initializer->getType();
2856 : }
2857 :
2858 : // - If the initializer is a braced-init-list, the object is
2859 : // list-initialized (8.5.4).
753: branch 1 taken
24513: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
2860 25266: if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
2861 753: TryListInitialization(S, Entity, Kind, InitList, *this);
2862 753: return;
2863 : }
2864 :
2865 : // - If the destination type is a reference type, see 8.5.3.
1361: branch 2 taken
23152: branch 3 taken
0: branch 6 not taken
0: branch 7 not taken
2866 24513: if (DestType->isReferenceType()) {
2867 : // C++0x [dcl.init.ref]p1:
2868 : // A variable declared to be a T& or T&&, that is, "reference to type T"
2869 : // (8.3.2), shall be initialized by an object, or function, of type T or
2870 : // by an object that can be converted into a T.
2871 : // (Therefore, multiple arguments are not permitted.)
1: branch 0 taken
1360: branch 1 taken
1360: branch 2 taken
1360: branch 3 taken
2872 1361: if (NumArgs != 1)
2873 1: SetFailed(FK_TooManyInitsForReference);
2874 : else
2875 1360: TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
2876 1361: return;
2877 : }
2878 :
2879 : // - If the destination type is an array of characters, an array of
2880 : // char16_t, an array of char32_t, or an array of wchar_t, and the
2881 : // initializer is a string literal, see 8.5.2.
18256: branch 0 taken
4896: branch 1 taken
46: branch 3 taken
18210: branch 4 taken
46: branch 5 taken
23106: branch 6 taken
23106: branch 7 taken
23106: branch 8 taken
0: branch 10 not taken
0: branch 11 not taken
0: branch 12 not taken
0: branch 13 not taken
2882 23152: if (Initializer && IsStringInit(Initializer, DestType, Context)) {
2883 46: TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
2884 46: return;
2885 : }
2886 :
2887 : // - If the initializer is (), the object is value-initialized.
22034: branch 1 taken
1072: branch 2 taken
461: branch 4 taken
21573: branch 5 taken
63: branch 6 taken
398: branch 7 taken
1135: branch 8 taken
21971: branch 9 taken
0: branch 11 not taken
0: branch 12 not taken
0: branch 14 not taken
0: branch 15 not taken
0: branch 16 not taken
0: branch 17 not taken
0: branch 18 not taken
0: branch 19 not taken
2888 23106: if (Kind.getKind() == InitializationKind::IK_Value ||
2889 : (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) {
2890 1135: TryValueInitialization(S, Entity, Kind, *this);
2891 1135: return;
2892 : }
2893 :
2894 : // Handle default initialization.
3681: branch 1 taken
18290: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
2895 21971: if (Kind.getKind() == InitializationKind::IK_Default){
2896 3681: TryDefaultInitialization(S, Entity, Kind, *this);
2897 3681: return;
2898 : }
2899 :
2900 : // - Otherwise, if the destination type is an array, the program is
2901 : // ill-formed.
15: branch 1 taken
18275: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
2902 18290: if (const ArrayType *AT = Context.getAsArrayType(DestType)) {
3: branch 3 taken
12: branch 4 taken
0: branch 8 not taken
0: branch 9 not taken
2903 15: if (AT->getElementType()->isAnyCharacterType())
2904 3: SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
2905 : else
2906 12: SetFailed(FK_ArrayNeedsInitList);
2907 :
2908 15: return;
2909 : }
2910 :
2911 : // Handle initialization in C
13301: branch 1 taken
4974: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
2912 18275: if (!S.getLangOptions().CPlusPlus) {
2913 13301: setSequenceKind(CAssignment);
2914 13301: AddCAssignmentStep(DestType);
2915 13301: return;
2916 : }
2917 :
2918 : // - If the destination type is a (possibly cv-qualified) class type:
649: branch 2 taken
4325: branch 3 taken
0: branch 6 not taken
0: branch 7 not taken
2919 4974: if (DestType->isRecordType()) {
2920 : // - If the initialization is direct-initialization, or if it is
2921 : // copy-initialization where the cv-unqualified version of the
2922 : // source type is the same class as, or a derived class of, the
2923 : // class of the destination, constructors are considered. [...]
467: branch 1 taken
182: branch 2 taken
467: branch 4 taken
0: branch 5 not taken
120: branch 7 taken
347: branch 8 taken
12: branch 10 taken
108: branch 11 taken
541: branch 12 taken
108: branch 13 taken
0: branch 15 not taken
0: branch 16 not taken
0: branch 18 not taken
0: branch 19 not taken
0: branch 21 not taken
0: branch 22 not taken
0: branch 24 not taken
0: branch 25 not taken
0: branch 26 not taken
0: branch 27 not taken
2924 649: if (Kind.getKind() == InitializationKind::IK_Direct ||
2925 : (Kind.getKind() == InitializationKind::IK_Copy &&
2926 : (Context.hasSameUnqualifiedType(SourceType, DestType) ||
2927 : S.IsDerivedFrom(SourceType, DestType))))
2928 : TryConstructorInitialization(S, Entity, Kind, Args, NumArgs,
2929 541: Entity.getType(), *this);
2930 : // - Otherwise (i.e., for the remaining copy-initialization cases),
2931 : // user-defined conversion sequences that can convert from the source
2932 : // type to the destination type or (when a conversion function is
2933 : // used) to a derived class thereof are enumerated as described in
2934 : // 13.3.1.4, and the best one is chosen through overload resolution
2935 : // (13.3).
2936 : else
2937 108: TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
2938 649: return;
2939 : }
2940 :
2: branch 0 taken
4323: branch 1 taken
4323: branch 2 taken
4323: branch 3 taken
2941 4325: if (NumArgs > 1) {
2942 2: SetFailed(FK_TooManyInitsForScalar);
2943 2: return;
2944 : }
0: branch 0 not taken
4323: branch 1 taken
0: branch 3 not taken
0: branch 4 not taken
2945 4323: assert(NumArgs == 1 && "Zero-argument case handled above");
2946 :
2947 : // - Otherwise, if the source type is a (possibly cv-qualified) class
2948 : // type, conversion functions are considered.
4323: branch 1 taken
0: branch 2 not taken
39: branch 5 taken
4284: branch 6 taken
39: branch 7 taken
4284: branch 8 taken
0: branch 10 not taken
0: branch 11 not taken
0: branch 14 not taken
0: branch 15 not taken
0: branch 16 not taken
0: branch 17 not taken
2949 4323: if (!SourceType.isNull() && SourceType->isRecordType()) {
2950 39: TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
2951 39: return;
2952 : }
2953 :
2954 : // - Otherwise, the initial value of the object being initialized is the
2955 : // (possibly converted) value of the initializer expression. Standard
2956 : // conversions (Clause 4) will be used, if necessary, to convert the
2957 : // initializer expression to the cv-unqualified version of the
2958 : // destination type; no user-defined conversions are considered.
2959 4284: setSequenceKind(StandardConversion);
2960 4284: TryImplicitConversion(S, Entity, Kind, Initializer, *this);
2961 : }
2962 :
2963 25385: InitializationSequence::~InitializationSequence() {
23335: branch 1 taken
25385: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
2964 74105: for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(),
2965 25385: StepEnd = Steps.end();
2966 : Step != StepEnd; ++Step)
2967 23335: Step->Destroy();
2968 25385: }
2969 :
2970 : //===----------------------------------------------------------------------===//
2971 : // Perform initialization
2972 : //===----------------------------------------------------------------------===//
2973 : static Sema::AssignmentAction
2974 13301: getAssignmentAction(const InitializedEntity &Entity) {
3252: branch 1 taken
5229: branch 2 taken
3031: branch 3 taken
0: branch 4 not taken
8: branch 5 taken
1781: branch 6 taken
0: branch 7 not taken
2975 13301: switch(Entity.getKind()) {
2976 : case InitializedEntity::EK_Variable:
2977 : case InitializedEntity::EK_New:
2978 3252: return Sema::AA_Initializing;
2979 :
2980 : case InitializedEntity::EK_Parameter:
2981 : // FIXME: Can we tell when we're sending vs. passing?
2982 5229: return Sema::AA_Passing;
2983 :
2984 : case InitializedEntity::EK_Result:
2985 3031: return Sema::AA_Returning;
2986 :
2987 : case InitializedEntity::EK_Exception:
2988 : case InitializedEntity::EK_Base:
2989 0: llvm_unreachable("No assignment action for C++-specific initialization");
2990 : break;
2991 :
2992 : case InitializedEntity::EK_Temporary:
2993 : // FIXME: Can we tell apart casting vs. converting?
2994 8: return Sema::AA_Casting;
2995 :
2996 : case InitializedEntity::EK_Member:
2997 : case InitializedEntity::EK_ArrayElement:
2998 : case InitializedEntity::EK_VectorElement:
2999 1781: return Sema::AA_Initializing;
3000 : }
3001 :
3002 0: return Sema::AA_Converting;
3003 : }
3004 :
3005 : static bool shouldBindAsTemporary(const InitializedEntity &Entity,
3006 1908: bool IsCopy) {
164: branch 1 taken
1403: branch 2 taken
341: branch 3 taken
0: branch 4 not taken
3007 1908: switch (Entity.getKind()) {
3008 : case InitializedEntity::EK_Result:
3009 : case InitializedEntity::EK_ArrayElement:
3010 : case InitializedEntity::EK_Member:
3011 164: return !IsCopy;
3012 :
3013 : case InitializedEntity::EK_New:
3014 : case InitializedEntity::EK_Variable:
3015 : case InitializedEntity::EK_Base:
3016 : case InitializedEntity::EK_VectorElement:
3017 : case InitializedEntity::EK_Exception:
3018 1403: return false;
3019 :
3020 : case InitializedEntity::EK_Parameter:
3021 : case InitializedEntity::EK_Temporary:
3022 341: return true;
3023 : }
3024 :
3025 0: llvm_unreachable("missed an InitializedEntity kind?");
3026 : }
3027 :
3028 : /// \brief If we need to perform an additional copy of the initialized object
3029 : /// for this kind of entity (e.g., the result of a function or an object being
3030 : /// thrown), make the copy.
3031 : static Sema::OwningExprResult CopyIfRequiredForEntity(Sema &S,
3032 : const InitializedEntity &Entity,
3033 : const InitializationKind &Kind,
3034 1552: Sema::OwningExprResult CurInit) {
3035 1552: Expr *CurInitExpr = (Expr *)CurInit.get();
3036 :
3037 1552: SourceLocation Loc;
3038 :
12: branch 1 taken
0: branch 2 not taken
723: branch 3 taken
95: branch 4 taken
62: branch 5 taken
660: branch 6 taken
0: branch 7 not taken
3039 1552: switch (Entity.getKind()) {
3040 : case InitializedEntity::EK_Result:
0: branch 3 not taken
12: branch 4 taken
3041 12: if (Entity.getType()->isReferenceType())
3042 0: return move(CurInit);
3043 12: Loc = Entity.getReturnLoc();
3044 12: break;
3045 :
3046 : case InitializedEntity::EK_Exception:
3047 0: Loc = Entity.getThrowLoc();
3048 0: break;
3049 :
3050 : case InitializedEntity::EK_Variable:
705: branch 3 taken
18: branch 4 taken
650: branch 6 taken