]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Parse/ParseInit.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Parse / ParseInit.cpp
1 //===--- ParseInit.cpp - Initializer Parsing ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements initializer parsing as specified by C99 6.7.8.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Parse/Parser.h"
15 #include "clang/Parse/ParseDiagnostic.h"
16 #include "RAIIObjectsForParser.h"
17 #include "clang/Sema/Designator.h"
18 #include "clang/Sema/Scope.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
22
23
24 /// MayBeDesignationStart - Return true if this token might be the start of a
25 /// designator.  If we can tell it is impossible that it is a designator, return
26 /// false.
27 static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) {
28   switch (K) {
29   default: return false;
30   case tok::period:      // designator: '.' identifier
31   case tok::l_square:    // designator: array-designator
32       return true;
33   case tok::identifier:  // designation: identifier ':'
34     return PP.LookAhead(0).is(tok::colon);
35   }
36 }
37
38 static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc,
39                                        Designation &Desig) {
40   // If we have exactly one array designator, this used the GNU
41   // 'designation: array-designator' extension, otherwise there should be no
42   // designators at all!
43   if (Desig.getNumDesignators() == 1 &&
44       (Desig.getDesignator(0).isArrayDesignator() ||
45        Desig.getDesignator(0).isArrayRangeDesignator()))
46     P.Diag(Loc, diag::ext_gnu_missing_equal_designator);
47   else if (Desig.getNumDesignators() > 0)
48     P.Diag(Loc, diag::err_expected_equal_designator);
49 }
50
51 /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production
52 /// checking to see if the token stream starts with a designator.
53 ///
54 ///       designation:
55 ///         designator-list '='
56 /// [GNU]   array-designator
57 /// [GNU]   identifier ':'
58 ///
59 ///       designator-list:
60 ///         designator
61 ///         designator-list designator
62 ///
63 ///       designator:
64 ///         array-designator
65 ///         '.' identifier
66 ///
67 ///       array-designator:
68 ///         '[' constant-expression ']'
69 /// [GNU]   '[' constant-expression '...' constant-expression ']'
70 ///
71 /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an
72 /// initializer (because it is an expression).  We need to consider this case
73 /// when parsing array designators.
74 ///
75 ExprResult Parser::ParseInitializerWithPotentialDesignator() {
76
77   // If this is the old-style GNU extension:
78   //   designation ::= identifier ':'
79   // Handle it as a field designator.  Otherwise, this must be the start of a
80   // normal expression.
81   if (Tok.is(tok::identifier)) {
82     const IdentifierInfo *FieldName = Tok.getIdentifierInfo();
83
84     llvm::SmallString<256> NewSyntax;
85     llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName()
86                                          << " = ";
87
88     SourceLocation NameLoc = ConsumeToken(); // Eat the identifier.
89
90     assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!");
91     SourceLocation ColonLoc = ConsumeToken();
92
93     Diag(Tok, diag::ext_gnu_old_style_field_designator)
94       << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc),
95                                       NewSyntax.str());
96
97     Designation D;
98     D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc));
99     return Actions.ActOnDesignatedInitializer(D, ColonLoc, true,
100                                               ParseInitializer());
101   }
102
103   // Desig - This is initialized when we see our first designator.  We may have
104   // an objc message send with no designator, so we don't want to create this
105   // eagerly.
106   Designation Desig;
107
108   // Parse each designator in the designator list until we find an initializer.
109   while (Tok.is(tok::period) || Tok.is(tok::l_square)) {
110     if (Tok.is(tok::period)) {
111       // designator: '.' identifier
112       SourceLocation DotLoc = ConsumeToken();
113
114       if (Tok.isNot(tok::identifier)) {
115         Diag(Tok.getLocation(), diag::err_expected_field_designator);
116         return ExprError();
117       }
118
119       Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc,
120                                                Tok.getLocation()));
121       ConsumeToken(); // Eat the identifier.
122       continue;
123     }
124
125     // We must have either an array designator now or an objc message send.
126     assert(Tok.is(tok::l_square) && "Unexpected token!");
127
128     // Handle the two forms of array designator:
129     //   array-designator: '[' constant-expression ']'
130     //   array-designator: '[' constant-expression '...' constant-expression ']'
131     //
132     // Also, we have to handle the case where the expression after the
133     // designator an an objc message send: '[' objc-message-expr ']'.
134     // Interesting cases are:
135     //   [foo bar]         -> objc message send
136     //   [foo]             -> array designator
137     //   [foo ... bar]     -> array designator
138     //   [4][foo bar]      -> obsolete GNU designation with objc message send.
139     //
140     InMessageExpressionRAIIObject InMessage(*this, true);
141     
142     SourceLocation StartLoc = ConsumeBracket();
143     ExprResult Idx;
144
145     // If Objective-C is enabled and this is a typename (class message
146     // send) or send to 'super', parse this as a message send
147     // expression.  We handle C++ and C separately, since C++ requires
148     // much more complicated parsing.
149     if  (getLang().ObjC1 && getLang().CPlusPlus) {
150       // Send to 'super'.
151       if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
152           NextToken().isNot(tok::period) && 
153           getCurScope()->isInObjcMethodScope()) {
154         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
155         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
156                                                            ConsumeToken(),
157                                                            ParsedType(), 
158                                                            0);
159       }
160
161       // Parse the receiver, which is either a type or an expression.
162       bool IsExpr;
163       void *TypeOrExpr;
164       if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
165         SkipUntil(tok::r_square);
166         return ExprError();
167       }
168       
169       // If the receiver was a type, we have a class message; parse
170       // the rest of it.
171       if (!IsExpr) {
172         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
173         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 
174                                                            SourceLocation(), 
175                                    ParsedType::getFromOpaquePtr(TypeOrExpr),
176                                                            0);
177       }
178
179       // If the receiver was an expression, we still don't know
180       // whether we have a message send or an array designator; just
181       // adopt the expression for further analysis below.
182       // FIXME: potentially-potentially evaluated expression above?
183       Idx = ExprResult(static_cast<Expr*>(TypeOrExpr));
184     } else if (getLang().ObjC1 && Tok.is(tok::identifier)) {
185       IdentifierInfo *II = Tok.getIdentifierInfo();
186       SourceLocation IILoc = Tok.getLocation();
187       ParsedType ReceiverType;
188       // Three cases. This is a message send to a type: [type foo]
189       // This is a message send to super:  [super foo]
190       // This is a message sent to an expr:  [super.bar foo]
191       switch (Sema::ObjCMessageKind Kind
192                 = Actions.getObjCMessageKind(getCurScope(), II, IILoc, 
193                                              II == Ident_super,
194                                              NextToken().is(tok::period),
195                                              ReceiverType)) {
196       case Sema::ObjCSuperMessage:
197       case Sema::ObjCClassMessage:
198         CheckArrayDesignatorSyntax(*this, StartLoc, Desig);
199         if (Kind == Sema::ObjCSuperMessage)
200           return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
201                                                              ConsumeToken(),
202                                                              ParsedType(),
203                                                              0);
204         ConsumeToken(); // the identifier
205         if (!ReceiverType) {
206           SkipUntil(tok::r_square);
207           return ExprError();
208         }
209
210         return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 
211                                                            SourceLocation(), 
212                                                            ReceiverType, 
213                                                            0);
214
215       case Sema::ObjCInstanceMessage:
216         // Fall through; we'll just parse the expression and
217         // (possibly) treat this like an Objective-C message send
218         // later.
219         break;
220       }
221     }
222
223     // Parse the index expression, if we haven't already gotten one
224     // above (which can only happen in Objective-C++).
225     // Note that we parse this as an assignment expression, not a constant
226     // expression (allowing *=, =, etc) to handle the objc case.  Sema needs
227     // to validate that the expression is a constant.
228     // FIXME: We also need to tell Sema that we're in a
229     // potentially-potentially evaluated context.
230     if (!Idx.get()) {
231       Idx = ParseAssignmentExpression();
232       if (Idx.isInvalid()) {
233         SkipUntil(tok::r_square);
234         return move(Idx);
235       }
236     }
237
238     // Given an expression, we could either have a designator (if the next
239     // tokens are '...' or ']' or an objc message send.  If this is an objc
240     // message send, handle it now.  An objc-message send is the start of
241     // an assignment-expression production.
242     if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) &&
243         Tok.isNot(tok::r_square)) {
244       CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig);
245       return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
246                                                          SourceLocation(),
247                                                          ParsedType(),
248                                                          Idx.take());
249     }
250
251     // If this is a normal array designator, remember it.
252     if (Tok.isNot(tok::ellipsis)) {
253       Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc));
254     } else {
255       // Handle the gnu array range extension.
256       Diag(Tok, diag::ext_gnu_array_range);
257       SourceLocation EllipsisLoc = ConsumeToken();
258
259       ExprResult RHS(ParseConstantExpression());
260       if (RHS.isInvalid()) {
261         SkipUntil(tok::r_square);
262         return move(RHS);
263       }
264       Desig.AddDesignator(Designator::getArrayRange(Idx.release(),
265                                                     RHS.release(),
266                                                     StartLoc, EllipsisLoc));
267     }
268
269     SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
270     Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc);
271   }
272
273   // Okay, we're done with the designator sequence.  We know that there must be
274   // at least one designator, because the only case we can get into this method
275   // without a designator is when we have an objc message send.  That case is
276   // handled and returned from above.
277   assert(!Desig.empty() && "Designator is empty?");
278
279   // Handle a normal designator sequence end, which is an equal.
280   if (Tok.is(tok::equal)) {
281     SourceLocation EqualLoc = ConsumeToken();
282     return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false,
283                                               ParseInitializer());
284   }
285
286   // We read some number of designators and found something that isn't an = or
287   // an initializer.  If we have exactly one array designator, this
288   // is the GNU 'designation: array-designator' extension.  Otherwise, it is a
289   // parse error.
290   if (Desig.getNumDesignators() == 1 &&
291       (Desig.getDesignator(0).isArrayDesignator() ||
292        Desig.getDesignator(0).isArrayRangeDesignator())) {
293     Diag(Tok, diag::ext_gnu_missing_equal_designator)
294       << FixItHint::CreateInsertion(Tok.getLocation(), "= ");
295     return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(),
296                                               true, ParseInitializer());
297   }
298
299   Diag(Tok, diag::err_expected_equal_designator);
300   return ExprError();
301 }
302
303
304 /// ParseBraceInitializer - Called when parsing an initializer that has a
305 /// leading open brace.
306 ///
307 ///       initializer: [C99 6.7.8]
308 ///         '{' initializer-list '}'
309 ///         '{' initializer-list ',' '}'
310 /// [GNU]   '{' '}'
311 ///
312 ///       initializer-list:
313 ///         designation[opt] initializer ...[opt]
314 ///         initializer-list ',' designation[opt] initializer ...[opt]
315 ///
316 ExprResult Parser::ParseBraceInitializer() {
317   InMessageExpressionRAIIObject InMessage(*this, false);
318   
319   SourceLocation LBraceLoc = ConsumeBrace();
320
321   /// InitExprs - This is the actual list of expressions contained in the
322   /// initializer.
323   ExprVector InitExprs(Actions);
324
325   if (Tok.is(tok::r_brace)) {
326     // Empty initializers are a C++ feature and a GNU extension to C.
327     if (!getLang().CPlusPlus)
328       Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
329     // Match the '}'.
330     return Actions.ActOnInitList(LBraceLoc, MultiExprArg(Actions),
331                                  ConsumeBrace());
332   }
333
334   bool InitExprsOk = true;
335
336   while (1) {
337     // Parse: designation[opt] initializer
338
339     // If we know that this cannot be a designation, just parse the nested
340     // initializer directly.
341     ExprResult SubElt;
342     if (MayBeDesignationStart(Tok.getKind(), PP))
343       SubElt = ParseInitializerWithPotentialDesignator();
344     else
345       SubElt = ParseInitializer();
346
347     if (Tok.is(tok::ellipsis))
348       SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken());
349     
350     // If we couldn't parse the subelement, bail out.
351     if (!SubElt.isInvalid()) {
352       InitExprs.push_back(SubElt.release());
353     } else {
354       InitExprsOk = false;
355
356       // We have two ways to try to recover from this error: if the code looks
357       // grammatically ok (i.e. we have a comma coming up) try to continue
358       // parsing the rest of the initializer.  This allows us to emit
359       // diagnostics for later elements that we find.  If we don't see a comma,
360       // assume there is a parse error, and just skip to recover.
361       // FIXME: This comment doesn't sound right. If there is a r_brace
362       // immediately, it can't be an error, since there is no other way of
363       // leaving this loop except through this if.
364       if (Tok.isNot(tok::comma)) {
365         SkipUntil(tok::r_brace, false, true);
366         break;
367       }
368     }
369
370     // If we don't have a comma continued list, we're done.
371     if (Tok.isNot(tok::comma)) break;
372
373     // TODO: save comma locations if some client cares.
374     ConsumeToken();
375
376     // Handle trailing comma.
377     if (Tok.is(tok::r_brace)) break;
378   }
379   if (InitExprsOk && Tok.is(tok::r_brace))
380     return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs),
381                                  ConsumeBrace());
382
383   // Match the '}'.
384   MatchRHSPunctuation(tok::r_brace, LBraceLoc);
385   return ExprError(); // an error occurred.
386 }
387