From 248388e3131be2fda43387653ced370b6aaf95b2 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 26 Oct 2008 23:35:51 +0000 Subject: [PATCH] pass designators into sema. This completes parser-level designator support as far as I know. llvm-svn: 58217 --- clang/Driver/PrintParserCallbacks.cpp | 1 + clang/include/clang/Parse/Action.h | 1 + clang/include/clang/Parse/Designator.h | 13 +++++++++++++ clang/lib/Parse/ParseInit.cpp | 21 +++++++++++---------- clang/lib/Sema/Sema.h | 1 + clang/lib/Sema/SemaExpr.cpp | 4 +++- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/clang/Driver/PrintParserCallbacks.cpp b/clang/Driver/PrintParserCallbacks.cpp index b616268376f9..94cc0e223743 100644 --- a/clang/Driver/PrintParserCallbacks.cpp +++ b/clang/Driver/PrintParserCallbacks.cpp @@ -485,6 +485,7 @@ namespace { } virtual ExprResult ActOnInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit, + InitListDesignations &Designators, SourceLocation RParenLoc) { llvm::cout << __FUNCTION__ << "\n"; return 0; diff --git a/clang/include/clang/Parse/Action.h b/clang/include/clang/Parse/Action.h index 7e71c9a14b25..63f5be704a71 100644 --- a/clang/include/clang/Parse/Action.h +++ b/clang/include/clang/Parse/Action.h @@ -459,6 +459,7 @@ public: } virtual ExprResult ActOnInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit, + InitListDesignations &Designators, SourceLocation RParenLoc) { return 0; } diff --git a/clang/include/clang/Parse/Designator.h b/clang/include/clang/Parse/Designator.h index 5abf70ddee04..6a5cff63e904 100644 --- a/clang/include/clang/Parse/Designator.h +++ b/clang/include/clang/Parse/Designator.h @@ -208,6 +208,19 @@ public: 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; + } + }; } // end namespace clang diff --git a/clang/lib/Parse/ParseInit.cpp b/clang/lib/Parse/ParseInit.cpp index 655d8e75c729..82a33c21dada 100644 --- a/clang/lib/Parse/ParseInit.cpp +++ b/clang/lib/Parse/ParseInit.cpp @@ -236,14 +236,6 @@ ParseInitializerWithPotentialDesignator(InitListDesignations &Designations, Parser::ExprResult Parser::ParseBraceInitializer() { SourceLocation LBraceLoc = ConsumeBrace(); - // We support empty initializers, but tell the user that they aren't using - // C99-clean code. - if (Tok.is(tok::r_brace)) { - Diag(LBraceLoc, diag::ext_gnu_empty_initializer); - // Match the '}'. - return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace()); - } - /// InitExprs - This is the actual list of expressions contained in the /// initializer. llvm::SmallVector InitExprs; @@ -252,6 +244,15 @@ Parser::ExprResult Parser::ParseBraceInitializer() { /// was specified for it, if any. InitListDesignations InitExprDesignations(Actions); + // We support empty initializers, but tell the user that they aren't using + // C99-clean code. + if (Tok.is(tok::r_brace)) { + Diag(LBraceLoc, diag::ext_gnu_empty_initializer); + // Match the '}'. + return Actions.ActOnInitList(LBraceLoc, 0, 0, InitExprDesignations, + ConsumeBrace()); + } + bool InitExprsOk = true; while (1) { @@ -293,8 +294,8 @@ Parser::ExprResult Parser::ParseBraceInitializer() { if (Tok.is(tok::r_brace)) break; } if (InitExprsOk && Tok.is(tok::r_brace)) - return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(), - ConsumeBrace()); + return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(), + InitExprDesignations, ConsumeBrace()); // On error, delete any parsed subexpressions. for (unsigned i = 0, e = InitExprs.size(); i != e; ++i) diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index ef34a105340e..c68776a80fb3 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -614,6 +614,7 @@ public: virtual ExprResult ActOnInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit, + InitListDesignations &Designators, SourceLocation RParenLoc); virtual ExprResult ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind, diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 13b3959d8c35..fee94568b78c 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -1247,11 +1247,13 @@ ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, if (CheckForConstantInitializer(literalExpr, literalType)) return true; } - return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope); + return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, + isFileScope); } Action::ExprResult Sema:: ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit, + InitListDesignations &Designators, SourceLocation RBraceLoc) { Expr **InitList = reinterpret_cast(initlist);