blob: a6b757d706685bb244da85ab78a981e218c745f0 [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
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 semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000015#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000016#include "clang/AST/Decl.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
18#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000019#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000022#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/TargetInfo.h"
24#include "llvm/ADT/SmallString.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000025using namespace llvm;
26using namespace clang;
27
Steve Naroffdf7855b2007-02-21 23:46:25 +000028/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000029/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
30/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
31/// multiple tokens. However, the common case is that StringToks points to one
32/// string.
33///
34Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000035Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000036 assert(NumStringToks && "Must have at least one string!");
37
Steve Naroff4f88b312007-03-13 22:37:02 +000038 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000041
Chris Lattner5b183d82006-11-10 05:03:26 +000042 SmallVector<SourceLocation, 4> StringTokLocs;
43 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000045
46 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000047 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000048
Chris Lattner5b183d82006-11-10 05:03:26 +000049 // FIXME: use factory.
Chris Lattner5b183d82006-11-10 05:03:26 +000050 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000051 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Narofff1e53692007-03-23 22:27:02 +000052 Literal.AnyWide, t);
Chris Lattner5b183d82006-11-10 05:03:26 +000053}
54
Chris Lattnere168f762006-11-10 05:29:30 +000055
Chris Lattnerac18be92006-11-20 06:49:47 +000056/// ParseIdentifierExpr - The parser read an identifier in expression context,
57/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
58/// identifier is used in an function call context.
59Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
60 IdentifierInfo &II,
61 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000062 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000063 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000064 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000065 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000066 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000067 if (HasTrailingLParen &&
68 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000069 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000070 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000071 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000072 // If this name wasn't predeclared and if this is not a function call,
73 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000074 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000075 }
Chris Lattner17ed4872006-11-20 04:58:19 +000076 }
77
Steve Naroff46ba1eb2007-04-03 23:13:13 +000078 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
79 return new DeclRefExpr(VD, VD->getType());
80 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000081 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
82
83 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000084}
Chris Lattnere168f762006-11-10 05:29:30 +000085
Chris Lattner17ed4872006-11-20 04:58:19 +000086Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
87 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000088 switch (Kind) {
89 default:
90 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +000091 case tok::char_constant: // constant: character-constant
Chris Lattner17ed4872006-11-20 04:58:19 +000092 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000093 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]
Chris Lattner17ed4872006-11-20 04:58:19 +000096 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +000097 }
98}
99
Steve Naroff8160ea22007-03-06 01:09:46 +0000100Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000101 // fast path for a single digit (which is quite common). A single digit
102 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
103 if (Tok.getLength() == 1) {
104 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
105 return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy));
106 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000107 SmallString<512> IntegerBuffer;
108 IntegerBuffer.resize(Tok.getLength());
109 const char *ThisTokBegin = &IntegerBuffer[0];
110
111 // Get the spelling of the token, which eliminates trigraphs, etc. Notes:
112 // - We know that ThisTokBuf points to a buffer that is big enough for the
113 // whole token and 'spelled' tokens can only shrink.
114 // - In practice, the local buffer is only used when the spelling doesn't
115 // match the original token (which is rare). The common case simply returns
116 // a pointer to a *constant* buffer (avoiding a copy).
117
118 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000119 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000120 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000121 if (Literal.hadError)
122 return ExprResult(true);
123
Steve Naroff09ef4742007-03-09 23:16:33 +0000124 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000125 QualType t;
Steve Naroff09ef4742007-03-09 23:16:33 +0000126 if (Literal.hasSuffix()) {
127 if (Literal.isLong)
128 t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
129 else if (Literal.isLongLong)
130 t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
131 else
132 t = Context.UnsignedIntTy;
133 } else {
134 t = Context.IntTy; // implicit type is "int"
135 }
Steve Naroff451d8f162007-03-12 23:22:38 +0000136 uintmax_t val;
137 if (Literal.GetIntegerValue(val)) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000138 return new IntegerLiteral(val, t);
Steve Naroff09ef4742007-03-09 23:16:33 +0000139 }
140 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000141 // FIXME: fill in the value and compute the real type...
142 return new FloatingLiteral(7.7, Context.FloatTy);
Steve Naroff09ef4742007-03-09 23:16:33 +0000143 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000144 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000145}
146
147Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
148 ExprTy *Val) {
149 return Val;
150}
151
152
153// Unary Operators. 'Tok' is the token for the operator.
154Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
155 ExprTy *Input) {
156 UnaryOperator::Opcode Opc;
157 switch (Op) {
158 default: assert(0 && "Unknown unary op!");
159 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
160 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
161 case tok::amp: Opc = UnaryOperator::AddrOf; break;
162 case tok::star: Opc = UnaryOperator::Deref; break;
163 case tok::plus: Opc = UnaryOperator::Plus; break;
164 case tok::minus: Opc = UnaryOperator::Minus; break;
165 case tok::tilde: Opc = UnaryOperator::Not; break;
166 case tok::exclaim: Opc = UnaryOperator::LNot; break;
167 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
168 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
169 case tok::kw___real: Opc = UnaryOperator::Real; break;
170 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
171 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
172 case tok::kw___extension__:
173 return Input;
174 //Opc = UnaryOperator::Extension;
175 //break;
176 }
Steve Naroff95af0132007-03-30 23:47:58 +0000177 if (Opc == UnaryOperator::PreInc || Opc == UnaryOperator::PreDec)
178 return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
Steve Naroff47500512007-04-19 23:00:49 +0000179 else if (Opc == UnaryOperator::AddrOf)
180 return CheckAddressOfOperand((Expr *)Input, OpLoc, Opc);
181 else if (Opc == UnaryOperator::Deref)
182 return CheckIndirectionOperand((Expr *)Input, OpLoc, Opc);
Steve Naroff4b7ce032007-04-20 22:26:17 +0000183 else {
184 // handle the arithmetic unary operators (C99 6.5.3.3)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000185 Expr *operand = ImplicitConversion((Expr *)Input);
186 QualType opType = operand->getType();
Steve Naroff4b7ce032007-04-20 22:26:17 +0000187 assert(!opType.isNull() && "no type for arithmetic unary expression");
188 QualType resultType = opType;
189
190 switch (Opc) {
191 case UnaryOperator::Plus:
192 case UnaryOperator::Minus:
193 if (!opType->isArithmeticType()) // C99 6.5.3.3p1
194 return Diag(OpLoc, diag::err_typecheck_unary_expr, opType);
195
196 if (opType->isPromotableIntegerType()) // C99 6.5.3.3p2
197 resultType = Context.IntTy;
198 break;
199 case UnaryOperator::Not: // bitwise complement
200 if (!opType->isIntegralType()) // C99 6.5.3.3p1
201 return Diag(OpLoc, diag::err_typecheck_unary_expr, opType);
202
203 if (opType->isPromotableIntegerType()) // C99 6.5.3.3p2
204 resultType = Context.IntTy;
205 break;
206 case UnaryOperator::LNot: // logical negation
207 if (!opType->isScalarType()) // C99 6.5.3.3p1
208 return Diag(OpLoc, diag::err_typecheck_unary_expr, opType);
209
210 if (opType->isPromotableIntegerType()) // C99 6.5.3.3p2
211 resultType = Context.IntTy;
212 break;
213 case UnaryOperator::SizeOf: // C99 6.5.3.4 TODO
214 break;
215 default:
216 break;
217 }
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000218 return new UnaryOperator(operand, Opc, resultType);
Steve Naroff4b7ce032007-04-20 22:26:17 +0000219 }
Chris Lattnere168f762006-11-10 05:29:30 +0000220}
221
222Action::ExprResult Sema::
223ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
224 SourceLocation LParenLoc, TypeTy *Ty,
225 SourceLocation RParenLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000226 // If error parsing type, ignore.
227 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000228
229 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000230 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000231
232 if (isa<FunctionType>(ArgTy) && isSizeof) {
233 // alignof(function) is allowed.
234 Diag(OpLoc, diag::ext_sizeof_function_type);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000235 return new IntegerLiteral(1, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000236 } else if (ArgTy->isVoidType()) {
237 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
238 } else if (ArgTy->isIncompleteType()) {
239 std::string TypeName;
240 ArgTy->getAsString(TypeName);
241 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
242 diag::err_alignof_incomplete_type, TypeName);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000243 return new IntegerLiteral(0, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000244 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000245 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
246 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, Context.getSizeType());
Chris Lattnere168f762006-11-10 05:29:30 +0000247}
248
249
250Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
251 tok::TokenKind Kind,
252 ExprTy *Input) {
253 UnaryOperator::Opcode Opc;
254 switch (Kind) {
255 default: assert(0 && "Unknown unary op!");
256 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
257 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
258 }
Steve Naroff95af0132007-03-30 23:47:58 +0000259 return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
Chris Lattnere168f762006-11-10 05:29:30 +0000260}
261
262Action::ExprResult Sema::
263ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
264 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000265 QualType t1 = ((Expr *)Base)->getType();
266 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000267
268 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000269 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000270
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000271 QualType canonT1 = t1.getCanonicalType();
272 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000273
Steve Naroffc1aadb12007-03-28 21:49:40 +0000274 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
275 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000276 // in the subscript position. As a result, we need to derive the array base
277 // and index from the expression types.
278
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000279 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000280 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
281 baseType = canonT1;
282 indexType = canonT2;
283 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
284 baseType = canonT2;
285 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000286 } else
287 return Diag(LLoc, diag::err_typecheck_subscript_value);
288
Steve Naroffc1aadb12007-03-28 21:49:40 +0000289 // C99 6.5.2.1p1
290 if (!indexType->isIntegralType())
Steve Narofff1e53692007-03-23 22:27:02 +0000291 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000292
Steve Naroff95af0132007-03-30 23:47:58 +0000293 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000294 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000295 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
296 resultType = ary->getElementType();
297 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
298 resultType = ary->getPointeeType();
299 // in practice, the following check catches trying to index a pointer
300 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000301 if (!resultType->isObjectType())
302 return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000303 }
304 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000305}
306
307Action::ExprResult Sema::
308ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
309 tok::TokenKind OpKind, SourceLocation MemberLoc,
310 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000311 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000312
313 assert(!qualifiedType.isNull() && "no type for member expression");
314
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000315 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000316
317 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000318 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
319 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000320 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000321 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000322 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
323 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000324 if (!isa<RecordType>(canonType))
325 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
326
327 // get the struct/union definition from the type.
328 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000329
Steve Naroff92e30f82007-04-02 22:35:25 +0000330 if (canonType->isIncompleteType())
331 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000332
Steve Naroff92e30f82007-04-02 22:35:25 +0000333 FieldDecl *MemberDecl = RD->getMember(&Member);
334 if (!MemberDecl)
335 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
336
337 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000338}
339
340/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
341/// This provides the location of the left/right parens and a list of comma
342/// locations.
343Action::ExprResult Sema::
344ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
345 ExprTy **Args, unsigned NumArgs,
346 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
347 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs);
348}
349
350Action::ExprResult Sema::
351ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
352 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000353 // If error parsing type, ignore.
354 if (Ty == 0) return true;
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000355 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
Chris Lattnere168f762006-11-10 05:29:30 +0000356}
357
358
359
360// Binary Operators. 'Tok' is the token for the operator.
361Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
362 ExprTy *LHS, ExprTy *RHS) {
363 BinaryOperator::Opcode Opc;
364 switch (Kind) {
365 default: assert(0 && "Unknown binop!");
366 case tok::star: Opc = BinaryOperator::Mul; break;
367 case tok::slash: Opc = BinaryOperator::Div; break;
368 case tok::percent: Opc = BinaryOperator::Rem; break;
369 case tok::plus: Opc = BinaryOperator::Add; break;
370 case tok::minus: Opc = BinaryOperator::Sub; break;
371 case tok::lessless: Opc = BinaryOperator::Shl; break;
372 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
373 case tok::lessequal: Opc = BinaryOperator::LE; break;
374 case tok::less: Opc = BinaryOperator::LT; break;
375 case tok::greaterequal: Opc = BinaryOperator::GE; break;
376 case tok::greater: Opc = BinaryOperator::GT; break;
377 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
378 case tok::equalequal: Opc = BinaryOperator::EQ; break;
379 case tok::amp: Opc = BinaryOperator::And; break;
380 case tok::caret: Opc = BinaryOperator::Xor; break;
381 case tok::pipe: Opc = BinaryOperator::Or; break;
382 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
383 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
384 case tok::equal: Opc = BinaryOperator::Assign; break;
385 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
386 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
387 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
388 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
389 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
390 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
391 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
392 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
393 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
394 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
395 case tok::comma: Opc = BinaryOperator::Comma; break;
396 }
Steve Narofff1e53692007-03-23 22:27:02 +0000397
398 // perform implicit conversions (C99 6.3)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000399 Expr *lhs = ImplicitConversion((Expr*)LHS);
400 Expr *rhs = ImplicitConversion((Expr*)RHS);
Chris Lattnere168f762006-11-10 05:29:30 +0000401
Steve Naroff26c8ea52007-03-21 21:08:52 +0000402 if (BinaryOperator::isMultiplicativeOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000403 return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000404 else if (BinaryOperator::isAdditiveOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000405 CheckAdditiveOperands(lhs, rhs);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000406 else if (BinaryOperator::isShiftOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000407 CheckShiftOperands(lhs, rhs);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000408 else if (BinaryOperator::isRelationalOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000409 CheckRelationalOperands(lhs, rhs);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000410 else if (BinaryOperator::isEqualityOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000411 CheckEqualityOperands(lhs, rhs);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000412 else if (BinaryOperator::isBitwiseOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000413 CheckBitwiseOperands(lhs, rhs);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000414 else if (BinaryOperator::isLogicalOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000415 CheckLogicalOperands(lhs, rhs);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000416
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000417 return new BinaryOperator(lhs, rhs, Opc);
Chris Lattnere168f762006-11-10 05:29:30 +0000418}
419
420/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
421/// in the case of a the GNU conditional expr extension.
422Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
423 SourceLocation ColonLoc,
424 ExprTy *Cond, ExprTy *LHS,
425 ExprTy *RHS) {
426 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS);
427}
428
Steve Naroff4b7ce032007-04-20 22:26:17 +0000429/// ImplicitConversion - Performs various conversions that are common to most
430/// operators. At present, this routine only handles conversions that require
431/// synthesizing an expression/type. Arithmetic type promotions are done locally,
432/// since they don't require a new expression.
Steve Narofff1e53692007-03-23 22:27:02 +0000433Expr *Sema::ImplicitConversion(Expr *E) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000434 QualType t = E->getType();
Steve Naroff4b7ce032007-04-20 22:26:17 +0000435 assert(!t.isNull() && "no type for implicit conversion");
436
437 if (t->isFunctionType()) // C99 6.3.2.1p4
438 return new UnaryOperator(E, UnaryOperator::AddrOf, Context.getPointerType(t));
439 else if (t->isArrayType()) { // C99 6.3.2.1p3
440 QualType elt = cast<ArrayType>(t)->getElementType();
441 QualType convertedType = Context.getPointerType(elt);
442 return new UnaryOperator(E, UnaryOperator::AddrOf, convertedType);
443 }
Steve Narofff1e53692007-03-23 22:27:02 +0000444 return E;
445}
446
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000447Action::ExprResult Sema::CheckMultiplicativeOperands(
448 Expr *lexpr, Expr *rexpr, SourceLocation loc, unsigned code)
449{
450 lexpr = ImplicitConversion((Expr *)lexpr);
451 rexpr = ImplicitConversion((Expr *)rexpr);
452 QualType ltype = lexpr->getType();
453 QualType rtype = rexpr->getType();
454 assert(!ltype.isNull() && "no left type for CheckMultiplicativeOperands()");
455 assert(!rtype.isNull() && "no right type for CheckMultiplicativeOperands()");
456
457 if ((BinaryOperator::Opcode)code == BinaryOperator::Rem) {
458 if (!ltype->isIntegralType() || !rtype->isIntegralType())
459 return Diag(loc, diag::err_typecheck_invalid_operands);
460 } else {
461 if (!ltype->isArithmeticType() || !rtype->isArithmeticType())
462 return Diag(loc, diag::err_typecheck_invalid_operands);
463 }
464 return new BinaryOperator(lexpr, rexpr, (BinaryOperator::Opcode)code);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000465}
466
Steve Naroff95af0132007-03-30 23:47:58 +0000467Action::ExprResult Sema::CheckAdditiveOperands(Expr *op1, Expr *op2) {
468 return false;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000469}
470
Steve Naroff95af0132007-03-30 23:47:58 +0000471Action::ExprResult Sema::CheckShiftOperands(Expr *op1, Expr *op2) {
472 return false;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000473}
474
Steve Naroff95af0132007-03-30 23:47:58 +0000475Action::ExprResult Sema::CheckRelationalOperands(Expr *op1, Expr *op2) {
476 return false;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000477}
478
Steve Naroff95af0132007-03-30 23:47:58 +0000479Action::ExprResult Sema::CheckEqualityOperands(Expr *op1, Expr *op2) {
480 return false;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000481}
482
Steve Naroff95af0132007-03-30 23:47:58 +0000483Action::ExprResult Sema::CheckBitwiseOperands(Expr *op1, Expr *op2) {
484 return false;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000485}
486
Steve Naroff95af0132007-03-30 23:47:58 +0000487Action::ExprResult Sema::CheckLogicalOperands(Expr *op1, Expr *op2) {
488 return false;
489}
490
491Action::ExprResult
492Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc,
Steve Naroff78403362007-04-02 22:55:05 +0000493 unsigned OpCode) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000494 QualType qType = op->getType();
Steve Naroff95af0132007-03-30 23:47:58 +0000495
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000496 assert(!qType.isNull() && "no type for increment/decrement expression");
497
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000498 QualType canonType = qType.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000499
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000500 // C99 6.5.2.4p1
501 if (const PointerType *pt = dyn_cast<PointerType>(canonType)) {
502 if (!pt->getPointeeType()->isObjectType()) // C99 6.5.2.4p2, 6.5.6p2
503 return Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, qType);
504 } else if (!canonType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000505 // FIXME: Allow Complex as a GCC extension.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000506 return Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, qType);
507 }
Steve Naroff95af0132007-03-30 23:47:58 +0000508 // At this point, we know we have a real or pointer type. As a result, the
509 // following predicate is overkill (i.e. it will check for types we know we
510 // don't have in this context). Nevertheless, we model the C99 spec closely.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000511 if (!canonType.isModifiableLvalue())
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000512 return Diag(OpLoc, diag::err_typecheck_not_modifiable, qType);
Steve Naroff95af0132007-03-30 23:47:58 +0000513
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000514 return new UnaryOperator(op, (UnaryOperator::Opcode)OpCode, qType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000515}
516
Steve Naroff47500512007-04-19 23:00:49 +0000517/// getDecl - This is currently a helper function for CheckAddressOfOperand().
518/// This routine allows us to typecheck complex/recursive expressions
519/// where the declaration is needed for type checking. Here are some
520/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
521Decl *Sema::getDecl(Expr *e) {
522 switch (e->getStmtClass()) {
523 case Stmt::DeclRefExprClass:
524 return cast<DeclRefExpr>(e)->getDecl();
525 case Stmt::MemberExprClass:
526 return getDecl(cast<MemberExpr>(e)->getBase());
527 case Stmt::ArraySubscriptExprClass:
528 return getDecl(cast<ArraySubscriptExpr>(e)->getBase());
529 case Stmt::CallExprClass:
530 return getDecl(cast<CallExpr>(e)->getCallee());
531 case Stmt::UnaryOperatorClass:
532 return getDecl(cast<UnaryOperator>(e)->getSubExpr());
533 case Stmt::ParenExprClass:
534 return getDecl(cast<ParenExpr>(e)->getSubExpr());
535 default:
536 return 0;
537 }
538}
539
540/// CheckAddressOfOperand - The operand of & must be either a function
541/// designator or an lvalue designating an object. If it is an lvalue, the
542/// object cannot be declared with storage class register or be a bit field.
543/// Note: The usual conversions are *not* applied to the operand of the &
544/// operator, and its result is never an lvalue.
545Action::ExprResult
546Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc, unsigned OpCode) {
547 Decl *dcl = getDecl(op);
548
549 if (!op->isLvalue()) {
550 if (dcl && isa<FunctionDecl>(dcl))
551 ; // C99 6.5.3.2p1: Allow function designators.
552 else
553 return Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
554 } else if (dcl) {
555 // We have an lvalue with a decl. Make sure the decl is not declared
556 // with the register storage-class specifier.
557 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
558 if (vd->getStorageClass() == VarDecl::Register)
559 return Diag(OpLoc, diag::err_typecheck_address_of_register);
560 }
561 // FIXME: add check for bitfields!
562 }
563 // If the operand has type "type", the result has type "pointer to type".
564 return new UnaryOperator(op, (UnaryOperator::Opcode)OpCode,
565 Context.getPointerType(op->getType()));
566}
567
568Action::ExprResult
569Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc, unsigned OpCode) {
570 QualType qType = op->getType();
571
572 assert(!qType.isNull() && "no type for * expression");
573
574 QualType canonType = qType.getCanonicalType();
575
576 return new UnaryOperator(op, (UnaryOperator::Opcode)OpCode, QualType());
577}