blob: c2c533c8a6c7df085c1e4f4312e0bf7e18b88e86 [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"
Steve Naroff17f76e02007-05-03 21:03:48 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026using namespace llvm;
27using namespace clang;
28
Steve Naroffe4718892007-04-27 18:30:00 +000029// Sema.h avoids including Expr.h. As a result, all the Check* functions take
30// an unsigned which is really an enum. These typedefs provide a short hand
31// notiation for casting (to keep the lines within 80 columns:-)
32typedef BinaryOperator::Opcode BOP;
33typedef UnaryOperator::Opcode UOP;
34
Steve Naroffdf7855b2007-02-21 23:46:25 +000035/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000036/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
37/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
38/// multiple tokens. However, the common case is that StringToks points to one
39/// string.
40///
41Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000042Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000043 assert(NumStringToks && "Must have at least one string!");
44
Steve Naroff4f88b312007-03-13 22:37:02 +000045 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
46 if (Literal.hadError)
47 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000048
Chris Lattner5b183d82006-11-10 05:03:26 +000049 SmallVector<SourceLocation, 4> StringTokLocs;
50 for (unsigned i = 0; i != NumStringToks; ++i)
51 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000052
53 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000054 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000055
Chris Lattner5b183d82006-11-10 05:03:26 +000056 // FIXME: use factory.
Chris Lattner5b183d82006-11-10 05:03:26 +000057 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000058 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Narofff1e53692007-03-23 22:27:02 +000059 Literal.AnyWide, t);
Chris Lattner5b183d82006-11-10 05:03:26 +000060}
61
Chris Lattnere168f762006-11-10 05:29:30 +000062
Chris Lattnerac18be92006-11-20 06:49:47 +000063/// ParseIdentifierExpr - The parser read an identifier in expression context,
64/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
65/// identifier is used in an function call context.
66Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
67 IdentifierInfo &II,
68 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000069 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000070 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000071 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000072 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000073 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000074 if (HasTrailingLParen &&
75 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000076 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000077 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000078 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000079 // If this name wasn't predeclared and if this is not a function call,
80 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000081 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000082 }
Chris Lattner17ed4872006-11-20 04:58:19 +000083 }
84
Steve Naroff46ba1eb2007-04-03 23:13:13 +000085 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
86 return new DeclRefExpr(VD, VD->getType());
87 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000088 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
89
90 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000091}
Chris Lattnere168f762006-11-10 05:29:30 +000092
Chris Lattner17ed4872006-11-20 04:58:19 +000093Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
94 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000095 switch (Kind) {
96 default:
97 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000098 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000099 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
100 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
101 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +0000102 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +0000103 }
104}
105
Steve Naroffae4143e2007-04-26 20:39:23 +0000106Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
107 SmallString<16> CharBuffer;
108 CharBuffer.resize(Tok.getLength());
109 const char *ThisTokBegin = &CharBuffer[0];
110 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
111
112 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
113 Tok.getLocation(), PP);
114 if (Literal.hadError())
115 return ExprResult(true);
116 return new CharacterLiteral(Literal.getValue(), Context.IntTy);
117}
118
Steve Naroff8160ea22007-03-06 01:09:46 +0000119Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000120 // fast path for a single digit (which is quite common). A single digit
121 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
122 if (Tok.getLength() == 1) {
123 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
124 return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy));
125 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000126 SmallString<512> IntegerBuffer;
127 IntegerBuffer.resize(Tok.getLength());
128 const char *ThisTokBegin = &IntegerBuffer[0];
129
130 // Get the spelling of the token, which eliminates trigraphs, etc. Notes:
131 // - We know that ThisTokBuf points to a buffer that is big enough for the
132 // whole token and 'spelled' tokens can only shrink.
133 // - In practice, the local buffer is only used when the spelling doesn't
134 // match the original token (which is rare). The common case simply returns
135 // a pointer to a *constant* buffer (avoiding a copy).
136
137 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000138 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000139 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000140 if (Literal.hadError)
141 return ExprResult(true);
142
Steve Naroff09ef4742007-03-09 23:16:33 +0000143 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000144 QualType t;
Steve Naroff09ef4742007-03-09 23:16:33 +0000145 if (Literal.hasSuffix()) {
146 if (Literal.isLong)
147 t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
148 else if (Literal.isLongLong)
149 t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
150 else
151 t = Context.UnsignedIntTy;
152 } else {
153 t = Context.IntTy; // implicit type is "int"
154 }
Steve Naroff451d8f162007-03-12 23:22:38 +0000155 uintmax_t val;
156 if (Literal.GetIntegerValue(val)) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000157 return new IntegerLiteral(val, t);
Steve Naroff09ef4742007-03-09 23:16:33 +0000158 }
159 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000160 // FIXME: fill in the value and compute the real type...
161 return new FloatingLiteral(7.7, Context.FloatTy);
Steve Naroff09ef4742007-03-09 23:16:33 +0000162 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000163 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000164}
165
166Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
167 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000168 Expr *e = (Expr *)Val;
169 assert((e != 0) && "ParseParenExpr() missing expr");
170 return e;
Chris Lattnere168f762006-11-10 05:29:30 +0000171}
172
Chris Lattnere168f762006-11-10 05:29:30 +0000173Action::ExprResult Sema::
174ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
175 SourceLocation LParenLoc, TypeTy *Ty,
176 SourceLocation RParenLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000177 // If error parsing type, ignore.
178 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000179
180 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000181 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000182
183 if (isa<FunctionType>(ArgTy) && isSizeof) {
184 // alignof(function) is allowed.
185 Diag(OpLoc, diag::ext_sizeof_function_type);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000186 return new IntegerLiteral(1, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000187 } else if (ArgTy->isVoidType()) {
188 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
189 } else if (ArgTy->isIncompleteType()) {
190 std::string TypeName;
191 ArgTy->getAsString(TypeName);
192 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
193 diag::err_alignof_incomplete_type, TypeName);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000194 return new IntegerLiteral(0, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000195 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000196 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
197 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, Context.getSizeType());
Chris Lattnere168f762006-11-10 05:29:30 +0000198}
199
200
201Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
202 tok::TokenKind Kind,
203 ExprTy *Input) {
204 UnaryOperator::Opcode Opc;
205 switch (Kind) {
206 default: assert(0 && "Unknown unary op!");
207 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
208 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
209 }
Steve Naroff35d85152007-05-07 00:24:15 +0000210 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
211 if (result.isNull())
212 return true;
213 return new UnaryOperator((Expr *)Input, Opc, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000214}
215
216Action::ExprResult Sema::
217ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
218 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000219 QualType t1 = ((Expr *)Base)->getType();
220 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000221
222 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000223 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000224
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000225 QualType canonT1 = t1.getCanonicalType();
226 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000227
Steve Naroffc1aadb12007-03-28 21:49:40 +0000228 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
229 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000230 // in the subscript position. As a result, we need to derive the array base
231 // and index from the expression types.
232
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000233 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000234 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
235 baseType = canonT1;
236 indexType = canonT2;
237 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
238 baseType = canonT2;
239 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000240 } else
241 return Diag(LLoc, diag::err_typecheck_subscript_value);
242
Steve Naroffc1aadb12007-03-28 21:49:40 +0000243 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000244 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000245 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000246
Steve Naroff95af0132007-03-30 23:47:58 +0000247 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000248 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000249 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
250 resultType = ary->getElementType();
251 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
252 resultType = ary->getPointeeType();
253 // in practice, the following check catches trying to index a pointer
254 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000255 if (!resultType->isObjectType())
256 return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000257 }
258 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000259}
260
261Action::ExprResult Sema::
262ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
263 tok::TokenKind OpKind, SourceLocation MemberLoc,
264 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000265 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000266
267 assert(!qualifiedType.isNull() && "no type for member expression");
268
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000269 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000270
271 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000272 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
273 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000274 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000275 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000276 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
277 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000278 if (!isa<RecordType>(canonType))
279 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
280
281 // get the struct/union definition from the type.
282 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000283
Steve Naroff92e30f82007-04-02 22:35:25 +0000284 if (canonType->isIncompleteType())
285 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000286
Steve Naroff92e30f82007-04-02 22:35:25 +0000287 FieldDecl *MemberDecl = RD->getMember(&Member);
288 if (!MemberDecl)
289 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
290
291 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000292}
293
294/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
295/// This provides the location of the left/right parens and a list of comma
296/// locations.
297Action::ExprResult Sema::
298ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000299 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000300 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000301 QualType qType = ((Expr *)Fn)->getType();
302
303 assert(!qType.isNull() && "no type for function call expression");
304
Steve Naroff17f76e02007-05-03 21:03:48 +0000305 const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000306
Steve Naroff17f76e02007-05-03 21:03:48 +0000307 assert(funcT && "ParseCallExpr(): not a function type");
Steve Naroffb8c289d2007-05-08 22:18:00 +0000308
Steve Naroff17f76e02007-05-03 21:03:48 +0000309 // If a prototype isn't declared, the parser implicitly defines a func decl
310 QualType resultType = funcT->getResultType();
311
312 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
313 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
314 // assignment, to the types of the corresponding parameter, ...
315
316 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000317 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000318
Steve Naroffb8c289d2007-05-08 22:18:00 +0000319 if (NumArgsInCall < NumArgsInProto)
Steve Naroff1f4d7272007-05-11 04:00:31 +0000320 Diag(LParenLoc, diag::err_typecheck_call_too_few_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000321 else if (NumArgsInCall > NumArgsInProto) {
322 if (!proto->isVariadic())
Steve Naroff1f4d7272007-05-11 04:00:31 +0000323 Diag(LParenLoc, diag::err_typecheck_call_too_many_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000324 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000325 }
326 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000327 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000328 QualType lhsType = proto->getArgType(i);
329 QualType rhsType = ((Expr **)Args)[i]->getType();
330
331 if (lhsType == rhsType) // common case, fast path...
332 continue;
333 AssignmentConversionResult result;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000334 UsualAssignmentConversions(lhsType, rhsType, result);
Steve Naroff17f76e02007-05-03 21:03:48 +0000335
336 SourceLocation l = (i == 0) ? LParenLoc : CommaLocs[i-1];
337
338 // decode the result (notice that AST's are still created for extensions).
339 // FIXME: consider fancier error diagnostics (since this is quite common).
340 // #1: emit the actual prototype arg...requires adding source loc info.
341 // #2: pass Diag the offending argument type...requires hacking Diag.
342 switch (result) {
343 case Compatible:
344 break;
345 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000346 // check for null pointer constant (C99 6.3.2.3p3)
347 if (!((Expr **)Args)[i]->isNullPointerConstant())
348 Diag(l, diag::ext_typecheck_passing_pointer_from_int, utostr(i+1));
Steve Naroff17f76e02007-05-03 21:03:48 +0000349 break;
350 case IntFromPointer:
351 Diag(l, diag::ext_typecheck_passing_int_from_pointer, utostr(i+1));
352 break;
353 case IncompatiblePointer:
354 Diag(l, diag::ext_typecheck_passing_incompatible_pointer, utostr(i+1));
355 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000356 case CompatiblePointerDiscardsQualifiers:
357 Diag(l, diag::ext_typecheck_passing_discards_qualifiers, utostr(i+1));
358 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000359 case Incompatible:
360 return Diag(l, diag::err_typecheck_passing_incompatible, utostr(i+1));
361 }
362 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000363 // Even if the types checked, bail if we had the wrong number of arguments.
364 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
365 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000366 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000367 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000368}
369
370Action::ExprResult Sema::
371ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
372 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000373 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000374 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000375 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
Chris Lattnere168f762006-11-10 05:29:30 +0000376}
377
Chris Lattnere168f762006-11-10 05:29:30 +0000378/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
379/// in the case of a the GNU conditional expr extension.
380Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
381 SourceLocation ColonLoc,
382 ExprTy *Cond, ExprTy *LHS,
383 ExprTy *RHS) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000384 QualType lhs = ((Expr *)LHS)->getType();
385 QualType rhs = ((Expr *)RHS)->getType();
386
387 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
388 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
389
Steve Naroffe4718892007-04-27 18:30:00 +0000390 QualType canonType = rhs.getCanonicalType(); // FIXME
Steve Naroffae4143e2007-04-26 20:39:23 +0000391 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, canonType);
Chris Lattnere168f762006-11-10 05:29:30 +0000392}
393
Steve Naroff1926c832007-04-24 00:23:05 +0000394/// UsualUnaryConversion - Performs various conversions that are common to most
395/// operators (C99 6.3). The conversions of array and function types are
396/// sometimes surpressed. For example, the array->pointer conversion doesn't
397/// apply if the array is an argument to the sizeof or address (&) operators.
398/// In these instances, this routine should *not* be called.
399QualType Sema::UsualUnaryConversion(QualType t) {
400 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000401
Steve Naroff1926c832007-04-24 00:23:05 +0000402 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
403 return Context.IntTy;
404 else if (t->isFunctionType()) // C99 6.3.2.1p4
405 return Context.getPointerType(t);
406 else if (t->isArrayType()) // C99 6.3.2.1p3
407 return Context.getPointerType(cast<ArrayType>(t)->getElementType());
408 return t;
409}
410
411/// UsualArithmeticConversions - Performs various conversions that are common to
412/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
413/// routine returns the first non-arithmetic type found. The client is
414/// responsible for emitting appropriate error diagnostics.
415QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000416 QualType lhs = UsualUnaryConversion(t1);
417 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000418
419 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000420 if (!lhs->isArithmeticType())
421 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000422 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000423 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000424
Steve Narofff633d092007-04-25 19:01:39 +0000425 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000426 if (lhs == rhs)
427 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000428
Steve Naroffb01bbe32007-04-25 01:22:31 +0000429 // at this point, we have two different arithmetic types.
430
431 // Handle complex types first (C99 6.3.1.8p1).
432 if (lhs->isComplexType() || rhs->isComplexType()) {
433 // if we have an integer operand, the result is the complex type.
434 if (rhs->isIntegerType())
435 return lhs;
436 if (lhs->isIntegerType())
437 return rhs;
438
Steve Naroffe4718892007-04-27 18:30:00 +0000439 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000440 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000441 // Now handle "real" floating types (i.e. float, double, long double).
442 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
443 // if we have an integer operand, the result is the real floating type.
444 if (rhs->isIntegerType())
445 return lhs;
446 if (lhs->isIntegerType())
447 return rhs;
448
449 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000450 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000451 }
Steve Naroffe4718892007-04-27 18:30:00 +0000452 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000453}
454
Steve Naroff3f597292007-05-11 22:18:03 +0000455// CheckPointerTypesForAssignment - This is a very tricky routine (despite
456// being closely modeled after the C99 spec:-). The odd characteristic of this
457// routine is it effectively iqnores the qualifiers on the top level pointee.
458// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
459// FIXME: add a couple examples in this comment.
460QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
461 QualType rhsType,
462 AssignmentConversionResult &r) {
463 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000464
465 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000466 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
467 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000468
469 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000470 lhptee = lhptee.getCanonicalType();
471 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000472
Steve Naroff3f597292007-05-11 22:18:03 +0000473 // C99 6.5.16.1p1: This following citation is common to constraints
474 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
475 // qualifiers of the type *pointed to* by the right;
476 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
477 rhptee.getQualifiers())
478 r = CompatiblePointerDiscardsQualifiers;
479
480 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
481 // incomplete type and the other is a pointer to a qualified or unqualified
482 // version of void...
483 if (lhptee.getUnqualifiedType()->isVoidType() &&
484 (rhptee->isObjectType() || rhptee->isIncompleteType()))
485 ;
486 else if (rhptee.getUnqualifiedType()->isVoidType() &&
487 (lhptee->isObjectType() || lhptee->isIncompleteType()))
488 ;
489 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
490 // unqualified versions of compatible types, ...
491 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
492 rhptee.getUnqualifiedType()))
493 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
494 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000495}
496
Steve Naroff17f76e02007-05-03 21:03:48 +0000497/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
498/// has code to accommodate several GCC extensions when type checking
499/// pointers. Here are some objectionable examples that GCC considers warnings:
500///
501/// int a, *pint;
502/// short *pshort;
503/// struct foo *pfoo;
504///
505/// pint = pshort; // warning: assignment from incompatible pointer type
506/// a = pint; // warning: assignment makes integer from pointer without a cast
507/// pint = a; // warning: assignment makes pointer from integer without a cast
508/// pint = pfoo; // warning: assignment from incompatible pointer type
509///
510/// As a result, the code for dealing with pointers is more complex than the
511/// C99 spec dictates.
512/// Note: the warning above turn into errors when -pedantic-errors is enabled.
513///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000514QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000515 AssignmentConversionResult &r) {
Steve Naroffb891de32007-05-02 23:51:10 +0000516 // this check seems unnatural, however it necessary to insure the proper
517 // conversion of functions/arrays. If the conversion where done for all
518 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
519 // unary expressions that surpress this implicit conversion (&, sizeof).
520 if (rhsType->isFunctionType() || rhsType->isArrayType())
521 rhsType = UsualUnaryConversion(rhsType);
522
Steve Naroff17f76e02007-05-03 21:03:48 +0000523 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000524 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
525 return lhsType;
526 else if (lhsType->isPointerType()) {
527 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000528 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000529 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000530 }
Steve Naroff3f597292007-05-11 22:18:03 +0000531 if (rhsType->isPointerType())
532 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000533 } else if (rhsType->isPointerType()) {
534 if (lhsType->isIntegerType()) {
535 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
536 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000537 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000538 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000539 }
Steve Naroff3f597292007-05-11 22:18:03 +0000540 if (lhsType->isPointerType())
541 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000542 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
543 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000544 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000545 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000546 r = Incompatible;
547 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000548}
549
Steve Naroff218bc2b2007-05-04 21:54:46 +0000550inline QualType Sema::CheckMultiplyDivideOperands(
551 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000552{
Steve Naroff1926c832007-04-24 00:23:05 +0000553 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000554
Steve Naroff218bc2b2007-05-04 21:54:46 +0000555 if (resType->isArithmeticType())
556 return resType;
557 Diag(loc, diag::err_typecheck_invalid_operands);
558 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000559}
560
Steve Naroff218bc2b2007-05-04 21:54:46 +0000561inline QualType Sema::CheckRemainderOperands(
562 Expr *lex, Expr *rex, SourceLocation loc)
563{
564 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
565
566 if (resType->isIntegerType())
567 return resType;
568 Diag(loc, diag::err_typecheck_invalid_operands);
569 return QualType();
570}
571
572inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
573 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000574{
Steve Naroffe4718892007-04-27 18:30:00 +0000575 QualType lhsType = lex->getType(), rhsType = rex->getType();
576 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
577
578 // handle the common case first (both operands are arithmetic).
579 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000580 return resType;
581
582 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
583 (lhsType->isIntegerType() && rhsType->isPointerType()))
584 return resType;
585 Diag(loc, diag::err_typecheck_invalid_operands);
586 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000587}
588
Steve Naroff218bc2b2007-05-04 21:54:46 +0000589inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
590 Expr *lex, Expr *rex, SourceLocation loc)
591{
592 QualType lhsType = lex->getType(), rhsType = rex->getType();
593 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
594
595 // handle the common case first (both operands are arithmetic).
596 if (resType->isArithmeticType())
597 return resType;
598 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
599 (lhsType->isPointerType() && rhsType->isPointerType()))
600 return resType;
601 Diag(loc, diag::err_typecheck_invalid_operands);
602 return QualType();
603}
604
605inline QualType Sema::CheckShiftOperands( // C99 6.5.7
606 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000607{
608 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
609
Steve Naroff218bc2b2007-05-04 21:54:46 +0000610 if (resType->isIntegerType())
611 return resType;
612 Diag(loc, diag::err_typecheck_invalid_operands);
613 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000614}
615
Steve Naroff218bc2b2007-05-04 21:54:46 +0000616inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
617 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000618{
619 QualType lType = lex->getType(), rType = rex->getType();
620
621 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000622 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000623
624 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000625 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000626
627 if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000628 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
629 else
630 Diag(loc, diag::err_typecheck_invalid_operands);
631 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000632}
633
Steve Naroff218bc2b2007-05-04 21:54:46 +0000634inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
635 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000636{
637 QualType lType = lex->getType(), rType = rex->getType();
638
639 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000640 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000641 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000642 return Context.IntTy;
643
Steve Naroffe4718892007-04-27 18:30:00 +0000644 if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000645 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
646 else
647 Diag(loc, diag::err_typecheck_invalid_operands);
648 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000649}
650
Steve Naroff218bc2b2007-05-04 21:54:46 +0000651inline QualType Sema::CheckBitwiseOperands(
652 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000653{
654 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
655
Steve Naroff218bc2b2007-05-04 21:54:46 +0000656 if (resType->isIntegerType())
657 return resType;
658 Diag(loc, diag::err_typecheck_invalid_operands);
659 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000660}
661
Steve Naroff218bc2b2007-05-04 21:54:46 +0000662inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
663 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000664{
Steve Naroffe4718892007-04-27 18:30:00 +0000665 QualType lhsType = UsualUnaryConversion(lex->getType());
666 QualType rhsType = UsualUnaryConversion(rex->getType());
667
Steve Naroff218bc2b2007-05-04 21:54:46 +0000668 if (lhsType->isScalarType() || rhsType->isScalarType())
669 return Context.IntTy;
670 Diag(loc, diag::err_typecheck_invalid_operands);
671 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000672}
673
Steve Naroff35d85152007-05-07 00:24:15 +0000674inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
675 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000676{
Steve Naroff0af91202007-04-27 21:51:21 +0000677 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000678 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000679 bool hadError = false;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000680
681 // this check is done first to give a more precise diagnostic.
Steve Naroff1f4d7272007-05-11 04:00:31 +0000682 // isModifiableLvalue() will also check for "const".
Steve Naroff218bc2b2007-05-04 21:54:46 +0000683 if (lhsType.isConstQualified()) {
684 Diag(loc, diag::err_typecheck_assign_const);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000685 hadError = true;
686 } else if (!lex->isModifiableLvalue()) { // C99 6.5.16p2
687 Diag(loc, diag::err_typecheck_assign_non_lvalue);
688 return QualType(); // no need to continue checking...
Steve Naroff218bc2b2007-05-04 21:54:46 +0000689 }
690 if (lhsType == rhsType) // common case, fast path...
691 return lhsType;
692
693 AssignmentConversionResult result;
694 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
695
696 // decode the result (notice that extensions still return a type).
697 switch (result) {
698 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000699 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000700 case Incompatible:
701 Diag(loc, diag::err_typecheck_assign_incompatible);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000702 hadError = true;
703 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000704 case PointerFromInt:
705 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000706 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000707 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000708 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000709 case IntFromPointer:
710 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000711 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000712 case IncompatiblePointer:
713 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000714 break;
715 case CompatiblePointerDiscardsQualifiers:
716 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers);
717 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000718 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000719 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000720}
721
Steve Naroff218bc2b2007-05-04 21:54:46 +0000722inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroffae4143e2007-04-26 20:39:23 +0000723 Expr *lex, Expr *rex, SourceLocation loc)
724{
Steve Naroff218bc2b2007-05-04 21:54:46 +0000725 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000726}
727
Steve Naroff35d85152007-05-07 00:24:15 +0000728QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000729 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000730 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000731
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000732 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000733 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
734 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
735 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, resType);
736 return QualType();
737 }
738 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000739 // FIXME: Allow Complex as a GCC extension.
Steve Naroff35d85152007-05-07 00:24:15 +0000740 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, resType);
741 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000742 }
Steve Naroff094046f2007-05-13 03:21:25 +0000743 // At this point, we know we have a real or pointer type. Now make sure
744 // the operand is a modifiable lvalue.
745 if (!op->isModifiableLvalue()) {
746 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr);
Steve Naroff35d85152007-05-07 00:24:15 +0000747 return QualType();
748 }
749 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000750}
751
Steve Naroff1926c832007-04-24 00:23:05 +0000752/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000753/// This routine allows us to typecheck complex/recursive expressions
754/// where the declaration is needed for type checking. Here are some
755/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000756static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000757 switch (e->getStmtClass()) {
758 case Stmt::DeclRefExprClass:
759 return cast<DeclRefExpr>(e)->getDecl();
760 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000761 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000762 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000763 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000764 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000765 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000766 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000767 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000768 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000769 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000770 default:
771 return 0;
772 }
773}
774
775/// CheckAddressOfOperand - The operand of & must be either a function
776/// designator or an lvalue designating an object. If it is an lvalue, the
777/// object cannot be declared with storage class register or be a bit field.
778/// Note: The usual conversions are *not* applied to the operand of the &
779/// operator, and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000780QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000781 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000782
Steve Naroff17f76e02007-05-03 21:03:48 +0000783 if (!op->isModifiableLvalue()) {
Steve Naroff47500512007-04-19 23:00:49 +0000784 if (dcl && isa<FunctionDecl>(dcl))
785 ; // C99 6.5.3.2p1: Allow function designators.
Steve Naroff35d85152007-05-07 00:24:15 +0000786 else {
Steve Naroff3f597292007-05-11 22:18:03 +0000787 /* FIXME: The following produces an (incorrect) error. The
788 Type::isModifiableLvalue() predicate is causing problems.
789
790 const char **cpp;
791 const char c = 'A';
792
793 int main() { *cpp = &c; // valid }
794 */
Steve Naroff35d85152007-05-07 00:24:15 +0000795 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
796 return QualType();
797 }
Steve Naroff47500512007-04-19 23:00:49 +0000798 } else if (dcl) {
799 // We have an lvalue with a decl. Make sure the decl is not declared
800 // with the register storage-class specifier.
801 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000802 if (vd->getStorageClass() == VarDecl::Register) {
803 Diag(OpLoc, diag::err_typecheck_address_of_register);
804 return QualType();
805 }
Steve Narofff633d092007-04-25 19:01:39 +0000806 } else
807 assert(0 && "Unknown/unexpected decl type");
808
Steve Naroff47500512007-04-19 23:00:49 +0000809 // FIXME: add check for bitfields!
810 }
811 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +0000812 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +0000813}
814
Steve Naroff35d85152007-05-07 00:24:15 +0000815QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
816 QualType qType = UsualUnaryConversion(op->getType());
817
Steve Naroff47500512007-04-19 23:00:49 +0000818 assert(!qType.isNull() && "no type for * expression");
819
Steve Naroff35d85152007-05-07 00:24:15 +0000820 if (PointerType *PT = dyn_cast<PointerType>(qType))
821 return PT->getPointeeType();
822 Diag(OpLoc, diag::err_typecheck_unary_expr, qType);
823 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +0000824}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000825
826static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
827 tok::TokenKind Kind) {
828 BinaryOperator::Opcode Opc;
829 switch (Kind) {
830 default: assert(0 && "Unknown binop!");
831 case tok::star: Opc = BinaryOperator::Mul; break;
832 case tok::slash: Opc = BinaryOperator::Div; break;
833 case tok::percent: Opc = BinaryOperator::Rem; break;
834 case tok::plus: Opc = BinaryOperator::Add; break;
835 case tok::minus: Opc = BinaryOperator::Sub; break;
836 case tok::lessless: Opc = BinaryOperator::Shl; break;
837 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
838 case tok::lessequal: Opc = BinaryOperator::LE; break;
839 case tok::less: Opc = BinaryOperator::LT; break;
840 case tok::greaterequal: Opc = BinaryOperator::GE; break;
841 case tok::greater: Opc = BinaryOperator::GT; break;
842 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
843 case tok::equalequal: Opc = BinaryOperator::EQ; break;
844 case tok::amp: Opc = BinaryOperator::And; break;
845 case tok::caret: Opc = BinaryOperator::Xor; break;
846 case tok::pipe: Opc = BinaryOperator::Or; break;
847 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
848 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
849 case tok::equal: Opc = BinaryOperator::Assign; break;
850 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
851 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
852 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
853 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
854 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
855 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
856 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
857 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
858 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
859 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
860 case tok::comma: Opc = BinaryOperator::Comma; break;
861 }
862 return Opc;
863}
864
Steve Naroff35d85152007-05-07 00:24:15 +0000865static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
866 tok::TokenKind Kind) {
867 UnaryOperator::Opcode Opc;
868 switch (Kind) {
869 default: assert(0 && "Unknown unary op!");
870 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
871 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
872 case tok::amp: Opc = UnaryOperator::AddrOf; break;
873 case tok::star: Opc = UnaryOperator::Deref; break;
874 case tok::plus: Opc = UnaryOperator::Plus; break;
875 case tok::minus: Opc = UnaryOperator::Minus; break;
876 case tok::tilde: Opc = UnaryOperator::Not; break;
877 case tok::exclaim: Opc = UnaryOperator::LNot; break;
878 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
879 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
880 case tok::kw___real: Opc = UnaryOperator::Real; break;
881 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
882 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
883 // FIXME: case tok::kw___extension__:
884 }
885 return Opc;
886}
887
Steve Naroff218bc2b2007-05-04 21:54:46 +0000888// Binary Operators. 'Tok' is the token for the operator.
889Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
890 ExprTy *LHS, ExprTy *RHS) {
891 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
892 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
893
894 assert((lhs != 0) && "ParseBinOp(): missing left expression");
895 assert((rhs != 0) && "ParseBinOp(): missing right expression");
896
897 QualType result;
898
899 switch (Opc) {
900 default:
901 assert(0 && "Unknown binary expr!");
902 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +0000903 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000904 break;
905 case BinaryOperator::Mul:
906 case BinaryOperator::Div:
907 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
908 break;
909 case BinaryOperator::Rem:
910 result = CheckRemainderOperands(lhs, rhs, TokLoc);
911 break;
912 case BinaryOperator::Add:
913 result = CheckAdditionOperands(lhs, rhs, TokLoc);
914 break;
915 case BinaryOperator::Sub:
916 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
917 break;
918 case BinaryOperator::Shl:
919 case BinaryOperator::Shr:
920 result = CheckShiftOperands(lhs, rhs, TokLoc);
921 break;
922 case BinaryOperator::LE:
923 case BinaryOperator::LT:
924 case BinaryOperator::GE:
925 case BinaryOperator::GT:
926 result = CheckRelationalOperands(lhs, rhs, TokLoc);
927 break;
928 case BinaryOperator::EQ:
929 case BinaryOperator::NE:
930 result = CheckEqualityOperands(lhs, rhs, TokLoc);
931 break;
932 case BinaryOperator::And:
933 case BinaryOperator::Xor:
934 case BinaryOperator::Or:
935 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
936 break;
937 case BinaryOperator::LAnd:
938 case BinaryOperator::LOr:
939 result = CheckLogicalOperands(lhs, rhs, TokLoc);
940 break;
941 case BinaryOperator::MulAssign:
942 case BinaryOperator::DivAssign:
943 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
944 if (result.isNull())
945 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000946 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000947 break;
948 case BinaryOperator::RemAssign:
949 result = CheckRemainderOperands(lhs, rhs, TokLoc);
950 if (result.isNull())
951 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000952 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000953 break;
954 case BinaryOperator::AddAssign:
955 result = CheckAdditionOperands(lhs, rhs, TokLoc);
956 if (result.isNull())
957 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000958 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000959 break;
960 case BinaryOperator::SubAssign:
961 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
962 if (result.isNull())
963 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000964 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000965 break;
966 case BinaryOperator::ShlAssign:
967 case BinaryOperator::ShrAssign:
968 result = CheckShiftOperands(lhs, rhs, TokLoc);
969 if (result.isNull())
970 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000971 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000972 break;
973 case BinaryOperator::AndAssign:
974 case BinaryOperator::XorAssign:
975 case BinaryOperator::OrAssign:
976 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
977 if (result.isNull())
978 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000979 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000980 break;
981 case BinaryOperator::Comma:
982 result = CheckCommaOperands(lhs, rhs, TokLoc);
983 break;
984 }
985 if (result.isNull())
986 return true;
987 return new BinaryOperator(lhs, rhs, Opc, result);
988}
989
Steve Naroff35d85152007-05-07 00:24:15 +0000990// Unary Operators. 'Tok' is the token for the operator.
991Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
992 ExprTy *Input) {
993 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
994 QualType resultType;
995 switch (Opc) {
996 default:
997 assert(0 && "Unimplemented unary expr!");
998 case UnaryOperator::PreInc:
999 case UnaryOperator::PreDec:
1000 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
1001 break;
1002 case UnaryOperator::AddrOf:
1003 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1004 break;
1005 case UnaryOperator::Deref:
1006 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1007 break;
1008 case UnaryOperator::Plus:
1009 case UnaryOperator::Minus:
1010 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1011 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1012 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
1013 break;
1014 case UnaryOperator::Not: // bitwise complement
1015 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1016 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1017 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
1018 break;
1019 case UnaryOperator::LNot: // logical negation
1020 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1021 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1022 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
1023 break;
1024 case UnaryOperator::SizeOf:
1025 case UnaryOperator::AlignOf:
1026 assert(0 && "need to implement type checking for sizeof/alignof");
1027 }
1028 if (resultType.isNull())
1029 return true;
1030 return new UnaryOperator((Expr *)Input, Opc, resultType);
1031}