ParseInit.cpp revision 226633
1138568Ssam//===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2138568Ssam//
3138568Ssam//                     The LLVM Compiler Infrastructure
4138568Ssam//
5138568Ssam// This file is distributed under the University of Illinois Open Source
6138568Ssam// License. See LICENSE.TXT for details.
7138568Ssam//
8138568Ssam//===----------------------------------------------------------------------===//
9138568Ssam//
10138568Ssam// This file implements initializer parsing as specified by C99 6.7.8.
11138568Ssam//
12138568Ssam//===----------------------------------------------------------------------===//
13138568Ssam
14138568Ssam#include "clang/Parse/Parser.h"
15138568Ssam#include "clang/Parse/ParseDiagnostic.h"
16138568Ssam#include "RAIIObjectsForParser.h"
17138568Ssam#include "clang/Sema/Designator.h"
18138568Ssam#include "clang/Sema/Scope.h"
19138568Ssam#include "llvm/ADT/SmallString.h"
20138568Ssam#include "llvm/Support/raw_ostream.h"
21138568Ssamusing namespace clang;
22138568Ssam
23138568Ssam
24138568Ssam/// MayBeDesignationStart - Return true if this token might be the start of a
25138568Ssam/// designator.  If we can tell it is impossible that it is a designator, return
26138568Ssam/// false.
27138568Ssamstatic bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
28138568Ssam  switch (K) {
29138568Ssam  default: return false;
30138568Ssam  case tok::period:      // designator: '.' identifier
31138568Ssam  case tok::l_square:    // designator: array-designator
32138568Ssam      return true;
33138568Ssam  case tok::identifier:  // designation: identifier ':'
34138568Ssam    return PP.LookAhead(0).is(tok::colon);
35138568Ssam  }
36138568Ssam}
37138568Ssam
38138568Ssamstatic void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
39138568Ssam                                       Designation &Desig) {
40138568Ssam  // If we have exactly one array designator, this used the GNU
41138568Ssam  // 'designation: array-designator' extension, otherwise there should be no
42138568Ssam  // designators at all!
43138568Ssam  if (Desig.getNumDesignators() == 1 &&
44138568Ssam      (Desig.getDesignator(0).isArrayDesignator() ||
45138568Ssam       Desig.getDesignator(0).isArrayRangeDesignator()))
46138568Ssam    P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
47138568Ssam  else if (Desig.getNumDesignators() > 0)
48138568Ssam    P.Diag(Loc, diag::err_expected_equal_designator);
49138568Ssam}
50138568Ssam
51138568Ssam/// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
52138568Ssam/// checking to see if the token stream starts with a designator.
53138568Ssam///
54138568Ssam///       designation:
55138568Ssam///         designator-list '='
56138568Ssam/// [GNU]   array-designator
57138568Ssam/// [GNU]   identifier ':'
58138568Ssam///
59138568Ssam///       designator-list:
60138568Ssam///         designator
61138568Ssam///         designator-list designator
62138568Ssam///
63138568Ssam///       designator:
64138568Ssam///         array-designator
65138568Ssam///         '.' identifier
66138568Ssam///
67138568Ssam///       array-designator:
68138568Ssam///         '[' constant-expression ']'
69138568Ssam/// [GNU]   '[' constant-expression '...' constant-expression ']'
70138568Ssam///
71138568Ssam/// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
72138568Ssam/// initializer (because it is an expression).  We need to consider this case
73138568Ssam/// when parsing array designators.
74138568Ssam///
75138568SsamExprResult Parser::ParseInitializerWithPotentialDesignator() {
76138568Ssam
77138568Ssam  // If this is the old-style GNU extension:
78138568Ssam  //   designation ::= identifier ':'
79138568Ssam  // Handle it as a field designator.  Otherwise, this must be the start of a
80138568Ssam  // normal expression.
81138568Ssam  if (Tok.is(tok::identifier)) {
82138568Ssam    const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
83138568Ssam
84138568Ssam    llvm::SmallString<256> NewSyntax;
85138568Ssam    llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
86138568Ssam                                         << " = ";
87138568Ssam
88138568Ssam    SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
89138568Ssam
90138568Ssam    assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
91138568Ssam    SourceLocation ColonLoc = ConsumeToken();
92138568Ssam
93138568Ssam    Diag(NameLoc, diag::ext_gnu_old_style_field_designator)
94138568Ssam      << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
95138568Ssam                                      NewSyntax.str());
96138568Ssam
97138568Ssam    Designation D;
98138568Ssam    D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
99138568Ssam    return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
100138568Ssam                                              ParseInitializer());
101138568Ssam  }
102138568Ssam
103138568Ssam  // Desig - This is initialized when we see our first designator.  We may have
104138568Ssam  // an objc message send with no designator, so we don't want to create this
105138568Ssam  // eagerly.
106138568Ssam  Designation Desig;
107138568Ssam
108138568Ssam  // Parse each designator in the designator list until we find an initializer.
109138568Ssam  while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
110138568Ssam    if (Tok.is(tok::period)) {
111138568Ssam      // designator: '.' identifier
112138568Ssam      SourceLocation DotLoc = ConsumeToken();
113138568Ssam
114138568Ssam      if (Tok.isNot(tok::identifier)) {
115138568Ssam        Diag(Tok.getLocation(), diag::err_expected_field_designator);
116138568Ssam        return ExprError();
117138568Ssam      }
118138568Ssam
119138568Ssam      Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
120138568Ssam                                               Tok.getLocation()));
121138568Ssam      ConsumeToken(); // Eat the identifier.
122138568Ssam      continue;
123138568Ssam    }
124138568Ssam
125138568Ssam    // We must have either an array designator now or an objc message send.
126138568Ssam    assert(Tok.is(tok::l_square) && "Unexpected token!");
127138568Ssam
128138568Ssam    // Handle the two forms of array designator:
129138568Ssam    //   array-designator: '[' constant-expression ']'
130138568Ssam    //   array-designator: '[' constant-expression '...' constant-expression ']'
131138568Ssam    //
132138568Ssam    // Also, we have to handle the case where the expression after the
133138568Ssam    // designator an an objc message send: '[' objc-message-expr ']'.
134138568Ssam    // Interesting cases are:
135138568Ssam    //   [foo bar]         -> objc message send
136138568Ssam    //   [foo]             -> array designator
137138568Ssam    //   [foo ... bar]     -> array designator
138138568Ssam    //   [4][foo bar]      -> obsolete GNU designation with objc message send.
139138568Ssam    //
140138568Ssam    InMessageExpressionRAIIObject InMessage(*this, true);
141138568Ssam
142138568Ssam    BalancedDelimiterTracker T(*this, tok::l_square);
143138568Ssam    T.consumeOpen();
144138568Ssam    SourceLocation StartLoc = T.getOpenLocation();
145138568Ssam
146138568Ssam    ExprResult Idx;
147138568Ssam
148138568Ssam    // If Objective-C is enabled and this is a typename (class message
149138568Ssam    // send) or send to 'super', parse this as a message send
150138568Ssam    // expression.  We handle C++ and C separately, since C++ requires
151138568Ssam    // much more complicated parsing.
152138568Ssam    if  (getLang().ObjC1 && getLang().CPlusPlus) {
153138568Ssam      // Send to 'super'.
154138568Ssam      if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
155138568Ssam          NextToken().isNot(tok::period) &&
156138568Ssam          getCurScope()->isInObjcMethodScope()) {
157138568Ssam        CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
158138568Ssam        return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
159138568Ssam                                                           ConsumeToken(),
160138568Ssam                                                           ParsedType(),
161138568Ssam                                                           0);
162138568Ssam      }
163138568Ssam
164138568Ssam      // Parse the receiver, which is either a type or an expression.
165138568Ssam      bool IsExpr;
166138568Ssam      void *TypeOrExpr;
167138568Ssam      if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
168138568Ssam        SkipUntil(tok::r_square);
169138568Ssam        return ExprError();
170138568Ssam      }
171138568Ssam
172138568Ssam      // If the receiver was a type, we have a class message; parse
173138568Ssam      // the rest of it.
174138568Ssam      if (!IsExpr) {
175138568Ssam        CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
176138568Ssam        return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
177138568Ssam                                                           SourceLocation(),
178138568Ssam                                   ParsedType::getFromOpaquePtr(TypeOrExpr),
179138568Ssam                                                           0);
180138568Ssam      }
181138568Ssam
182138568Ssam      // If the receiver was an expression, we still don't know
183138568Ssam      // whether we have a message send or an array designator; just
184138568Ssam      // adopt the expression for further analysis below.
185138568Ssam      // FIXME: potentially-potentially evaluated expression above?
186138568Ssam      Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
187138568Ssam    } else if (getLang().ObjC1 && Tok.is(tok::identifier)) {
188138568Ssam      IdentifierInfo *II = Tok.getIdentifierInfo();
189138568Ssam      SourceLocation IILoc = Tok.getLocation();
190138568Ssam      ParsedType ReceiverType;
191138568Ssam      // Three cases. This is a message send to a type: [type foo]
192138568Ssam      // This is a message send to super:  [super foo]
193138568Ssam      // This is a message sent to an expr:  [super.bar foo]
194138568Ssam      switch (Sema::ObjCMessageKind Kind
195138568Ssam                = Actions.getObjCMessageKind(getCurScope(), II, IILoc,
196138568Ssam                                             II == Ident_super,
197138568Ssam                                             NextToken().is(tok::period),
198138568Ssam                                             ReceiverType)) {
199138568Ssam      case Sema::ObjCSuperMessage:
200138568Ssam      case Sema::ObjCClassMessage:
201138568Ssam        CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
202138568Ssam        if (Kind == Sema::ObjCSuperMessage)
203138568Ssam          return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
204138568Ssam                                                             ConsumeToken(),
205138568Ssam                                                             ParsedType(),
206138568Ssam                                                             0);
207138568Ssam        ConsumeToken(); // the identifier
208138568Ssam        if (!ReceiverType) {
209138568Ssam          SkipUntil(tok::r_square);
210138568Ssam          return ExprError();
211138568Ssam        }
212138568Ssam
213138568Ssam        return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
214138568Ssam                                                           SourceLocation(),
215138568Ssam                                                           ReceiverType,
216138568Ssam                                                           0);
217138568Ssam
218138568Ssam      case Sema::ObjCInstanceMessage:
219138568Ssam        // Fall through; we'll just parse the expression and
220138568Ssam        // (possibly) treat this like an Objective-C message send
221138568Ssam        // later.
222138568Ssam        break;
223138568Ssam      }
224138568Ssam    }
225138568Ssam
226138568Ssam    // Parse the index expression, if we haven't already gotten one
227138568Ssam    // above (which can only happen in Objective-C++).
228138568Ssam    // Note that we parse this as an assignment expression, not a constant
229138568Ssam    // expression (allowing *=, =, etc) to handle the objc case.  Sema needs
230138568Ssam    // to validate that the expression is a constant.
231138568Ssam    // FIXME: We also need to tell Sema that we're in a
232138568Ssam    // potentially-potentially evaluated context.
233138568Ssam    if (!Idx.get()) {
234138568Ssam      Idx = ParseAssignmentExpression();
235138568Ssam      if (Idx.isInvalid()) {
236138568Ssam        SkipUntil(tok::r_square);
237138568Ssam        return move(Idx);
238138568Ssam      }
239138568Ssam    }
240138568Ssam
241138568Ssam    // Given an expression, we could either have a designator (if the next
242138568Ssam    // tokens are '...' or ']' or an objc message send.  If this is an objc
243138568Ssam    // message send, handle it now.  An objc-message send is the start of
244138568Ssam    // an assignment-expression production.
245138568Ssam    if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
246138568Ssam        Tok.isNot(tok::r_square)) {
247138568Ssam      CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
248138568Ssam      return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
249138568Ssam                                                         SourceLocation(),
250138568Ssam                                                         ParsedType(),
251138568Ssam                                                         Idx.take());
252138568Ssam    }
253138568Ssam
254138568Ssam    // If this is a normal array designator, remember it.
255138568Ssam    if (Tok.isNot(tok::ellipsis)) {
256138568Ssam      Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
257138568Ssam    } else {
258138568Ssam      // Handle the gnu array range extension.
259138568Ssam      Diag(Tok, diag::ext_gnu_array_range);
260138568Ssam      SourceLocation EllipsisLoc = ConsumeToken();
261138568Ssam
262138568Ssam      ExprResult RHS(ParseConstantExpression());
263138568Ssam      if (RHS.isInvalid()) {
264138568Ssam        SkipUntil(tok::r_square);
265138568Ssam        return move(RHS);
266138568Ssam      }
267138568Ssam      Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
268138568Ssam                                                    RHS.release(),
269138568Ssam                                                    StartLoc, EllipsisLoc));
270138568Ssam    }
271138568Ssam
272138568Ssam    T.consumeClose();
273138568Ssam    Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(
274138568Ssam                                                        T.getCloseLocation());
275138568Ssam  }
276138568Ssam
277138568Ssam  // Okay, we're done with the designator sequence.  We know that there must be
278138568Ssam  // at least one designator, because the only case we can get into this method
279138568Ssam  // without a designator is when we have an objc message send.  That case is
280138568Ssam  // handled and returned from above.
281138568Ssam  assert(!Desig.empty() && "Designator is empty?");
282138568Ssam
283138568Ssam  // Handle a normal designator sequence end, which is an equal.
284138568Ssam  if (Tok.is(tok::equal)) {
285138568Ssam    SourceLocation EqualLoc = ConsumeToken();
286138568Ssam    return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
287138568Ssam                                              ParseInitializer());
288138568Ssam  }
289138568Ssam
290138568Ssam  // We read some number of designators and found something that isn't an = or
291138568Ssam  // an initializer.  If we have exactly one array designator, this
292138568Ssam  // is the GNU 'designation: array-designator' extension.  Otherwise, it is a
293138568Ssam  // parse error.
294138568Ssam  if (Desig.getNumDesignators() == 1 &&
295138568Ssam      (Desig.getDesignator(0).isArrayDesignator() ||
296138568Ssam       Desig.getDesignator(0).isArrayRangeDesignator())) {
297138568Ssam    Diag(Tok, diag::ext_gnu_missing_equal_designator)
298138568Ssam      << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
299138568Ssam    return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
300138568Ssam                                              true, ParseInitializer());
301138568Ssam  }
302
303  Diag(Tok, diag::err_expected_equal_designator);
304  return ExprError();
305}
306
307
308/// ParseBraceInitializer - Called when parsing an initializer that has a
309/// leading open brace.
310///
311///       initializer: [C99 6.7.8]
312///         '{' initializer-list '}'
313///         '{' initializer-list ',' '}'
314/// [GNU]   '{' '}'
315///
316///       initializer-list:
317///         designation[opt] initializer ...[opt]
318///         initializer-list ',' designation[opt] initializer ...[opt]
319///
320ExprResult Parser::ParseBraceInitializer() {
321  InMessageExpressionRAIIObject InMessage(*this, false);
322
323  BalancedDelimiterTracker T(*this, tok::l_brace);
324  T.consumeOpen();
325  SourceLocation LBraceLoc = T.getOpenLocation();
326
327  /// InitExprs - This is the actual list of expressions contained in the
328  /// initializer.
329  ExprVector InitExprs(Actions);
330
331  if (Tok.is(tok::r_brace)) {
332    // Empty initializers are a C++ feature and a GNU extension to C.
333    if (!getLang().CPlusPlus)
334      Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
335    // Match the '}'.
336    return Actions.ActOnInitList(LBraceLoc, MultiExprArg(Actions),
337                                 ConsumeBrace());
338  }
339
340  bool InitExprsOk = true;
341
342  while (1) {
343    // Parse: designation[opt] initializer
344
345    // If we know that this cannot be a designation, just parse the nested
346    // initializer directly.
347    ExprResult SubElt;
348    if (MayBeDesignationStart(Tok.getKind(), PP))
349      SubElt = ParseInitializerWithPotentialDesignator();
350    else
351      SubElt = ParseInitializer();
352
353    if (Tok.is(tok::ellipsis))
354      SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
355
356    // If we couldn't parse the subelement, bail out.
357    if (!SubElt.isInvalid()) {
358      InitExprs.push_back(SubElt.release());
359    } else {
360      InitExprsOk = false;
361
362      // We have two ways to try to recover from this error: if the code looks
363      // grammatically ok (i.e. we have a comma coming up) try to continue
364      // parsing the rest of the initializer.  This allows us to emit
365      // diagnostics for later elements that we find.  If we don't see a comma,
366      // assume there is a parse error, and just skip to recover.
367      // FIXME: This comment doesn't sound right. If there is a r_brace
368      // immediately, it can't be an error, since there is no other way of
369      // leaving this loop except through this if.
370      if (Tok.isNot(tok::comma)) {
371        SkipUntil(tok::r_brace, false, true);
372        break;
373      }
374    }
375
376    // If we don't have a comma continued list, we're done.
377    if (Tok.isNot(tok::comma)) break;
378
379    // TODO: save comma locations if some client cares.
380    ConsumeToken();
381
382    // Handle trailing comma.
383    if (Tok.is(tok::r_brace)) break;
384  }
385
386  bool closed = !T.consumeClose();
387
388  if (InitExprsOk && closed)
389    return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
390                                 T.getCloseLocation());
391
392  return ExprError(); // an error occurred.
393}
394
395