blob: 4506ec9fee83a1ef243ee507fa5aab991be10f57 [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
Steve Naroff043d45d2007-05-15 02:32:35 +0000173QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
174 SourceLocation OpLoc, bool isSizeof) {
175 // C99 6.5.3.4p1:
176 if (isa<FunctionType>(exprType) && isSizeof)
177 // alignof(function) is allowed.
178 Diag(OpLoc, diag::ext_sizeof_function_type);
179 else if (exprType->isVoidType())
180 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
181 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000182 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000183 diag::err_alignof_incomplete_type,
184 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000185 return QualType(); // error
186 }
187 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
188 return Context.getSizeType();
189}
190
Chris Lattnere168f762006-11-10 05:29:30 +0000191Action::ExprResult Sema::
192ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
193 SourceLocation LParenLoc, TypeTy *Ty,
194 SourceLocation RParenLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000195 // If error parsing type, ignore.
196 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000197
198 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000199 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000200
Steve Naroff043d45d2007-05-15 02:32:35 +0000201 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
202
203 if (resultType.isNull())
204 return true;
205 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000206}
207
208
209Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
210 tok::TokenKind Kind,
211 ExprTy *Input) {
212 UnaryOperator::Opcode Opc;
213 switch (Kind) {
214 default: assert(0 && "Unknown unary op!");
215 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
216 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
217 }
Steve Naroff35d85152007-05-07 00:24:15 +0000218 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
219 if (result.isNull())
220 return true;
221 return new UnaryOperator((Expr *)Input, Opc, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000222}
223
224Action::ExprResult Sema::
225ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
226 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000227 QualType t1 = ((Expr *)Base)->getType();
228 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000229
230 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000231 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000232
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000233 QualType canonT1 = t1.getCanonicalType();
234 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000235
Steve Naroffc1aadb12007-03-28 21:49:40 +0000236 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
237 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000238 // in the subscript position. As a result, we need to derive the array base
239 // and index from the expression types.
240
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000241 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000242 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
243 baseType = canonT1;
244 indexType = canonT2;
245 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
246 baseType = canonT2;
247 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000248 } else
249 return Diag(LLoc, diag::err_typecheck_subscript_value);
250
Steve Naroffc1aadb12007-03-28 21:49:40 +0000251 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000252 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000253 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000254
Steve Naroff95af0132007-03-30 23:47:58 +0000255 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000256 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000257 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
258 resultType = ary->getElementType();
259 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
260 resultType = ary->getPointeeType();
261 // in practice, the following check catches trying to index a pointer
262 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000263 if (!resultType->isObjectType())
264 return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000265 }
266 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000267}
268
269Action::ExprResult Sema::
270ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
271 tok::TokenKind OpKind, SourceLocation MemberLoc,
272 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000273 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000274
275 assert(!qualifiedType.isNull() && "no type for member expression");
276
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000277 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000278
279 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000280 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
281 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000282 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000283 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000284 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
285 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000286 if (!isa<RecordType>(canonType))
287 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
288
289 // get the struct/union definition from the type.
290 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000291
Steve Naroff92e30f82007-04-02 22:35:25 +0000292 if (canonType->isIncompleteType())
293 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000294
Steve Naroff92e30f82007-04-02 22:35:25 +0000295 FieldDecl *MemberDecl = RD->getMember(&Member);
296 if (!MemberDecl)
297 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
298
299 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000300}
301
302/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
303/// This provides the location of the left/right parens and a list of comma
304/// locations.
305Action::ExprResult Sema::
306ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000307 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000308 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000309 QualType qType = ((Expr *)Fn)->getType();
310
311 assert(!qType.isNull() && "no type for function call expression");
312
Steve Naroff17f76e02007-05-03 21:03:48 +0000313 const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000314
Steve Naroff17f76e02007-05-03 21:03:48 +0000315 assert(funcT && "ParseCallExpr(): not a function type");
Steve Naroffb8c289d2007-05-08 22:18:00 +0000316
Steve Naroff17f76e02007-05-03 21:03:48 +0000317 // If a prototype isn't declared, the parser implicitly defines a func decl
318 QualType resultType = funcT->getResultType();
319
320 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
321 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
322 // assignment, to the types of the corresponding parameter, ...
323
324 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000325 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000326
Steve Naroffb8c289d2007-05-08 22:18:00 +0000327 if (NumArgsInCall < NumArgsInProto)
Steve Naroff1f4d7272007-05-11 04:00:31 +0000328 Diag(LParenLoc, diag::err_typecheck_call_too_few_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000329 else if (NumArgsInCall > NumArgsInProto) {
330 if (!proto->isVariadic())
Steve Naroff1f4d7272007-05-11 04:00:31 +0000331 Diag(LParenLoc, diag::err_typecheck_call_too_many_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000332 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000333 }
334 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000335 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000336 QualType lhsType = proto->getArgType(i);
337 QualType rhsType = ((Expr **)Args)[i]->getType();
338
339 if (lhsType == rhsType) // common case, fast path...
340 continue;
341 AssignmentConversionResult result;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000342 UsualAssignmentConversions(lhsType, rhsType, result);
Steve Naroff17f76e02007-05-03 21:03:48 +0000343
344 SourceLocation l = (i == 0) ? LParenLoc : CommaLocs[i-1];
345
346 // decode the result (notice that AST's are still created for extensions).
347 // FIXME: consider fancier error diagnostics (since this is quite common).
348 // #1: emit the actual prototype arg...requires adding source loc info.
349 // #2: pass Diag the offending argument type...requires hacking Diag.
350 switch (result) {
351 case Compatible:
352 break;
353 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000354 // check for null pointer constant (C99 6.3.2.3p3)
355 if (!((Expr **)Args)[i]->isNullPointerConstant())
356 Diag(l, diag::ext_typecheck_passing_pointer_from_int, utostr(i+1));
Steve Naroff17f76e02007-05-03 21:03:48 +0000357 break;
358 case IntFromPointer:
359 Diag(l, diag::ext_typecheck_passing_int_from_pointer, utostr(i+1));
360 break;
361 case IncompatiblePointer:
362 Diag(l, diag::ext_typecheck_passing_incompatible_pointer, utostr(i+1));
363 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000364 case CompatiblePointerDiscardsQualifiers:
365 Diag(l, diag::ext_typecheck_passing_discards_qualifiers, utostr(i+1));
366 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000367 case Incompatible:
368 return Diag(l, diag::err_typecheck_passing_incompatible, utostr(i+1));
369 }
370 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000371 // Even if the types checked, bail if we had the wrong number of arguments.
372 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
373 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000374 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000375 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000376}
377
378Action::ExprResult Sema::
379ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
380 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000381 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000382 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000383 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
Chris Lattnere168f762006-11-10 05:29:30 +0000384}
385
Steve Narofff8a28c52007-05-15 20:29:32 +0000386inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
387 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
388 QualType cond = Cond->getType().getCanonicalType();
389 QualType lhs = LHS->getType().getCanonicalType();
390 QualType rhs = RHS->getType().getCanonicalType();
391
392 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
393 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
394 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
395
396 // C99 6.5.15p2,3
397 return rhs;
398}
399
Chris Lattnere168f762006-11-10 05:29:30 +0000400/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
401/// in the case of a the GNU conditional expr extension.
402Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
403 SourceLocation ColonLoc,
404 ExprTy *Cond, ExprTy *LHS,
405 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000406 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
407 (Expr *)RHS, QuestionLoc);
408 if (result.isNull())
409 return true;
410 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000411}
412
Steve Naroff1926c832007-04-24 00:23:05 +0000413/// UsualUnaryConversion - Performs various conversions that are common to most
414/// operators (C99 6.3). The conversions of array and function types are
415/// sometimes surpressed. For example, the array->pointer conversion doesn't
416/// apply if the array is an argument to the sizeof or address (&) operators.
417/// In these instances, this routine should *not* be called.
418QualType Sema::UsualUnaryConversion(QualType t) {
419 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000420
Steve Naroff1926c832007-04-24 00:23:05 +0000421 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
422 return Context.IntTy;
423 else if (t->isFunctionType()) // C99 6.3.2.1p4
424 return Context.getPointerType(t);
425 else if (t->isArrayType()) // C99 6.3.2.1p3
426 return Context.getPointerType(cast<ArrayType>(t)->getElementType());
427 return t;
428}
429
430/// UsualArithmeticConversions - Performs various conversions that are common to
431/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
432/// routine returns the first non-arithmetic type found. The client is
433/// responsible for emitting appropriate error diagnostics.
434QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000435 QualType lhs = UsualUnaryConversion(t1);
436 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000437
438 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000439 if (!lhs->isArithmeticType())
440 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000441 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000442 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000443
Steve Narofff633d092007-04-25 19:01:39 +0000444 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000445 if (lhs == rhs)
446 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000447
Steve Naroffb01bbe32007-04-25 01:22:31 +0000448 // at this point, we have two different arithmetic types.
449
450 // Handle complex types first (C99 6.3.1.8p1).
451 if (lhs->isComplexType() || rhs->isComplexType()) {
452 // if we have an integer operand, the result is the complex type.
453 if (rhs->isIntegerType())
454 return lhs;
455 if (lhs->isIntegerType())
456 return rhs;
457
Steve Naroffe4718892007-04-27 18:30:00 +0000458 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000459 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000460 // Now handle "real" floating types (i.e. float, double, long double).
461 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
462 // if we have an integer operand, the result is the real floating type.
463 if (rhs->isIntegerType())
464 return lhs;
465 if (lhs->isIntegerType())
466 return rhs;
467
468 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000469 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000470 }
Steve Naroffe4718892007-04-27 18:30:00 +0000471 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000472}
473
Steve Naroff3f597292007-05-11 22:18:03 +0000474// CheckPointerTypesForAssignment - This is a very tricky routine (despite
475// being closely modeled after the C99 spec:-). The odd characteristic of this
476// routine is it effectively iqnores the qualifiers on the top level pointee.
477// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
478// FIXME: add a couple examples in this comment.
479QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
480 QualType rhsType,
481 AssignmentConversionResult &r) {
482 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000483
484 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000485 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
486 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000487
488 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000489 lhptee = lhptee.getCanonicalType();
490 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000491
Steve Naroff3f597292007-05-11 22:18:03 +0000492 // C99 6.5.16.1p1: This following citation is common to constraints
493 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
494 // qualifiers of the type *pointed to* by the right;
495 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
496 rhptee.getQualifiers())
497 r = CompatiblePointerDiscardsQualifiers;
498
499 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
500 // incomplete type and the other is a pointer to a qualified or unqualified
501 // version of void...
502 if (lhptee.getUnqualifiedType()->isVoidType() &&
503 (rhptee->isObjectType() || rhptee->isIncompleteType()))
504 ;
505 else if (rhptee.getUnqualifiedType()->isVoidType() &&
506 (lhptee->isObjectType() || lhptee->isIncompleteType()))
507 ;
508 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
509 // unqualified versions of compatible types, ...
510 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
511 rhptee.getUnqualifiedType()))
512 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
513 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000514}
515
Steve Naroff17f76e02007-05-03 21:03:48 +0000516/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
517/// has code to accommodate several GCC extensions when type checking
518/// pointers. Here are some objectionable examples that GCC considers warnings:
519///
520/// int a, *pint;
521/// short *pshort;
522/// struct foo *pfoo;
523///
524/// pint = pshort; // warning: assignment from incompatible pointer type
525/// a = pint; // warning: assignment makes integer from pointer without a cast
526/// pint = a; // warning: assignment makes pointer from integer without a cast
527/// pint = pfoo; // warning: assignment from incompatible pointer type
528///
529/// As a result, the code for dealing with pointers is more complex than the
530/// C99 spec dictates.
531/// Note: the warning above turn into errors when -pedantic-errors is enabled.
532///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000533QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000534 AssignmentConversionResult &r) {
Steve Naroffb891de32007-05-02 23:51:10 +0000535 // this check seems unnatural, however it necessary to insure the proper
536 // conversion of functions/arrays. If the conversion where done for all
537 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
538 // unary expressions that surpress this implicit conversion (&, sizeof).
539 if (rhsType->isFunctionType() || rhsType->isArrayType())
540 rhsType = UsualUnaryConversion(rhsType);
541
Steve Naroff17f76e02007-05-03 21:03:48 +0000542 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000543 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
544 return lhsType;
545 else if (lhsType->isPointerType()) {
546 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000547 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000548 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000549 }
Steve Naroff3f597292007-05-11 22:18:03 +0000550 if (rhsType->isPointerType())
551 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000552 } else if (rhsType->isPointerType()) {
553 if (lhsType->isIntegerType()) {
554 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
555 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000556 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000557 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000558 }
Steve Naroff3f597292007-05-11 22:18:03 +0000559 if (lhsType->isPointerType())
560 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000561 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
562 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000563 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000564 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000565 r = Incompatible;
566 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000567}
568
Steve Naroff218bc2b2007-05-04 21:54:46 +0000569inline QualType Sema::CheckMultiplyDivideOperands(
570 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000571{
Steve Naroff1926c832007-04-24 00:23:05 +0000572 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000573
Steve Naroff218bc2b2007-05-04 21:54:46 +0000574 if (resType->isArithmeticType())
575 return resType;
576 Diag(loc, diag::err_typecheck_invalid_operands);
577 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000578}
579
Steve Naroff218bc2b2007-05-04 21:54:46 +0000580inline QualType Sema::CheckRemainderOperands(
581 Expr *lex, Expr *rex, SourceLocation loc)
582{
583 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
584
585 if (resType->isIntegerType())
586 return resType;
587 Diag(loc, diag::err_typecheck_invalid_operands);
588 return QualType();
589}
590
591inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
592 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000593{
Steve Naroffe4718892007-04-27 18:30:00 +0000594 QualType lhsType = lex->getType(), rhsType = rex->getType();
595 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
596
597 // handle the common case first (both operands are arithmetic).
598 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000599 return resType;
600
601 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
602 (lhsType->isIntegerType() && rhsType->isPointerType()))
603 return resType;
604 Diag(loc, diag::err_typecheck_invalid_operands);
605 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000606}
607
Steve Naroff218bc2b2007-05-04 21:54:46 +0000608inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
609 Expr *lex, Expr *rex, SourceLocation loc)
610{
611 QualType lhsType = lex->getType(), rhsType = rex->getType();
612 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
613
614 // handle the common case first (both operands are arithmetic).
615 if (resType->isArithmeticType())
616 return resType;
617 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
618 (lhsType->isPointerType() && rhsType->isPointerType()))
619 return resType;
620 Diag(loc, diag::err_typecheck_invalid_operands);
621 return QualType();
622}
623
624inline QualType Sema::CheckShiftOperands( // C99 6.5.7
625 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000626{
627 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
628
Steve Naroff218bc2b2007-05-04 21:54:46 +0000629 if (resType->isIntegerType())
630 return resType;
631 Diag(loc, diag::err_typecheck_invalid_operands);
632 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000633}
634
Steve Naroff218bc2b2007-05-04 21:54:46 +0000635inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
636 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000637{
638 QualType lType = lex->getType(), rType = rex->getType();
639
640 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000641 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000642
643 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000644 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000645
Steve Naroff043d45d2007-05-15 02:32:35 +0000646 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000647 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000648 return Context.IntTy;
649 }
650 Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000651 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000652}
653
Steve Naroff218bc2b2007-05-04 21:54:46 +0000654inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
655 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000656{
657 QualType lType = lex->getType(), rType = rex->getType();
658
659 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000660 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000661 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000662 return Context.IntTy;
663
Steve Naroff043d45d2007-05-15 02:32:35 +0000664 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000665 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000666 return Context.IntTy;
667 }
668 Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000669 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000670}
671
Steve Naroff218bc2b2007-05-04 21:54:46 +0000672inline QualType Sema::CheckBitwiseOperands(
673 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000674{
675 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
676
Steve Naroff218bc2b2007-05-04 21:54:46 +0000677 if (resType->isIntegerType())
678 return resType;
679 Diag(loc, diag::err_typecheck_invalid_operands);
680 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000681}
682
Steve Naroff218bc2b2007-05-04 21:54:46 +0000683inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
684 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000685{
Steve Naroffe4718892007-04-27 18:30:00 +0000686 QualType lhsType = UsualUnaryConversion(lex->getType());
687 QualType rhsType = UsualUnaryConversion(rex->getType());
688
Steve Naroff218bc2b2007-05-04 21:54:46 +0000689 if (lhsType->isScalarType() || rhsType->isScalarType())
690 return Context.IntTy;
691 Diag(loc, diag::err_typecheck_invalid_operands);
692 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000693}
694
Steve Naroff35d85152007-05-07 00:24:15 +0000695inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
696 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000697{
Steve Naroff0af91202007-04-27 21:51:21 +0000698 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000699 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000700 bool hadError = false;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000701
702 // this check is done first to give a more precise diagnostic.
Steve Naroff1f4d7272007-05-11 04:00:31 +0000703 // isModifiableLvalue() will also check for "const".
Steve Naroff218bc2b2007-05-04 21:54:46 +0000704 if (lhsType.isConstQualified()) {
705 Diag(loc, diag::err_typecheck_assign_const);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000706 hadError = true;
707 } else if (!lex->isModifiableLvalue()) { // C99 6.5.16p2
708 Diag(loc, diag::err_typecheck_assign_non_lvalue);
709 return QualType(); // no need to continue checking...
Steve Naroff218bc2b2007-05-04 21:54:46 +0000710 }
711 if (lhsType == rhsType) // common case, fast path...
712 return lhsType;
713
714 AssignmentConversionResult result;
715 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
716
717 // decode the result (notice that extensions still return a type).
718 switch (result) {
719 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000720 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000721 case Incompatible:
722 Diag(loc, diag::err_typecheck_assign_incompatible);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000723 hadError = true;
724 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000725 case PointerFromInt:
726 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000727 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000728 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000729 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000730 case IntFromPointer:
731 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000732 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000733 case IncompatiblePointer:
734 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000735 break;
736 case CompatiblePointerDiscardsQualifiers:
737 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers);
738 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000739 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000740 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000741}
742
Steve Naroff218bc2b2007-05-04 21:54:46 +0000743inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroffae4143e2007-04-26 20:39:23 +0000744 Expr *lex, Expr *rex, SourceLocation loc)
745{
Steve Naroff218bc2b2007-05-04 21:54:46 +0000746 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000747}
748
Steve Naroff35d85152007-05-07 00:24:15 +0000749QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000750 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000751 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000752
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000753 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000754 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
755 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
756 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, resType);
757 return QualType();
758 }
759 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000760 // FIXME: Allow Complex as a GCC extension.
Steve Naroff35d85152007-05-07 00:24:15 +0000761 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, resType);
762 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000763 }
Steve Naroff094046f2007-05-13 03:21:25 +0000764 // At this point, we know we have a real or pointer type. Now make sure
765 // the operand is a modifiable lvalue.
766 if (!op->isModifiableLvalue()) {
767 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr);
Steve Naroff35d85152007-05-07 00:24:15 +0000768 return QualType();
769 }
770 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000771}
772
Steve Naroff1926c832007-04-24 00:23:05 +0000773/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000774/// This routine allows us to typecheck complex/recursive expressions
775/// where the declaration is needed for type checking. Here are some
776/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000777static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000778 switch (e->getStmtClass()) {
779 case Stmt::DeclRefExprClass:
780 return cast<DeclRefExpr>(e)->getDecl();
781 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000782 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000783 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000784 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000785 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000786 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000787 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000788 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000789 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000790 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000791 default:
792 return 0;
793 }
794}
795
796/// CheckAddressOfOperand - The operand of & must be either a function
797/// designator or an lvalue designating an object. If it is an lvalue, the
798/// object cannot be declared with storage class register or be a bit field.
799/// Note: The usual conversions are *not* applied to the operand of the &
800/// operator, and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000801QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000802 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000803
Steve Naroff5dd642e2007-05-14 18:14:51 +0000804 if (!op->isLvalue()) { // C99 6.5.3.2p1
805 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
806 ;
Steve Naroff35d85152007-05-07 00:24:15 +0000807 else {
808 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
809 return QualType();
810 }
Steve Naroff47500512007-04-19 23:00:49 +0000811 } else if (dcl) {
812 // We have an lvalue with a decl. Make sure the decl is not declared
813 // with the register storage-class specifier.
814 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000815 if (vd->getStorageClass() == VarDecl::Register) {
816 Diag(OpLoc, diag::err_typecheck_address_of_register);
817 return QualType();
818 }
Steve Narofff633d092007-04-25 19:01:39 +0000819 } else
820 assert(0 && "Unknown/unexpected decl type");
821
Steve Naroff47500512007-04-19 23:00:49 +0000822 // FIXME: add check for bitfields!
823 }
824 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +0000825 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +0000826}
827
Steve Naroff35d85152007-05-07 00:24:15 +0000828QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
829 QualType qType = UsualUnaryConversion(op->getType());
830
Steve Naroff47500512007-04-19 23:00:49 +0000831 assert(!qType.isNull() && "no type for * expression");
832
Steve Naroff35d85152007-05-07 00:24:15 +0000833 if (PointerType *PT = dyn_cast<PointerType>(qType))
834 return PT->getPointeeType();
835 Diag(OpLoc, diag::err_typecheck_unary_expr, qType);
836 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +0000837}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000838
839static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
840 tok::TokenKind Kind) {
841 BinaryOperator::Opcode Opc;
842 switch (Kind) {
843 default: assert(0 && "Unknown binop!");
844 case tok::star: Opc = BinaryOperator::Mul; break;
845 case tok::slash: Opc = BinaryOperator::Div; break;
846 case tok::percent: Opc = BinaryOperator::Rem; break;
847 case tok::plus: Opc = BinaryOperator::Add; break;
848 case tok::minus: Opc = BinaryOperator::Sub; break;
849 case tok::lessless: Opc = BinaryOperator::Shl; break;
850 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
851 case tok::lessequal: Opc = BinaryOperator::LE; break;
852 case tok::less: Opc = BinaryOperator::LT; break;
853 case tok::greaterequal: Opc = BinaryOperator::GE; break;
854 case tok::greater: Opc = BinaryOperator::GT; break;
855 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
856 case tok::equalequal: Opc = BinaryOperator::EQ; break;
857 case tok::amp: Opc = BinaryOperator::And; break;
858 case tok::caret: Opc = BinaryOperator::Xor; break;
859 case tok::pipe: Opc = BinaryOperator::Or; break;
860 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
861 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
862 case tok::equal: Opc = BinaryOperator::Assign; break;
863 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
864 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
865 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
866 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
867 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
868 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
869 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
870 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
871 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
872 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
873 case tok::comma: Opc = BinaryOperator::Comma; break;
874 }
875 return Opc;
876}
877
Steve Naroff35d85152007-05-07 00:24:15 +0000878static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
879 tok::TokenKind Kind) {
880 UnaryOperator::Opcode Opc;
881 switch (Kind) {
882 default: assert(0 && "Unknown unary op!");
883 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
884 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
885 case tok::amp: Opc = UnaryOperator::AddrOf; break;
886 case tok::star: Opc = UnaryOperator::Deref; break;
887 case tok::plus: Opc = UnaryOperator::Plus; break;
888 case tok::minus: Opc = UnaryOperator::Minus; break;
889 case tok::tilde: Opc = UnaryOperator::Not; break;
890 case tok::exclaim: Opc = UnaryOperator::LNot; break;
891 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
892 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
893 case tok::kw___real: Opc = UnaryOperator::Real; break;
894 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
895 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
896 // FIXME: case tok::kw___extension__:
897 }
898 return Opc;
899}
900
Steve Naroff218bc2b2007-05-04 21:54:46 +0000901// Binary Operators. 'Tok' is the token for the operator.
902Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
903 ExprTy *LHS, ExprTy *RHS) {
904 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
905 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
906
907 assert((lhs != 0) && "ParseBinOp(): missing left expression");
908 assert((rhs != 0) && "ParseBinOp(): missing right expression");
909
910 QualType result;
911
912 switch (Opc) {
913 default:
914 assert(0 && "Unknown binary expr!");
915 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +0000916 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000917 break;
918 case BinaryOperator::Mul:
919 case BinaryOperator::Div:
920 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
921 break;
922 case BinaryOperator::Rem:
923 result = CheckRemainderOperands(lhs, rhs, TokLoc);
924 break;
925 case BinaryOperator::Add:
926 result = CheckAdditionOperands(lhs, rhs, TokLoc);
927 break;
928 case BinaryOperator::Sub:
929 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
930 break;
931 case BinaryOperator::Shl:
932 case BinaryOperator::Shr:
933 result = CheckShiftOperands(lhs, rhs, TokLoc);
934 break;
935 case BinaryOperator::LE:
936 case BinaryOperator::LT:
937 case BinaryOperator::GE:
938 case BinaryOperator::GT:
939 result = CheckRelationalOperands(lhs, rhs, TokLoc);
940 break;
941 case BinaryOperator::EQ:
942 case BinaryOperator::NE:
943 result = CheckEqualityOperands(lhs, rhs, TokLoc);
944 break;
945 case BinaryOperator::And:
946 case BinaryOperator::Xor:
947 case BinaryOperator::Or:
948 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
949 break;
950 case BinaryOperator::LAnd:
951 case BinaryOperator::LOr:
952 result = CheckLogicalOperands(lhs, rhs, TokLoc);
953 break;
954 case BinaryOperator::MulAssign:
955 case BinaryOperator::DivAssign:
956 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
957 if (result.isNull())
958 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000959 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000960 break;
961 case BinaryOperator::RemAssign:
962 result = CheckRemainderOperands(lhs, rhs, TokLoc);
963 if (result.isNull())
964 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000965 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000966 break;
967 case BinaryOperator::AddAssign:
968 result = CheckAdditionOperands(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::SubAssign:
974 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
975 if (result.isNull())
976 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000977 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000978 break;
979 case BinaryOperator::ShlAssign:
980 case BinaryOperator::ShrAssign:
981 result = CheckShiftOperands(lhs, rhs, TokLoc);
982 if (result.isNull())
983 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000984 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000985 break;
986 case BinaryOperator::AndAssign:
987 case BinaryOperator::XorAssign:
988 case BinaryOperator::OrAssign:
989 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
990 if (result.isNull())
991 return true;
Steve Naroff35d85152007-05-07 00:24:15 +0000992 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000993 break;
994 case BinaryOperator::Comma:
995 result = CheckCommaOperands(lhs, rhs, TokLoc);
996 break;
997 }
998 if (result.isNull())
999 return true;
1000 return new BinaryOperator(lhs, rhs, Opc, result);
1001}
1002
Steve Naroff35d85152007-05-07 00:24:15 +00001003// Unary Operators. 'Tok' is the token for the operator.
1004Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1005 ExprTy *Input) {
1006 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1007 QualType resultType;
1008 switch (Opc) {
1009 default:
1010 assert(0 && "Unimplemented unary expr!");
1011 case UnaryOperator::PreInc:
1012 case UnaryOperator::PreDec:
1013 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
1014 break;
1015 case UnaryOperator::AddrOf:
1016 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1017 break;
1018 case UnaryOperator::Deref:
1019 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1020 break;
1021 case UnaryOperator::Plus:
1022 case UnaryOperator::Minus:
1023 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1024 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1025 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
1026 break;
1027 case UnaryOperator::Not: // bitwise complement
1028 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1029 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
1030 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
1031 break;
1032 case UnaryOperator::LNot: // logical negation
1033 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1034 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1035 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
1036 break;
1037 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001038 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1039 true);
1040 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001041 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001042 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1043 false);
1044 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001045 }
1046 if (resultType.isNull())
1047 return true;
1048 return new UnaryOperator((Expr *)Input, Opc, resultType);
1049}