From 220ad7c8d1adc23799e480faf189332f1eb032e6 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58217 91177308-0d34-0410-b5e6-96231b3b80d8 --- Driver/PrintParserCallbacks.cpp | 1 + include/clang/Parse/Action.h | 1 + include/clang/Parse/Designator.h | 13 +++++++++++++ lib/Parse/ParseInit.cpp | 21 +++++++++++---------- lib/Sema/Sema.h | 1 + lib/Sema/SemaExpr.cpp | 4 +++- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Driver/PrintParserCallbacks.cpp b/Driver/PrintParserCallbacks.cpp index b616268376..94cc0e2237 100644 --- a/Driver/PrintParserCallbacks.cpp +++ b/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/include/clang/Parse/Action.h b/include/clang/Parse/Action.h index 7e71c9a14b..63f5be704a 100644 --- a/include/clang/Parse/Action.h +++ b/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/include/clang/Parse/Designator.h b/include/clang/Parse/Designator.h index 5abf70ddee..6a5cff63e9 100644 --- a/include/clang/Parse/Designator.h +++ b/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/lib/Parse/ParseInit.cpp b/lib/Parse/ParseInit.cpp index 655d8e75c7..82a33c21da 100644 --- a/lib/Parse/ParseInit.cpp +++ b/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/lib/Sema/Sema.h b/lib/Sema/Sema.h index ef34a10534..c68776a80f 100644 --- a/lib/Sema/Sema.h +++ b/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/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 13b3959d8c..fee94568b7 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/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); -- 2.40.0