/// Designation - Represent a full designation, which is a sequence of
/// designators. This class is mostly a helper for InitListDesignations.
class Designation {
- friend class InitListDesignations;
-
/// InitIndex - The index of the initializer expression this is for. For
/// example, if the initializer were "{ A, .foo=B, C }" a Designation would
/// exist with InitIndex=1, because element #1 has a designation.
Designation(unsigned Idx) : InitIndex(Idx) {}
public:
-
+ Designation() : InitIndex(4000) {}
+
/// AddDesignator - Add a designator to the end of this list.
void AddDesignator(Designator D) {
Designators.push_back(D);
}
-
+
+ bool empty() const { return Designators.empty(); }
+
unsigned getNumDesignators() const { return Designators.size(); }
const Designator &getDesignator(unsigned Idx) const {
assert(Idx < Designators.size());
Designators[i].FreeExprs(Actions);
}
};
-
-
-/// InitListDesignations - This contains all the designators for an
-/// initializer list. This is somewhat like a two dimensional array of
-/// Designators, but is optimized for the cases when designators are not
-/// present.
-class InitListDesignations {
- Action &Actions;
-
- /// Designations - All of the designators in this init list. These are kept
- /// in order sorted by their InitIndex.
- llvm::SmallVector<Designation, 3> Designations;
-
- InitListDesignations(const InitListDesignations&); // DO NOT IMPLEMENT
- void operator=(const InitListDesignations&); // DO NOT IMPLEMENT
-public:
- InitListDesignations(Action &A) : Actions(A) {}
-
- ~InitListDesignations() {
- // Release any unclaimed memory for the expressions in this init list.
- for (unsigned i = 0, e = Designations.size(); i != e; ++i)
- Designations[i].FreeExprs(Actions);
- }
-
- bool hasAnyDesignators() const {
- return !Designations.empty();
- }
-
- Designation &CreateDesignation(unsigned Idx) {
- assert((Designations.empty() || Designations.back().InitIndex < Idx) &&
- "not sorted by InitIndex!");
- Designations.push_back(Designation(Idx));
- return Designations.back();
- }
-
- /// getDesignationForInitializer - If there is a designator for the specified
- /// initializer, return it, otherwise return null.
- const Designation *getDesignationForInitializer(unsigned Idx) const {
- // The common case is no designators.
- if (!hasAnyDesignators()) return 0;
-
- // FIXME: This should do a binary search, not a linear one.
- for (unsigned i = 0, e = Designations.size(); i != e; ++i)
- if (Designations[i].InitIndex == Idx)
- return &Designations[i];
- return 0;
- }
-
- /// EraseDesignation - If there is a designator for the specified initializer
- /// index, remove it.
- void EraseDesignation(unsigned Idx) {
- Designation *D =const_cast<Designation*>(getDesignationForInitializer(Idx));
- if (D == 0) return; // No designator.
-
- D->FreeExprs(Actions);
- unsigned SlotNo = D-&Designations[0];
- Designations.erase(Designations.begin()+SlotNo);
- }
-
-};
-
+
} // end namespace clang
#endif
/// initializer (because it is an expression). We need to consider this case
/// when parsing array designators.
///
-Parser::OwningExprResult Parser::
-ParseInitializerWithPotentialDesignator(InitListDesignations &Designations,
- unsigned InitNum) {
+Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
// If this is the old-style GNU extension:
// designation ::= identifier ':'
assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
SourceLocation ColonLoc = ConsumeToken();
- Designation &D = Designations.CreateDesignation(InitNum);
+ Designation D;
D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
ParseInitializer());
// Desig - This is initialized when we see our first designator. We may have
// an objc message send with no designator, so we don't want to create this
// eagerly.
- Designation *Desig = 0;
+ Designation Desig;
// Parse each designator in the designator list until we find an initializer.
while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
// designator: '.' identifier
SourceLocation DotLoc = ConsumeToken();
- // Create designation if we haven't already.
- if (Desig == 0)
- Desig = &Designations.CreateDesignation(InitNum);
-
if (Tok.isNot(tok::identifier)) {
Diag(Tok.getLocation(), diag::err_expected_field_designator);
return ExprError();
}
- Desig->AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
- Tok.getLocation()));
+ Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
+ Tok.getLocation()));
ConsumeToken(); // Eat the identifier.
continue;
}
// If we have exactly one array designator, this used the GNU
// 'designation: array-designator' extension, otherwise there should be no
// designators at all!
- if (Desig) {
- if (Desig->getNumDesignators() == 1 &&
- (Desig->getDesignator(0).isArrayDesignator() ||
- Desig->getDesignator(0).isArrayRangeDesignator()))
- Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
- else
- Diag(Tok, diag::err_expected_equal_designator);
- }
+ if (Desig.getNumDesignators() == 1 &&
+ (Desig.getDesignator(0).isArrayDesignator() ||
+ Desig.getDesignator(0).isArrayRangeDesignator()))
+ Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
+ else if (Desig.getNumDesignators() > 0)
+ Diag(Tok, diag::err_expected_equal_designator);
IdentifierInfo *Name = Tok.getIdentifierInfo();
SourceLocation NameLoc = ConsumeToken();
// If we have exactly one array designator, this used the GNU
// 'designation: array-designator' extension, otherwise there should be no
// designators at all!
- if (Desig) {
- if (Desig->getNumDesignators() == 1 &&
- (Desig->getDesignator(0).isArrayDesignator() ||
- Desig->getDesignator(0).isArrayRangeDesignator()))
- Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
- else
- Diag(Tok, diag::err_expected_equal_designator);
- }
+ if (Desig.getNumDesignators() == 1 &&
+ (Desig.getDesignator(0).isArrayDesignator() ||
+ Desig.getDesignator(0).isArrayRangeDesignator()))
+ Diag(StartLoc, diag::ext_gnu_missing_equal_designator);
+ else if (Desig.getNumDesignators() > 0)
+ Diag(Tok, diag::err_expected_equal_designator);
return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
SourceLocation(),
0, move(Idx));
}
- // Create designation if we haven't already.
- if (Desig == 0)
- Desig = &Designations.CreateDesignation(InitNum);
-
// If this is a normal array designator, remember it.
if (Tok.isNot(tok::ellipsis)) {
- Desig->AddDesignator(Designator::getArray(Idx.release(),
- StartLoc));
+ Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
} else {
// Handle the gnu array range extension.
Diag(Tok, diag::ext_gnu_array_range);
SkipUntil(tok::r_square);
return move(RHS);
}
- Desig->AddDesignator(Designator::getArrayRange(Idx.release(),
- RHS.release(),
- StartLoc, EllipsisLoc));
+ Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
+ RHS.release(),
+ StartLoc, EllipsisLoc));
}
SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
- Desig->getDesignator(Desig->getNumDesignators() - 1).setRBracketLoc(EndLoc);
+ Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
}
// Okay, we're done with the designator sequence. We know that there must be
// at least one designator, because the only case we can get into this method
// without a designator is when we have an objc message send. That case is
// handled and returned from above.
- assert(Desig && "Designator didn't get created?");
+ assert(!Desig.empty() && "Designator is empty?");
// Handle a normal designator sequence end, which is an equal.
if (Tok.is(tok::equal)) {
SourceLocation EqualLoc = ConsumeToken();
- return Actions.ActOnDesignatedInitializer(*Desig, EqualLoc, false,
+ return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
ParseInitializer());
}
// an initializer. If we have exactly one array designator, this
// is the GNU 'designation: array-designator' extension. Otherwise, it is a
// parse error.
- if (Desig->getNumDesignators() == 1 &&
- (Desig->getDesignator(0).isArrayDesignator() ||
- Desig->getDesignator(0).isArrayRangeDesignator())) {
+ if (Desig.getNumDesignators() == 1 &&
+ (Desig.getDesignator(0).isArrayDesignator() ||
+ Desig.getDesignator(0).isArrayRangeDesignator())) {
Diag(Tok, diag::ext_gnu_missing_equal_designator);
return ParseInitializer();
}
/// initializer.
ExprVector InitExprs(Actions);
- /// ExprDesignators - For each initializer, keep track of the designator that
- /// was specified for it, if any.
- InitListDesignations InitExprDesignations(Actions);
-
if (Tok.is(tok::r_brace)) {
// Empty initializers are a C++ feature and a GNU extension to C.
if (!getLang().CPlusPlus)
Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
// Match the '}'.
return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions),
- InitExprDesignations, ConsumeBrace());
+ ConsumeBrace());
}
bool InitExprsOk = true;
// If we know that this cannot be a designation, just parse the nested
// initializer directly.
OwningExprResult SubElt(Actions);
- if (!MayBeDesignationStart(Tok.getKind(), PP))
+ if (MayBeDesignationStart(Tok.getKind(), PP))
+ SubElt = ParseInitializerWithPotentialDesignator();
+ else
SubElt = ParseInitializer();
- else {
- SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations,
- InitExprs.size());
-
- // If we had an erroneous initializer, and we had a potentially valid
- // designator, make sure to remove the designator from
- // InitExprDesignations, otherwise we'll end up with a designator with no
- // matching initializer.
- if (SubElt.isInvalid())
- InitExprDesignations.EraseDesignation(InitExprs.size());
- }
// If we couldn't parse the subelement, bail out.
if (!SubElt.isInvalid()) {
}
if (InitExprsOk && Tok.is(tok::r_brace))
return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
- InitExprDesignations, ConsumeBrace());
+ ConsumeBrace());
// Match the '}'.
MatchRHSPunctuation(tok::r_brace, LBraceLoc);