diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -5071,37 +5071,6 @@ NumDesignators(0), NumSubExprs(NumSubExprs), Designators(nullptr) { } public: - /// A field designator, e.g., ".x". - struct FieldDesignator { - /// Refers to the field that is being initialized. The low bit - /// of this field determines whether this is actually a pointer - /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When - /// initially constructed, a field designator will store an - /// IdentifierInfo*. After semantic analysis has resolved that - /// name, the field designator will instead store a FieldDecl*. - uintptr_t NameOrField; - - /// The location of the '.' in the designated initializer. - SourceLocation DotLoc; - - /// The location of the field name in the designated initializer. - SourceLocation FieldLoc; - }; - - /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". - struct ArrayOrRangeDesignator { - /// Location of the first index expression within the designated - /// initializer expression's list of subexpressions. - unsigned Index; - /// The location of the '[' starting the array range designator. - SourceLocation LBracketLoc; - /// The location of the ellipsis separating the start and end - /// indices. Only valid for GNU array-range designators. - SourceLocation EllipsisLoc; - /// The location of the ']' terminating the array range designator. - SourceLocation RBracketLoc; - }; - /// Represents a single C99 designator. /// /// @todo This class is infuriatingly similar to clang::Designator, @@ -5109,118 +5078,184 @@ /// keep us from reusing it. Try harder, later, to rectify these /// differences. class Designator { + /// A field designator, e.g., ".x". + struct FieldDesignatorInfo { + /// Refers to the field that is being initialized. The low bit + /// of this field determines whether this is actually a pointer + /// to an IdentifierInfo (if 1) or a FieldDecl (if 0). When + /// initially constructed, a field designator will store an + /// IdentifierInfo*. After semantic analysis has resolved that + /// name, the field designator will instead store a FieldDecl*. + uintptr_t NameOrField; + + /// The location of the '.' in the designated initializer. + SourceLocation DotLoc; + + /// The location of the field name in the designated initializer. + SourceLocation FieldLoc; + + FieldDesignatorInfo(const IdentifierInfo *II, SourceLocation DotLoc, + SourceLocation FieldLoc) + : NameOrField(reinterpret_cast(II) | 0x1), DotLoc(DotLoc), + FieldLoc(FieldLoc) {} + }; + + /// An array or GNU array-range designator, e.g., "[9]" or "[10...15]". + struct ArrayOrRangeDesignatorInfo { + /// Location of the first index expression within the designated + /// initializer expression's list of subexpressions. + unsigned Index; + + /// The location of the '[' starting the array range designator. + SourceLocation LBracketLoc; + + /// The location of the ellipsis separating the start and end + /// indices. Only valid for GNU array-range designators. + SourceLocation EllipsisLoc; + + /// The location of the ']' terminating the array range designator. + SourceLocation RBracketLoc; + + ArrayOrRangeDesignatorInfo(unsigned Index, SourceLocation LBracketLoc, + SourceLocation RBracketLoc) + : Index(Index), LBracketLoc(LBracketLoc), RBracketLoc(RBracketLoc) {} + + ArrayOrRangeDesignatorInfo(unsigned Index, + SourceLocation LBracketLoc, + SourceLocation EllipsisLoc, + SourceLocation RBracketLoc) + : Index(Index), LBracketLoc(LBracketLoc), EllipsisLoc(EllipsisLoc), + RBracketLoc(RBracketLoc) {} + }; + /// The kind of designator this describes. - enum { + enum DesignatorKind { FieldDesignator, ArrayDesignator, ArrayRangeDesignator - } Kind; + }; + + DesignatorKind Kind; union { /// A field designator, e.g., ".x". - struct FieldDesignator Field; + struct FieldDesignatorInfo FieldInfo; + /// An array or GNU array-range designator, e.g., "[9]" or "[10..15]". - struct ArrayOrRangeDesignator ArrayOrRange; + struct ArrayOrRangeDesignatorInfo ArrayOrRangeInfo; }; - friend class DesignatedInitExpr; + + Designator(DesignatorKind Kind) : Kind(Kind) {} public: Designator() {} - /// Initializes a field designator. - Designator(const IdentifierInfo *FieldName, SourceLocation DotLoc, - SourceLocation FieldLoc) - : Kind(FieldDesignator) { - new (&Field) DesignatedInitExpr::FieldDesignator; - Field.NameOrField = reinterpret_cast(FieldName) | 0x01; - Field.DotLoc = DotLoc; - Field.FieldLoc = FieldLoc; - } - - /// Initializes an array designator. - Designator(unsigned Index, SourceLocation LBracketLoc, - SourceLocation RBracketLoc) - : Kind(ArrayDesignator) { - new (&ArrayOrRange) DesignatedInitExpr::ArrayOrRangeDesignator; - ArrayOrRange.Index = Index; - ArrayOrRange.LBracketLoc = LBracketLoc; - ArrayOrRange.EllipsisLoc = SourceLocation(); - ArrayOrRange.RBracketLoc = RBracketLoc; - } - - /// Initializes a GNU array-range designator. - Designator(unsigned Index, SourceLocation LBracketLoc, - SourceLocation EllipsisLoc, SourceLocation RBracketLoc) - : Kind(ArrayRangeDesignator) { - new (&ArrayOrRange) DesignatedInitExpr::ArrayOrRangeDesignator; - ArrayOrRange.Index = Index; - ArrayOrRange.LBracketLoc = LBracketLoc; - ArrayOrRange.EllipsisLoc = EllipsisLoc; - ArrayOrRange.RBracketLoc = RBracketLoc; - } - bool isFieldDesignator() const { return Kind == FieldDesignator; } bool isArrayDesignator() const { return Kind == ArrayDesignator; } bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } - IdentifierInfo *getFieldName() const; + //===------------------------------------------------------------------===// + // FieldDesignatorInfo + + /// Initializes a field designator. + static Designator CreateFieldDesignator(const IdentifierInfo *FieldName, + SourceLocation DotLoc, + SourceLocation FieldLoc) { + Designator D(FieldDesignator); + new (&D.FieldInfo) FieldDesignatorInfo(FieldName, DotLoc, FieldLoc); + return D; + } + + const IdentifierInfo *getFieldName() const; FieldDecl *getField() const { - assert(Kind == FieldDesignator && "Only valid on a field designator"); - if (Field.NameOrField & 0x01) + assert(isFieldDesignator() && "Only valid on a field designator"); + if (FieldInfo.NameOrField & 0x01) return nullptr; else - return reinterpret_cast(Field.NameOrField); + return reinterpret_cast(FieldInfo.NameOrField); } void setField(FieldDecl *FD) { - assert(Kind == FieldDesignator && "Only valid on a field designator"); - Field.NameOrField = reinterpret_cast(FD); + assert(isFieldDesignator() && "Only valid on a field designator"); + FieldInfo.NameOrField = reinterpret_cast(FD); } SourceLocation getDotLoc() const { - assert(Kind == FieldDesignator && "Only valid on a field designator"); - return Field.DotLoc; + assert(isFieldDesignator() && "Only valid on a field designator"); + return FieldInfo.DotLoc; } SourceLocation getFieldLoc() const { - assert(Kind == FieldDesignator && "Only valid on a field designator"); - return Field.FieldLoc; + assert(isFieldDesignator() && "Only valid on a field designator"); + return FieldInfo.FieldLoc; + } + + //===------------------------------------------------------------------===// + // ArrayOrRangeDesignator + + /// Initializes an array designator. + static Designator CreateArrayDesignator(unsigned Index, + SourceLocation LBracketLoc, + SourceLocation RBracketLoc) { + Designator D(ArrayDesignator); + new (&D.ArrayOrRangeInfo) ArrayOrRangeDesignatorInfo(Index, LBracketLoc, + RBracketLoc); + return D; + } + + /// Initializes a GNU array-range designator. + static Designator CreateArrayRangeDesignator(unsigned Index, + SourceLocation LBracketLoc, + SourceLocation EllipsisLoc, + SourceLocation RBracketLoc) { + Designator D(ArrayRangeDesignator); + new (&D.ArrayOrRangeInfo) ArrayOrRangeDesignatorInfo(Index, LBracketLoc, + EllipsisLoc, + RBracketLoc); + return D; + } + + unsigned getArrayIndex() const { + assert((isArrayDesignator() || isArrayRangeDesignator()) && + "Only valid on an array or array-range designator"); + return ArrayOrRangeInfo.Index; } SourceLocation getLBracketLoc() const { - assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && + assert((isArrayDesignator() || isArrayRangeDesignator()) && "Only valid on an array or array-range designator"); - return ArrayOrRange.LBracketLoc; - } - - SourceLocation getRBracketLoc() const { - assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && - "Only valid on an array or array-range designator"); - return ArrayOrRange.RBracketLoc; + return ArrayOrRangeInfo.LBracketLoc; } SourceLocation getEllipsisLoc() const { - assert(Kind == ArrayRangeDesignator && + assert(isArrayRangeDesignator() && "Only valid on an array-range designator"); - return ArrayOrRange.EllipsisLoc; + return ArrayOrRangeInfo.EllipsisLoc; } - unsigned getFirstExprIndex() const { - assert((Kind == ArrayDesignator || Kind == ArrayRangeDesignator) && + SourceLocation getRBracketLoc() const { + assert((isArrayDesignator() || isArrayRangeDesignator()) && "Only valid on an array or array-range designator"); - return ArrayOrRange.Index; + return ArrayOrRangeInfo.RBracketLoc; + } + + unsigned xgetFirstExprIndex() const { + assert((isArrayDesignator() || isArrayRangeDesignator()) && + "Only valid on an array or array-range designator"); + return ArrayOrRangeInfo.Index; } SourceLocation getBeginLoc() const LLVM_READONLY { - if (Kind == FieldDesignator) - return getDotLoc().isInvalid()? getFieldLoc() : getDotLoc(); - else - return getLBracketLoc(); + if (isFieldDesignator()) + return getDotLoc().isInvalid() ? getFieldLoc() : getDotLoc(); + return getLBracketLoc(); } + SourceLocation getEndLoc() const LLVM_READONLY { - return Kind == FieldDesignator ? getFieldLoc() : getRBracketLoc(); + return isFieldDesignator() ? getFieldLoc() : getRBracketLoc(); } + SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(getBeginLoc(), getEndLoc()); } diff --git a/clang/include/clang/Sema/Designator.h b/clang/include/clang/Sema/Designator.h --- a/clang/include/clang/Sema/Designator.h +++ b/clang/include/clang/Sema/Designator.h @@ -26,52 +26,100 @@ /// Designator - A designator in a C99 designated initializer. /// /// This class is a discriminated union which holds the various -/// different sorts of designators possible. A Designation is an array of +/// different sorts of designators possible. A Designation is an array of /// these. An example of a designator are things like this: -/// [8] .field [47] // C99 designation: 3 designators -/// [8 ... 47] field: // GNU extensions: 2 designators +/// +/// [8] .field [47] // C99 designation: 3 designators +/// [8 ... 47] field: // GNU extensions: 2 designators +/// /// These occur in initializers, e.g.: -/// int a[10] = {2, 4, [8]=9, 10}; +/// +/// int a[10] = {2, 4, [8]=9, 10}; /// class Designator { -public: - enum DesignatorKind { - FieldDesignator, ArrayDesignator, ArrayRangeDesignator - }; -private: - Designator() {}; - - DesignatorKind Kind; - + /// A field designator, e.g., ".x = 42". struct FieldDesignatorInfo { + /// Refers to the field being initialized. const IdentifierInfo *II; + + /// The location of the '.' in the designated initializer. SourceLocation DotLoc; + + /// The location of the field name in the designated initializer. SourceLocation NameLoc; + + FieldDesignatorInfo(const IdentifierInfo *II, SourceLocation DotLoc, + SourceLocation NameLoc) + : II(II), DotLoc(DotLoc), NameLoc(NameLoc) {} }; + + /// An array designator, e.g., "[42] = 0". struct ArrayDesignatorInfo { Expr *Index; + + // The location of the '[' in the designated initializer. SourceLocation LBracketLoc; + + // The location of the ']' in the designated initializer. mutable SourceLocation RBracketLoc; + + ArrayDesignatorInfo(Expr *Index, SourceLocation LBracketLoc) + : Index(Index), LBracketLoc(LBracketLoc) {} }; + + /// An array range designator, e.g. "[42 ... 50] = 1". struct ArrayRangeDesignatorInfo { - Expr *Start, *End; - SourceLocation LBracketLoc, EllipsisLoc; + Expr *Start; + Expr *End; + + // The location of the '[' in the designated initializer. + SourceLocation LBracketLoc; + + // The location of the '...' in the designated initializer. + SourceLocation EllipsisLoc; + + // The location of the ']' in the designated initializer. mutable SourceLocation RBracketLoc; + + ArrayRangeDesignatorInfo(Expr *Start, Expr *End, SourceLocation LBracketLoc, + SourceLocation EllipsisLoc) + : Start(Start), End(End), LBracketLoc(LBracketLoc), + EllipsisLoc(EllipsisLoc) {} }; + /// The kind of designator this describes. + enum DesignatorKind { + FieldDesignator, + ArrayDesignator, + ArrayRangeDesignator + }; + + DesignatorKind Kind; + union { FieldDesignatorInfo FieldInfo; ArrayDesignatorInfo ArrayInfo; ArrayRangeDesignatorInfo ArrayRangeInfo; }; -public: + Designator(DesignatorKind Kind) : Kind(Kind) {} - DesignatorKind getKind() const { return Kind; } +public: bool isFieldDesignator() const { return Kind == FieldDesignator; } bool isArrayDesignator() const { return Kind == ArrayDesignator; } bool isArrayRangeDesignator() const { return Kind == ArrayRangeDesignator; } + //===--------------------------------------------------------------------===// + // FieldDesignatorInfo + + static Designator CreateFieldDesignator(const IdentifierInfo *II, + SourceLocation DotLoc, + SourceLocation NameLoc) { + Designator D(FieldDesignator); + new (&D.FieldInfo) FieldDesignatorInfo(II, DotLoc, NameLoc); + return D; + } + const IdentifierInfo *getField() const { assert(isFieldDesignator() && "Invalid accessor"); return FieldInfo.II; @@ -87,80 +135,62 @@ return FieldInfo.NameLoc; } + //===--------------------------------------------------------------------===// + // ArrayDesignatorInfo: + + static Designator CreateArrayDesignator(Expr *Index, + SourceLocation LBracketLoc) { + Designator D(ArrayDesignator); + new (&D.ArrayInfo) ArrayDesignatorInfo(Index, LBracketLoc); + return D; + } + Expr *getArrayIndex() const { assert(isArrayDesignator() && "Invalid accessor"); return ArrayInfo.Index; } + SourceLocation getLBracketLoc() const { + assert((isArrayDesignator() || isArrayRangeDesignator()) && + "Invalid accessor"); + return isArrayDesignator() ? ArrayInfo.LBracketLoc + : ArrayRangeInfo.LBracketLoc; + } + + SourceLocation getRBracketLoc() const { + assert((isArrayDesignator() || isArrayRangeDesignator()) && + "Invalid accessor"); + return isArrayDesignator() ? ArrayInfo.RBracketLoc + : ArrayRangeInfo.RBracketLoc; + } + + //===--------------------------------------------------------------------===// + // ArrayRangeDesignatorInfo: + + static Designator CreateArrayRangeDesignator(Expr *Start, Expr *End, + SourceLocation LBracketLoc, + SourceLocation EllipsisLoc) { + Designator D(ArrayRangeDesignator); + new (&D.ArrayRangeInfo) + ArrayRangeDesignatorInfo(Start, End, LBracketLoc, EllipsisLoc); + return D; + } + Expr *getArrayRangeStart() const { assert(isArrayRangeDesignator() && "Invalid accessor"); return ArrayRangeInfo.Start; } + Expr *getArrayRangeEnd() const { assert(isArrayRangeDesignator() && "Invalid accessor"); return ArrayRangeInfo.End; } - SourceLocation getLBracketLoc() const { - assert((isArrayDesignator() || isArrayRangeDesignator()) && - "Invalid accessor"); - if (isArrayDesignator()) - return ArrayInfo.LBracketLoc; - else - return ArrayRangeInfo.LBracketLoc; - } - - SourceLocation getRBracketLoc() const { - assert((isArrayDesignator() || isArrayRangeDesignator()) && - "Invalid accessor"); - if (isArrayDesignator()) - return ArrayInfo.RBracketLoc; - else - return ArrayRangeInfo.RBracketLoc; - } - SourceLocation getEllipsisLoc() const { assert(isArrayRangeDesignator() && "Invalid accessor"); return ArrayRangeInfo.EllipsisLoc; } - static Designator getField(const IdentifierInfo *II, SourceLocation DotLoc, - SourceLocation NameLoc) { - Designator D; - D.Kind = FieldDesignator; - new (&D.FieldInfo) FieldDesignatorInfo; - D.FieldInfo.II = II; - D.FieldInfo.DotLoc = DotLoc; - D.FieldInfo.NameLoc = NameLoc; - return D; - } - - static Designator getArray(Expr *Index, - SourceLocation LBracketLoc) { - Designator D; - D.Kind = ArrayDesignator; - new (&D.ArrayInfo) ArrayDesignatorInfo; - D.ArrayInfo.Index = Index; - D.ArrayInfo.LBracketLoc = LBracketLoc; - D.ArrayInfo.RBracketLoc = SourceLocation(); - return D; - } - - static Designator getArrayRange(Expr *Start, - Expr *End, - SourceLocation LBracketLoc, - SourceLocation EllipsisLoc) { - Designator D; - D.Kind = ArrayRangeDesignator; - new (&D.ArrayRangeInfo) ArrayRangeDesignatorInfo; - D.ArrayRangeInfo.Start = Start; - D.ArrayRangeInfo.End = End; - D.ArrayRangeInfo.LBracketLoc = LBracketLoc; - D.ArrayRangeInfo.EllipsisLoc = EllipsisLoc; - D.ArrayRangeInfo.RBracketLoc = SourceLocation(); - return D; - } - void setRBracketLoc(SourceLocation RBracketLoc) const { assert((isArrayDesignator() || isArrayRangeDesignator()) && "Invalid accessor"); @@ -169,17 +199,8 @@ else ArrayRangeInfo.RBracketLoc = RBracketLoc; } - - /// ClearExprs - Null out any expression references, which prevents - /// them from being 'delete'd later. - void ClearExprs(Sema &Actions) {} - - /// FreeExprs - Release any unclaimed memory for the expressions in - /// this designator. - void FreeExprs(Sema &Actions) {} }; - /// Designation - Represent a full designation, which is a sequence of /// designators. This class is mostly a helper for InitListDesignations. class Designation { @@ -188,9 +209,7 @@ public: /// AddDesignator - Add a designator to the end of this list. - void AddDesignator(Designator D) { - Designators.push_back(D); - } + void AddDesignator(Designator D) { Designators.push_back(D); } bool empty() const { return Designators.empty(); } @@ -199,14 +218,6 @@ assert(Idx < Designators.size()); return Designators[Idx]; } - - /// ClearExprs - Null out any expression references, which prevents them from - /// being 'delete'd later. - void ClearExprs(Sema &Actions) {} - - /// FreeExprs - Release any unclaimed memory for the expressions in this - /// designation. - void FreeExprs(Sema &Actions) {} }; } // end namespace clang diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -976,7 +976,8 @@ if (!ToFieldLocOrErr) return ToFieldLocOrErr.takeError(); - return Designator(ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr); + return DesignatedInitExpr::Designator::CreateFieldDesignator( + ToFieldName, *ToDotLocOrErr, *ToFieldLocOrErr); } ExpectedSLoc ToLBracketLocOrErr = import(D.getLBracketLoc()); @@ -988,16 +989,17 @@ return ToRBracketLocOrErr.takeError(); if (D.isArrayDesignator()) - return Designator(D.getFirstExprIndex(), - *ToLBracketLocOrErr, *ToRBracketLocOrErr); + return Designator::CreateArrayDesignator(D.getArrayIndex(), + *ToLBracketLocOrErr, + *ToRBracketLocOrErr); ExpectedSLoc ToEllipsisLocOrErr = import(D.getEllipsisLoc()); if (!ToEllipsisLocOrErr) return ToEllipsisLocOrErr.takeError(); assert(D.isArrayRangeDesignator()); - return Designator( - D.getFirstExprIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr, + return Designator::CreateArrayRangeDesignator( + D.getArrayIndex(), *ToLBracketLocOrErr, *ToEllipsisLocOrErr, *ToRBracketLocOrErr); } diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -4403,10 +4403,10 @@ // DesignatedInitExpr //===----------------------------------------------------------------------===// -IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const { - assert(Kind == FieldDesignator && "Only valid on a field designator"); - if (Field.NameOrField & 0x01) - return reinterpret_cast(Field.NameOrField & ~0x01); +const IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const { + assert(isFieldDesignator() && "Only valid on a field designator"); + if (FieldInfo.NameOrField & 0x01) + return reinterpret_cast(FieldInfo.NameOrField & ~0x01); return getField()->getIdentifier(); } @@ -4482,14 +4482,11 @@ } SourceLocation DesignatedInitExpr::getBeginLoc() const { - SourceLocation StartLoc; auto *DIE = const_cast(this); Designator &First = *DIE->getDesignator(0); if (First.isFieldDesignator()) - StartLoc = GNUSyntax ? First.Field.FieldLoc : First.Field.DotLoc; - else - StartLoc = First.ArrayOrRange.LBracketLoc; - return StartLoc; + return GNUSyntax ? First.getFieldLoc() : First.getDotLoc(); + return First.getLBracketLoc(); } SourceLocation DesignatedInitExpr::getEndLoc() const { @@ -4497,20 +4494,18 @@ } Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const { - assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); - return getSubExpr(D.ArrayOrRange.Index + 1); + assert(D.isArrayDesignator() && "Requires array designator"); + return getSubExpr(D.getArrayIndex() + 1); } Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const { - assert(D.Kind == Designator::ArrayRangeDesignator && - "Requires array range designator"); - return getSubExpr(D.ArrayOrRange.Index + 1); + assert(D.isArrayRangeDesignator() && "Requires array range designator"); + return getSubExpr(D.getArrayIndex() + 1); } Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const { - assert(D.Kind == Designator::ArrayRangeDesignator && - "Requires array range designator"); - return getSubExpr(D.ArrayOrRange.Index + 2); + assert(D.isArrayRangeDesignator() && "Requires array range designator"); + return getSubExpr(D.getArrayIndex() + 2); } /// Replaces the designator at index @p Idx with the series diff --git a/clang/lib/AST/StmtPrinter.cpp b/clang/lib/AST/StmtPrinter.cpp --- a/clang/lib/AST/StmtPrinter.cpp +++ b/clang/lib/AST/StmtPrinter.cpp @@ -1740,7 +1740,7 @@ for (const DesignatedInitExpr::Designator &D : Node->designators()) { if (D.isFieldDesignator()) { if (D.getDotLoc().isInvalid()) { - if (IdentifierInfo *II = D.getFieldName()) { + if (const IdentifierInfo *II = D.getFieldName()) { OS << II->getName() << ":"; NeedsEquals = false; } diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp --- a/clang/lib/AST/StmtProfile.cpp +++ b/clang/lib/AST/StmtProfile.cpp @@ -1530,7 +1530,7 @@ assert(D.isArrayRangeDesignator()); ID.AddInteger(2); } - ID.AddInteger(D.getFirstExprIndex()); + ID.AddInteger(D.getArrayIndex()); } } diff --git a/clang/lib/Parse/ParseInit.cpp b/clang/lib/Parse/ParseInit.cpp --- a/clang/lib/Parse/ParseInit.cpp +++ b/clang/lib/Parse/ParseInit.cpp @@ -181,7 +181,8 @@ NewSyntax); Designation D; - D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); + D.AddDesignator(Designator::CreateFieldDesignator( + FieldName, SourceLocation(), NameLoc)); PreferredType.enterDesignatedInitializer( Tok.getLocation(), DesignatorCompletion.PreferredBaseType, D); return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, @@ -210,8 +211,8 @@ return ExprError(); } - Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, - Tok.getLocation())); + Desig.AddDesignator(Designator::CreateFieldDesignator( + Tok.getIdentifierInfo(), DotLoc, Tok.getLocation())); ConsumeToken(); // Eat the identifier. continue; } @@ -360,7 +361,8 @@ // If this is a normal array designator, remember it. if (Tok.isNot(tok::ellipsis)) { - Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc)); + Desig.AddDesignator(Designator::CreateArrayDesignator(Idx.get(), + StartLoc)); } else { // Handle the gnu array range extension. Diag(Tok, diag::ext_gnu_array_range); @@ -371,9 +373,8 @@ SkipUntil(tok::r_square, StopAtSemi); return RHS; } - Desig.AddDesignator(Designator::getArrayRange(Idx.get(), - RHS.get(), - StartLoc, EllipsisLoc)); + Desig.AddDesignator(Designator::CreateArrayRangeDesignator( + Idx.get(), RHS.get(), StartLoc, EllipsisLoc)); } T.consumeClose(); diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -6217,7 +6217,7 @@ // Look for designated initializers. // They're in their syntactic form, not yet resolved to fields. - IdentifierInfo *DesignatedFieldName = nullptr; + const IdentifierInfo *DesignatedFieldName = nullptr; unsigned ArgsAfterDesignator = 0; for (const Expr *Arg : Args) { if (const auto *DIE = dyn_cast(Arg)) { diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -2363,12 +2363,12 @@ for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), PE = IndirectField->chain_end(); PI != PE; ++PI) { if (PI + 1 == PE) - Replacements.push_back(Designator((IdentifierInfo *)nullptr, - DIE->getDesignator(DesigIdx)->getDotLoc(), - DIE->getDesignator(DesigIdx)->getFieldLoc())); + Replacements.push_back(Designator::CreateFieldDesignator( + (IdentifierInfo *)nullptr, DIE->getDesignator(DesigIdx)->getDotLoc(), + DIE->getDesignator(DesigIdx)->getFieldLoc())); else - Replacements.push_back(Designator((IdentifierInfo *)nullptr, - SourceLocation(), SourceLocation())); + Replacements.push_back(Designator::CreateFieldDesignator( + (IdentifierInfo *)nullptr, SourceLocation(), SourceLocation())); assert(isa(*PI)); Replacements.back().setField(cast(*PI)); } @@ -2593,7 +2593,7 @@ FieldDecl *KnownField = D->getField(); if (!KnownField) { - IdentifierInfo *FieldName = D->getFieldName(); + const IdentifierInfo *FieldName = D->getFieldName(); DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); for (NamedDecl *ND : Lookup) { if (auto *FD = dyn_cast(ND)) { @@ -3241,13 +3241,11 @@ // Build designators and check array designator expressions. for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { const Designator &D = Desig.getDesignator(Idx); - switch (D.getKind()) { - case Designator::FieldDesignator: - Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), - D.getFieldLoc())); - break; - case Designator::ArrayDesignator: { + if (D.isFieldDesignator()) { + Designators.push_back(ASTDesignator::CreateFieldDesignator( + D.getField(), D.getDotLoc(), D.getFieldLoc())); + } else if (D.isArrayDesignator()) { Expr *Index = static_cast(D.getArrayIndex()); llvm::APSInt IndexValue; if (!Index->isTypeDependent() && !Index->isValueDependent()) @@ -3255,15 +3253,11 @@ if (!Index) Invalid = true; else { - Designators.push_back(ASTDesignator(InitExpressions.size(), - D.getLBracketLoc(), - D.getRBracketLoc())); + Designators.push_back(ASTDesignator::CreateArrayDesignator( + InitExpressions.size(), D.getLBracketLoc(), D.getRBracketLoc())); InitExpressions.push_back(Index); } - break; - } - - case Designator::ArrayRangeDesignator: { + } else if (D.isArrayRangeDesignator()) { Expr *StartIndex = static_cast(D.getArrayRangeStart()); Expr *EndIndex = static_cast(D.getArrayRangeEnd()); llvm::APSInt StartValue; @@ -3295,25 +3289,19 @@ << StartIndex->getSourceRange() << EndIndex->getSourceRange(); Invalid = true; } else { - Designators.push_back(ASTDesignator(InitExpressions.size(), - D.getLBracketLoc(), - D.getEllipsisLoc(), - D.getRBracketLoc())); + Designators.push_back(ASTDesignator::CreateArrayRangeDesignator( + InitExpressions.size(), D.getLBracketLoc(), D.getEllipsisLoc(), + D.getRBracketLoc())); InitExpressions.push_back(StartIndex); InitExpressions.push_back(EndIndex); } } - break; - } } } if (Invalid || Init.isInvalid()) return ExprError(); - // Clear out the expressions within the designation. - Desig.ClearExprs(*this); - return DesignatedInitExpr::Create(Context, Designators, InitExpressions, EqualOrColonLoc, GNUSyntax, Init.getAs()); diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -11661,9 +11661,8 @@ bool ExprChanged = false; for (const DesignatedInitExpr::Designator &D : E->designators()) { if (D.isFieldDesignator()) { - Desig.AddDesignator(Designator::getField(D.getFieldName(), - D.getDotLoc(), - D.getFieldLoc())); + Desig.AddDesignator(Designator::CreateFieldDesignator( + D.getFieldName(), D.getDotLoc(), D.getFieldLoc())); if (D.getField()) { FieldDecl *Field = cast_or_null( getDerived().TransformDecl(D.getFieldLoc(), D.getField())); @@ -11686,7 +11685,7 @@ return ExprError(); Desig.AddDesignator( - Designator::getArray(Index.get(), D.getLBracketLoc())); + Designator::CreateArrayDesignator(Index.get(), D.getLBracketLoc())); ExprChanged = ExprChanged || Init.get() != E->getArrayIndex(D); ArrayExprs.push_back(Index.get()); @@ -11703,10 +11702,8 @@ if (End.isInvalid()) return ExprError(); - Desig.AddDesignator(Designator::getArrayRange(Start.get(), - End.get(), - D.getLBracketLoc(), - D.getEllipsisLoc())); + Desig.AddDesignator(Designator::CreateArrayRangeDesignator( + Start.get(), End.get(), D.getLBracketLoc(), D.getEllipsisLoc())); ExprChanged = ExprChanged || Start.get() != E->getArrayRangeStart(D) || End.get() != E->getArrayRangeEnd(D); diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -1216,8 +1216,8 @@ auto *Field = readDeclAs(); SourceLocation DotLoc = readSourceLocation(); SourceLocation FieldLoc = readSourceLocation(); - Designators.push_back(Designator(Field->getIdentifier(), DotLoc, - FieldLoc)); + Designators.push_back(Designator::CreateFieldDesignator( + Field->getIdentifier(), DotLoc, FieldLoc)); Designators.back().setField(Field); break; } @@ -1226,7 +1226,8 @@ const IdentifierInfo *Name = Record.readIdentifier(); SourceLocation DotLoc = readSourceLocation(); SourceLocation FieldLoc = readSourceLocation(); - Designators.push_back(Designator(Name, DotLoc, FieldLoc)); + Designators.push_back(Designator::CreateFieldDesignator(Name, DotLoc, + FieldLoc)); break; } @@ -1234,7 +1235,9 @@ unsigned Index = Record.readInt(); SourceLocation LBracketLoc = readSourceLocation(); SourceLocation RBracketLoc = readSourceLocation(); - Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc)); + Designators.push_back(Designator::CreateArrayDesignator(Index, + LBracketLoc, + RBracketLoc)); break; } @@ -1243,8 +1246,8 @@ SourceLocation LBracketLoc = readSourceLocation(); SourceLocation EllipsisLoc = readSourceLocation(); SourceLocation RBracketLoc = readSourceLocation(); - Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc, - RBracketLoc)); + Designators.push_back(Designator::CreateArrayRangeDesignator( + Index, LBracketLoc, EllipsisLoc, RBracketLoc)); break; } } diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -1098,13 +1098,13 @@ Record.AddSourceLocation(D.getFieldLoc()); } else if (D.isArrayDesignator()) { Record.push_back(serialization::DESIG_ARRAY); - Record.push_back(D.getFirstExprIndex()); + Record.push_back(D.getArrayIndex()); Record.AddSourceLocation(D.getLBracketLoc()); Record.AddSourceLocation(D.getRBracketLoc()); } else { assert(D.isArrayRangeDesignator() && "Unknown designator"); Record.push_back(serialization::DESIG_ARRAY_RANGE); - Record.push_back(D.getFirstExprIndex()); + Record.push_back(D.getArrayIndex()); Record.AddSourceLocation(D.getLBracketLoc()); Record.AddSourceLocation(D.getEllipsisLoc()); Record.AddSourceLocation(D.getRBracketLoc());