2006-07-31 05:13:43 +00:00
|
|
|
//===--- ParserDeclarations.cpp - Declaration Parsing ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Declaration portions of the Parser interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2006-08-04 04:39:53 +00:00
|
|
|
#include "clang/Parse/Declarations.h"
|
2006-07-31 05:13:43 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C99 6.7: Declarations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ParseDeclarationSpecifiers
|
|
|
|
/// declaration-specifiers: [C99 6.7]
|
2006-08-04 05:25:55 +00:00
|
|
|
/// storage-class-specifier declaration-specifiers [opt]
|
2006-07-31 05:13:43 +00:00
|
|
|
/// type-specifier declaration-specifiers [opt]
|
2006-08-04 05:25:55 +00:00
|
|
|
/// type-qualifier declaration-specifiers [opt]
|
|
|
|
/// [C99] function-specifier declaration-specifiers [opt]
|
|
|
|
/// [GNU] attributes declaration-specifiers [opt] [TODO]
|
2006-07-31 05:13:43 +00:00
|
|
|
///
|
2006-08-05 03:28:50 +00:00
|
|
|
/// storage-class-specifier: [C99 6.7.1]
|
2006-08-04 05:25:55 +00:00
|
|
|
/// 'typedef'
|
|
|
|
/// 'extern'
|
|
|
|
/// 'static'
|
|
|
|
/// 'auto'
|
|
|
|
/// 'register'
|
|
|
|
/// [GNU] '__thread'
|
2006-07-31 05:13:43 +00:00
|
|
|
/// type-specifier: [C99 6.7.2]
|
|
|
|
/// 'void'
|
|
|
|
/// 'char'
|
|
|
|
/// 'short'
|
|
|
|
/// 'int'
|
|
|
|
/// 'long'
|
|
|
|
/// 'float'
|
|
|
|
/// 'double'
|
|
|
|
/// 'signed'
|
|
|
|
/// 'unsigned'
|
2006-08-05 03:30:45 +00:00
|
|
|
/// struct-or-union-specifier [TODO]
|
|
|
|
/// enum-specifier [TODO]
|
|
|
|
/// typedef-name [TODO]
|
2006-07-31 05:13:43 +00:00
|
|
|
/// [C99] '_Bool'
|
|
|
|
/// [C99] '_Complex'
|
2006-08-04 05:25:55 +00:00
|
|
|
/// [C99] '_Imaginary' // Removed in TC2?
|
|
|
|
/// [GNU] '_Decimal32'
|
|
|
|
/// [GNU] '_Decimal64'
|
|
|
|
/// [GNU] '_Decimal128'
|
2006-08-05 03:28:50 +00:00
|
|
|
/// [GNU] typeof-specifier [TODO]
|
2006-08-04 05:25:55 +00:00
|
|
|
/// [OBJC] class-name objc-protocol-refs [opt] [TODO]
|
|
|
|
/// [OBJC] typedef-name objc-protocol-refs [TODO]
|
|
|
|
/// [OBJC] objc-protocol-refs [TODO]
|
|
|
|
/// type-qualifier:
|
|
|
|
/// const
|
|
|
|
/// volatile
|
|
|
|
/// [C99] restrict
|
2006-08-04 04:39:53 +00:00
|
|
|
/// function-specifier: [C99 6.7.4]
|
|
|
|
/// [C99] inline
|
|
|
|
///
|
|
|
|
void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
|
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
2006-07-31 05:13:43 +00:00
|
|
|
while (1) {
|
2006-08-04 05:25:55 +00:00
|
|
|
int isInvalid = false;
|
2006-08-04 04:39:53 +00:00
|
|
|
const char *PrevSpec = 0;
|
2006-07-31 05:13:43 +00:00
|
|
|
switch (Tok.getKind()) {
|
2006-08-04 04:39:53 +00:00
|
|
|
default:
|
|
|
|
// If this is not a declaration specifier token, we're done reading decl
|
|
|
|
// specifiers. First verify that DeclSpec's are consistent.
|
2006-08-04 06:15:52 +00:00
|
|
|
DS.Finish(StartLoc, Diags, getLang());
|
2006-08-04 04:39:53 +00:00
|
|
|
return;
|
2006-08-05 03:28:50 +00:00
|
|
|
|
|
|
|
// storage-class-specifier
|
|
|
|
case tok::kw_typedef:
|
|
|
|
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_extern:
|
|
|
|
if (DS.SCS_thread_specified)
|
|
|
|
Diag(Tok, diag::ext_thread_before, "extern");
|
|
|
|
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_static:
|
|
|
|
if (DS.SCS_thread_specified)
|
|
|
|
Diag(Tok, diag::ext_thread_before, "static");
|
|
|
|
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_auto:
|
|
|
|
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_register:
|
|
|
|
isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw___thread:
|
|
|
|
if (DS.SCS_thread_specified)
|
|
|
|
isInvalid = 2, PrevSpec = "__thread";
|
|
|
|
else
|
|
|
|
DS.SCS_thread_specified = true;
|
|
|
|
break;
|
|
|
|
|
2006-07-31 05:13:43 +00:00
|
|
|
// type-specifiers
|
2006-08-04 04:39:53 +00:00
|
|
|
case tok::kw_short:
|
|
|
|
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_long:
|
|
|
|
if (DS.TypeSpecWidth != DeclSpec::TSW_long) {
|
|
|
|
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, PrevSpec);
|
|
|
|
} else {
|
|
|
|
DS.TypeSpecWidth = DeclSpec::TSW_unspecified;
|
|
|
|
isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, PrevSpec);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case tok::kw_signed:
|
|
|
|
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_unsigned:
|
|
|
|
isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw__Complex:
|
|
|
|
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw__Imaginary:
|
|
|
|
isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_void:
|
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, PrevSpec);
|
|
|
|
break;
|
2006-07-31 05:13:43 +00:00
|
|
|
case tok::kw_char:
|
2006-08-04 04:39:53 +00:00
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, PrevSpec);
|
|
|
|
break;
|
2006-07-31 05:13:43 +00:00
|
|
|
case tok::kw_int:
|
2006-08-04 04:39:53 +00:00
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw_float:
|
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, PrevSpec);
|
|
|
|
break;
|
2006-07-31 05:13:43 +00:00
|
|
|
case tok::kw_double:
|
2006-08-04 04:39:53 +00:00
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, PrevSpec);
|
|
|
|
break;
|
2006-07-31 05:13:43 +00:00
|
|
|
case tok::kw__Bool:
|
2006-08-04 04:39:53 +00:00
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw__Decimal32:
|
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw__Decimal64:
|
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, PrevSpec);
|
|
|
|
break;
|
|
|
|
case tok::kw__Decimal128:
|
|
|
|
isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, PrevSpec);
|
2006-07-31 05:13:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
//case tok::kw_struct:
|
|
|
|
//case tok::kw_union:
|
|
|
|
//case tok::kw_enum:
|
2006-08-04 05:25:55 +00:00
|
|
|
|
2006-08-06 17:24:14 +00:00
|
|
|
//case tok::identifier:
|
|
|
|
// TODO: handle typedef names.
|
|
|
|
|
2006-08-04 05:25:55 +00:00
|
|
|
// type-qualifier
|
|
|
|
case tok::kw_const:
|
|
|
|
isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
|
|
|
|
break;
|
|
|
|
case tok::kw_volatile:
|
|
|
|
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
|
|
|
|
break;
|
|
|
|
case tok::kw_restrict:
|
|
|
|
isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
|
|
|
|
break;
|
2006-08-04 04:39:53 +00:00
|
|
|
|
|
|
|
// function-specifier
|
|
|
|
case tok::kw_inline:
|
2006-08-05 03:28:50 +00:00
|
|
|
// 'inline inline' is ok.
|
|
|
|
DS.FS_inline_specified = true;
|
2006-08-04 04:39:53 +00:00
|
|
|
break;
|
2006-07-31 05:13:43 +00:00
|
|
|
}
|
2006-08-04 04:39:53 +00:00
|
|
|
// If the specifier combination wasn't legal, issue a diagnostic.
|
|
|
|
if (isInvalid) {
|
|
|
|
assert(PrevSpec && "Method did not return previous specifier!");
|
2006-08-04 05:25:55 +00:00
|
|
|
if (isInvalid == 1) // Error.
|
|
|
|
Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
|
|
|
|
else // extwarn.
|
|
|
|
Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
|
2006-08-04 04:39:53 +00:00
|
|
|
}
|
|
|
|
ConsumeToken();
|
2006-07-31 05:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-06 17:24:14 +00:00
|
|
|
/// isDeclarationSpecifier() - Return true if the current token is part of a
|
|
|
|
/// declaration specifier.
|
|
|
|
bool Parser::isDeclarationSpecifier() const {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
default: return false;
|
|
|
|
// storage-class-specifier
|
|
|
|
case tok::kw_typedef:
|
|
|
|
case tok::kw_extern:
|
|
|
|
case tok::kw_static:
|
|
|
|
case tok::kw_auto:
|
|
|
|
case tok::kw_register:
|
|
|
|
case tok::kw___thread:
|
|
|
|
|
|
|
|
// type-specifiers
|
|
|
|
case tok::kw_short:
|
|
|
|
case tok::kw_long:
|
|
|
|
case tok::kw_signed:
|
|
|
|
case tok::kw_unsigned:
|
|
|
|
case tok::kw__Complex:
|
|
|
|
case tok::kw__Imaginary:
|
|
|
|
case tok::kw_void:
|
|
|
|
case tok::kw_char:
|
|
|
|
case tok::kw_int:
|
|
|
|
case tok::kw_float:
|
|
|
|
case tok::kw_double:
|
|
|
|
case tok::kw__Bool:
|
|
|
|
case tok::kw__Decimal32:
|
|
|
|
case tok::kw__Decimal64:
|
|
|
|
case tok::kw__Decimal128:
|
|
|
|
|
|
|
|
// struct-or-union-specifier
|
|
|
|
case tok::kw_struct:
|
|
|
|
case tok::kw_union:
|
|
|
|
// enum-specifier
|
|
|
|
case tok::kw_enum:
|
|
|
|
// type-qualifier
|
|
|
|
case tok::kw_const:
|
|
|
|
case tok::kw_volatile:
|
|
|
|
case tok::kw_restrict:
|
|
|
|
// function-specifier
|
|
|
|
case tok::kw_inline:
|
|
|
|
return true;
|
|
|
|
// typedef-name
|
|
|
|
case tok::identifier:
|
|
|
|
// FIXME: if this is a typedef return true.
|
|
|
|
return false;
|
|
|
|
// TODO: Attributes.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-04 04:39:53 +00:00
|
|
|
|
2006-07-31 05:13:43 +00:00
|
|
|
/// ParseDeclarator
|
|
|
|
/// declarator: [C99 6.7.5]
|
|
|
|
/// pointer[opt] direct-declarator
|
|
|
|
///
|
|
|
|
/// pointer: [C99 6.7.5]
|
|
|
|
/// '*' type-qualifier-list[opt]
|
|
|
|
/// '*' type-qualifier-list[opt] pointer
|
|
|
|
///
|
2006-08-06 00:02:28 +00:00
|
|
|
void Parser::ParseDeclarator(Declarator &D) {
|
2006-07-31 05:13:43 +00:00
|
|
|
while (Tok.getKind() == tok::star) { // '*' -> pointer.
|
|
|
|
ConsumeToken(); // Eat the *.
|
2006-08-05 06:26:47 +00:00
|
|
|
DeclSpec DS;
|
|
|
|
ParseTypeQualifierListOpt(DS);
|
|
|
|
// TODO: do something with DS.
|
2006-07-31 05:13:43 +00:00
|
|
|
}
|
|
|
|
|
2006-08-06 00:02:28 +00:00
|
|
|
ParseDirectDeclarator(D);
|
2006-07-31 05:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTypeQualifierListOpt
|
|
|
|
/// type-qualifier-list: [C99 6.7.5]
|
|
|
|
/// type-qualifier
|
|
|
|
/// [GNU] attributes [TODO]
|
|
|
|
/// type-qualifier-list type-qualifier
|
|
|
|
/// [GNU] type-qualifier-list attributes [TODO]
|
|
|
|
///
|
2006-08-05 06:26:47 +00:00
|
|
|
void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
|
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
2006-07-31 05:13:43 +00:00
|
|
|
while (1) {
|
2006-08-05 06:26:47 +00:00
|
|
|
int isInvalid = false;
|
|
|
|
const char *PrevSpec = 0;
|
|
|
|
|
2006-07-31 05:13:43 +00:00
|
|
|
switch (Tok.getKind()) {
|
2006-08-05 06:26:47 +00:00
|
|
|
default:
|
|
|
|
// If this is not a declaration specifier token, we're done reading decl
|
|
|
|
// specifiers. First verify that DeclSpec's are consistent.
|
|
|
|
DS.Finish(StartLoc, Diags, getLang());
|
|
|
|
return;
|
2006-07-31 05:13:43 +00:00
|
|
|
// TODO: attributes.
|
|
|
|
case tok::kw_const:
|
2006-08-05 06:26:47 +00:00
|
|
|
isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , PrevSpec, getLang())*2;
|
|
|
|
break;
|
2006-07-31 05:13:43 +00:00
|
|
|
case tok::kw_volatile:
|
2006-08-05 06:26:47 +00:00
|
|
|
isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, PrevSpec, getLang())*2;
|
|
|
|
break;
|
2006-07-31 05:13:43 +00:00
|
|
|
case tok::kw_restrict:
|
2006-08-05 06:26:47 +00:00
|
|
|
isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, PrevSpec, getLang())*2;
|
2006-07-31 05:13:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-08-05 06:26:47 +00:00
|
|
|
|
|
|
|
// If the specifier combination wasn't legal, issue a diagnostic.
|
|
|
|
if (isInvalid) {
|
|
|
|
assert(PrevSpec && "Method did not return previous specifier!");
|
|
|
|
if (isInvalid == 1) // Error.
|
|
|
|
Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
|
|
|
|
else // extwarn.
|
|
|
|
Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
|
|
|
|
}
|
|
|
|
ConsumeToken();
|
2006-07-31 05:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ParseDirectDeclarator
|
|
|
|
/// direct-declarator: [C99 6.7.5]
|
|
|
|
/// identifier
|
|
|
|
/// '(' declarator ')'
|
|
|
|
/// [GNU] '(' attributes declarator ')'
|
2006-08-06 18:30:15 +00:00
|
|
|
/// [C90] direct-declarator '[' constant-expression[opt] ']'
|
|
|
|
/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
|
|
|
|
/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
|
|
|
|
/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
|
|
|
|
/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
|
2006-07-31 05:13:43 +00:00
|
|
|
/// direct-declarator '(' parameter-type-list ')'
|
|
|
|
/// direct-declarator '(' identifier-list[opt] ')'
|
|
|
|
/// [GNU] direct-declarator '(' parameter-forward-declarations
|
|
|
|
/// parameter-type-list[opt] ')'
|
|
|
|
///
|
2006-08-06 17:24:14 +00:00
|
|
|
void Parser::ParseDirectDeclarator(Declarator &D) {
|
|
|
|
// Parse the first direct-declarator seen.
|
|
|
|
if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) {
|
|
|
|
assert(Tok.getIdentifierInfo() && "Not an identifier?");
|
|
|
|
D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
|
|
|
|
ConsumeToken();
|
|
|
|
} else if (Tok.getKind() == tok::l_paren) {
|
|
|
|
// direct-declarator: '(' declarator ')'
|
|
|
|
// direct-declarator: '(' attributes declarator ')' [TODO]
|
|
|
|
// Example: 'char (*X)' or 'int (*XX)(void)'
|
|
|
|
ParseParenDeclarator(D);
|
|
|
|
} else if (Tok.getKind() == tok::l_square &&
|
|
|
|
D.mayOmitIdentifier()) {
|
|
|
|
// direct-abstract-declarator[opt] '[' assignment-expression[opt] ']'
|
|
|
|
// direct-abstract-declarator[opt] '[' '*' ']'
|
|
|
|
|
|
|
|
// direct-abstract-declarator was not specified. Remember that this is the
|
|
|
|
// place where the identifier would have been.
|
|
|
|
D.SetIdentifier(0, Tok.getLocation());
|
|
|
|
// Don't consume the '[', handle it below.
|
|
|
|
} else if (D.mayOmitIdentifier()) {
|
|
|
|
// This could be something simple like "int" (in which case the declarator
|
|
|
|
// portion is empty), if an abstract-declarator is allowed.
|
|
|
|
D.SetIdentifier(0, Tok.getLocation());
|
|
|
|
} else {
|
|
|
|
// expected identifier or '(' or '['.
|
|
|
|
assert(0 && "ERROR: should recover!");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(D.isPastIdentifier() &&
|
|
|
|
"Haven't past the location of the identifier yet?");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (Tok.getKind() == tok::l_paren) {
|
|
|
|
ParseParenDeclarator(D);
|
|
|
|
} else if (Tok.getKind() == tok::l_square) {
|
2006-08-06 18:30:15 +00:00
|
|
|
ParseBracketDeclarator(D);
|
2006-08-06 17:24:14 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseParenDeclarator - We parsed the declarator D up to a paren. This may
|
|
|
|
/// either be before the identifier (in which case these are just grouping
|
|
|
|
/// parens for precedence) or it may be after the identifier, in which case
|
|
|
|
/// these are function arguments.
|
|
|
|
///
|
|
|
|
/// This method also handles this portion of the grammar:
|
2006-07-31 05:13:43 +00:00
|
|
|
/// parameter-type-list: [C99 6.7.5]
|
|
|
|
/// parameter-list
|
|
|
|
/// parameter-list ',' '...'
|
|
|
|
///
|
|
|
|
/// parameter-list: [C99 6.7.5]
|
|
|
|
/// parameter-declaration
|
|
|
|
/// parameter-list ',' parameter-declaration
|
|
|
|
///
|
|
|
|
/// parameter-declaration: [C99 6.7.5]
|
|
|
|
/// declaration-specifiers declarator
|
2006-08-06 17:24:14 +00:00
|
|
|
/// [GNU] declaration-specifiers declarator attributes [TODO]
|
2006-07-31 05:13:43 +00:00
|
|
|
/// declaration-specifiers abstract-declarator[opt]
|
2006-08-06 17:24:14 +00:00
|
|
|
/// [GNU] declaration-specifiers abstract-declarator[opt] attributes [TODO]
|
2006-07-31 05:13:43 +00:00
|
|
|
///
|
|
|
|
/// identifier-list: [C99 6.7.5]
|
|
|
|
/// identifier
|
|
|
|
/// identifier-list ',' identifier
|
|
|
|
///
|
2006-08-06 17:24:14 +00:00
|
|
|
void Parser::ParseParenDeclarator(Declarator &D) {
|
|
|
|
ConsumeParen();
|
2006-08-05 06:26:47 +00:00
|
|
|
|
2006-08-06 17:24:14 +00:00
|
|
|
// If we haven't past the identifier yet (or where the identifier would be
|
|
|
|
// stored, if this is an abstract declarator), then this is probably just
|
|
|
|
// grouping parens.
|
|
|
|
if (!D.isPastIdentifier()) {
|
|
|
|
// Okay, this is probably a grouping paren. However, if this could be an
|
|
|
|
// abstract-declarator, then this could also be the start of function
|
|
|
|
// arguments (consider 'void()').
|
|
|
|
bool isGrouping;
|
|
|
|
|
|
|
|
if (!D.mayOmitIdentifier()) {
|
|
|
|
// If this can't be an abstract-declarator, this *must* be a grouping
|
|
|
|
// paren, because we haven't seen the identifier yet.
|
|
|
|
isGrouping = true;
|
|
|
|
} else if (Tok.getKind() == tok::r_paren || // 'int()' is a function.
|
|
|
|
isDeclarationSpecifier()) { // 'int(int)' is a function.
|
|
|
|
|
|
|
|
isGrouping = false;
|
2006-08-05 06:26:47 +00:00
|
|
|
} else {
|
2006-08-06 17:24:14 +00:00
|
|
|
// Otherwise, 'int (*X)', this is a grouping paren.
|
|
|
|
isGrouping = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a grouping paren, handle:
|
|
|
|
// direct-declarator: '(' declarator ')'
|
|
|
|
// direct-declarator: '(' attributes declarator ')' [TODO]
|
|
|
|
if (isGrouping) {
|
|
|
|
ParseDeclarator(D);
|
|
|
|
// expected ')': skip until we find ')'.
|
|
|
|
if (Tok.getKind() != tok::r_paren)
|
|
|
|
assert(0 && "Recover!");
|
|
|
|
ConsumeParen();
|
|
|
|
return;
|
2006-08-05 06:26:47 +00:00
|
|
|
}
|
2006-08-06 17:24:14 +00:00
|
|
|
|
|
|
|
// Okay, if this wasn't a grouping paren, it must be the start of a function
|
|
|
|
// argument list. Recognize that this will never have an identifier (and
|
|
|
|
// where it would be), then fall through to the handling of argument lists.
|
|
|
|
D.SetIdentifier(0, Tok.getLocation());
|
2006-08-05 06:26:47 +00:00
|
|
|
}
|
|
|
|
|
2006-08-06 17:24:14 +00:00
|
|
|
// Okay, this is the parameter list of a function definition, or it is an
|
|
|
|
// identifier list of a K&R-style function.
|
|
|
|
|
|
|
|
// FIXME: enter function-declaration scope, limiting any declarators for
|
|
|
|
// arguments to the function scope.
|
|
|
|
// NOTE: better to only create a scope if not '()'
|
|
|
|
bool isVariadic;
|
|
|
|
bool HasPrototype;
|
|
|
|
if (Tok.getKind() == tok::r_paren) {
|
|
|
|
// int() -> no prototype, no '...'.
|
|
|
|
isVariadic = false;
|
|
|
|
HasPrototype = false;
|
|
|
|
} else if (Tok.getKind() == tok::identifier &&
|
|
|
|
0/*TODO: !isatypedefname(Tok.getIdentifierInfo())*/) {
|
|
|
|
// Identifier list. Note that '(' identifier-list ')' is only allowed for
|
|
|
|
// normal declarators, not for abstract-declarators.
|
|
|
|
assert(D.isPastIdentifier() && "Identifier (if present) must be passed!");
|
|
|
|
|
|
|
|
// If there was no identifier specified, either we are in an
|
|
|
|
// abstract-declarator, or we are in a parameter declarator which was found
|
|
|
|
// to be abstract. In abstract-declarators, identifier lists are not valid,
|
|
|
|
// diagnose this.
|
|
|
|
if (!D.getIdentifier())
|
|
|
|
Diag(Tok, diag::ext_ident_list_in_param);
|
|
|
|
|
|
|
|
// FIXME: Remember token.
|
|
|
|
ConsumeToken();
|
|
|
|
while (Tok.getKind() == tok::comma) {
|
|
|
|
// Eat the comma.
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// FIXME: if not identifier, consume until ')' then break.
|
|
|
|
assert(Tok.getKind() == tok::identifier);
|
|
|
|
|
|
|
|
// Eat the id.
|
|
|
|
// FIXME: remember it!
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: if not identifier, consume until ')' then break.
|
|
|
|
assert(Tok.getKind() == tok::r_paren);
|
|
|
|
|
|
|
|
// K&R 'prototype'.
|
|
|
|
isVariadic = false;
|
|
|
|
HasPrototype = false;
|
|
|
|
} else {
|
|
|
|
isVariadic = false;
|
|
|
|
bool ReadArg = false;
|
|
|
|
// Finally, a normal, non-empty parameter type list.
|
|
|
|
while (1) {
|
|
|
|
if (Tok.getKind() == tok::ellipsis) {
|
|
|
|
isVariadic = true;
|
|
|
|
|
|
|
|
// Check to see if this is "void(...)" which is not allowed.
|
|
|
|
if (!ReadArg) {
|
2006-08-06 18:30:15 +00:00
|
|
|
// Otherwise, parse parameter type list. If it starts with an
|
|
|
|
// ellipsis, diagnose the malformed function.
|
2006-08-06 17:24:14 +00:00
|
|
|
Diag(Tok, diag::err_ellipsis_first_arg);
|
|
|
|
isVariadic = false; // Treat this like 'void()'.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume the ellipsis.
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadArg = true;
|
|
|
|
|
|
|
|
// Parse the declaration-specifiers.
|
|
|
|
DeclSpec DS;
|
|
|
|
ParseDeclarationSpecifiers(DS);
|
|
|
|
|
|
|
|
// Parse the declarator. This is "PrototypeContext", because we must
|
|
|
|
// accept either 'declarator' or 'abstract-declarator' here.
|
|
|
|
Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
|
|
|
|
ParseDeclarator(DeclaratorInfo);
|
|
|
|
|
|
|
|
// TODO: do something with the declarator, if it is valid.
|
|
|
|
|
|
|
|
// If the next token is a comma, consume it and keep reading arguments.
|
|
|
|
if (Tok.getKind() != tok::comma) break;
|
|
|
|
|
|
|
|
// Consume the comma.
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
HasPrototype = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// expected ')': skip until we find ')'.
|
|
|
|
if (Tok.getKind() != tok::r_paren)
|
|
|
|
assert(0 && "Recover!");
|
|
|
|
ConsumeParen();
|
2006-07-31 05:13:43 +00:00
|
|
|
}
|
2006-08-06 17:24:14 +00:00
|
|
|
|
2006-08-06 18:30:15 +00:00
|
|
|
|
|
|
|
/// [C90] direct-declarator '[' constant-expression[opt] ']'
|
|
|
|
/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
|
|
|
|
/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
|
|
|
|
/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
|
|
|
|
/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
|
|
|
|
void Parser::ParseBracketDeclarator(Declarator &D) {
|
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
|
|
|
ConsumeSquare();
|
|
|
|
|
|
|
|
// If valid, this location is the position where we read the 'static' keyword.
|
|
|
|
SourceLocation StaticLoc;
|
|
|
|
if (Tok.getKind() == tok::kw_static) {
|
|
|
|
StaticLoc = Tok.getLocation();
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a type-qualifier-list, read it now.
|
|
|
|
DeclSpec DS;
|
|
|
|
ParseTypeQualifierListOpt(DS);
|
|
|
|
// TODO: do something with DS.
|
|
|
|
|
|
|
|
// If we haven't already read 'static', check to see if there is one after the
|
|
|
|
// type-qualifier-list.
|
|
|
|
if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) {
|
|
|
|
StaticLoc = Tok.getLocation();
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle "direct-declarator [ type-qual-list[opt] * ]".
|
|
|
|
// Check that the ']' token is present to avoid incorrectly parsing
|
|
|
|
// expressions starting with '*' as [*].
|
|
|
|
bool isStar = false;
|
|
|
|
if (Tok.getKind() == tok::star /*FIXME: && nexttok == tok::r_square*/) {
|
|
|
|
if (StaticLoc.isValid())
|
|
|
|
Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
|
|
|
|
StaticLoc = SourceLocation(); // Drop the static.
|
|
|
|
isStar = true;
|
|
|
|
ConsumeToken();
|
|
|
|
} else if (Tok.getKind() != tok::r_square) {
|
|
|
|
// Parse the assignment-expression now.
|
|
|
|
assert(0 && "expr parsing not impl yet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsumeSquare();
|
|
|
|
|
|
|
|
// If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
|
|
|
|
// it was not a constant expression.
|
|
|
|
if (!getLang().C99) {
|
|
|
|
// TODO: check C90 array constant exprness.
|
2006-08-06 18:33:32 +00:00
|
|
|
if (isStar || StaticLoc.isValid() || 0/*constantexpr*/)
|
|
|
|
Diag(StartLoc, diag::ext_c99_array_usage);
|
2006-08-06 18:30:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|