blob: e84971062eda6742a0986aa5c7b944104574f61d [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.
84 if (CurMethodDecl) {
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()) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +000092 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface(), *DeclClass;
93 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, DeclClass)) {
94 // FIXME: This should use a new expr for a direct reference, don't turn
95 // this into Self->ivar, just return a BareIVarExpr or something.
96 IdentifierInfo &II = Context.Idents.get("self");
97 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
98 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
99 static_cast<Expr*>(SelfExpr.Val), true, true);
100 }
101 }
Steve Naroffe90d4cc2008-06-05 18:14:25 +0000102 if (SD == 0 && !strcmp(II.getName(), "super")) {
Steve Naroff6f786252008-06-02 23:03:37 +0000103 QualType T = Context.getPointerType(Context.getObjCInterfaceType(
104 CurMethodDecl->getClassInterface()));
105 return new ObjCSuperRefExpr(T, Loc);
106 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000107 }
108
Chris Lattner4b009652007-07-25 00:24:17 +0000109 if (D == 0) {
110 // Otherwise, this could be an implicitly declared function reference (legal
111 // in C90, extension in C99).
112 if (HasTrailingLParen &&
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000113 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattner4b009652007-07-25 00:24:17 +0000114 D = ImplicitlyDefineFunction(Loc, II, S);
115 else {
116 // If this name wasn't predeclared and if this is not a function call,
117 // diagnose the problem.
118 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
119 }
120 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000121
Steve Naroff91b03f72007-08-28 03:03:08 +0000122 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneree4c3bf2008-02-29 16:48:43 +0000123 // check if referencing an identifier with __attribute__((deprecated)).
124 if (VD->getAttr<DeprecatedAttr>())
125 Diag(Loc, diag::warn_deprecated, VD->getName());
126
Steve Naroffcae537d2007-08-28 18:45:29 +0000127 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000128 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000129 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000130 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000131 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000132
Chris Lattner4b009652007-07-25 00:24:17 +0000133 if (isa<TypedefDecl>(D))
134 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000135 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000136 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000137 if (isa<NamespaceDecl>(D))
138 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000139
140 assert(0 && "Invalid decl");
141 abort();
142}
143
Steve Naroff87d58b42007-09-16 03:34:24 +0000144Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000145 tok::TokenKind Kind) {
146 PreDefinedExpr::IdentType IT;
147
148 switch (Kind) {
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000149 default: assert(0 && "Unknown simple primary expr!");
150 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
151 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
152 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000153 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000154
155 // Verify that this is in a function context.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000156 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000157 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000158
Chris Lattner7e637512008-01-12 08:14:25 +0000159 // Pre-defined identifiers are of type char[x], where x is the length of the
160 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000161 unsigned Length;
162 if (CurFunctionDecl)
163 Length = CurFunctionDecl->getIdentifier()->getLength();
164 else
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000165 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000166
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000167 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000168 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000169 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000170 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000171}
172
Steve Naroff87d58b42007-09-16 03:34:24 +0000173Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000174 llvm::SmallString<16> CharBuffer;
175 CharBuffer.resize(Tok.getLength());
176 const char *ThisTokBegin = &CharBuffer[0];
177 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
178
179 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
180 Tok.getLocation(), PP);
181 if (Literal.hadError())
182 return ExprResult(true);
Chris Lattner6b22fb72008-03-01 08:32:21 +0000183
184 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
185
Chris Lattner1aaf71c2008-06-07 22:35:38 +0000186 return new CharacterLiteral(Literal.getValue(), Literal.isWide(), type,
187 Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000188}
189
Steve Naroff87d58b42007-09-16 03:34:24 +0000190Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000191 // fast path for a single digit (which is quite common). A single digit
192 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
193 if (Tok.getLength() == 1) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000194 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000195
Chris Lattner8cd0e932008-03-05 18:54:05 +0000196 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner48d7f382008-04-02 04:24:33 +0000197 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner4b009652007-07-25 00:24:17 +0000198 Context.IntTy,
199 Tok.getLocation()));
200 }
201 llvm::SmallString<512> IntegerBuffer;
202 IntegerBuffer.resize(Tok.getLength());
203 const char *ThisTokBegin = &IntegerBuffer[0];
204
205 // Get the spelling of the token, which eliminates trigraphs, etc.
206 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
207 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
208 Tok.getLocation(), PP);
209 if (Literal.hadError)
210 return ExprResult(true);
211
Chris Lattner1de66eb2007-08-26 03:42:43 +0000212 Expr *Res;
213
214 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000215 QualType Ty;
216 const llvm::fltSemantics *Format;
Chris Lattner858eece2007-09-22 18:29:59 +0000217
218 if (Literal.isFloat) {
219 Ty = Context.FloatTy;
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000220 Format = Context.Target.getFloatFormat();
221 } else if (!Literal.isLong) {
Chris Lattner858eece2007-09-22 18:29:59 +0000222 Ty = Context.DoubleTy;
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000223 Format = Context.Target.getDoubleFormat();
224 } else {
225 Ty = Context.LongDoubleTy;
226 Format = Context.Target.getLongDoubleFormat();
Chris Lattner858eece2007-09-22 18:29:59 +0000227 }
228
Ted Kremenekddedbe22007-11-29 00:56:49 +0000229 // isExact will be set by GetFloatValue().
230 bool isExact = false;
231
232 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
233 Ty, Tok.getLocation());
234
Chris Lattner1de66eb2007-08-26 03:42:43 +0000235 } else if (!Literal.isIntegerLiteral()) {
236 return ExprResult(true);
237 } else {
Chris Lattner48d7f382008-04-02 04:24:33 +0000238 QualType Ty;
Chris Lattner4b009652007-07-25 00:24:17 +0000239
Neil Booth7421e9c2007-08-29 22:00:19 +0000240 // long long is a C99 feature.
241 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000242 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000243 Diag(Tok.getLocation(), diag::ext_longlong);
244
Chris Lattner4b009652007-07-25 00:24:17 +0000245 // Get the value in the widest-possible width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000246 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000247
248 if (Literal.GetIntegerValue(ResultVal)) {
249 // If this value didn't fit into uintmax_t, warn and force to ull.
250 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner48d7f382008-04-02 04:24:33 +0000251 Ty = Context.UnsignedLongLongTy;
252 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner8cd0e932008-03-05 18:54:05 +0000253 "long long is not intmax_t?");
Chris Lattner4b009652007-07-25 00:24:17 +0000254 } else {
255 // If this value fits into a ULL, try to figure out what else it fits into
256 // according to the rules of C99 6.4.4.1p5.
257
258 // Octal, Hexadecimal, and integers with a U suffix are allowed to
259 // be an unsigned int.
260 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
261
262 // Check from smallest to largest, picking the smallest type we can.
Chris Lattnere4068872008-05-09 05:59:00 +0000263 unsigned Width = 0;
Chris Lattner98540b62007-08-23 21:58:08 +0000264 if (!Literal.isLong && !Literal.isLongLong) {
265 // Are int/unsigned possibilities?
Chris Lattnere4068872008-05-09 05:59:00 +0000266 unsigned IntSize = Context.Target.getIntWidth();
267
Chris Lattner4b009652007-07-25 00:24:17 +0000268 // Does it fit in a unsigned int?
269 if (ResultVal.isIntN(IntSize)) {
270 // Does it fit in a signed int?
271 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000272 Ty = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000273 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000274 Ty = Context.UnsignedIntTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000275 Width = IntSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000276 }
Chris Lattner4b009652007-07-25 00:24:17 +0000277 }
278
279 // Are long/unsigned long possibilities?
Chris Lattner48d7f382008-04-02 04:24:33 +0000280 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattnere4068872008-05-09 05:59:00 +0000281 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000282
283 // Does it fit in a unsigned long?
284 if (ResultVal.isIntN(LongSize)) {
285 // Does it fit in a signed long?
286 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000287 Ty = Context.LongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000288 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000289 Ty = Context.UnsignedLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000290 Width = LongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000291 }
Chris Lattner4b009652007-07-25 00:24:17 +0000292 }
293
294 // Finally, check long long if needed.
Chris Lattner48d7f382008-04-02 04:24:33 +0000295 if (Ty.isNull()) {
Chris Lattnere4068872008-05-09 05:59:00 +0000296 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000297
298 // Does it fit in a unsigned long long?
299 if (ResultVal.isIntN(LongLongSize)) {
300 // Does it fit in a signed long long?
301 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000302 Ty = Context.LongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000303 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000304 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000305 Width = LongLongSize;
Chris Lattner4b009652007-07-25 00:24:17 +0000306 }
307 }
308
309 // If we still couldn't decide a type, we probably have something that
310 // does not fit in a signed long long, but has no U suffix.
Chris Lattner48d7f382008-04-02 04:24:33 +0000311 if (Ty.isNull()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000312 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner48d7f382008-04-02 04:24:33 +0000313 Ty = Context.UnsignedLongLongTy;
Chris Lattnere4068872008-05-09 05:59:00 +0000314 Width = Context.Target.getLongLongWidth();
Chris Lattner4b009652007-07-25 00:24:17 +0000315 }
Chris Lattnere4068872008-05-09 05:59:00 +0000316
317 if (ResultVal.getBitWidth() != Width)
318 ResultVal.trunc(Width);
Chris Lattner4b009652007-07-25 00:24:17 +0000319 }
320
Chris Lattner48d7f382008-04-02 04:24:33 +0000321 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000322 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000323
324 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
325 if (Literal.isImaginary)
326 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
327
328 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000329}
330
Steve Naroff87d58b42007-09-16 03:34:24 +0000331Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000332 ExprTy *Val) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000333 Expr *E = (Expr *)Val;
334 assert((E != 0) && "ActOnParenExpr() missing expr");
335 return new ParenExpr(L, R, E);
Chris Lattner4b009652007-07-25 00:24:17 +0000336}
337
338/// The UsualUnaryConversions() function is *not* called by this routine.
339/// See C99 6.3.2.1p[2-4] for more details.
340QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
341 SourceLocation OpLoc, bool isSizeof) {
342 // C99 6.5.3.4p1:
343 if (isa<FunctionType>(exprType) && isSizeof)
344 // alignof(function) is allowed.
345 Diag(OpLoc, diag::ext_sizeof_function_type);
346 else if (exprType->isVoidType())
347 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
348 else if (exprType->isIncompleteType()) {
349 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
350 diag::err_alignof_incomplete_type,
351 exprType.getAsString());
352 return QualType(); // error
353 }
354 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
355 return Context.getSizeType();
356}
357
358Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000359ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000360 SourceLocation LPLoc, TypeTy *Ty,
361 SourceLocation RPLoc) {
362 // If error parsing type, ignore.
363 if (Ty == 0) return true;
364
365 // Verify that this is a valid expression.
366 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
367
368 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
369
370 if (resultType.isNull())
371 return true;
372 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
373}
374
Chris Lattner5110ad52007-08-24 21:41:10 +0000375QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000376 DefaultFunctionArrayConversion(V);
377
Chris Lattnera16e42d2007-08-26 05:39:26 +0000378 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000379 if (const ComplexType *CT = V->getType()->getAsComplexType())
380 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000381
382 // Otherwise they pass through real integer and floating point types here.
383 if (V->getType()->isArithmeticType())
384 return V->getType();
385
386 // Reject anything else.
387 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
388 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000389}
390
391
Chris Lattner4b009652007-07-25 00:24:17 +0000392
Steve Naroff87d58b42007-09-16 03:34:24 +0000393Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000394 tok::TokenKind Kind,
395 ExprTy *Input) {
396 UnaryOperator::Opcode Opc;
397 switch (Kind) {
398 default: assert(0 && "Unknown unary op!");
399 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
400 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
401 }
402 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
403 if (result.isNull())
404 return true;
405 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
406}
407
408Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000409ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000410 ExprTy *Idx, SourceLocation RLoc) {
411 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
412
413 // Perform default conversions.
414 DefaultFunctionArrayConversion(LHSExp);
415 DefaultFunctionArrayConversion(RHSExp);
416
417 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
418
419 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000420 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000421 // in the subscript position. As a result, we need to derive the array base
422 // and index from the expression types.
423 Expr *BaseExpr, *IndexExpr;
424 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000425 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000426 BaseExpr = LHSExp;
427 IndexExpr = RHSExp;
428 // FIXME: need to deal with const...
429 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000430 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000431 // Handle the uncommon case of "123[Ptr]".
432 BaseExpr = RHSExp;
433 IndexExpr = LHSExp;
434 // FIXME: need to deal with const...
435 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000436 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
437 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000438 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000439
440 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000441 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
442 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000443 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000444 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000445 // FIXME: need to deal with const...
446 ResultType = VTy->getElementType();
447 } else {
448 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
449 RHSExp->getSourceRange());
450 }
451 // C99 6.5.2.1p1
452 if (!IndexExpr->getType()->isIntegerType())
453 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
454 IndexExpr->getSourceRange());
455
456 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
457 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000458 // void (*)(int)) and pointers to incomplete types. Functions are not
459 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000460 if (!ResultType->isObjectType())
461 return Diag(BaseExpr->getLocStart(),
462 diag::err_typecheck_subscript_not_object,
463 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
464
465 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
466}
467
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000468QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000469CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000470 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000471 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanc8e51f82008-05-09 06:41:27 +0000472
473 // This flag determines whether or not the component is to be treated as a
474 // special name, or a regular GLSL-style component access.
475 bool SpecialComponent = false;
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000476
477 // The vector accessor can't exceed the number of elements.
478 const char *compStr = CompName.getName();
479 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000480 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000481 baseType.getAsString(), SourceRange(CompLoc));
482 return QualType();
483 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000484
485 // Check that we've found one of the special components, or that the component
486 // names must come from the same set.
487 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
488 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
489 SpecialComponent = true;
490 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner9096b792007-08-02 22:33:49 +0000491 do
492 compStr++;
493 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
494 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
495 do
496 compStr++;
497 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
498 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
499 do
500 compStr++;
501 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
502 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000503
Nate Begemanc8e51f82008-05-09 06:41:27 +0000504 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000505 // We didn't get to the end of the string. This means the component names
506 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000507 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000508 std::string(compStr,compStr+1), SourceRange(CompLoc));
509 return QualType();
510 }
511 // Each component accessor can't exceed the vector type.
512 compStr = CompName.getName();
513 while (*compStr) {
514 if (vecType->isAccessorWithinNumElements(*compStr))
515 compStr++;
516 else
517 break;
518 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000519 if (!SpecialComponent && *compStr) {
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000520 // We didn't get to the end of the string. This means a component accessor
521 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000522 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000523 baseType.getAsString(), SourceRange(CompLoc));
524 return QualType();
525 }
Nate Begemanc8e51f82008-05-09 06:41:27 +0000526
527 // If we have a special component name, verify that the current vector length
528 // is an even number, since all special component names return exactly half
529 // the elements.
530 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
531 return QualType();
532 }
533
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000534 // The component accessor looks fine - now we need to compute the actual type.
535 // The vector type is implied by the component accessor. For example,
536 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanc8e51f82008-05-09 06:41:27 +0000537 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
538 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
539 : strlen(CompName.getName());
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000540 if (CompSize == 1)
541 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000542
Nate Begemanaf6ed502008-04-18 23:10:10 +0000543 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000544 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000545 // diagostics look bad. We want extended vector types to appear built-in.
546 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
547 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
548 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000549 }
550 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000551}
552
Chris Lattner4b009652007-07-25 00:24:17 +0000553Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000554ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000555 tok::TokenKind OpKind, SourceLocation MemberLoc,
556 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000557 Expr *BaseExpr = static_cast<Expr *>(Base);
558 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000559
560 // Perform default conversions.
561 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000562
Steve Naroff2cb66382007-07-26 03:11:44 +0000563 QualType BaseType = BaseExpr->getType();
564 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000565
Chris Lattner4b009652007-07-25 00:24:17 +0000566 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000567 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000568 BaseType = PT->getPointeeType();
569 else
570 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
571 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000572 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000573 // The base type is either a record or an ExtVectorType.
Chris Lattnere35a1042007-07-31 19:29:30 +0000574 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000575 RecordDecl *RDecl = RTy->getDecl();
576 if (RTy->isIncompleteType())
577 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
578 BaseExpr->getSourceRange());
579 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000580 FieldDecl *MemberDecl = RDecl->getMember(&Member);
581 if (!MemberDecl)
Steve Naroff2cb66382007-07-26 03:11:44 +0000582 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
583 SourceRange(MemberLoc));
Eli Friedman76b49832008-02-06 22:48:16 +0000584
585 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000586 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000587 QualType MemberType = MemberDecl->getType();
588 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000589 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000590 MemberType = MemberType.getQualifiedType(combinedQualifiers);
591
592 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
593 MemberLoc, MemberType);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000594 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff89345522007-08-03 22:40:33 +0000595 // Component access limited to variables (reject vec4.rg.g).
Nate Begemanc8e51f82008-05-09 06:41:27 +0000596 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
597 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000598 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000599 SourceRange(MemberLoc));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000600 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000601 if (ret.isNull())
602 return true;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000603 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000604 } else if (BaseType->isObjCInterfaceType()) {
605 ObjCInterfaceDecl *IFace;
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000606 QualType CanonType = BaseType.getCanonicalType();
607 if (isa<ObjCInterfaceType>(CanonType))
608 IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000609 else
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000610 IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
Ted Kremenek42730c52008-01-07 19:49:32 +0000611 ObjCInterfaceDecl *clsDeclared;
612 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000613 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
614 OpKind==tok::arrow);
Steve Naroff05391d22008-05-30 00:40:33 +0000615 } else if (isObjCObjectPointerType(BaseType)) {
616 PointerType *pointerType = static_cast<PointerType*>(BaseType.getTypePtr());
617 BaseType = pointerType->getPointeeType();
618 ObjCInterfaceDecl *IFace;
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000619 QualType CanonType = BaseType.getCanonicalType();
620 if (isa<ObjCInterfaceType>(CanonType))
621 IFace = dyn_cast<ObjCInterfaceType>(CanonType)->getDecl();
Steve Naroff05391d22008-05-30 00:40:33 +0000622 else
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000623 IFace = dyn_cast<ObjCQualifiedInterfaceType>(CanonType)->getDecl();
Steve Naroff05391d22008-05-30 00:40:33 +0000624 ObjCInterfaceDecl *clsDeclared;
625 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
626 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
627 OpKind==tok::arrow);
628 // Check for properties.
629 if (OpKind==tok::period) {
630 // Before we look for explicit property declarations, we check for
631 // nullary methods (which allow '.' notation).
632 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
633 ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel);
634 if (MD)
635 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
636 MemberLoc, BaseExpr);
637 // FIXME: Need to deal with setter methods that take 1 argument. E.g.:
638 // @interface NSBundle : NSObject {}
639 // - (NSString *)bundlePath;
640 // - (void)setBundlePath:(NSString *)x;
641 // @end
642 // void someMethod() { frameworkBundle.bundlePath = 0; }
643 //
Steve Naroff858813d2008-06-03 21:56:14 +0000644 ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(&Member);
Steve Naroffd1f0eb42008-06-05 13:55:23 +0000645
646 if (!PD) { // Lastly, check protocols on qualified interfaces.
647 if (ObjCQualifiedInterfaceType *QIT =
648 dyn_cast<ObjCQualifiedInterfaceType>(CanonType)) {
649 for (unsigned i = 0; i < QIT->getNumProtocols(); i++)
650 if ((PD = QIT->getProtocols(i)->FindPropertyDeclaration(&Member)))
651 break;
652 }
653 }
Steve Naroff858813d2008-06-03 21:56:14 +0000654 if (PD)
655 return new ObjCPropertyRefExpr(PD, PD->getType(), MemberLoc, BaseExpr);
Steve Naroff05391d22008-05-30 00:40:33 +0000656 }
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000657 }
658 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
659 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000660}
661
Steve Naroff87d58b42007-09-16 03:34:24 +0000662/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000663/// This provides the location of the left/right parens and a list of comma
664/// locations.
665Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000666ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000667 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000668 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
669 Expr *Fn = static_cast<Expr *>(fn);
670 Expr **Args = reinterpret_cast<Expr**>(args);
671 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +0000672 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000673
674 // Promote the function operand.
675 UsualUnaryConversions(Fn);
676
677 // If we're directly calling a function, get the declaration for
678 // that function.
679 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
680 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
681 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
682
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000683 // Make the call expr early, before semantic checks. This guarantees cleanup
684 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +0000685 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000686 Context.BoolTy, RParenLoc));
687
Chris Lattner4b009652007-07-25 00:24:17 +0000688 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
689 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000690 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000691 if (PT == 0)
692 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
693 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000694 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
695 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000696 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
697 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000698
699 // We know the result type of the call, set it.
700 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000701
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000702 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000703 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
704 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000705 unsigned NumArgsInProto = Proto->getNumArgs();
706 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000707
Chris Lattner3e254fb2008-04-08 04:40:51 +0000708 // If too few arguments are available (and we don't have default
709 // arguments for the remaining parameters), don't make the call.
710 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +0000711 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000712 // Use default arguments for missing arguments
713 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +0000714 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000715 } else
716 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
717 Fn->getSourceRange());
718 }
719
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000720 // If too many are passed and not variadic, error on the extras and drop
721 // them.
722 if (NumArgs > NumArgsInProto) {
723 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000724 Diag(Args[NumArgsInProto]->getLocStart(),
725 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
726 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000727 Args[NumArgs-1]->getLocEnd()));
728 // This deletes the extra arguments.
729 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000730 }
731 NumArgsToCheck = NumArgsInProto;
732 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000733
Chris Lattner4b009652007-07-25 00:24:17 +0000734 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000735 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +0000736 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000737
738 Expr *Arg;
739 if (i < NumArgs)
740 Arg = Args[i];
741 else
742 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +0000743 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000744
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000745 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +0000746 AssignConvertType ConvTy =
747 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000748 TheCall->setArg(i, Arg);
749
Chris Lattner005ed752008-01-04 18:04:52 +0000750 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
751 ArgType, Arg, "passing"))
752 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000753 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000754
755 // If this is a variadic call, handle args passed through "...".
756 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +0000757 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000758 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
759 Expr *Arg = Args[i];
760 DefaultArgumentPromotion(Arg);
761 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000762 }
Steve Naroffdb65e052007-08-28 23:30:39 +0000763 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000764 } else {
765 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
766
Steve Naroffdb65e052007-08-28 23:30:39 +0000767 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000768 for (unsigned i = 0; i != NumArgs; i++) {
769 Expr *Arg = Args[i];
770 DefaultArgumentPromotion(Arg);
771 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000772 }
Chris Lattner4b009652007-07-25 00:24:17 +0000773 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000774
Chris Lattner2e64c072007-08-10 20:18:51 +0000775 // Do special checking on direct calls to functions.
Eli Friedmand0e9d092008-05-14 19:38:39 +0000776 if (FDecl)
777 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner2e64c072007-08-10 20:18:51 +0000778
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000779 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +0000780}
781
782Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000783ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000784 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000785 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000786 QualType literalType = QualType::getFromOpaquePtr(Ty);
787 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +0000788 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000789 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +0000790
Eli Friedman8c2173d2008-05-20 05:22:08 +0000791 if (literalType->isArrayType()) {
792 if (literalType->getAsVariableArrayType())
793 return Diag(LParenLoc,
794 diag::err_variable_object_no_init,
795 SourceRange(LParenLoc,
796 literalExpr->getSourceRange().getEnd()));
797 } else if (literalType->isIncompleteType()) {
798 return Diag(LParenLoc,
799 diag::err_typecheck_decl_incomplete_type,
800 literalType.getAsString(),
801 SourceRange(LParenLoc,
802 literalExpr->getSourceRange().getEnd()));
803 }
804
Steve Narofff0b23542008-01-10 22:15:12 +0000805 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +0000806 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +0000807
808 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
809 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +0000810 if (CheckForConstantInitializer(literalExpr, literalType))
811 return true;
812 }
Steve Naroffbe37fc02008-01-14 18:19:28 +0000813 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000814}
815
816Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000817ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +0000818 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000819 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000820
Steve Naroff0acc9c92007-09-15 18:49:24 +0000821 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +0000822 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +0000823
Chris Lattner48d7f382008-04-02 04:24:33 +0000824 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
825 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
826 return E;
Chris Lattner4b009652007-07-25 00:24:17 +0000827}
828
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000829bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000830 assert(VectorTy->isVectorType() && "Not a vector type!");
831
832 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000833 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000834 return Diag(R.getBegin(),
835 Ty->isVectorType() ?
836 diag::err_invalid_conversion_between_vectors :
837 diag::err_invalid_conversion_between_vector_and_integer,
838 VectorTy.getAsString().c_str(),
839 Ty.getAsString().c_str(), R);
840 } else
841 return Diag(R.getBegin(),
842 diag::err_invalid_conversion_between_vector_and_scalar,
843 VectorTy.getAsString().c_str(),
844 Ty.getAsString().c_str(), R);
845
846 return false;
847}
848
Chris Lattner4b009652007-07-25 00:24:17 +0000849Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000850ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000851 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000852 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000853
854 Expr *castExpr = static_cast<Expr*>(Op);
855 QualType castType = QualType::getFromOpaquePtr(Ty);
856
Steve Naroff68adb482007-08-31 00:32:44 +0000857 UsualUnaryConversions(castExpr);
858
Chris Lattner4b009652007-07-25 00:24:17 +0000859 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
860 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +0000861 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff5ad85292008-06-03 12:56:35 +0000862 if (!castType->isScalarType() && !castType->isVectorType()) {
863 // GCC struct/union extension.
864 if (castType == castExpr->getType() &&
Steve Naroff7f1c5b52008-06-03 13:21:30 +0000865 castType->isStructureType() || castType->isUnionType()) {
866 Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar,
867 SourceRange(LParenLoc, RParenLoc));
868 return new CastExpr(castType, castExpr, LParenLoc);
869 } else
Steve Naroff5ad85292008-06-03 12:56:35 +0000870 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
871 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
872 }
Steve Narofff459ee52008-01-24 22:55:05 +0000873 if (!castExpr->getType()->isScalarType() &&
874 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000875 return Diag(castExpr->getLocStart(),
876 diag::err_typecheck_expect_scalar_operand,
877 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000878
879 if (castExpr->getType()->isVectorType()) {
880 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
881 castExpr->getType(), castType))
882 return true;
883 } else if (castType->isVectorType()) {
884 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
885 castType, castExpr->getType()))
886 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +0000887 }
Chris Lattner4b009652007-07-25 00:24:17 +0000888 }
889 return new CastExpr(castType, castExpr, LParenLoc);
890}
891
Chris Lattner98a425c2007-11-26 01:40:58 +0000892/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
893/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +0000894inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
895 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
896 UsualUnaryConversions(cond);
897 UsualUnaryConversions(lex);
898 UsualUnaryConversions(rex);
899 QualType condT = cond->getType();
900 QualType lexT = lex->getType();
901 QualType rexT = rex->getType();
902
903 // first, check the condition.
904 if (!condT->isScalarType()) { // C99 6.5.15p2
905 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
906 condT.getAsString());
907 return QualType();
908 }
Chris Lattner992ae932008-01-06 22:42:25 +0000909
910 // Now check the two expressions.
911
912 // If both operands have arithmetic type, do the usual arithmetic conversions
913 // to find a common type: C99 6.5.15p3,5.
914 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000915 UsualArithmeticConversions(lex, rex);
916 return lex->getType();
917 }
Chris Lattner992ae932008-01-06 22:42:25 +0000918
919 // If both operands are the same structure or union type, the result is that
920 // type.
Chris Lattner71225142007-07-31 21:27:01 +0000921 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +0000922 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +0000923 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +0000924 // "If both the operands have structure or union type, the result has
925 // that type." This implies that CV qualifiers are dropped.
926 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000927 }
Chris Lattner992ae932008-01-06 22:42:25 +0000928
929 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroff95cb3892008-05-12 21:44:38 +0000930 // The following || allows only one side to be void (a GCC-ism).
931 if (lexT->isVoidType() || rexT->isVoidType()) {
Eli Friedmanf025aac2008-06-04 19:47:51 +0000932 if (!lexT->isVoidType())
Steve Naroff95cb3892008-05-12 21:44:38 +0000933 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
934 rex->getSourceRange());
935 if (!rexT->isVoidType())
936 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
Nuno Lopes4ba41fd2008-06-04 19:14:12 +0000937 lex->getSourceRange());
Eli Friedmanf025aac2008-06-04 19:47:51 +0000938 ImpCastExprToType(lex, Context.VoidTy);
939 ImpCastExprToType(rex, Context.VoidTy);
940 return Context.VoidTy;
Steve Naroff95cb3892008-05-12 21:44:38 +0000941 }
Steve Naroff12ebf272008-01-08 01:11:38 +0000942 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
943 // the type of the other operand."
944 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000945 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000946 return lexT;
947 }
948 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000949 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000950 return rexT;
951 }
Chris Lattner0ac51632008-01-06 22:50:31 +0000952 // Handle the case where both operands are pointers before we handle null
953 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +0000954 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
955 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
956 // get the "pointed to" types
957 QualType lhptee = LHSPT->getPointeeType();
958 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000959
Chris Lattner71225142007-07-31 21:27:01 +0000960 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
961 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000962 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000963 // Figure out necessary qualifiers (C99 6.5.15p6)
964 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000965 QualType destType = Context.getPointerType(destPointee);
966 ImpCastExprToType(lex, destType); // add qualifiers if necessary
967 ImpCastExprToType(rex, destType); // promote to void*
968 return destType;
969 }
Chris Lattner9db553e2008-04-02 06:59:01 +0000970 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000971 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000972 QualType destType = Context.getPointerType(destPointee);
973 ImpCastExprToType(lex, destType); // add qualifiers if necessary
974 ImpCastExprToType(rex, destType); // promote to void*
975 return destType;
976 }
Chris Lattner4b009652007-07-25 00:24:17 +0000977
Steve Naroff85f0dc52007-10-15 20:41:53 +0000978 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
979 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +0000980 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +0000981 lexT.getAsString(), rexT.getAsString(),
982 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +0000983 // In this situation, we assume void* type. No especially good
984 // reason, but this is what gcc does, and we do have to pick
985 // to get a consistent AST.
986 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
987 ImpCastExprToType(lex, voidPtrTy);
988 ImpCastExprToType(rex, voidPtrTy);
989 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +0000990 }
991 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000992 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
993 // differently qualified versions of compatible types, the result type is
994 // a pointer to an appropriately qualified version of the *composite*
995 // type.
Eli Friedmane38150e2008-05-16 20:37:07 +0000996 // FIXME: Need to calculate the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +0000997 // FIXME: Need to add qualifiers
Eli Friedmane38150e2008-05-16 20:37:07 +0000998 QualType compositeType = lexT;
999 ImpCastExprToType(lex, compositeType);
1000 ImpCastExprToType(rex, compositeType);
1001 return compositeType;
Chris Lattner4b009652007-07-25 00:24:17 +00001002 }
Chris Lattner4b009652007-07-25 00:24:17 +00001003 }
Steve Naroff605896f2008-05-31 22:33:45 +00001004 // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
1005 // evaluates to "struct objc_object *" (and is handled above when comparing
1006 // id with statically typed objects). FIXME: Do we need an ImpCastExprToType?
1007 if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) {
1008 if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true))
1009 return Context.getObjCIdType();
1010 }
Chris Lattner992ae932008-01-06 22:42:25 +00001011 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +00001012 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
1013 lexT.getAsString(), rexT.getAsString(),
1014 lex->getSourceRange(), rex->getSourceRange());
1015 return QualType();
1016}
1017
Steve Naroff87d58b42007-09-16 03:34:24 +00001018/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +00001019/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +00001020Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00001021 SourceLocation ColonLoc,
1022 ExprTy *Cond, ExprTy *LHS,
1023 ExprTy *RHS) {
1024 Expr *CondExpr = (Expr *) Cond;
1025 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +00001026
1027 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1028 // was the condition.
1029 bool isLHSNull = LHSExpr == 0;
1030 if (isLHSNull)
1031 LHSExpr = CondExpr;
1032
Chris Lattner4b009652007-07-25 00:24:17 +00001033 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1034 RHSExpr, QuestionLoc);
1035 if (result.isNull())
1036 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +00001037 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1038 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +00001039}
1040
Steve Naroffdb65e052007-08-28 23:30:39 +00001041/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffbbaed752008-01-29 02:42:22 +00001042/// do not have a prototype. Arguments that have type float are promoted to
1043/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001044void Sema::DefaultArgumentPromotion(Expr *&Expr) {
1045 QualType Ty = Expr->getType();
1046 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffdb65e052007-08-28 23:30:39 +00001047
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001048 if (Ty == Context.FloatTy)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001049 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffbbaed752008-01-29 02:42:22 +00001050 else
1051 UsualUnaryConversions(Expr);
Steve Naroffdb65e052007-08-28 23:30:39 +00001052}
1053
Chris Lattner4b009652007-07-25 00:24:17 +00001054/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattner48d7f382008-04-02 04:24:33 +00001055void Sema::DefaultFunctionArrayConversion(Expr *&E) {
1056 QualType Ty = E->getType();
1057 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001058
Chris Lattner48d7f382008-04-02 04:24:33 +00001059 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +00001060 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattner48d7f382008-04-02 04:24:33 +00001061 Ty = E->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001062 }
Chris Lattner48d7f382008-04-02 04:24:33 +00001063 if (Ty->isFunctionType())
1064 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattner19eb97e2008-04-02 05:18:44 +00001065 else if (Ty->isArrayType())
1066 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Chris Lattner4b009652007-07-25 00:24:17 +00001067}
1068
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001069/// UsualUnaryConversions - Performs various conversions that are common to most
Chris Lattner4b009652007-07-25 00:24:17 +00001070/// operators (C99 6.3). The conversions of array and function types are
1071/// sometimes surpressed. For example, the array->pointer conversion doesn't
1072/// apply if the array is an argument to the sizeof or address (&) operators.
1073/// In these instances, this routine should *not* be called.
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001074Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
1075 QualType Ty = Expr->getType();
1076 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +00001077
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001078 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +00001079 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001080 Ty = Expr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001081 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001082 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnere992d6c2008-01-16 19:17:22 +00001083 ImpCastExprToType(Expr, Context.IntTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001084 else
Chris Lattner83bd5eb2007-12-28 05:29:59 +00001085 DefaultFunctionArrayConversion(Expr);
1086
1087 return Expr;
Chris Lattner4b009652007-07-25 00:24:17 +00001088}
1089
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001090/// UsualArithmeticConversions - Performs various conversions that are common to
Chris Lattner4b009652007-07-25 00:24:17 +00001091/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
1092/// routine returns the first non-arithmetic type found. The client is
1093/// responsible for emitting appropriate error diagnostics.
Chris Lattner48d7f382008-04-02 04:24:33 +00001094/// FIXME: verify the conversion rules for "complex int" are consistent with
1095/// GCC.
Steve Naroff8f708362007-08-24 19:07:16 +00001096QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
1097 bool isCompAssign) {
Steve Naroffb2f9f552007-08-25 19:54:59 +00001098 if (!isCompAssign) {
1099 UsualUnaryConversions(lhsExpr);
1100 UsualUnaryConversions(rhsExpr);
1101 }
Steve Naroff7438fdf2007-10-18 18:55:53 +00001102 // For conversion purposes, we ignore any qualifiers.
1103 // For example, "const float" and "float" are equivalent.
Steve Naroff1ddb6f52007-11-10 19:45:54 +00001104 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
1105 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001106
1107 // If both types are identical, no conversion is needed.
Steve Naroff7438fdf2007-10-18 18:55:53 +00001108 if (lhs == rhs)
1109 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001110
1111 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1112 // The caller can deal with this (e.g. pointer + int).
1113 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001114 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001115
1116 // At this point, we have two different arithmetic types.
1117
1118 // Handle complex types first (C99 6.3.1.8p1).
1119 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff43001212008-01-15 19:36:10 +00001120 // if we have an integer operand, the result is the complex type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001121 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001122 // convert the rhs to the lhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001123 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001124 return lhs;
Steve Naroff43001212008-01-15 19:36:10 +00001125 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001126 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001127 // convert the lhs to the rhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001128 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001129 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001130 }
Steve Naroff3cf497f2007-08-27 01:27:54 +00001131 // This handles complex/complex, complex/float, or float/complex.
1132 // When both operands are complex, the shorter operand is converted to the
1133 // type of the longer, and that is the type of the result. This corresponds
1134 // to what is done when combining two real floating-point operands.
1135 // The fun begins when size promotion occur across type domains.
1136 // From H&S 6.3.4: When one operand is complex and the other is a real
1137 // floating-point type, the less precise type is converted, within it's
1138 // real or complex domain, to the precision of the other type. For example,
1139 // when combining a "long double" with a "double _Complex", the
1140 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnerd7135b42008-04-06 23:38:49 +00001141 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001142
1143 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff3b565d62007-08-27 21:32:55 +00001144 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1145 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001146 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001147 } else if (result < 0) { // The right side is bigger, convert lhs.
1148 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1149 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001150 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001151 }
1152 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1153 // domains match. This is a requirement for our implementation, C99
1154 // does not require this promotion.
1155 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1156 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001157 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001158 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001159 return rhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001160 } else { // handle "_Complex double, double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001161 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001162 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001163 return lhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001164 }
Chris Lattner4b009652007-07-25 00:24:17 +00001165 }
Steve Naroff3b6157f2007-08-27 21:43:43 +00001166 return lhs; // The domain/size match exactly.
Chris Lattner4b009652007-07-25 00:24:17 +00001167 }
1168 // Now handle "real" floating types (i.e. float, double, long double).
1169 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1170 // if we have an integer operand, the result is the real floating type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001171 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001172 // convert rhs to the lhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001173 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001174 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001175 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001176 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001177 // convert lhs to the rhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001178 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001179 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001180 }
1181 // We have two real floating types, float/complex combos were handled above.
1182 // Convert the smaller operand to the bigger result.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001183 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001184
1185 if (result > 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001186 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001187 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001188 }
Steve Naroff45fc9822007-08-27 15:30:22 +00001189 if (result < 0) { // convert the lhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001190 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff45fc9822007-08-27 15:30:22 +00001191 return rhs;
1192 }
1193 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Chris Lattner4b009652007-07-25 00:24:17 +00001194 }
Steve Naroff43001212008-01-15 19:36:10 +00001195 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1196 // Handle GCC complex int extension.
Steve Naroff43001212008-01-15 19:36:10 +00001197 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman50727042008-02-08 01:19:44 +00001198 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff43001212008-01-15 19:36:10 +00001199
Eli Friedman50727042008-02-08 01:19:44 +00001200 if (lhsComplexInt && rhsComplexInt) {
Chris Lattner51285d82008-04-06 23:55:33 +00001201 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1202 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman94075c02008-02-08 01:24:30 +00001203 // convert the rhs
1204 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1205 return lhs;
Eli Friedman50727042008-02-08 01:19:44 +00001206 }
1207 if (!isCompAssign)
Eli Friedman94075c02008-02-08 01:24:30 +00001208 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman50727042008-02-08 01:19:44 +00001209 return rhs;
1210 } else if (lhsComplexInt && rhs->isIntegerType()) {
1211 // convert the rhs to the lhs complex type.
1212 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1213 return lhs;
1214 } else if (rhsComplexInt && lhs->isIntegerType()) {
1215 // convert the lhs to the rhs complex type.
1216 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1217 return rhs;
1218 }
Steve Naroff43001212008-01-15 19:36:10 +00001219 }
Chris Lattner4b009652007-07-25 00:24:17 +00001220 // Finally, we have two differing integer types.
Chris Lattner51285d82008-04-06 23:55:33 +00001221 if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001222 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001223 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001224 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001225 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff8f708362007-08-24 19:07:16 +00001226 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001227}
1228
1229// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1230// being closely modeled after the C99 spec:-). The odd characteristic of this
1231// routine is it effectively iqnores the qualifiers on the top level pointee.
1232// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1233// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001234Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001235Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1236 QualType lhptee, rhptee;
1237
1238 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001239 lhptee = lhsType->getAsPointerType()->getPointeeType();
1240 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001241
1242 // make sure we operate on the canonical type
1243 lhptee = lhptee.getCanonicalType();
1244 rhptee = rhptee.getCanonicalType();
1245
Chris Lattner005ed752008-01-04 18:04:52 +00001246 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001247
1248 // C99 6.5.16.1p1: This following citation is common to constraints
1249 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1250 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001251 // FIXME: Handle ASQualType
1252 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1253 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001254 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001255
1256 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1257 // incomplete type and the other is a pointer to a qualified or unqualified
1258 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001259 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001260 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001261 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001262
1263 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001264 assert(rhptee->isFunctionType());
1265 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001266 }
1267
1268 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001269 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001270 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001271
1272 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001273 assert(lhptee->isFunctionType());
1274 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001275 }
1276
Chris Lattner4b009652007-07-25 00:24:17 +00001277 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1278 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001279 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1280 rhptee.getUnqualifiedType()))
1281 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001282 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001283}
1284
1285/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1286/// has code to accommodate several GCC extensions when type checking
1287/// pointers. Here are some objectionable examples that GCC considers warnings:
1288///
1289/// int a, *pint;
1290/// short *pshort;
1291/// struct foo *pfoo;
1292///
1293/// pint = pshort; // warning: assignment from incompatible pointer type
1294/// a = pint; // warning: assignment makes integer from pointer without a cast
1295/// pint = a; // warning: assignment makes pointer from integer without a cast
1296/// pint = pfoo; // warning: assignment from incompatible pointer type
1297///
1298/// As a result, the code for dealing with pointers is more complex than the
1299/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001300///
Chris Lattner005ed752008-01-04 18:04:52 +00001301Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001302Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001303 // Get canonical types. We're not formatting these types, just comparing
1304 // them.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001305 lhsType = lhsType.getCanonicalType().getUnqualifiedType();
1306 rhsType = rhsType.getCanonicalType().getUnqualifiedType();
1307
1308 if (lhsType == rhsType)
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001309 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001310
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001311 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001312 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001313 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001314 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001315 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001316
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001317 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1318 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001319 return Compatible;
Steve Naroff936c4362008-06-03 14:04:54 +00001320 // Relax integer conversions like we do for pointers below.
1321 if (rhsType->isIntegerType())
1322 return IntToPointer;
1323 if (lhsType->isIntegerType())
1324 return PointerToInt;
Chris Lattner1853da22008-01-04 23:18:45 +00001325 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001326 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001327
Chris Lattner390564e2008-04-07 06:49:41 +00001328 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001329 // For ExtVector, allow vector splats; float -> <n x float>
1330 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001331 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1332 return Compatible;
1333 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001334
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001335 // If LHS and RHS are both vectors of integer or both vectors of floating
1336 // point types, and the total vector length is the same, allow the
1337 // conversion. This is a bitcast; no bits are changed but the result type
1338 // is different.
1339 if (getLangOptions().LaxVectorConversions &&
1340 lhsType->isVectorType() && rhsType->isVectorType()) {
1341 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1342 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001343 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begemanec2d1062007-12-30 02:59:45 +00001344 return Compatible;
1345 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001346 }
1347 return Incompatible;
1348 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001349
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001350 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001351 return Compatible;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001352
Chris Lattner390564e2008-04-07 06:49:41 +00001353 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001354 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001355 return IntToPointer;
Eli Friedman48d0bb02008-05-30 18:07:22 +00001356
Chris Lattner390564e2008-04-07 06:49:41 +00001357 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001358 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001359 return Incompatible;
1360 }
1361
Chris Lattner390564e2008-04-07 06:49:41 +00001362 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001363 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedman48d0bb02008-05-30 18:07:22 +00001364 if (lhsType == Context.BoolTy)
1365 return Compatible;
1366
1367 if (lhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001368 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001369
Chris Lattner390564e2008-04-07 06:49:41 +00001370 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001371 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001372 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001373 }
Eli Friedman48d0bb02008-05-30 18:07:22 +00001374
Chris Lattner1853da22008-01-04 23:18:45 +00001375 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001376 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001377 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001378 }
1379 return Incompatible;
1380}
1381
Chris Lattner005ed752008-01-04 18:04:52 +00001382Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001383Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001384 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1385 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001386 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001387 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001388 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001389 return Compatible;
1390 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001391 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001392 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001393 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001394 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001395 //
1396 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1397 // are better understood.
1398 if (!lhsType->isReferenceType())
1399 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001400
Chris Lattner005ed752008-01-04 18:04:52 +00001401 Sema::AssignConvertType result =
1402 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001403
1404 // C99 6.5.16.1p2: The value of the right operand is converted to the
1405 // type of the assignment expression.
1406 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001407 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001408 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001409}
1410
Chris Lattner005ed752008-01-04 18:04:52 +00001411Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001412Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1413 return CheckAssignmentConstraints(lhsType, rhsType);
1414}
1415
Chris Lattner2c8bff72007-12-12 05:47:28 +00001416QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001417 Diag(loc, diag::err_typecheck_invalid_operands,
1418 lex->getType().getAsString(), rex->getType().getAsString(),
1419 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001420 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001421}
1422
1423inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1424 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001425 // For conversion purposes, we ignore any qualifiers.
1426 // For example, "const float" and "float" are equivalent.
1427 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1428 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001429
1430 // make sure the vector types are identical.
Nate Begeman03105572008-04-04 01:30:25 +00001431 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001432 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001433
Nate Begemanaf6ed502008-04-18 23:10:10 +00001434 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001435 // promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001436 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001437 if (V->getElementType().getCanonicalType().getTypePtr()
1438 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001439 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001440 return lhsType;
1441 }
1442 }
1443
Nate Begemanaf6ed502008-04-18 23:10:10 +00001444 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001445 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001446 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001447 if (V->getElementType().getCanonicalType().getTypePtr()
1448 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001449 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001450 return rhsType;
1451 }
1452 }
1453
Chris Lattner4b009652007-07-25 00:24:17 +00001454 // You cannot convert between vector values of different size.
1455 Diag(loc, diag::err_typecheck_vector_not_convertable,
1456 lex->getType().getAsString(), rex->getType().getAsString(),
1457 lex->getSourceRange(), rex->getSourceRange());
1458 return QualType();
1459}
1460
1461inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001462 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001463{
1464 QualType lhsType = lex->getType(), rhsType = rex->getType();
1465
1466 if (lhsType->isVectorType() || rhsType->isVectorType())
1467 return CheckVectorOperands(loc, lex, rex);
1468
Steve Naroff8f708362007-08-24 19:07:16 +00001469 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001470
Chris Lattner4b009652007-07-25 00:24:17 +00001471 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001472 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001473 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001474}
1475
1476inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001477 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001478{
1479 QualType lhsType = lex->getType(), rhsType = rex->getType();
1480
Steve Naroff8f708362007-08-24 19:07:16 +00001481 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001482
Chris Lattner4b009652007-07-25 00:24:17 +00001483 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001484 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001485 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001486}
1487
1488inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001489 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001490{
1491 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1492 return CheckVectorOperands(loc, lex, rex);
1493
Steve Naroff8f708362007-08-24 19:07:16 +00001494 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001495
Chris Lattner4b009652007-07-25 00:24:17 +00001496 // handle the common case first (both operands are arithmetic).
1497 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001498 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001499
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001500 // Put any potential pointer into PExp
1501 Expr* PExp = lex, *IExp = rex;
1502 if (IExp->getType()->isPointerType())
1503 std::swap(PExp, IExp);
1504
1505 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1506 if (IExp->getType()->isIntegerType()) {
1507 // Check for arithmetic on pointers to incomplete types
1508 if (!PTy->getPointeeType()->isObjectType()) {
1509 if (PTy->getPointeeType()->isVoidType()) {
1510 Diag(loc, diag::ext_gnu_void_ptr,
1511 lex->getSourceRange(), rex->getSourceRange());
1512 } else {
1513 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1514 lex->getType().getAsString(), lex->getSourceRange());
1515 return QualType();
1516 }
1517 }
1518 return PExp->getType();
1519 }
1520 }
1521
Chris Lattner2c8bff72007-12-12 05:47:28 +00001522 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001523}
1524
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001525// C99 6.5.6
1526QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1527 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001528 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1529 return CheckVectorOperands(loc, lex, rex);
1530
Steve Naroff8f708362007-08-24 19:07:16 +00001531 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001532
Chris Lattnerf6da2912007-12-09 21:53:25 +00001533 // Enforce type constraints: C99 6.5.6p3.
1534
1535 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001536 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001537 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001538
1539 // Either ptr - int or ptr - ptr.
1540 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001541 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001542
Chris Lattnerf6da2912007-12-09 21:53:25 +00001543 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001544 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001545 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001546 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001547 Diag(loc, diag::ext_gnu_void_ptr,
1548 lex->getSourceRange(), rex->getSourceRange());
1549 } else {
1550 Diag(loc, diag::err_typecheck_sub_ptr_object,
1551 lex->getType().getAsString(), lex->getSourceRange());
1552 return QualType();
1553 }
1554 }
1555
1556 // The result type of a pointer-int computation is the pointer type.
1557 if (rex->getType()->isIntegerType())
1558 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001559
Chris Lattnerf6da2912007-12-09 21:53:25 +00001560 // Handle pointer-pointer subtractions.
1561 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001562 QualType rpointee = RHSPTy->getPointeeType();
1563
Chris Lattnerf6da2912007-12-09 21:53:25 +00001564 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001565 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001566 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001567 if (rpointee->isVoidType()) {
1568 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-12-09 21:53:25 +00001569 Diag(loc, diag::ext_gnu_void_ptr,
1570 lex->getSourceRange(), rex->getSourceRange());
1571 } else {
1572 Diag(loc, diag::err_typecheck_sub_ptr_object,
1573 rex->getType().getAsString(), rex->getSourceRange());
1574 return QualType();
1575 }
1576 }
1577
1578 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001579 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1580 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001581 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1582 lex->getType().getAsString(), rex->getType().getAsString(),
1583 lex->getSourceRange(), rex->getSourceRange());
1584 return QualType();
1585 }
1586
1587 return Context.getPointerDiffType();
1588 }
1589 }
1590
Chris Lattner2c8bff72007-12-12 05:47:28 +00001591 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001592}
1593
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001594// C99 6.5.7
1595QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1596 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001597 // C99 6.5.7p2: Each of the operands shall have integer type.
1598 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1599 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001600
Chris Lattner2c8bff72007-12-12 05:47:28 +00001601 // Shifts don't perform usual arithmetic conversions, they just do integer
1602 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001603 if (!isCompAssign)
1604 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001605 UsualUnaryConversions(rex);
1606
1607 // "The type of the result is that of the promoted left operand."
1608 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001609}
1610
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001611// C99 6.5.8
1612QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1613 bool isRelational) {
Chris Lattner254f3bc2007-08-26 01:18:55 +00001614 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001615 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1616 UsualArithmeticConversions(lex, rex);
1617 else {
1618 UsualUnaryConversions(lex);
1619 UsualUnaryConversions(rex);
1620 }
Chris Lattner4b009652007-07-25 00:24:17 +00001621 QualType lType = lex->getType();
1622 QualType rType = rex->getType();
1623
Ted Kremenek486509e2007-10-29 17:13:39 +00001624 // For non-floating point types, check for self-comparisons of the form
1625 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1626 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001627 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001628 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1629 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001630 if (DRL->getDecl() == DRR->getDecl())
1631 Diag(loc, diag::warn_selfcomparison);
1632 }
1633
Chris Lattner254f3bc2007-08-26 01:18:55 +00001634 if (isRelational) {
1635 if (lType->isRealType() && rType->isRealType())
1636 return Context.IntTy;
1637 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001638 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001639 if (lType->isFloatingType()) {
1640 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001641 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001642 }
1643
Chris Lattner254f3bc2007-08-26 01:18:55 +00001644 if (lType->isArithmeticType() && rType->isArithmeticType())
1645 return Context.IntTy;
1646 }
Chris Lattner4b009652007-07-25 00:24:17 +00001647
Chris Lattner22be8422007-08-26 01:10:14 +00001648 bool LHSIsNull = lex->isNullPointerConstant(Context);
1649 bool RHSIsNull = rex->isNullPointerConstant(Context);
1650
Chris Lattner254f3bc2007-08-26 01:18:55 +00001651 // All of the following pointer related warnings are GCC extensions, except
1652 // when handling null pointer constants. One day, we can consider making them
1653 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001654 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001655 QualType LCanPointeeTy =
1656 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1657 QualType RCanPointeeTy =
1658 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman50727042008-02-08 01:19:44 +00001659
Steve Naroff3b435622007-11-13 14:57:38 +00001660 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001661 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1662 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1663 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001664 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1665 lType.getAsString(), rType.getAsString(),
1666 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001667 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001668 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001669 return Context.IntTy;
1670 }
Steve Naroff936c4362008-06-03 14:04:54 +00001671 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1672 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1673 ImpCastExprToType(rex, lType);
1674 return Context.IntTy;
1675 }
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001676 }
Steve Naroff936c4362008-06-03 14:04:54 +00001677 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1678 rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001679 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001680 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1681 lType.getAsString(), rType.getAsString(),
1682 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001683 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001684 return Context.IntTy;
1685 }
Steve Naroff936c4362008-06-03 14:04:54 +00001686 if (lType->isIntegerType() &&
1687 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattner22be8422007-08-26 01:10:14 +00001688 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001689 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1690 lType.getAsString(), rType.getAsString(),
1691 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001692 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001693 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001694 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001695 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001696}
1697
Chris Lattner4b009652007-07-25 00:24:17 +00001698inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001699 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001700{
1701 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1702 return CheckVectorOperands(loc, lex, rex);
1703
Steve Naroff8f708362007-08-24 19:07:16 +00001704 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001705
1706 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001707 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001708 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001709}
1710
1711inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1712 Expr *&lex, Expr *&rex, SourceLocation loc)
1713{
1714 UsualUnaryConversions(lex);
1715 UsualUnaryConversions(rex);
1716
Eli Friedmanbea3f842008-05-13 20:16:47 +00001717 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Chris Lattner4b009652007-07-25 00:24:17 +00001718 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001719 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001720}
1721
1722inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001723 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001724{
1725 QualType lhsType = lex->getType();
1726 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001727 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1728
1729 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001730 case Expr::MLV_Valid:
1731 break;
1732 case Expr::MLV_ConstQualified:
1733 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1734 return QualType();
1735 case Expr::MLV_ArrayType:
1736 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1737 lhsType.getAsString(), lex->getSourceRange());
1738 return QualType();
1739 case Expr::MLV_NotObjectType:
1740 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1741 lhsType.getAsString(), lex->getSourceRange());
1742 return QualType();
1743 case Expr::MLV_InvalidExpression:
1744 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1745 lex->getSourceRange());
1746 return QualType();
1747 case Expr::MLV_IncompleteType:
1748 case Expr::MLV_IncompleteVoidType:
1749 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1750 lhsType.getAsString(), lex->getSourceRange());
1751 return QualType();
1752 case Expr::MLV_DuplicateVectorComponents:
1753 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1754 lex->getSourceRange());
1755 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001756 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001757
Chris Lattner005ed752008-01-04 18:04:52 +00001758 AssignConvertType ConvTy;
1759 if (compoundType.isNull())
1760 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1761 else
1762 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1763
1764 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1765 rex, "assigning"))
1766 return QualType();
1767
Chris Lattner4b009652007-07-25 00:24:17 +00001768 // C99 6.5.16p3: The type of an assignment expression is the type of the
1769 // left operand unless the left operand has qualified type, in which case
1770 // it is the unqualified version of the type of the left operand.
1771 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1772 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001773 // C++ 5.17p1: the type of the assignment expression is that of its left
1774 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001775 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001776}
1777
1778inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1779 Expr *&lex, Expr *&rex, SourceLocation loc) {
1780 UsualUnaryConversions(rex);
1781 return rex->getType();
1782}
1783
1784/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1785/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1786QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1787 QualType resType = op->getType();
1788 assert(!resType.isNull() && "no type for increment/decrement expression");
1789
Steve Naroffd30e1932007-08-24 17:20:07 +00001790 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001791 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand9b1fec2008-05-18 18:08:51 +00001792 if (pt->getPointeeType()->isVoidType()) {
1793 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1794 } else if (!pt->getPointeeType()->isObjectType()) {
1795 // C99 6.5.2.4p2, 6.5.6p2
Chris Lattner4b009652007-07-25 00:24:17 +00001796 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1797 resType.getAsString(), op->getSourceRange());
1798 return QualType();
1799 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001800 } else if (!resType->isRealType()) {
1801 if (resType->isComplexType())
1802 // C99 does not support ++/-- on complex types.
1803 Diag(OpLoc, diag::ext_integer_increment_complex,
1804 resType.getAsString(), op->getSourceRange());
1805 else {
1806 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1807 resType.getAsString(), op->getSourceRange());
1808 return QualType();
1809 }
Chris Lattner4b009652007-07-25 00:24:17 +00001810 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001811 // At this point, we know we have a real, complex or pointer type.
1812 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001813 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1814 if (mlval != Expr::MLV_Valid) {
1815 // FIXME: emit a more precise diagnostic...
1816 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1817 op->getSourceRange());
1818 return QualType();
1819 }
1820 return resType;
1821}
1822
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001823/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001824/// This routine allows us to typecheck complex/recursive expressions
1825/// where the declaration is needed for type checking. Here are some
1826/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner48d7f382008-04-02 04:24:33 +00001827static ValueDecl *getPrimaryDecl(Expr *E) {
1828 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001829 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001830 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001831 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001832 // Fields cannot be declared with a 'register' storage class.
1833 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00001834 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00001835 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00001836 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001837 case Stmt::ArraySubscriptExprClass: {
1838 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1839
Chris Lattner48d7f382008-04-02 04:24:33 +00001840 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001841 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001842 return 0;
1843 else
1844 return VD;
1845 }
Chris Lattner4b009652007-07-25 00:24:17 +00001846 case Stmt::UnaryOperatorClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001847 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001848 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001849 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001850 case Stmt::ImplicitCastExprClass:
1851 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00001852 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001853 default:
1854 return 0;
1855 }
1856}
1857
1858/// CheckAddressOfOperand - The operand of & must be either a function
1859/// designator or an lvalue designating an object. If it is an lvalue, the
1860/// object cannot be declared with storage class register or be a bit field.
1861/// Note: The usual conversions are *not* applied to the operand of the &
1862/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1863QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001864 if (getLangOptions().C99) {
1865 // Implement C99-only parts of addressof rules.
1866 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1867 if (uOp->getOpcode() == UnaryOperator::Deref)
1868 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1869 // (assuming the deref expression is valid).
1870 return uOp->getSubExpr()->getType();
1871 }
1872 // Technically, there should be a check for array subscript
1873 // expressions here, but the result of one is always an lvalue anyway.
1874 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001875 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00001876 Expr::isLvalueResult lval = op->isLvalue();
1877
1878 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00001879 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1880 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00001881 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1882 op->getSourceRange());
1883 return QualType();
1884 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00001885 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1886 if (MemExpr->getMemberDecl()->isBitField()) {
1887 Diag(OpLoc, diag::err_typecheck_address_of,
1888 std::string("bit-field"), op->getSourceRange());
1889 return QualType();
1890 }
1891 // Check for Apple extension for accessing vector components.
1892 } else if (isa<ArraySubscriptExpr>(op) &&
1893 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1894 Diag(OpLoc, diag::err_typecheck_address_of,
1895 std::string("vector"), op->getSourceRange());
1896 return QualType();
1897 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00001898 // We have an lvalue with a decl. Make sure the decl is not declared
1899 // with the register storage-class specifier.
1900 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1901 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00001902 Diag(OpLoc, diag::err_typecheck_address_of,
1903 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001904 return QualType();
1905 }
1906 } else
1907 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00001908 }
1909 // If the operand has type "type", the result has type "pointer to type".
1910 return Context.getPointerType(op->getType());
1911}
1912
1913QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1914 UsualUnaryConversions(op);
1915 QualType qType = op->getType();
1916
Chris Lattner7931f4a2007-07-31 16:53:04 +00001917 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001918 // Note that per both C89 and C99, this is always legal, even
1919 // if ptype is an incomplete type or void.
1920 // It would be possible to warn about dereferencing a
1921 // void pointer, but it's completely well-defined,
1922 // and such a warning is unlikely to catch any mistakes.
1923 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001924 }
1925 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1926 qType.getAsString(), op->getSourceRange());
1927 return QualType();
1928}
1929
1930static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1931 tok::TokenKind Kind) {
1932 BinaryOperator::Opcode Opc;
1933 switch (Kind) {
1934 default: assert(0 && "Unknown binop!");
1935 case tok::star: Opc = BinaryOperator::Mul; break;
1936 case tok::slash: Opc = BinaryOperator::Div; break;
1937 case tok::percent: Opc = BinaryOperator::Rem; break;
1938 case tok::plus: Opc = BinaryOperator::Add; break;
1939 case tok::minus: Opc = BinaryOperator::Sub; break;
1940 case tok::lessless: Opc = BinaryOperator::Shl; break;
1941 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1942 case tok::lessequal: Opc = BinaryOperator::LE; break;
1943 case tok::less: Opc = BinaryOperator::LT; break;
1944 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1945 case tok::greater: Opc = BinaryOperator::GT; break;
1946 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1947 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1948 case tok::amp: Opc = BinaryOperator::And; break;
1949 case tok::caret: Opc = BinaryOperator::Xor; break;
1950 case tok::pipe: Opc = BinaryOperator::Or; break;
1951 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1952 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1953 case tok::equal: Opc = BinaryOperator::Assign; break;
1954 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1955 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1956 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1957 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1958 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1959 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1960 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1961 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1962 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1963 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1964 case tok::comma: Opc = BinaryOperator::Comma; break;
1965 }
1966 return Opc;
1967}
1968
1969static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1970 tok::TokenKind Kind) {
1971 UnaryOperator::Opcode Opc;
1972 switch (Kind) {
1973 default: assert(0 && "Unknown unary op!");
1974 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1975 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1976 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1977 case tok::star: Opc = UnaryOperator::Deref; break;
1978 case tok::plus: Opc = UnaryOperator::Plus; break;
1979 case tok::minus: Opc = UnaryOperator::Minus; break;
1980 case tok::tilde: Opc = UnaryOperator::Not; break;
1981 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1982 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1983 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1984 case tok::kw___real: Opc = UnaryOperator::Real; break;
1985 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1986 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1987 }
1988 return Opc;
1989}
1990
1991// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001992Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00001993 ExprTy *LHS, ExprTy *RHS) {
1994 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1995 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1996
Steve Naroff87d58b42007-09-16 03:34:24 +00001997 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1998 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001999
2000 QualType ResultTy; // Result type of the binary operator.
2001 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
2002
2003 switch (Opc) {
2004 default:
2005 assert(0 && "Unknown binary expr!");
2006 case BinaryOperator::Assign:
2007 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
2008 break;
2009 case BinaryOperator::Mul:
2010 case BinaryOperator::Div:
2011 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
2012 break;
2013 case BinaryOperator::Rem:
2014 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
2015 break;
2016 case BinaryOperator::Add:
2017 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2018 break;
2019 case BinaryOperator::Sub:
2020 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2021 break;
2022 case BinaryOperator::Shl:
2023 case BinaryOperator::Shr:
2024 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2025 break;
2026 case BinaryOperator::LE:
2027 case BinaryOperator::LT:
2028 case BinaryOperator::GE:
2029 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002030 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002031 break;
2032 case BinaryOperator::EQ:
2033 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00002034 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00002035 break;
2036 case BinaryOperator::And:
2037 case BinaryOperator::Xor:
2038 case BinaryOperator::Or:
2039 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2040 break;
2041 case BinaryOperator::LAnd:
2042 case BinaryOperator::LOr:
2043 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2044 break;
2045 case BinaryOperator::MulAssign:
2046 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002047 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002048 if (!CompTy.isNull())
2049 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2050 break;
2051 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002052 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002053 if (!CompTy.isNull())
2054 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2055 break;
2056 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002057 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002058 if (!CompTy.isNull())
2059 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2060 break;
2061 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002062 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002063 if (!CompTy.isNull())
2064 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2065 break;
2066 case BinaryOperator::ShlAssign:
2067 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002068 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002069 if (!CompTy.isNull())
2070 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2071 break;
2072 case BinaryOperator::AndAssign:
2073 case BinaryOperator::XorAssign:
2074 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00002075 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00002076 if (!CompTy.isNull())
2077 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2078 break;
2079 case BinaryOperator::Comma:
2080 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2081 break;
2082 }
2083 if (ResultTy.isNull())
2084 return true;
2085 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00002086 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002087 else
Chris Lattnerf420df12007-08-28 18:36:55 +00002088 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00002089}
2090
2091// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00002092Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00002093 ExprTy *input) {
2094 Expr *Input = (Expr*)input;
2095 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2096 QualType resultType;
2097 switch (Opc) {
2098 default:
2099 assert(0 && "Unimplemented unary expr!");
2100 case UnaryOperator::PreInc:
2101 case UnaryOperator::PreDec:
2102 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2103 break;
2104 case UnaryOperator::AddrOf:
2105 resultType = CheckAddressOfOperand(Input, OpLoc);
2106 break;
2107 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00002108 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00002109 resultType = CheckIndirectionOperand(Input, OpLoc);
2110 break;
2111 case UnaryOperator::Plus:
2112 case UnaryOperator::Minus:
2113 UsualUnaryConversions(Input);
2114 resultType = Input->getType();
2115 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2116 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2117 resultType.getAsString());
2118 break;
2119 case UnaryOperator::Not: // bitwise complement
2120 UsualUnaryConversions(Input);
2121 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00002122 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2123 if (!resultType->isIntegerType()) {
2124 if (resultType->isComplexType())
2125 // C99 does not support '~' for complex conjugation.
2126 Diag(OpLoc, diag::ext_integer_complement_complex,
2127 resultType.getAsString());
2128 else
2129 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2130 resultType.getAsString());
2131 }
Chris Lattner4b009652007-07-25 00:24:17 +00002132 break;
2133 case UnaryOperator::LNot: // logical negation
2134 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
2135 DefaultFunctionArrayConversion(Input);
2136 resultType = Input->getType();
2137 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2138 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2139 resultType.getAsString());
2140 // LNot always has type int. C99 6.5.3.3p5.
2141 resultType = Context.IntTy;
2142 break;
2143 case UnaryOperator::SizeOf:
2144 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2145 break;
2146 case UnaryOperator::AlignOf:
2147 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2148 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002149 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002150 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002151 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002152 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002153 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002154 resultType = Input->getType();
2155 break;
2156 }
2157 if (resultType.isNull())
2158 return true;
2159 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2160}
2161
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002162/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2163Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002164 SourceLocation LabLoc,
2165 IdentifierInfo *LabelII) {
2166 // Look up the record for this label identifier.
2167 LabelStmt *&LabelDecl = LabelMap[LabelII];
2168
2169 // If we haven't seen this label yet, create a forward reference.
2170 if (LabelDecl == 0)
2171 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2172
2173 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002174 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2175 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002176}
2177
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002178Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002179 SourceLocation RPLoc) { // "({..})"
2180 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2181 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2182 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2183
2184 // FIXME: there are a variety of strange constraints to enforce here, for
2185 // example, it is not possible to goto into a stmt expression apparently.
2186 // More semantic analysis is needed.
2187
2188 // FIXME: the last statement in the compount stmt has its value used. We
2189 // should not warn about it being unused.
2190
2191 // If there are sub stmts in the compound stmt, take the type of the last one
2192 // as the type of the stmtexpr.
2193 QualType Ty = Context.VoidTy;
2194
2195 if (!Compound->body_empty())
2196 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2197 Ty = LastExpr->getType();
2198
2199 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2200}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002201
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002202Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002203 SourceLocation TypeLoc,
2204 TypeTy *argty,
2205 OffsetOfComponent *CompPtr,
2206 unsigned NumComponents,
2207 SourceLocation RPLoc) {
2208 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2209 assert(!ArgTy.isNull() && "Missing type argument!");
2210
2211 // We must have at least one component that refers to the type, and the first
2212 // one is known to be a field designator. Verify that the ArgTy represents
2213 // a struct/union/class.
2214 if (!ArgTy->isRecordType())
2215 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2216
2217 // Otherwise, create a compound literal expression as the base, and
2218 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002219 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002220
Chris Lattnerb37522e2007-08-31 21:49:13 +00002221 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2222 // GCC extension, diagnose them.
2223 if (NumComponents != 1)
2224 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2225 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2226
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002227 for (unsigned i = 0; i != NumComponents; ++i) {
2228 const OffsetOfComponent &OC = CompPtr[i];
2229 if (OC.isBrackets) {
2230 // Offset of an array sub-field. TODO: Should we allow vector elements?
2231 const ArrayType *AT = Res->getType()->getAsArrayType();
2232 if (!AT) {
2233 delete Res;
2234 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2235 Res->getType().getAsString());
2236 }
2237
Chris Lattner2af6a802007-08-30 17:59:59 +00002238 // FIXME: C++: Verify that operator[] isn't overloaded.
2239
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002240 // C99 6.5.2.1p1
2241 Expr *Idx = static_cast<Expr*>(OC.U.E);
2242 if (!Idx->getType()->isIntegerType())
2243 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2244 Idx->getSourceRange());
2245
2246 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2247 continue;
2248 }
2249
2250 const RecordType *RC = Res->getType()->getAsRecordType();
2251 if (!RC) {
2252 delete Res;
2253 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2254 Res->getType().getAsString());
2255 }
2256
2257 // Get the decl corresponding to this.
2258 RecordDecl *RD = RC->getDecl();
2259 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2260 if (!MemberDecl)
2261 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2262 OC.U.IdentInfo->getName(),
2263 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002264
2265 // FIXME: C++: Verify that MemberDecl isn't a static field.
2266 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002267 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2268 // matter here.
2269 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002270 }
2271
2272 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2273 BuiltinLoc);
2274}
2275
2276
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002277Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002278 TypeTy *arg1, TypeTy *arg2,
2279 SourceLocation RPLoc) {
2280 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2281 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2282
2283 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2284
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002285 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002286}
2287
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002288Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002289 ExprTy *expr1, ExprTy *expr2,
2290 SourceLocation RPLoc) {
2291 Expr *CondExpr = static_cast<Expr*>(cond);
2292 Expr *LHSExpr = static_cast<Expr*>(expr1);
2293 Expr *RHSExpr = static_cast<Expr*>(expr2);
2294
2295 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2296
2297 // The conditional expression is required to be a constant expression.
2298 llvm::APSInt condEval(32);
2299 SourceLocation ExpLoc;
2300 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2301 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2302 CondExpr->getSourceRange());
2303
2304 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2305 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2306 RHSExpr->getType();
2307 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2308}
2309
Nate Begemanbd881ef2008-01-30 20:50:20 +00002310/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002311/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002312/// The number of arguments has already been validated to match the number of
2313/// arguments in FnType.
2314static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002315 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002316 for (unsigned i = 0; i != NumParams; ++i) {
2317 QualType ExprTy = Args[i]->getType().getCanonicalType();
2318 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2319
2320 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002321 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002322 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002323 return true;
2324}
2325
2326Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2327 SourceLocation *CommaLocs,
2328 SourceLocation BuiltinLoc,
2329 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002330 // __builtin_overload requires at least 2 arguments
2331 if (NumArgs < 2)
2332 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2333 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002334
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002335 // The first argument is required to be a constant expression. It tells us
2336 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002337 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002338 Expr *NParamsExpr = Args[0];
2339 llvm::APSInt constEval(32);
2340 SourceLocation ExpLoc;
2341 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2342 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2343 NParamsExpr->getSourceRange());
2344
2345 // Verify that the number of parameters is > 0
2346 unsigned NumParams = constEval.getZExtValue();
2347 if (NumParams == 0)
2348 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2349 NParamsExpr->getSourceRange());
2350 // Verify that we have at least 1 + NumParams arguments to the builtin.
2351 if ((NumParams + 1) > NumArgs)
2352 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2353 SourceRange(BuiltinLoc, RParenLoc));
2354
2355 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002356 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002357 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002358 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2359 // UsualUnaryConversions will convert the function DeclRefExpr into a
2360 // pointer to function.
2361 Expr *Fn = UsualUnaryConversions(Args[i]);
2362 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002363 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2364 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2365 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2366 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002367
2368 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2369 // parameters, and the number of parameters must match the value passed to
2370 // the builtin.
2371 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002372 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2373 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002374
2375 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002376 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002377 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002378 if (ExprsMatchFnType(Args+1, FnType)) {
2379 if (OE)
2380 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2381 OE->getFn()->getSourceRange());
2382 // Remember our match, and continue processing the remaining arguments
2383 // to catch any errors.
2384 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2385 BuiltinLoc, RParenLoc);
2386 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002387 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002388 // Return the newly created OverloadExpr node, if we succeded in matching
2389 // exactly one of the candidate functions.
2390 if (OE)
2391 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002392
2393 // If we didn't find a matching function Expr in the __builtin_overload list
2394 // the return an error.
2395 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002396 for (unsigned i = 0; i != NumParams; ++i) {
2397 if (i != 0) typeNames += ", ";
2398 typeNames += Args[i+1]->getType().getAsString();
2399 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002400
2401 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2402 SourceRange(BuiltinLoc, RParenLoc));
2403}
2404
Anders Carlsson36760332007-10-15 20:28:48 +00002405Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2406 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002407 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002408 Expr *E = static_cast<Expr*>(expr);
2409 QualType T = QualType::getFromOpaquePtr(type);
2410
2411 InitBuiltinVaListType();
2412
Chris Lattner005ed752008-01-04 18:04:52 +00002413 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2414 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002415 return Diag(E->getLocStart(),
2416 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2417 E->getType().getAsString(),
2418 E->getSourceRange());
2419
2420 // FIXME: Warn if a non-POD type is passed in.
2421
2422 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2423}
2424
Chris Lattner005ed752008-01-04 18:04:52 +00002425bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2426 SourceLocation Loc,
2427 QualType DstType, QualType SrcType,
2428 Expr *SrcExpr, const char *Flavor) {
2429 // Decode the result (notice that AST's are still created for extensions).
2430 bool isInvalid = false;
2431 unsigned DiagKind;
2432 switch (ConvTy) {
2433 default: assert(0 && "Unknown conversion type");
2434 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002435 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002436 DiagKind = diag::ext_typecheck_convert_pointer_int;
2437 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002438 case IntToPointer:
2439 DiagKind = diag::ext_typecheck_convert_int_pointer;
2440 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002441 case IncompatiblePointer:
2442 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2443 break;
2444 case FunctionVoidPointer:
2445 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2446 break;
2447 case CompatiblePointerDiscardsQualifiers:
2448 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2449 break;
2450 case Incompatible:
2451 DiagKind = diag::err_typecheck_convert_incompatible;
2452 isInvalid = true;
2453 break;
2454 }
2455
2456 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2457 SrcExpr->getSourceRange());
2458 return isInvalid;
2459}
2460
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +00002461
2462