blob: 5ae202b3a85c43a0436a306c633934a36af9151d [file] [log] [blame]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001//===--- Expression.cpp - Expression Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expression parsing implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
15#include "clang/Basic/Diagnostic.h"
16using namespace llvm;
17using namespace clang;
18
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000019// C99 6.7.8
20void Parser::ParseInitializer() {
21 // FIXME: STUB.
22 ParseAssignmentExpression();
23}
24
25
26
Chris Lattnerc951dae2006-08-10 04:23:57 +000027Parser::ExprTy Parser::ParseExpression() {
Chris Lattner52a99e52006-08-10 20:56:00 +000028 ParsePostfixExpression();
Chris Lattnerc951dae2006-08-10 04:23:57 +000029 return 0;
30}
31
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000032// Expr that doesn't include commas.
33void Parser::ParseAssignmentExpression() {
34 ParseExpression();
35}
36
Chris Lattner52a99e52006-08-10 20:56:00 +000037/// ParsePostfixExpression
38/// postfix-expression: [C99 6.5.2]
39/// primary-expression
40/// postfix-expression '[' expression ']'
41/// postfix-expression '(' argument-expression-list[opt] ')'
42/// postfix-expression '.' identifier
43/// postfix-expression '->' identifier
44/// postfix-expression '++'
45/// postfix-expression '--'
46/// '(' type-name ')' '{' initializer-list '}'
47/// '(' type-name ')' '{' initializer-list ',' '}'
48///
49/// argument-expression-list: [C99 6.5.2]
50/// argument-expression
51/// argument-expression-list ',' argument-expression
52///
53/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +000054/// identifier
55/// constant
56/// string-literal
57/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +000058/// '__func__' [C99 6.4.2.2]
59/// [GNU] '__FUNCTION__'
60/// [GNU] '__PRETTY_FUNCTION__'
61/// [GNU] '(' compound-statement ')'
62/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
63/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
64/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
65/// assign-expr ')'
66/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
67/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
68/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
69/// [OBC] '@protocol' '(' identifier ')' [TODO]
70/// [OBC] '@encode' '(' type-name ')' [TODO]
71/// [OBC] objc-string-literal [TODO]
72///
73/// constant: [C99 6.4.4]
74/// integer-constant
75/// floating-constant
76/// enumeration-constant -> identifier
77/// character-constant
78///
79/// [GNU] offsetof-member-designator:
80/// [GNU] identifier
81/// [GNU] offsetof-member-designator '.' identifier
82/// [GNU] offsetof-member-designator '[' expression ']'
83///
84void Parser::ParsePostfixExpression() {
85 // First step, parse the primary expression.
86 switch (Tok.getKind()) {
87 // primary-expression
88 case tok::identifier: // primary-expression: identifier
89 // constant: enumeration-constant
90 case tok::numeric_constant: // constant: integer-constant
91 // constant: floating-constant
92 case tok::char_constant: // constant: character-constant
93 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
94 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
95 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
96 ConsumeToken();
97 break;
98 case tok::string_literal: // primary-expression: string-literal
99 ParseStringLiteralExpression();
100 break;
101 case tok::l_paren: // primary-expression: '(' expression ')'
102 ParseParenExpression();
103 break;
104
105 default:
106 Diag(Tok, diag::err_expected_expression);
107 return;
108 }
109}
110
111/// ParseStringLiteralExpression - This handles the various token types that
112/// form string literals, and also handles string concatenation [C99 5.1.1.2,
113/// translation phase #6].
114///
115/// primary-expression: [C99 6.5.1]
116/// string-literal
117void Parser::ParseStringLiteralExpression() {
118 assert(isStringLiteral() && "Not a string literal!");
119 ConsumeStringToken();
120
121 // String concat. Note that keywords like __func__ and __FUNCTION__ aren't
122 // considered to be strings.
123 while (isStringLiteral())
124 ConsumeStringToken();
125}
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000126
Chris Lattnerc951dae2006-08-10 04:23:57 +0000127
128/// ParseParenExpression - C99 c.5.1p5
129/// primary-expression:
130/// '(' expression ')'
131void Parser::ParseParenExpression() {
132 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
133 SourceLocation OpenLoc = Tok.getLocation();
134 ConsumeParen();
135
136 ParseExpression();
137
138 if (Tok.getKind() == tok::r_paren) {
139 ConsumeParen();
140 } else {
141 Diag(Tok, diag::err_expected_rparen);
142 Diag(OpenLoc, diag::err_matching);
143 }
144}