blob: 8ef94864590971f89bbdc6124f34d52111238141 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek30c66752007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/AST/Expr.h"
Chris Lattner3e254fb2008-04-08 04:40:51 +000018#include "clang/AST/ExprCXX.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000019#include "clang/AST/ExprObjC.h"
Steve Naroffc39ca262007-09-18 23:55:05 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/Lex/Preprocessor.h"
22#include "clang/Lex/LiteralSupport.h"
23#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner83bd5eb2007-12-28 05:29:59 +000025#include "llvm/ADT/OwningPtr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000026#include "llvm/ADT/SmallString.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000028using namespace clang;
29
Steve Naroff87d58b42007-09-16 03:34:24 +000030/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +000031/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
32/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
33/// multiple tokens. However, the common case is that StringToks points to one
34/// string.
35///
36Action::ExprResult
Steve Naroff87d58b42007-09-16 03:34:24 +000037Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +000038 assert(NumStringToks && "Must have at least one string!");
39
40 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
41 if (Literal.hadError)
42 return ExprResult(true);
43
44 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
45 for (unsigned i = 0; i != NumStringToks; ++i)
46 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattnera6dcce32008-02-11 00:02:17 +000047
48 // Verify that pascal strings aren't too large.
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000049 if (Literal.Pascal && Literal.GetStringLength() > 256)
50 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
51 SourceRange(StringToks[0].getLocation(),
52 StringToks[NumStringToks-1].getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +000053
Chris Lattnera6dcce32008-02-11 00:02:17 +000054 QualType StrTy = Context.CharTy;
Eli Friedman256b7d72008-05-27 07:57:14 +000055 if (Literal.AnyWide) StrTy = Context.getWcharType();
Chris Lattnera6dcce32008-02-11 00:02:17 +000056 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
57
58 // Get an array type for the string, according to C99 6.4.5. This includes
59 // the nul terminator character as well as the string length for pascal
60 // strings.
61 StrTy = Context.getConstantArrayType(StrTy,
62 llvm::APInt(32, Literal.GetStringLength()+1),
63 ArrayType::Normal, 0);
64
Chris Lattner4b009652007-07-25 00:24:17 +000065 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
66 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera6dcce32008-02-11 00:02:17 +000067 Literal.AnyWide, StrTy,
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000068 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +000069 StringToks[NumStringToks-1].getLocation());
70}
71
72
Steve Naroff0acc9c92007-09-15 18:49:24 +000073/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +000074/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroffe50e14c2008-03-19 23:46:26 +000075/// identifier is used in a function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +000076Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +000077 IdentifierInfo &II,
78 bool HasTrailingLParen) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +000079 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff6384a012008-04-02 14:35:35 +000080 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattnerc72d22d2008-03-31 00:36:02 +000081
82 // If this reference is in an Objective-C method, then ivar lookup happens as
83 // well.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000084 if (getCurMethodDecl()) {
Steve Naroffe57c21a2008-04-01 23:04:06 +000085 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattnerc72d22d2008-03-31 00:36:02 +000086 // There are two cases to handle here. 1) scoped lookup could have failed,
87 // in which case we should look for an ivar. 2) scoped lookup could have
88 // found a decl, but that decl is outside the current method (i.e. a global
89 // variable). In these two cases, we do a lookup for an ivar with this
90 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroffe57c21a2008-04-01 23:04:06 +000091 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Argiris Kirtzidis95256e62008-06-28 06:07:14 +000092 ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
93 ObjCInterfaceDecl *DeclClass;
Chris Lattnerc72d22d2008-03-31 00:36:02 +000094 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, DeclClass)) {
95 // FIXME: This should use a new expr for a direct reference, don't turn
96 // this into Self->ivar, just return a BareIVarExpr or something.
97 IdentifierInfo &II = Context.Idents.get("self");
98 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
99 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
100 static_cast<Expr*>(SelfExpr.Val), true, true);
101 }
102 }
Steve Naroffe90d4cc2008-06-05 18:14:25 +0000103 if (SD == 0 && !strcmp(II.getName(), "super")) {
Steve Naroff6f786252008-06-02 23:03:37 +0000104 QualType T = Context.getPointerType(Context.getObjCInterfaceType(
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000105 getCurMethodDecl()->getClassInterface()));
Steve Naroff6f786252008-06-02 23:03:37 +0000106 return new ObjCSuperRefExpr(T, Loc);
107 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000108 }
109
Chris Lattner4b009652007-07-25 00:24:17 +0000110 if (D == 0) {
111 // Otherwise, this could be an implicitly declared function reference (legal
112 // in C90, extension in C99).
113 if (HasTrailingLParen &&
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000114 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattner4b009652007-07-25 00:24:17 +0000115 D = ImplicitlyDefineFunction(Loc, II, S);
116 else {
117 // If this name wasn't predeclared and if this is not a function call,
118 // diagnose the problem.
119 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
120 }
121 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000122
Steve Naroff91b03f72007-08-28 03:03:08 +0000123 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneree4c3bf2008-02-29 16:48:43 +0000124 // check if referencing an identifier with __attribute__((deprecated)).
125 if (VD->getAttr<DeprecatedAttr>())
126 Diag(Loc, diag::warn_deprecated, VD->getName());
127
Steve Naroffcae537d2007-08-28 18:45:29 +0000128 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000129 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000130 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000131 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000132 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000133
Chris Lattner4b009652007-07-25 00:24:17 +0000134 if (isa<TypedefDecl>(D))
135 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000136 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000137 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000138 if (isa<NamespaceDecl>(D))
139 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000140
141 assert(0 && "Invalid decl");
142 abort();
143}
144
Steve Naroff87d58b42007-09-16 03:34:24 +0000145Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000146 tok::TokenKind Kind) {
147 PreDefinedExpr::IdentType IT;
148
149 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000150 default: assert(0 && "Unknown simple primary expr!");
151 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
152 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
153 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000154 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000155
156 // Verify that this is in a function context.
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000157 if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000158 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000159
Chris Lattner7e637512008-01-12 08:14:25 +0000160 // Pre-defined identifiers are of type char[x], where x is the length of the
161 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000162 unsigned Length;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000163 if (getCurFunctionDecl())
164 Length = getCurFunctionDecl()->getIdentifier()->getLength();
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000165 else
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000166 Length = getCurMethodDecl()->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000167
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000168 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000169 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000170 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000171 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000172}
173
Steve Naroff87d58b42007-09-16 03:34:24 +0000174Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 llvm::SmallString<16> CharBuffer;
176 CharBuffer.resize(Tok.getLength());
177 const char *ThisTokBegin = &CharBuffer[0];
178 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
179
180 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
181 Tok.getLocation(), PP);
182 if (Literal.hadError())
183 return ExprResult(true);
Chris Lattner6b22fb72008-03-01 08:32:21 +0000184
185 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
186
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000187 return new CharacterLiteral(Literal.getValue(), Literal.isWide(), type,
188 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000189}
190
Steve Naroff87d58b42007-09-16 03:34:24 +0000191Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000192 // fast path for a single digit (which is quite common). A single digit
193 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
194 if (Tok.getLength() == 1) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000195 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000196
Chris Lattner8cd0e932008-03-05 18:54:05 +0000197 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner48d7f382008-04-02 04:24:33 +0000198 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner4b009652007-07-25 00:24:17 +0000199 Context.IntTy,
200 Tok.getLocation()));
201 }
202 llvm::SmallString<512> IntegerBuffer;
203 IntegerBuffer.resize(Tok.getLength());
204 const char *ThisTokBegin = &IntegerBuffer[0];
205
206 // Get the spelling of the token, which eliminates trigraphs, etc.
207 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
208 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
209 Tok.getLocation(), PP);
210 if (Literal.hadError)
211 return ExprResult(true);
212
Chris Lattner1de66eb2007-08-26 03:42:43 +0000213 Expr *Res;
214
215 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000216 QualType Ty;
217 const llvm::fltSemantics *Format;
Chris Lattner858eece2007-09-22 18:29:59 +0000218
219 if (Literal.isFloat) {
220 Ty = Context.FloatTy;
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000221 Format = Context.Target.getFloatFormat();
222 } else if (!Literal.isLong) {
Chris Lattner858eece2007-09-22 18:29:59 +0000223 Ty = Context.DoubleTy;
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000224 Format = Context.Target.getDoubleFormat();
225 } else {
226 Ty = Context.LongDoubleTy;
227 Format = Context.Target.getLongDoubleFormat();
Chris Lattner858eece2007-09-22 18:29:59 +0000228 }
229
Ted Kremenekddedbe22007-11-29 00:56:49 +0000230 // isExact will be set by GetFloatValue().
231 bool isExact = false;
232
233 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
234 Ty, Tok.getLocation());
235
Chris Lattner1de66eb2007-08-26 03:42:43 +0000236 } else if (!Literal.isIntegerLiteral()) {
237 return ExprResult(true);
238 } else {
Chris Lattner48d7f382008-04-02 04:24:33 +0000239 QualType Ty;
Chris Lattner4b009652007-07-25 00:24:17 +0000240
Neil Booth7421e9c2007-08-29 22:00:19 +0000241 // long long is a C99 feature.
242 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000243 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000244 Diag(Tok.getLocation(), diag::ext_longlong);
245
Chris Lattner4b009652007-07-25 00:24:17 +0000246 // Get the value in the widest-possible width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000247 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000248
249 if (Literal.GetIntegerValue(ResultVal)) {
250 // If this value didn't fit into uintmax_t, warn and force to ull.
251 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner48d7f382008-04-02 04:24:33 +0000252 Ty = Context.UnsignedLongLongTy;
253 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner8cd0e932008-03-05 18:54:05 +0000254 "long long is not intmax_t?");
Chris Lattner4b009652007-07-25 00:24:17 +0000255 } else {
256 // If this value fits into a ULL, try to figure out what else it fits into
257 // according to the rules of C99 6.4.4.1p5.
258
259 // Octal, Hexadecimal, and integers with a U suffix are allowed to
260 // be an unsigned int.
261 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
262
263 // Check from smallest to largest, picking the smallest type we can.
Chris Lattnere4068872008-05-09 05:59:00 +0000264 unsigned Width = 0;
Chris Lattner98540b62007-08-23 21:58:08 +0000265 if (!Literal.isLong && !Literal.isLongLong) {
266 // Are int/unsigned possibilities?
Chris Lattnere4068872008-05-09 05:59:00 +0000267 unsigned IntSize = Context.Target.getIntWidth();
268
Chris Lattner4b009652007-07-25 00:24:17 +0000269 // Does it fit in a unsigned int?
270 if (ResultVal.isIntN(IntSize)) {
271 // Does it fit in a signed int?
272 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000273 Ty = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000274 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000275 Ty = Context.UnsignedIntTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000276 Width = IntSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000277 }
Chris Lattner4b009652007-07-25 00:24:17 +0000278 }
279
280 // Are long/unsigned long possibilities?
Chris Lattner48d7f382008-04-02 04:24:33 +0000281 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattnere4068872008-05-09 05:59:00 +0000282 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000283
284 // Does it fit in a unsigned long?
285 if (ResultVal.isIntN(LongSize)) {
286 // Does it fit in a signed long?
287 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000288 Ty = Context.LongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000289 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000290 Ty = Context.UnsignedLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000291 Width = LongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000292 }
Chris Lattner4b009652007-07-25 00:24:17 +0000293 }
294
295 // Finally, check long long if needed.
Chris Lattner48d7f382008-04-02 04:24:33 +0000296 if (Ty.isNull()) {
Chris Lattnere4068872008-05-09 05:59:00 +0000297 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000298
299 // Does it fit in a unsigned long long?
300 if (ResultVal.isIntN(LongLongSize)) {
301 // Does it fit in a signed long long?
302 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000303 Ty = Context.LongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000304 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000305 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000306 Width = LongLongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000307 }
308 }
309
310 // If we still couldn't decide a type, we probably have something that
311 // does not fit in a signed long long, but has no U suffix.
Chris Lattner48d7f382008-04-02 04:24:33 +0000312 if (Ty.isNull()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000313 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner48d7f382008-04-02 04:24:33 +0000314 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000315 Width = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000316 }
Chris Lattnere4068872008-05-09 05:59:00 +0000317
318 if (ResultVal.getBitWidth() != Width)
319 ResultVal.trunc(Width);
Chris Lattner4b009652007-07-25 00:24:17 +0000320 }
321
Chris Lattner48d7f382008-04-02 04:24:33 +0000322 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000323 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000324
325 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
326 if (Literal.isImaginary)
327 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
328
329 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000330}
331
Steve Naroff87d58b42007-09-16 03:34:24 +0000332Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000333 ExprTy *Val) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000334 Expr *E = (Expr *)Val;
335 assert((E != 0) && "ActOnParenExpr() missing expr");
336 return new ParenExpr(L, R, E);
Chris Lattner4b009652007-07-25 00:24:17 +0000337}
338
339/// The UsualUnaryConversions() function is *not* called by this routine.
340/// See C99 6.3.2.1p[2-4] for more details.
341QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
342 SourceLocation OpLoc, bool isSizeof) {
343 // C99 6.5.3.4p1:
344 if (isa<FunctionType>(exprType) && isSizeof)
345 // alignof(function) is allowed.
346 Diag(OpLoc, diag::ext_sizeof_function_type);
347 else if (exprType->isVoidType())
348 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
349 else if (exprType->isIncompleteType()) {
350 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
351 diag::err_alignof_incomplete_type,
352 exprType.getAsString());
353 return QualType(); // error
354 }
355 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
356 return Context.getSizeType();
357}
358
359Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000360ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000361 SourceLocation LPLoc, TypeTy *Ty,
362 SourceLocation RPLoc) {
363 // If error parsing type, ignore.
364 if (Ty == 0) return true;
365
366 // Verify that this is a valid expression.
367 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
368
369 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
370
371 if (resultType.isNull())
372 return true;
373 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
374}
375
Chris Lattner5110ad52007-08-24 21:41:10 +0000376QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000377 DefaultFunctionArrayConversion(V);
378
Chris Lattnera16e42d2007-08-26 05:39:26 +0000379 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000380 if (const ComplexType *CT = V->getType()->getAsComplexType())
381 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000382
383 // Otherwise they pass through real integer and floating point types here.
384 if (V->getType()->isArithmeticType())
385 return V->getType();
386
387 // Reject anything else.
388 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
389 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000390}
391
392
Chris Lattner4b009652007-07-25 00:24:17 +0000393
Steve Naroff87d58b42007-09-16 03:34:24 +0000394Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000395 tok::TokenKind Kind,
396 ExprTy *Input) {
397 UnaryOperator::Opcode Opc;
398 switch (Kind) {
399 default: assert(0 && "Unknown unary op!");
400 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
401 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
402 }
403 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
404 if (result.isNull())
405 return true;
406 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
407}
408
409Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000410ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000411 ExprTy *Idx, SourceLocation RLoc) {
412 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
413
414 // Perform default conversions.
415 DefaultFunctionArrayConversion(LHSExp);
416 DefaultFunctionArrayConversion(RHSExp);
417
418 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
419
420 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000421 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000422 // in the subscript position. As a result, we need to derive the array base
423 // and index from the expression types.
424 Expr *BaseExpr, *IndexExpr;
425 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000426 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000427 BaseExpr = LHSExp;
428 IndexExpr = RHSExp;
429 // FIXME: need to deal with const...
430 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000431 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000432 // Handle the uncommon case of "123[Ptr]".
433 BaseExpr = RHSExp;
434 IndexExpr = LHSExp;
435 // FIXME: need to deal with const...
436 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000437 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
438 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000439 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000440
441 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000442 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
443 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000444 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000445 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000446 // FIXME: need to deal with const...
447 ResultType = VTy->getElementType();
448 } else {
449 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
450 RHSExp->getSourceRange());
451 }
452 // C99 6.5.2.1p1
453 if (!IndexExpr->getType()->isIntegerType())
454 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
455 IndexExpr->getSourceRange());
456
457 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
458 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000459 // void (*)(int)) and pointers to incomplete types. Functions are not
460 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000461 if (!ResultType->isObjectType())
462 return Diag(BaseExpr->getLocStart(),
463 diag::err_typecheck_subscript_not_object,
464 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
465
466 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
467}
468
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000469QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000470CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000471 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000472 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanc8e51f82008-05-09 06:41:27 +0000473
474 // This flag determines whether or not the component is to be treated as a
475 // special name, or a regular GLSL-style component access.
476 bool SpecialComponent = false;
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000477
478 // The vector accessor can't exceed the number of elements.
479 const char *compStr = CompName.getName();
480 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000481 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000482 baseType.getAsString(), SourceRange(CompLoc));
483 return QualType();
484 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000485
486 // Check that we've found one of the special components, or that the component
487 // names must come from the same set.
488 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
489 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
490 SpecialComponent = true;
491 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner9096b792007-08-02 22:33:49 +0000492 do
493 compStr++;
494 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
495 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
496 do
497 compStr++;
498 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
499 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
500 do
501 compStr++;
502 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
503 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000504
Nate Begemanc8e51f82008-05-09 06:41:27 +0000505 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000506 // We didn't get to the end of the string. This means the component names
507 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000508 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000509 std::string(compStr,compStr+1), SourceRange(CompLoc));
510 return QualType();
511 }
512 // Each component accessor can't exceed the vector type.
513 compStr = CompName.getName();
514 while (*compStr) {
515 if (vecType->isAccessorWithinNumElements(*compStr))
516 compStr++;
517 else
518 break;
519 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000520 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000521 // We didn't get to the end of the string. This means a component accessor
522 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000523 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000524 baseType.getAsString(), SourceRange(CompLoc));
525 return QualType();
526 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000527
528 // If we have a special component name, verify that the current vector length
529 // is an even number, since all special component names return exactly half
530 // the elements.
531 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
532 return QualType();
533 }
534
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000535 // The component accessor looks fine - now we need to compute the actual type.
536 // The vector type is implied by the component accessor. For example,
537 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanc8e51f82008-05-09 06:41:27 +0000538 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
539 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
540 : strlen(CompName.getName());
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000541 if (CompSize == 1)
542 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000543
Nate Begemanaf6ed502008-04-18 23:10:10 +0000544 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000545 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000546 // diagostics look bad. We want extended vector types to appear built-in.
547 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
548 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
549 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000550 }
551 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000552}
553
Chris Lattner4b009652007-07-25 00:24:17 +0000554Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000555ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000556 tok::TokenKind OpKind, SourceLocation MemberLoc,
557 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000558 Expr *BaseExpr = static_cast<Expr *>(Base);
559 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000560
561 // Perform default conversions.
562 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000563
Steve Naroff2cb66382007-07-26 03:11:44 +0000564 QualType BaseType = BaseExpr->getType();
565 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000566
Chris Lattner4b009652007-07-25 00:24:17 +0000567 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000568 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000569 BaseType = PT->getPointeeType();
570 else
571 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
572 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000573 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000574 // The base type is either a record or an ExtVectorType.
Chris Lattnere35a1042007-07-31 19:29:30 +0000575 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000576 RecordDecl *RDecl = RTy->getDecl();
577 if (RTy->isIncompleteType())
578 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
579 BaseExpr->getSourceRange());
580 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000581 FieldDecl *MemberDecl = RDecl->getMember(&Member);
582 if (!MemberDecl)
Steve Naroff2cb66382007-07-26 03:11:44 +0000583 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
584 SourceRange(MemberLoc));
Eli Friedman76b49832008-02-06 22:48:16 +0000585
586 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000587 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000588 QualType MemberType = MemberDecl->getType();
589 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000590 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000591 MemberType = MemberType.getQualifiedType(combinedQualifiers);
592
593 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
594 MemberLoc, MemberType);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000595 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff89345522007-08-03 22:40:33 +0000596 // Component access limited to variables (reject vec4.rg.g).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000597 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
598 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000599 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000600 SourceRange(MemberLoc));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000601 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000602 if (ret.isNull())
603 return true;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000604 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000605 } else if (BaseType->isObjCInterfaceType()) {
606 ObjCInterfaceDecl *IFace;
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000607 QualType CanonType = BaseType.getCanonicalType();
608 if (isa<ObjCInterfaceType>(CanonType))
609 IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000610 else
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000611 IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
Ted Kremenek42730c52008-01-07 19:49:32 +0000612 ObjCInterfaceDecl *clsDeclared;
613 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000614 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
615 OpKind==tok::arrow);
Steve Naroff05391d22008-05-30 00:40:33 +0000616 } else if (isObjCObjectPointerType(BaseType)) {
617 PointerType *pointerType = static_cast<PointerType*>(BaseType.getTypePtr());
618 BaseType = pointerType->getPointeeType();
619 ObjCInterfaceDecl *IFace;
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000620 QualType CanonType = BaseType.getCanonicalType();
621 if (isa<ObjCInterfaceType>(CanonType))
622 IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
Steve Naroff05391d22008-05-30 00:40:33 +0000623 else
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000624 IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
Steve Naroff05391d22008-05-30 00:40:33 +0000625 ObjCInterfaceDecl *clsDeclared;
626 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
627 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
628 OpKind==tok::arrow);
629 // Check for properties.
630 if (OpKind==tok::period) {
631 // Before we look for explicit property declarations, we check for
632 // nullary methods (which allow '.' notation).
633 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
634 ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel);
635 if (MD)
636 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
637 MemberLoc, BaseExpr);
638 // FIXME: Need to deal with setter methods that take 1 argument. E.g.:
639 // @interface NSBundle : NSObject {}
640 // - (NSString *)bundlePath;
641 // - (void)setBundlePath:(NSString *)x;
642 // @end
643 // void someMethod() { frameworkBundle.bundlePath = 0; }
644 //
Steve Naroff858813d2008-06-03 21:56:14 +0000645 ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member);
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000646
647 if (!PD) { // Lastly, check protocols on qualified interfaces.
648 if (ObjCQualifiedInterfaceType *QIT =
649 dyn_cast<ObjCQualifiedInterfaceType>(CanonType)) {
650 for (unsigned i = 0; i < QIT->getNumProtocols(); i++)
651 if ((PD = QIT->getProtocols(i)->FindPropertyDeclaration(&Member)))
652 break;
653 }
654 }
Steve Naroff858813d2008-06-03 21:56:14 +0000655 if (PD)
656 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
Steve Naroff05391d22008-05-30 00:40:33 +0000657 }
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000658 }
659 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
660 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000661}
662
Steve Naroff87d58b42007-09-16 03:34:24 +0000663/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000664/// This provides the location of the left/right parens and a list of comma
665/// locations.
666Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000667ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000668 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000669 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
670 Expr *Fn = static_cast<Expr *>(fn);
671 Expr **Args = reinterpret_cast<Expr**>(args);
672 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +0000673 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000674
675 // Promote the function operand.
676 UsualUnaryConversions(Fn);
677
678 // If we're directly calling a function, get the declaration for
679 // that function.
680 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
681 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
682 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
683
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000684 // Make the call expr early, before semantic checks. This guarantees cleanup
685 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +0000686 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000687 Context.BoolTy, RParenLoc));
688
Chris Lattner4b009652007-07-25 00:24:17 +0000689 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
690 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000691 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000692 if (PT == 0)
693 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
694 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000695 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
696 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000697 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
698 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000699
700 // We know the result type of the call, set it.
701 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000702
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000703 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000704 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
705 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000706 unsigned NumArgsInProto = Proto->getNumArgs();
707 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000708
Chris Lattner3e254fb2008-04-08 04:40:51 +0000709 // If too few arguments are available (and we don't have default
710 // arguments for the remaining parameters), don't make the call.
711 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +0000712 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000713 // Use default arguments for missing arguments
714 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +0000715 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000716 } else
717 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
718 Fn->getSourceRange());
719 }
720
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000721 // If too many are passed and not variadic, error on the extras and drop
722 // them.
723 if (NumArgs > NumArgsInProto) {
724 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000725 Diag(Args[NumArgsInProto]->getLocStart(),
726 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
727 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000728 Args[NumArgs-1]->getLocEnd()));
729 // This deletes the extra arguments.
730 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000731 }
732 NumArgsToCheck = NumArgsInProto;
733 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000734
Chris Lattner4b009652007-07-25 00:24:17 +0000735 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000736 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +0000737 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000738
739 Expr *Arg;
740 if (i < NumArgs)
741 Arg = Args[i];
742 else
743 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +0000744 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000745
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000746 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +0000747 AssignConvertType ConvTy =
748 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000749 TheCall->setArg(i, Arg);
750
Chris Lattner005ed752008-01-04 18:04:52 +0000751 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
752 ArgType, Arg, "passing"))
753 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000754 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000755
756 // If this is a variadic call, handle args passed through "...".
757 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +0000758 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000759 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
760 Expr *Arg = Args[i];
761 DefaultArgumentPromotion(Arg);
762 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000763 }
Steve Naroffdb65e052007-08-28 23:30:39 +0000764 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000765 } else {
766 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
767
Steve Naroffdb65e052007-08-28 23:30:39 +0000768 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000769 for (unsigned i = 0; i != NumArgs; i++) {
770 Expr *Arg = Args[i];
771 DefaultArgumentPromotion(Arg);
772 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000773 }
Chris Lattner4b009652007-07-25 00:24:17 +0000774 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000775
Chris Lattner2e64c072007-08-10 20:18:51 +0000776 // Do special checking on direct calls to functions.
Eli Friedmand0e9d092008-05-14 19:38:39 +0000777 if (FDecl)
778 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner2e64c072007-08-10 20:18:51 +0000779
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000780 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +0000781}
782
783Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000784ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000785 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000786 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000787 QualType literalType = QualType::getFromOpaquePtr(Ty);
788 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +0000789 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000790 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +0000791
Eli Friedman8c2173d2008-05-20 05:22:08 +0000792 if (literalType->isArrayType()) {
793 if (literalType->getAsVariableArrayType())
794 return Diag(LParenLoc,
795 diag::err_variable_object_no_init,
796 SourceRange(LParenLoc,
797 literalExpr->getSourceRange().getEnd()));
798 } else if (literalType->isIncompleteType()) {
799 return Diag(LParenLoc,
800 diag::err_typecheck_decl_incomplete_type,
801 literalType.getAsString(),
802 SourceRange(LParenLoc,
803 literalExpr->getSourceRange().getEnd()));
804 }
805
Steve Narofff0b23542008-01-10 22:15:12 +0000806 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +0000807 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +0000808
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000809 bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000810 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +0000811 if (CheckForConstantInitializer(literalExpr, literalType))
812 return true;
813 }
Steve Naroffbe37fc02008-01-14 18:19:28 +0000814 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000815}
816
817Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000818ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +0000819 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000820 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000821
Steve Naroff0acc9c92007-09-15 18:49:24 +0000822 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +0000823 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +0000824
Chris Lattner48d7f382008-04-02 04:24:33 +0000825 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
826 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
827 return E;
Chris Lattner4b009652007-07-25 00:24:17 +0000828}
829
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000830bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000831 assert(VectorTy->isVectorType() && "Not a vector type!");
832
833 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000834 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000835 return Diag(R.getBegin(),
836 Ty->isVectorType() ?
837 diag::err_invalid_conversion_between_vectors :
838 diag::err_invalid_conversion_between_vector_and_integer,
839 VectorTy.getAsString().c_str(),
840 Ty.getAsString().c_str(), R);
841 } else
842 return Diag(R.getBegin(),
843 diag::err_invalid_conversion_between_vector_and_scalar,
844 VectorTy.getAsString().c_str(),
845 Ty.getAsString().c_str(), R);
846
847 return false;
848}
849
Chris Lattner4b009652007-07-25 00:24:17 +0000850Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000851ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000852 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000853 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000854
855 Expr *castExpr = static_cast<Expr*>(Op);
856 QualType castType = QualType::getFromOpaquePtr(Ty);
857
Steve Naroff68adb482007-08-31 00:32:44 +0000858 UsualUnaryConversions(castExpr);
859
Chris Lattner4b009652007-07-25 00:24:17 +0000860 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
861 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +0000862 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff5ad85292008-06-03 12:56:35 +0000863 if (!castType->isScalarType() && !castType->isVectorType()) {
864 // GCC struct/union extension.
865 if (castType == castExpr->getType() &&
Steve Naroff7f1c5b52008-06-03 13:21:30 +0000866 castType->isStructureType() || castType->isUnionType()) {
867 Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar,
868 SourceRange(LParenLoc, RParenLoc));
869 return new CastExpr(castType, castExpr, LParenLoc);
870 } else
Steve Naroff5ad85292008-06-03 12:56:35 +0000871 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
872 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
873 }
Steve Narofff459ee52008-01-24 22:55:05 +0000874 if (!castExpr->getType()->isScalarType() &&
875 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000876 return Diag(castExpr->getLocStart(),
877 diag::err_typecheck_expect_scalar_operand,
878 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000879
880 if (castExpr->getType()->isVectorType()) {
881 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
882 castExpr->getType(), castType))
883 return true;
884 } else if (castType->isVectorType()) {
885 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
886 castType, castExpr->getType()))
887 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +0000888 }
Chris Lattner4b009652007-07-25 00:24:17 +0000889 }
890 return new CastExpr(castType, castExpr, LParenLoc);
891}
892
Chris Lattner98a425c2007-11-26 01:40:58 +0000893/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
894/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +0000895inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
896 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
897 UsualUnaryConversions(cond);
898 UsualUnaryConversions(lex);
899 UsualUnaryConversions(rex);
900 QualType condT = cond->getType();
901 QualType lexT = lex->getType();
902 QualType rexT = rex->getType();
903
904 // first, check the condition.
905 if (!condT->isScalarType()) { // C99 6.5.15p2
906 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
907 condT.getAsString());
908 return QualType();
909 }
Chris Lattner992ae932008-01-06 22:42:25 +0000910
911 // Now check the two expressions.
912
913 // If both operands have arithmetic type, do the usual arithmetic conversions
914 // to find a common type: C99 6.5.15p3,5.
915 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000916 UsualArithmeticConversions(lex, rex);
917 return lex->getType();
918 }
Chris Lattner992ae932008-01-06 22:42:25 +0000919
920 // If both operands are the same structure or union type, the result is that
921 // type.
Chris Lattner71225142007-07-31 21:27:01 +0000922 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +0000923 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +0000924 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +0000925 // "If both the operands have structure or union type, the result has
926 // that type." This implies that CV qualifiers are dropped.
927 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000928 }
Chris Lattner992ae932008-01-06 22:42:25 +0000929
930 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroff95cb3892008-05-12 21:44:38 +0000931 // The following || allows only one side to be void (a GCC-ism).
932 if (lexT->isVoidType() || rexT->isVoidType()) {
Eli Friedmanf025aac2008-06-04 19:47:51 +0000933 if (!lexT->isVoidType())
Steve Naroff95cb3892008-05-12 21:44:38 +0000934 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
935 rex->getSourceRange());
936 if (!rexT->isVoidType())
937 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
Nuno Lopes4ba41fd2008-06-04 19:14:12 +0000938 lex->getSourceRange());
Eli Friedmanf025aac2008-06-04 19:47:51 +0000939 ImpCastExprToType(lex, Context.VoidTy);
940 ImpCastExprToType(rex, Context.VoidTy);
941 return Context.VoidTy;
Steve Naroff95cb3892008-05-12 21:44:38 +0000942 }
Steve Naroff12ebf272008-01-08 01:11:38 +0000943 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
944 // the type of the other operand."
945 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000946 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000947 return lexT;
948 }
949 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000950 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000951 return rexT;
952 }
Chris Lattner0ac51632008-01-06 22:50:31 +0000953 // Handle the case where both operands are pointers before we handle null
954 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +0000955 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
956 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
957 // get the "pointed to" types
958 QualType lhptee = LHSPT->getPointeeType();
959 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000960
Chris Lattner71225142007-07-31 21:27:01 +0000961 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
962 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000963 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000964 // Figure out necessary qualifiers (C99 6.5.15p6)
965 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000966 QualType destType = Context.getPointerType(destPointee);
967 ImpCastExprToType(lex, destType); // add qualifiers if necessary
968 ImpCastExprToType(rex, destType); // promote to void*
969 return destType;
970 }
Chris Lattner9db553e2008-04-02 06:59:01 +0000971 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000972 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000973 QualType destType = Context.getPointerType(destPointee);
974 ImpCastExprToType(lex, destType); // add qualifiers if necessary
975 ImpCastExprToType(rex, destType); // promote to void*
976 return destType;
977 }
Chris Lattner4b009652007-07-25 00:24:17 +0000978
Steve Naroff85f0dc52007-10-15 20:41:53 +0000979 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
980 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +0000981 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +0000982 lexT.getAsString(), rexT.getAsString(),
983 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +0000984 // In this situation, we assume void* type. No especially good
985 // reason, but this is what gcc does, and we do have to pick
986 // to get a consistent AST.
987 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
988 ImpCastExprToType(lex, voidPtrTy);
989 ImpCastExprToType(rex, voidPtrTy);
990 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +0000991 }
992 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000993 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
994 // differently qualified versions of compatible types, the result type is
995 // a pointer to an appropriately qualified version of the *composite*
996 // type.
Eli Friedmane38150e2008-05-16 20:37:07 +0000997 // FIXME: Need to calculate the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +0000998 // FIXME: Need to add qualifiers
Eli Friedmane38150e2008-05-16 20:37:07 +0000999 QualType compositeType = lexT;
1000 ImpCastExprToType(lex, compositeType);
1001 ImpCastExprToType(rex, compositeType);
1002 return compositeType;
Chris Lattner4b009652007-07-25 00:24:17 +00001003 }
Chris Lattner4b009652007-07-25 00:24:17 +00001004 }
Steve Naroff605896f2008-05-31 22:33:45 +00001005 // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
1006 // evaluates to "struct objc_object *" (and is handled above when comparing
1007 // id with statically typed objects). FIXME: Do we need an ImpCastExprToType?
1008 if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) {
1009 if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true))
1010 return Context.getObjCIdType();
1011 }
Chris Lattner992ae932008-01-06 22:42:25 +00001012 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +00001013 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
1014 lexT.getAsString(), rexT.getAsString(),
1015 lex->getSourceRange(), rex->getSourceRange());
1016 return QualType();
1017}
1018
Steve Naroff87d58b42007-09-16 03:34:24 +00001019/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +00001020/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +00001021Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001022 SourceLocation ColonLoc,
1023 ExprTy *Cond, ExprTy *LHS,
1024 ExprTy *RHS) {
1025 Expr *CondExpr = (Expr *) Cond;
1026 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +00001027
1028 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1029 // was the condition.
1030 bool isLHSNull = LHSExpr == 0;
1031 if (isLHSNull)
1032 LHSExpr = CondExpr;
1033
Chris Lattner4b009652007-07-25 00:24:17 +00001034 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1035 RHSExpr, QuestionLoc);
1036 if (result.isNull())
1037 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +00001038 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1039 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +00001040}
1041
Steve Naroffdb65e052007-08-28 23:30:39 +00001042/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffbbaed752008-01-29 02:42:22 +00001043/// do not have a prototype. Arguments that have type float are promoted to
1044/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001045void Sema::DefaultArgumentPromotion(Expr *&Expr) {
1046 QualType Ty = Expr->getType();
1047 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffdb65e052007-08-28 23:30:39 +00001048
Chris Lattner11216032008-06-27 22:48:56 +00001049 // If this is a 'float' (CVR qualified or typedef) promote to double.
1050 if (const BuiltinType *BT = Ty->getAsBuiltinType())
1051 if (BT->getKind() == BuiltinType::Float)
1052 return ImpCastExprToType(Expr, Context.DoubleTy);
1053
1054 UsualUnaryConversions(Expr);
Steve Naroffdb65e052007-08-28 23:30:39 +00001055}
1056
Chris Lattner4b009652007-07-25 00:24:17 +00001057/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattner48d7f382008-04-02 04:24:33 +00001058void Sema::DefaultFunctionArrayConversion(Expr *&E) {
1059 QualType Ty = E->getType();
1060 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001061
Chris Lattner48d7f382008-04-02 04:24:33 +00001062 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +00001063 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattner48d7f382008-04-02 04:24:33 +00001064 Ty = E->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001065 }
Chris Lattner48d7f382008-04-02 04:24:33 +00001066 if (Ty->isFunctionType())
1067 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattner19eb97e2008-04-02 05:18:44 +00001068 else if (Ty->isArrayType())
1069 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Chris Lattner4b009652007-07-25 00:24:17 +00001070}
1071
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001072/// UsualUnaryConversions - Performs various conversions that are common to most
Chris Lattner4b009652007-07-25 00:24:17 +00001073/// operators (C99 6.3). The conversions of array and function types are
1074/// sometimes surpressed. For example, the array->pointer conversion doesn't
1075/// apply if the array is an argument to the sizeof or address (&) operators.
1076/// In these instances, this routine should *not* be called.
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001077Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
1078 QualType Ty = Expr->getType();
1079 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001080
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001081 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +00001082 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001083 Ty = Expr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001084 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001085 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnere992d6c2008-01-16 19:17:22 +00001086 ImpCastExprToType(Expr, Context.IntTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001087 else
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001088 DefaultFunctionArrayConversion(Expr);
1089
1090 return Expr;
Chris Lattner4b009652007-07-25 00:24:17 +00001091}
1092
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001093/// UsualArithmeticConversions - Performs various conversions that are common to
Chris Lattner4b009652007-07-25 00:24:17 +00001094/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
1095/// routine returns the first non-arithmetic type found. The client is
1096/// responsible for emitting appropriate error diagnostics.
Chris Lattner48d7f382008-04-02 04:24:33 +00001097/// FIXME: verify the conversion rules for "complex int" are consistent with
1098/// GCC.
Steve Naroff8f708362007-08-24 19:07:16 +00001099QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
1100 bool isCompAssign) {
Steve Naroffb2f9f552007-08-25 19:54:59 +00001101 if (!isCompAssign) {
1102 UsualUnaryConversions(lhsExpr);
1103 UsualUnaryConversions(rhsExpr);
1104 }
Steve Naroff7438fdf2007-10-18 18:55:53 +00001105 // For conversion purposes, we ignore any qualifiers.
1106 // For example, "const float" and "float" are equivalent.
Steve Naroff1ddb6f52007-11-10 19:45:54 +00001107 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
1108 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001109
1110 // If both types are identical, no conversion is needed.
Steve Naroff7438fdf2007-10-18 18:55:53 +00001111 if (lhs == rhs)
1112 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001113
1114 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1115 // The caller can deal with this (e.g. pointer + int).
1116 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001117 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001118
1119 // At this point, we have two different arithmetic types.
1120
1121 // Handle complex types first (C99 6.3.1.8p1).
1122 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff43001212008-01-15 19:36:10 +00001123 // if we have an integer operand, the result is the complex type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001124 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001125 // convert the rhs to the lhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001126 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001127 return lhs;
Steve Naroff43001212008-01-15 19:36:10 +00001128 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001129 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001130 // convert the lhs to the rhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001131 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001132 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001133 }
Steve Naroff3cf497f2007-08-27 01:27:54 +00001134 // This handles complex/complex, complex/float, or float/complex.
1135 // When both operands are complex, the shorter operand is converted to the
1136 // type of the longer, and that is the type of the result. This corresponds
1137 // to what is done when combining two real floating-point operands.
1138 // The fun begins when size promotion occur across type domains.
1139 // From H&S 6.3.4: When one operand is complex and the other is a real
1140 // floating-point type, the less precise type is converted, within it's
1141 // real or complex domain, to the precision of the other type. For example,
1142 // when combining a "long double" with a "double _Complex", the
1143 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnerd7135b42008-04-06 23:38:49 +00001144 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001145
1146 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff3b565d62007-08-27 21:32:55 +00001147 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1148 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001149 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001150 } else if (result < 0) { // The right side is bigger, convert lhs.
1151 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1152 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001153 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001154 }
1155 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1156 // domains match. This is a requirement for our implementation, C99
1157 // does not require this promotion.
1158 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1159 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001160 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001161 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001162 return rhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001163 } else { // handle "_Complex double, double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001164 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001165 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001166 return lhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001167 }
Chris Lattner4b009652007-07-25 00:24:17 +00001168 }
Steve Naroff3b6157f2007-08-27 21:43:43 +00001169 return lhs; // The domain/size match exactly.
Chris Lattner4b009652007-07-25 00:24:17 +00001170 }
1171 // Now handle "real" floating types (i.e. float, double, long double).
1172 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1173 // if we have an integer operand, the result is the real floating type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001174 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001175 // convert rhs to the lhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001176 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001177 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001178 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001179 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001180 // convert lhs to the rhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001181 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001182 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001183 }
1184 // We have two real floating types, float/complex combos were handled above.
1185 // Convert the smaller operand to the bigger result.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001186 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001187
1188 if (result > 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001189 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001190 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001191 }
Steve Naroff45fc9822007-08-27 15:30:22 +00001192 if (result < 0) { // convert the lhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001193 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff45fc9822007-08-27 15:30:22 +00001194 return rhs;
1195 }
1196 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Chris Lattner4b009652007-07-25 00:24:17 +00001197 }
Steve Naroff43001212008-01-15 19:36:10 +00001198 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1199 // Handle GCC complex int extension.
Steve Naroff43001212008-01-15 19:36:10 +00001200 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman50727042008-02-08 01:19:44 +00001201 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff43001212008-01-15 19:36:10 +00001202
Eli Friedman50727042008-02-08 01:19:44 +00001203 if (lhsComplexInt && rhsComplexInt) {
Chris Lattner51285d82008-04-06 23:55:33 +00001204 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1205 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman94075c02008-02-08 01:24:30 +00001206 // convert the rhs
1207 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1208 return lhs;
Eli Friedman50727042008-02-08 01:19:44 +00001209 }
1210 if (!isCompAssign)
Eli Friedman94075c02008-02-08 01:24:30 +00001211 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman50727042008-02-08 01:19:44 +00001212 return rhs;
1213 } else if (lhsComplexInt && rhs->isIntegerType()) {
1214 // convert the rhs to the lhs complex type.
1215 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1216 return lhs;
1217 } else if (rhsComplexInt && lhs->isIntegerType()) {
1218 // convert the lhs to the rhs complex type.
1219 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1220 return rhs;
1221 }
Steve Naroff43001212008-01-15 19:36:10 +00001222 }
Chris Lattner4b009652007-07-25 00:24:17 +00001223 // Finally, we have two differing integer types.
Eli Friedman0832dbc2008-06-28 06:23:08 +00001224 // The rules for this case are in C99 6.3.1.8
1225 int compare = Context.getIntegerTypeOrder(lhs, rhs);
1226 bool lhsSigned = lhs->isSignedIntegerType(),
1227 rhsSigned = rhs->isSignedIntegerType();
1228 QualType destType;
1229 if (lhsSigned == rhsSigned) {
1230 // Same signedness; use the higher-ranked type
1231 destType = compare >= 0 ? lhs : rhs;
1232 } else if (compare != (lhsSigned ? 1 : -1)) {
1233 // The unsigned type has greater than or equal rank to the
1234 // signed type, so use the unsigned type
1235 destType = lhsSigned ? rhs : lhs;
1236 } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) {
1237 // The two types are different widths; if we are here, that
1238 // means the signed type is larger than the unsigned type, so
1239 // use the signed type.
1240 destType = lhsSigned ? lhs : rhs;
1241 } else {
1242 // The signed type is higher-ranked than the unsigned type,
1243 // but isn't actually any bigger (like unsigned int and long
1244 // on most 32-bit systems). Use the unsigned type corresponding
1245 // to the signed type.
1246 destType = Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
Chris Lattner4b009652007-07-25 00:24:17 +00001247 }
Eli Friedman0832dbc2008-06-28 06:23:08 +00001248 if (!isCompAssign) {
1249 ImpCastExprToType(lhsExpr, destType);
1250 ImpCastExprToType(rhsExpr, destType);
1251 }
1252 return destType;
Chris Lattner4b009652007-07-25 00:24:17 +00001253}
1254
1255// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1256// being closely modeled after the C99 spec:-). The odd characteristic of this
1257// routine is it effectively iqnores the qualifiers on the top level pointee.
1258// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1259// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001260Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001261Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1262 QualType lhptee, rhptee;
1263
1264 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001265 lhptee = lhsType->getAsPointerType()->getPointeeType();
1266 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001267
1268 // make sure we operate on the canonical type
1269 lhptee = lhptee.getCanonicalType();
1270 rhptee = rhptee.getCanonicalType();
1271
Chris Lattner005ed752008-01-04 18:04:52 +00001272 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001273
1274 // C99 6.5.16.1p1: This following citation is common to constraints
1275 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1276 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001277 // FIXME: Handle ASQualType
1278 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1279 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001280 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001281
1282 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1283 // incomplete type and the other is a pointer to a qualified or unqualified
1284 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001285 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001286 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001287 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001288
1289 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001290 assert(rhptee->isFunctionType());
1291 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001292 }
1293
1294 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001295 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001296 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001297
1298 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001299 assert(lhptee->isFunctionType());
1300 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001301 }
1302
Chris Lattner4b009652007-07-25 00:24:17 +00001303 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1304 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001305 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1306 rhptee.getUnqualifiedType()))
1307 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001308 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001309}
1310
1311/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1312/// has code to accommodate several GCC extensions when type checking
1313/// pointers. Here are some objectionable examples that GCC considers warnings:
1314///
1315/// int a, *pint;
1316/// short *pshort;
1317/// struct foo *pfoo;
1318///
1319/// pint = pshort; // warning: assignment from incompatible pointer type
1320/// a = pint; // warning: assignment makes integer from pointer without a cast
1321/// pint = a; // warning: assignment makes pointer from integer without a cast
1322/// pint = pfoo; // warning: assignment from incompatible pointer type
1323///
1324/// As a result, the code for dealing with pointers is more complex than the
1325/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001326///
Chris Lattner005ed752008-01-04 18:04:52 +00001327Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001328Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001329 // Get canonical types. We're not formatting these types, just comparing
1330 // them.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001331 lhsType = lhsType.getCanonicalType().getUnqualifiedType();
1332 rhsType = rhsType.getCanonicalType().getUnqualifiedType();
1333
1334 if (lhsType == rhsType)
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001335 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001336
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001337 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001338 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001339 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001340 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001341 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001342
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001343 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1344 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001345 return Compatible;
Steve Naroff936c4362008-06-03 14:04:54 +00001346 // Relax integer conversions like we do for pointers below.
1347 if (rhsType->isIntegerType())
1348 return IntToPointer;
1349 if (lhsType->isIntegerType())
1350 return PointerToInt;
Chris Lattner1853da22008-01-04 23:18:45 +00001351 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001352 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001353
Chris Lattner390564e2008-04-07 06:49:41 +00001354 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001355 // For ExtVector, allow vector splats; float -> <n x float>
1356 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001357 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1358 return Compatible;
1359 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001360
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001361 // If LHS and RHS are both vectors of integer or both vectors of floating
1362 // point types, and the total vector length is the same, allow the
1363 // conversion. This is a bitcast; no bits are changed but the result type
1364 // is different.
1365 if (getLangOptions().LaxVectorConversions &&
1366 lhsType->isVectorType() && rhsType->isVectorType()) {
1367 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1368 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001369 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begemanec2d1062007-12-30 02:59:45 +00001370 return Compatible;
1371 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001372 }
1373 return Incompatible;
1374 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001375
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001376 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001377 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001378
Chris Lattner390564e2008-04-07 06:49:41 +00001379 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001380 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001381 return IntToPointer;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001382
Chris Lattner390564e2008-04-07 06:49:41 +00001383 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001384 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001385 return Incompatible;
1386 }
1387
Chris Lattner390564e2008-04-07 06:49:41 +00001388 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001389 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001390 if (lhsType == Context.BoolTy)
1391 return Compatible;
1392
1393 if (lhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001394 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001395
Chris Lattner390564e2008-04-07 06:49:41 +00001396 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001397 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001398 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001399 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001400
Chris Lattner1853da22008-01-04 23:18:45 +00001401 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001402 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001403 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001404 }
1405 return Incompatible;
1406}
1407
Chris Lattner005ed752008-01-04 18:04:52 +00001408Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001409Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001410 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1411 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001412 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001413 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001414 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001415 return Compatible;
1416 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001417 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001418 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001419 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001420 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001421 //
1422 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1423 // are better understood.
1424 if (!lhsType->isReferenceType())
1425 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001426
Chris Lattner005ed752008-01-04 18:04:52 +00001427 Sema::AssignConvertType result =
1428 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001429
1430 // C99 6.5.16.1p2: The value of the right operand is converted to the
1431 // type of the assignment expression.
1432 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001433 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001434 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001435}
1436
Chris Lattner005ed752008-01-04 18:04:52 +00001437Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001438Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1439 return CheckAssignmentConstraints(lhsType, rhsType);
1440}
1441
Chris Lattner2c8bff72007-12-12 05:47:28 +00001442QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001443 Diag(loc, diag::err_typecheck_invalid_operands,
1444 lex->getType().getAsString(), rex->getType().getAsString(),
1445 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001446 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001447}
1448
1449inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1450 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001451 // For conversion purposes, we ignore any qualifiers.
1452 // For example, "const float" and "float" are equivalent.
1453 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1454 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001455
1456 // make sure the vector types are identical.
Nate Begeman03105572008-04-04 01:30:25 +00001457 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001458 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001459
Nate Begemanaf6ed502008-04-18 23:10:10 +00001460 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001461 // promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001462 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001463 if (V->getElementType().getCanonicalType().getTypePtr()
1464 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001465 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001466 return lhsType;
1467 }
1468 }
1469
Nate Begemanaf6ed502008-04-18 23:10:10 +00001470 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001471 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001472 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001473 if (V->getElementType().getCanonicalType().getTypePtr()
1474 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001475 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001476 return rhsType;
1477 }
1478 }
1479
Chris Lattner4b009652007-07-25 00:24:17 +00001480 // You cannot convert between vector values of different size.
1481 Diag(loc, diag::err_typecheck_vector_not_convertable,
1482 lex->getType().getAsString(), rex->getType().getAsString(),
1483 lex->getSourceRange(), rex->getSourceRange());
1484 return QualType();
1485}
1486
1487inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001488 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001489{
1490 QualType lhsType = lex->getType(), rhsType = rex->getType();
1491
1492 if (lhsType->isVectorType() || rhsType->isVectorType())
1493 return CheckVectorOperands(loc, lex, rex);
1494
Steve Naroff8f708362007-08-24 19:07:16 +00001495 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001496
Chris Lattner4b009652007-07-25 00:24:17 +00001497 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001498 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001499 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001500}
1501
1502inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001503 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001504{
1505 QualType lhsType = lex->getType(), rhsType = rex->getType();
1506
Steve Naroff8f708362007-08-24 19:07:16 +00001507 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001508
Chris Lattner4b009652007-07-25 00:24:17 +00001509 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001510 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001511 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001512}
1513
1514inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001515 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001516{
1517 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1518 return CheckVectorOperands(loc, lex, rex);
1519
Steve Naroff8f708362007-08-24 19:07:16 +00001520 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001521
Chris Lattner4b009652007-07-25 00:24:17 +00001522 // handle the common case first (both operands are arithmetic).
1523 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001524 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001525
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001526 // Put any potential pointer into PExp
1527 Expr* PExp = lex, *IExp = rex;
1528 if (IExp->getType()->isPointerType())
1529 std::swap(PExp, IExp);
1530
1531 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1532 if (IExp->getType()->isIntegerType()) {
1533 // Check for arithmetic on pointers to incomplete types
1534 if (!PTy->getPointeeType()->isObjectType()) {
1535 if (PTy->getPointeeType()->isVoidType()) {
1536 Diag(loc, diag::ext_gnu_void_ptr,
1537 lex->getSourceRange(), rex->getSourceRange());
1538 } else {
1539 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1540 lex->getType().getAsString(), lex->getSourceRange());
1541 return QualType();
1542 }
1543 }
1544 return PExp->getType();
1545 }
1546 }
1547
Chris Lattner2c8bff72007-12-12 05:47:28 +00001548 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001549}
1550
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001551// C99 6.5.6
1552QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1553 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001554 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1555 return CheckVectorOperands(loc, lex, rex);
1556
Steve Naroff8f708362007-08-24 19:07:16 +00001557 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001558
Chris Lattnerf6da2912007-12-09 21:53:25 +00001559 // Enforce type constraints: C99 6.5.6p3.
1560
1561 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001562 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001563 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001564
1565 // Either ptr - int or ptr - ptr.
1566 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001567 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001568
Chris Lattnerf6da2912007-12-09 21:53:25 +00001569 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001570 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001571 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001572 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001573 Diag(loc, diag::ext_gnu_void_ptr,
1574 lex->getSourceRange(), rex->getSourceRange());
1575 } else {
1576 Diag(loc, diag::err_typecheck_sub_ptr_object,
1577 lex->getType().getAsString(), lex->getSourceRange());
1578 return QualType();
1579 }
1580 }
1581
1582 // The result type of a pointer-int computation is the pointer type.
1583 if (rex->getType()->isIntegerType())
1584 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001585
Chris Lattnerf6da2912007-12-09 21:53:25 +00001586 // Handle pointer-pointer subtractions.
1587 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001588 QualType rpointee = RHSPTy->getPointeeType();
1589
Chris Lattnerf6da2912007-12-09 21:53:25 +00001590 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001591 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001592 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001593 if (rpointee->isVoidType()) {
1594 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001595 Diag(loc, diag::ext_gnu_void_ptr,
1596 lex->getSourceRange(), rex->getSourceRange());
1597 } else {
1598 Diag(loc, diag::err_typecheck_sub_ptr_object,
1599 rex->getType().getAsString(), rex->getSourceRange());
1600 return QualType();
1601 }
1602 }
1603
1604 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001605 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1606 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001607 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1608 lex->getType().getAsString(), rex->getType().getAsString(),
1609 lex->getSourceRange(), rex->getSourceRange());
1610 return QualType();
1611 }
1612
1613 return Context.getPointerDiffType();
1614 }
1615 }
1616
Chris Lattner2c8bff72007-12-12 05:47:28 +00001617 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001618}
1619
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001620// C99 6.5.7
1621QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1622 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001623 // C99 6.5.7p2: Each of the operands shall have integer type.
1624 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1625 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001626
Chris Lattner2c8bff72007-12-12 05:47:28 +00001627 // Shifts don't perform usual arithmetic conversions, they just do integer
1628 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001629 if (!isCompAssign)
1630 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001631 UsualUnaryConversions(rex);
1632
1633 // "The type of the result is that of the promoted left operand."
1634 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001635}
1636
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001637// C99 6.5.8
1638QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1639 bool isRelational) {
Chris Lattner254f3bc2007-08-26 01:18:55 +00001640 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001641 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1642 UsualArithmeticConversions(lex, rex);
1643 else {
1644 UsualUnaryConversions(lex);
1645 UsualUnaryConversions(rex);
1646 }
Chris Lattner4b009652007-07-25 00:24:17 +00001647 QualType lType = lex->getType();
1648 QualType rType = rex->getType();
1649
Ted Kremenek486509e2007-10-29 17:13:39 +00001650 // For non-floating point types, check for self-comparisons of the form
1651 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1652 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001653 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001654 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1655 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001656 if (DRL->getDecl() == DRR->getDecl())
1657 Diag(loc, diag::warn_selfcomparison);
1658 }
1659
Chris Lattner254f3bc2007-08-26 01:18:55 +00001660 if (isRelational) {
1661 if (lType->isRealType() && rType->isRealType())
1662 return Context.IntTy;
1663 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001664 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001665 if (lType->isFloatingType()) {
1666 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001667 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001668 }
1669
Chris Lattner254f3bc2007-08-26 01:18:55 +00001670 if (lType->isArithmeticType() && rType->isArithmeticType())
1671 return Context.IntTy;
1672 }
Chris Lattner4b009652007-07-25 00:24:17 +00001673
Chris Lattner22be8422007-08-26 01:10:14 +00001674 bool LHSIsNull = lex->isNullPointerConstant(Context);
1675 bool RHSIsNull = rex->isNullPointerConstant(Context);
1676
Chris Lattner254f3bc2007-08-26 01:18:55 +00001677 // All of the following pointer related warnings are GCC extensions, except
1678 // when handling null pointer constants. One day, we can consider making them
1679 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001680 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001681 QualType LCanPointeeTy =
1682 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1683 QualType RCanPointeeTy =
1684 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman50727042008-02-08 01:19:44 +00001685
Steve Naroff3b435622007-11-13 14:57:38 +00001686 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001687 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1688 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1689 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001690 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1691 lType.getAsString(), rType.getAsString(),
1692 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001693 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001694 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001695 return Context.IntTy;
1696 }
Steve Naroff936c4362008-06-03 14:04:54 +00001697 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1698 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1699 ImpCastExprToType(rex, lType);
1700 return Context.IntTy;
1701 }
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001702 }
Steve Naroff936c4362008-06-03 14:04:54 +00001703 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1704 rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001705 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001706 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1707 lType.getAsString(), rType.getAsString(),
1708 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001709 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001710 return Context.IntTy;
1711 }
Steve Naroff936c4362008-06-03 14:04:54 +00001712 if (lType->isIntegerType() &&
1713 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattner22be8422007-08-26 01:10:14 +00001714 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001715 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1716 lType.getAsString(), rType.getAsString(),
1717 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001718 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001719 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001720 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001721 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001722}
1723
Chris Lattner4b009652007-07-25 00:24:17 +00001724inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001725 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001726{
1727 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1728 return CheckVectorOperands(loc, lex, rex);
1729
Steve Naroff8f708362007-08-24 19:07:16 +00001730 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001731
1732 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001733 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001734 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001735}
1736
1737inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1738 Expr *&lex, Expr *&rex, SourceLocation loc)
1739{
1740 UsualUnaryConversions(lex);
1741 UsualUnaryConversions(rex);
1742
Eli Friedmanbea3f842008-05-13 20:16:47 +00001743 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Chris Lattner4b009652007-07-25 00:24:17 +00001744 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001745 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001746}
1747
1748inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001749 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001750{
1751 QualType lhsType = lex->getType();
1752 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001753 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1754
1755 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001756 case Expr::MLV_Valid:
1757 break;
1758 case Expr::MLV_ConstQualified:
1759 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1760 return QualType();
1761 case Expr::MLV_ArrayType:
1762 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1763 lhsType.getAsString(), lex->getSourceRange());
1764 return QualType();
1765 case Expr::MLV_NotObjectType:
1766 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1767 lhsType.getAsString(), lex->getSourceRange());
1768 return QualType();
1769 case Expr::MLV_InvalidExpression:
1770 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1771 lex->getSourceRange());
1772 return QualType();
1773 case Expr::MLV_IncompleteType:
1774 case Expr::MLV_IncompleteVoidType:
1775 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1776 lhsType.getAsString(), lex->getSourceRange());
1777 return QualType();
1778 case Expr::MLV_DuplicateVectorComponents:
1779 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1780 lex->getSourceRange());
1781 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001782 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001783
Chris Lattner005ed752008-01-04 18:04:52 +00001784 AssignConvertType ConvTy;
1785 if (compoundType.isNull())
1786 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1787 else
1788 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1789
1790 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1791 rex, "assigning"))
1792 return QualType();
1793
Chris Lattner4b009652007-07-25 00:24:17 +00001794 // C99 6.5.16p3: The type of an assignment expression is the type of the
1795 // left operand unless the left operand has qualified type, in which case
1796 // it is the unqualified version of the type of the left operand.
1797 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1798 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001799 // C++ 5.17p1: the type of the assignment expression is that of its left
1800 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001801 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001802}
1803
1804inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1805 Expr *&lex, Expr *&rex, SourceLocation loc) {
1806 UsualUnaryConversions(rex);
1807 return rex->getType();
1808}
1809
1810/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1811/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1812QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1813 QualType resType = op->getType();
1814 assert(!resType.isNull() && "no type for increment/decrement expression");
1815
Steve Naroffd30e1932007-08-24 17:20:07 +00001816 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001817 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001818 if (pt->getPointeeType()->isVoidType()) {
1819 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1820 } else if (!pt->getPointeeType()->isObjectType()) {
1821 // C99 6.5.2.4p2, 6.5.6p2
Chris Lattner4b009652007-07-25 00:24:17 +00001822 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1823 resType.getAsString(), op->getSourceRange());
1824 return QualType();
1825 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001826 } else if (!resType->isRealType()) {
1827 if (resType->isComplexType())
1828 // C99 does not support ++/-- on complex types.
1829 Diag(OpLoc, diag::ext_integer_increment_complex,
1830 resType.getAsString(), op->getSourceRange());
1831 else {
1832 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1833 resType.getAsString(), op->getSourceRange());
1834 return QualType();
1835 }
Chris Lattner4b009652007-07-25 00:24:17 +00001836 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001837 // At this point, we know we have a real, complex or pointer type.
1838 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001839 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1840 if (mlval != Expr::MLV_Valid) {
1841 // FIXME: emit a more precise diagnostic...
1842 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1843 op->getSourceRange());
1844 return QualType();
1845 }
1846 return resType;
1847}
1848
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001849/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001850/// This routine allows us to typecheck complex/recursive expressions
1851/// where the declaration is needed for type checking. Here are some
1852/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner48d7f382008-04-02 04:24:33 +00001853static ValueDecl *getPrimaryDecl(Expr *E) {
1854 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001855 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001856 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001857 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001858 // Fields cannot be declared with a 'register' storage class.
1859 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00001860 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00001861 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00001862 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001863 case Stmt::ArraySubscriptExprClass: {
1864 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1865
Chris Lattner48d7f382008-04-02 04:24:33 +00001866 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001867 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001868 return 0;
1869 else
1870 return VD;
1871 }
Chris Lattner4b009652007-07-25 00:24:17 +00001872 case Stmt::UnaryOperatorClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001873 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001874 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001875 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001876 case Stmt::ImplicitCastExprClass:
1877 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00001878 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001879 default:
1880 return 0;
1881 }
1882}
1883
1884/// CheckAddressOfOperand - The operand of & must be either a function
1885/// designator or an lvalue designating an object. If it is an lvalue, the
1886/// object cannot be declared with storage class register or be a bit field.
1887/// Note: The usual conversions are *not* applied to the operand of the &
1888/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1889QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001890 if (getLangOptions().C99) {
1891 // Implement C99-only parts of addressof rules.
1892 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1893 if (uOp->getOpcode() == UnaryOperator::Deref)
1894 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1895 // (assuming the deref expression is valid).
1896 return uOp->getSubExpr()->getType();
1897 }
1898 // Technically, there should be a check for array subscript
1899 // expressions here, but the result of one is always an lvalue anyway.
1900 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001901 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00001902 Expr::isLvalueResult lval = op->isLvalue();
1903
1904 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00001905 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1906 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00001907 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1908 op->getSourceRange());
1909 return QualType();
1910 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00001911 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1912 if (MemExpr->getMemberDecl()->isBitField()) {
1913 Diag(OpLoc, diag::err_typecheck_address_of,
1914 std::string("bit-field"), op->getSourceRange());
1915 return QualType();
1916 }
1917 // Check for Apple extension for accessing vector components.
1918 } else if (isa<ArraySubscriptExpr>(op) &&
1919 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1920 Diag(OpLoc, diag::err_typecheck_address_of,
1921 std::string("vector"), op->getSourceRange());
1922 return QualType();
1923 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00001924 // We have an lvalue with a decl. Make sure the decl is not declared
1925 // with the register storage-class specifier.
1926 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1927 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00001928 Diag(OpLoc, diag::err_typecheck_address_of,
1929 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001930 return QualType();
1931 }
1932 } else
1933 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00001934 }
1935 // If the operand has type "type", the result has type "pointer to type".
1936 return Context.getPointerType(op->getType());
1937}
1938
1939QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1940 UsualUnaryConversions(op);
1941 QualType qType = op->getType();
1942
Chris Lattner7931f4a2007-07-31 16:53:04 +00001943 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001944 // Note that per both C89 and C99, this is always legal, even
1945 // if ptype is an incomplete type or void.
1946 // It would be possible to warn about dereferencing a
1947 // void pointer, but it's completely well-defined,
1948 // and such a warning is unlikely to catch any mistakes.
1949 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001950 }
1951 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1952 qType.getAsString(), op->getSourceRange());
1953 return QualType();
1954}
1955
1956static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1957 tok::TokenKind Kind) {
1958 BinaryOperator::Opcode Opc;
1959 switch (Kind) {
1960 default: assert(0 && "Unknown binop!");
1961 case tok::star: Opc = BinaryOperator::Mul; break;
1962 case tok::slash: Opc = BinaryOperator::Div; break;
1963 case tok::percent: Opc = BinaryOperator::Rem; break;
1964 case tok::plus: Opc = BinaryOperator::Add; break;
1965 case tok::minus: Opc = BinaryOperator::Sub; break;
1966 case tok::lessless: Opc = BinaryOperator::Shl; break;
1967 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1968 case tok::lessequal: Opc = BinaryOperator::LE; break;
1969 case tok::less: Opc = BinaryOperator::LT; break;
1970 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1971 case tok::greater: Opc = BinaryOperator::GT; break;
1972 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1973 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1974 case tok::amp: Opc = BinaryOperator::And; break;
1975 case tok::caret: Opc = BinaryOperator::Xor; break;
1976 case tok::pipe: Opc = BinaryOperator::Or; break;
1977 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1978 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1979 case tok::equal: Opc = BinaryOperator::Assign; break;
1980 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1981 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1982 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1983 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1984 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1985 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1986 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1987 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1988 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1989 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1990 case tok::comma: Opc = BinaryOperator::Comma; break;
1991 }
1992 return Opc;
1993}
1994
1995static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1996 tok::TokenKind Kind) {
1997 UnaryOperator::Opcode Opc;
1998 switch (Kind) {
1999 default: assert(0 && "Unknown unary op!");
2000 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
2001 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
2002 case tok::amp: Opc = UnaryOperator::AddrOf; break;
2003 case tok::star: Opc = UnaryOperator::Deref; break;
2004 case tok::plus: Opc = UnaryOperator::Plus; break;
2005 case tok::minus: Opc = UnaryOperator::Minus; break;
2006 case tok::tilde: Opc = UnaryOperator::Not; break;
2007 case tok::exclaim: Opc = UnaryOperator::LNot; break;
2008 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
2009 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
2010 case tok::kw___real: Opc = UnaryOperator::Real; break;
2011 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
2012 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
2013 }
2014 return Opc;
2015}
2016
2017// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002018Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00002019 ExprTy *LHS, ExprTy *RHS) {
2020 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
2021 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
2022
Steve Naroff87d58b42007-09-16 03:34:24 +00002023 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
2024 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00002025
2026 QualType ResultTy; // Result type of the binary operator.
2027 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
2028
2029 switch (Opc) {
2030 default:
2031 assert(0 && "Unknown binary expr!");
2032 case BinaryOperator::Assign:
2033 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
2034 break;
2035 case BinaryOperator::Mul:
2036 case BinaryOperator::Div:
2037 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
2038 break;
2039 case BinaryOperator::Rem:
2040 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
2041 break;
2042 case BinaryOperator::Add:
2043 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2044 break;
2045 case BinaryOperator::Sub:
2046 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2047 break;
2048 case BinaryOperator::Shl:
2049 case BinaryOperator::Shr:
2050 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2051 break;
2052 case BinaryOperator::LE:
2053 case BinaryOperator::LT:
2054 case BinaryOperator::GE:
2055 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002056 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002057 break;
2058 case BinaryOperator::EQ:
2059 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002060 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00002061 break;
2062 case BinaryOperator::And:
2063 case BinaryOperator::Xor:
2064 case BinaryOperator::Or:
2065 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2066 break;
2067 case BinaryOperator::LAnd:
2068 case BinaryOperator::LOr:
2069 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2070 break;
2071 case BinaryOperator::MulAssign:
2072 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002073 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002074 if (!CompTy.isNull())
2075 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2076 break;
2077 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002078 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002079 if (!CompTy.isNull())
2080 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2081 break;
2082 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002083 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002084 if (!CompTy.isNull())
2085 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2086 break;
2087 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002088 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002089 if (!CompTy.isNull())
2090 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2091 break;
2092 case BinaryOperator::ShlAssign:
2093 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002094 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002095 if (!CompTy.isNull())
2096 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2097 break;
2098 case BinaryOperator::AndAssign:
2099 case BinaryOperator::XorAssign:
2100 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002101 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002102 if (!CompTy.isNull())
2103 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2104 break;
2105 case BinaryOperator::Comma:
2106 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2107 break;
2108 }
2109 if (ResultTy.isNull())
2110 return true;
2111 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00002112 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002113 else
Chris Lattnerf420df12007-08-28 18:36:55 +00002114 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002115}
2116
2117// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002118Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00002119 ExprTy *input) {
2120 Expr *Input = (Expr*)input;
2121 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2122 QualType resultType;
2123 switch (Opc) {
2124 default:
2125 assert(0 && "Unimplemented unary expr!");
2126 case UnaryOperator::PreInc:
2127 case UnaryOperator::PreDec:
2128 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2129 break;
2130 case UnaryOperator::AddrOf:
2131 resultType = CheckAddressOfOperand(Input, OpLoc);
2132 break;
2133 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00002134 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00002135 resultType = CheckIndirectionOperand(Input, OpLoc);
2136 break;
2137 case UnaryOperator::Plus:
2138 case UnaryOperator::Minus:
2139 UsualUnaryConversions(Input);
2140 resultType = Input->getType();
2141 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2142 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2143 resultType.getAsString());
2144 break;
2145 case UnaryOperator::Not: // bitwise complement
2146 UsualUnaryConversions(Input);
2147 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00002148 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2149 if (!resultType->isIntegerType()) {
2150 if (resultType->isComplexType())
2151 // C99 does not support '~' for complex conjugation.
2152 Diag(OpLoc, diag::ext_integer_complement_complex,
2153 resultType.getAsString());
2154 else
2155 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2156 resultType.getAsString());
2157 }
Chris Lattner4b009652007-07-25 00:24:17 +00002158 break;
2159 case UnaryOperator::LNot: // logical negation
2160 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
2161 DefaultFunctionArrayConversion(Input);
2162 resultType = Input->getType();
2163 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2164 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2165 resultType.getAsString());
2166 // LNot always has type int. C99 6.5.3.3p5.
2167 resultType = Context.IntTy;
2168 break;
2169 case UnaryOperator::SizeOf:
2170 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2171 break;
2172 case UnaryOperator::AlignOf:
2173 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2174 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002175 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002176 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002177 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002178 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002179 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002180 resultType = Input->getType();
2181 break;
2182 }
2183 if (resultType.isNull())
2184 return true;
2185 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2186}
2187
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002188/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2189Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002190 SourceLocation LabLoc,
2191 IdentifierInfo *LabelII) {
2192 // Look up the record for this label identifier.
2193 LabelStmt *&LabelDecl = LabelMap[LabelII];
2194
2195 // If we haven't seen this label yet, create a forward reference.
2196 if (LabelDecl == 0)
2197 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2198
2199 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002200 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2201 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002202}
2203
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002204Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002205 SourceLocation RPLoc) { // "({..})"
2206 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2207 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2208 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2209
2210 // FIXME: there are a variety of strange constraints to enforce here, for
2211 // example, it is not possible to goto into a stmt expression apparently.
2212 // More semantic analysis is needed.
2213
2214 // FIXME: the last statement in the compount stmt has its value used. We
2215 // should not warn about it being unused.
2216
2217 // If there are sub stmts in the compound stmt, take the type of the last one
2218 // as the type of the stmtexpr.
2219 QualType Ty = Context.VoidTy;
2220
2221 if (!Compound->body_empty())
2222 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2223 Ty = LastExpr->getType();
2224
2225 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2226}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002227
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002228Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002229 SourceLocation TypeLoc,
2230 TypeTy *argty,
2231 OffsetOfComponent *CompPtr,
2232 unsigned NumComponents,
2233 SourceLocation RPLoc) {
2234 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2235 assert(!ArgTy.isNull() && "Missing type argument!");
2236
2237 // We must have at least one component that refers to the type, and the first
2238 // one is known to be a field designator. Verify that the ArgTy represents
2239 // a struct/union/class.
2240 if (!ArgTy->isRecordType())
2241 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2242
2243 // Otherwise, create a compound literal expression as the base, and
2244 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002245 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002246
Chris Lattnerb37522e2007-08-31 21:49:13 +00002247 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2248 // GCC extension, diagnose them.
2249 if (NumComponents != 1)
2250 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2251 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2252
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002253 for (unsigned i = 0; i != NumComponents; ++i) {
2254 const OffsetOfComponent &OC = CompPtr[i];
2255 if (OC.isBrackets) {
2256 // Offset of an array sub-field. TODO: Should we allow vector elements?
2257 const ArrayType *AT = Res->getType()->getAsArrayType();
2258 if (!AT) {
2259 delete Res;
2260 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2261 Res->getType().getAsString());
2262 }
2263
Chris Lattner2af6a802007-08-30 17:59:59 +00002264 // FIXME: C++: Verify that operator[] isn't overloaded.
2265
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002266 // C99 6.5.2.1p1
2267 Expr *Idx = static_cast<Expr*>(OC.U.E);
2268 if (!Idx->getType()->isIntegerType())
2269 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2270 Idx->getSourceRange());
2271
2272 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2273 continue;
2274 }
2275
2276 const RecordType *RC = Res->getType()->getAsRecordType();
2277 if (!RC) {
2278 delete Res;
2279 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2280 Res->getType().getAsString());
2281 }
2282
2283 // Get the decl corresponding to this.
2284 RecordDecl *RD = RC->getDecl();
2285 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2286 if (!MemberDecl)
2287 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2288 OC.U.IdentInfo->getName(),
2289 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002290
2291 // FIXME: C++: Verify that MemberDecl isn't a static field.
2292 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002293 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2294 // matter here.
2295 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002296 }
2297
2298 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2299 BuiltinLoc);
2300}
2301
2302
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002303Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002304 TypeTy *arg1, TypeTy *arg2,
2305 SourceLocation RPLoc) {
2306 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2307 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2308
2309 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2310
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002311 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002312}
2313
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002314Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002315 ExprTy *expr1, ExprTy *expr2,
2316 SourceLocation RPLoc) {
2317 Expr *CondExpr = static_cast<Expr*>(cond);
2318 Expr *LHSExpr = static_cast<Expr*>(expr1);
2319 Expr *RHSExpr = static_cast<Expr*>(expr2);
2320
2321 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2322
2323 // The conditional expression is required to be a constant expression.
2324 llvm::APSInt condEval(32);
2325 SourceLocation ExpLoc;
2326 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2327 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2328 CondExpr->getSourceRange());
2329
2330 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2331 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2332 RHSExpr->getType();
2333 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2334}
2335
Nate Begemanbd881ef2008-01-30 20:50:20 +00002336/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002337/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002338/// The number of arguments has already been validated to match the number of
2339/// arguments in FnType.
2340static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002341 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002342 for (unsigned i = 0; i != NumParams; ++i) {
2343 QualType ExprTy = Args[i]->getType().getCanonicalType();
2344 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2345
2346 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002347 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002348 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002349 return true;
2350}
2351
2352Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2353 SourceLocation *CommaLocs,
2354 SourceLocation BuiltinLoc,
2355 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002356 // __builtin_overload requires at least 2 arguments
2357 if (NumArgs < 2)
2358 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2359 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002360
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002361 // The first argument is required to be a constant expression. It tells us
2362 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002363 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002364 Expr *NParamsExpr = Args[0];
2365 llvm::APSInt constEval(32);
2366 SourceLocation ExpLoc;
2367 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2368 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2369 NParamsExpr->getSourceRange());
2370
2371 // Verify that the number of parameters is > 0
2372 unsigned NumParams = constEval.getZExtValue();
2373 if (NumParams == 0)
2374 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2375 NParamsExpr->getSourceRange());
2376 // Verify that we have at least 1 + NumParams arguments to the builtin.
2377 if ((NumParams + 1) > NumArgs)
2378 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2379 SourceRange(BuiltinLoc, RParenLoc));
2380
2381 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002382 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002383 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002384 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2385 // UsualUnaryConversions will convert the function DeclRefExpr into a
2386 // pointer to function.
2387 Expr *Fn = UsualUnaryConversions(Args[i]);
2388 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002389 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2390 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2391 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2392 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002393
2394 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2395 // parameters, and the number of parameters must match the value passed to
2396 // the builtin.
2397 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002398 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2399 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002400
2401 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002402 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002403 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002404 if (ExprsMatchFnType(Args+1, FnType)) {
2405 if (OE)
2406 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2407 OE->getFn()->getSourceRange());
2408 // Remember our match, and continue processing the remaining arguments
2409 // to catch any errors.
2410 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2411 BuiltinLoc, RParenLoc);
2412 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002413 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002414 // Return the newly created OverloadExpr node, if we succeded in matching
2415 // exactly one of the candidate functions.
2416 if (OE)
2417 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002418
2419 // If we didn't find a matching function Expr in the __builtin_overload list
2420 // the return an error.
2421 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002422 for (unsigned i = 0; i != NumParams; ++i) {
2423 if (i != 0) typeNames += ", ";
2424 typeNames += Args[i+1]->getType().getAsString();
2425 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002426
2427 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2428 SourceRange(BuiltinLoc, RParenLoc));
2429}
2430
Anders Carlsson36760332007-10-15 20:28:48 +00002431Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2432 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002433 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002434 Expr *E = static_cast<Expr*>(expr);
2435 QualType T = QualType::getFromOpaquePtr(type);
2436
2437 InitBuiltinVaListType();
2438
Chris Lattner005ed752008-01-04 18:04:52 +00002439 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2440 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002441 return Diag(E->getLocStart(),
2442 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2443 E->getType().getAsString(),
2444 E->getSourceRange());
2445
2446 // FIXME: Warn if a non-POD type is passed in.
2447
2448 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2449}
2450
Chris Lattner005ed752008-01-04 18:04:52 +00002451bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2452 SourceLocation Loc,
2453 QualType DstType, QualType SrcType,
2454 Expr *SrcExpr, const char *Flavor) {
2455 // Decode the result (notice that AST's are still created for extensions).
2456 bool isInvalid = false;
2457 unsigned DiagKind;
2458 switch (ConvTy) {
2459 default: assert(0 && "Unknown conversion type");
2460 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002461 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002462 DiagKind = diag::ext_typecheck_convert_pointer_int;
2463 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002464 case IntToPointer:
2465 DiagKind = diag::ext_typecheck_convert_int_pointer;
2466 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002467 case IncompatiblePointer:
2468 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2469 break;
2470 case FunctionVoidPointer:
2471 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2472 break;
2473 case CompatiblePointerDiscardsQualifiers:
2474 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2475 break;
2476 case Incompatible:
2477 DiagKind = diag::err_typecheck_convert_incompatible;
2478 isInvalid = true;
2479 break;
2480 }
2481
2482 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2483 SrcExpr->getSourceRange());
2484 return isInvalid;
2485}