blob: 8b31d8856a78c1c69e47d14fd187f734565ab0e2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek588e5eb2007-11-25 00:58:00 +000015#include "SemaUtil.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/Expr.h"
Chris Lattner04421082008-04-08 04:40:51 +000018#include "clang/AST/ExprCXX.h"
Steve Narofff494b572008-05-29 21:12:08 +000019#include "clang/AST/ExprObjC.h"
Steve Naroff563477d2007-09-18 23:55:05 +000020#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Lex/Preprocessor.h"
22#include "clang/Lex/LiteralSupport.h"
23#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner925e60d2007-12-28 05:29:59 +000025#include "llvm/ADT/OwningPtr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026#include "llvm/ADT/SmallString.h"
Chris Lattner59907c42007-08-10 20:18:51 +000027#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Steve Narofff69936d2007-09-16 03:34:24 +000030/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Reid Spencer5f016e22007-07-11 17:01:13 +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 Narofff69936d2007-09-16 03:34:24 +000037Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnera7ad98f2008-02-11 00:02:17 +000047
48 // Verify that pascal strings aren't too large.
Anders Carlssonee98ac52007-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()));
Reid Spencer5f016e22007-07-11 17:01:13 +000053
Chris Lattnera7ad98f2008-02-11 00:02:17 +000054 QualType StrTy = Context.CharTy;
Eli Friedman8ef1f262008-05-27 07:57:14 +000055 if (Literal.AnyWide) StrTy = Context.getWcharType();
Chris Lattnera7ad98f2008-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
Reid Spencer5f016e22007-07-11 17:01:13 +000065 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
66 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera7ad98f2008-02-11 00:02:17 +000067 Literal.AnyWide, StrTy,
Anders Carlssonee98ac52007-10-15 02:50:23 +000068 StringToks[0].getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +000069 StringToks[NumStringToks-1].getLocation());
70}
71
72
Steve Naroff08d92e42007-09-15 18:49:24 +000073/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroff0d755ad2008-03-19 23:46:26 +000075/// identifier is used in a function call context.
Steve Naroff08d92e42007-09-15 18:49:24 +000076Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +000077 IdentifierInfo &II,
78 bool HasTrailingLParen) {
Chris Lattner8a934232008-03-31 00:36:02 +000079 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroffb327ce02008-04-02 14:35:35 +000080 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattner8a934232008-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 Naroffe8043c32008-04-01 23:04:06 +000085 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattner8a934232008-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 Naroffe8043c32008-04-01 23:04:06 +000091 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Chris Lattner8a934232008-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 Naroffe3e9add2008-06-02 23:03:37 +0000102 if (!strncmp(II.getName(), "super", 5)) {
103 QualType T = Context.getPointerType(Context.getObjCInterfaceType(
104 CurMethodDecl->getClassInterface()));
105 return new ObjCSuperRefExpr(T, Loc);
106 }
Chris Lattner8a934232008-03-31 00:36:02 +0000107 }
108
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner8a934232008-03-31 00:36:02 +0000113 !getLangOptions().CPlusPlus) // Not in C++.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner8a934232008-03-31 00:36:02 +0000121
Steve Naroffe1223f72007-08-28 03:03:08 +0000122 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner7e669b22008-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 Naroff53a32342007-08-28 18:45:29 +0000127 // Only create DeclRefExpr's for valid Decl's.
Steve Naroff5912a352007-08-28 20:14:24 +0000128 if (VD->isInvalidDecl())
Steve Naroffe1223f72007-08-28 03:03:08 +0000129 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroffe1223f72007-08-28 03:03:08 +0000131 }
Chris Lattner8a934232008-03-31 00:36:02 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 if (isa<TypedefDecl>(D))
134 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000135 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian5ef404f2007-12-05 18:16:33 +0000136 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000137 if (isa<NamespaceDecl>(D))
138 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Reid Spencer5f016e22007-07-11 17:01:13 +0000139
140 assert(0 && "Invalid decl");
Chris Lattnereddbe032007-07-21 04:57:45 +0000141 abort();
Reid Spencer5f016e22007-07-11 17:01:13 +0000142}
143
Steve Narofff69936d2007-09-16 03:34:24 +0000144Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson22742662007-07-21 05:21:51 +0000145 tok::TokenKind Kind) {
146 PreDefinedExpr::IdentType IT;
147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 switch (Kind) {
Chris Lattner1423ea42008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 }
Chris Lattner1423ea42008-01-12 18:39:25 +0000154
155 // Verify that this is in a function context.
Chris Lattner8f978d52008-01-12 19:32:28 +0000156 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattner1423ea42008-01-12 18:39:25 +0000157 return Diag(Loc, diag::err_predef_outside_function);
Anders Carlsson22742662007-07-21 05:21:51 +0000158
Chris Lattnerfa28b302008-01-12 08:14:25 +0000159 // Pre-defined identifiers are of type char[x], where x is the length of the
160 // string.
Chris Lattner8f978d52008-01-12 19:32:28 +0000161 unsigned Length;
162 if (CurFunctionDecl)
163 Length = CurFunctionDecl->getIdentifier()->getLength();
164 else
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000165 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattner1423ea42008-01-12 18:39:25 +0000166
Chris Lattner8f978d52008-01-12 19:32:28 +0000167 llvm::APInt LengthI(32, Length + 1);
Chris Lattner1423ea42008-01-12 18:39:25 +0000168 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattner8f978d52008-01-12 19:32:28 +0000169 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattnerfa28b302008-01-12 08:14:25 +0000170 return new PreDefinedExpr(Loc, ResTy, IT);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
Steve Narofff69936d2007-09-16 03:34:24 +0000173Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerfc62bfd2008-03-01 08:32:21 +0000183
184 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
185
186 return new CharacterLiteral(Literal.getValue(), type, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000187}
188
Steve Narofff69936d2007-09-16 03:34:24 +0000189Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // fast path for a single digit (which is quite common). A single digit
191 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
192 if (Tok.getLength() == 1) {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000193 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000194
Chris Lattner98be4942008-03-05 18:54:05 +0000195 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattnerf0467b32008-04-02 04:24:33 +0000196 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 Context.IntTy,
198 Tok.getLocation()));
199 }
200 llvm::SmallString<512> IntegerBuffer;
201 IntegerBuffer.resize(Tok.getLength());
202 const char *ThisTokBegin = &IntegerBuffer[0];
203
204 // Get the spelling of the token, which eliminates trigraphs, etc.
205 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
206 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
207 Tok.getLocation(), PP);
208 if (Literal.hadError)
209 return ExprResult(true);
210
Chris Lattner5d661452007-08-26 03:42:43 +0000211 Expr *Res;
212
213 if (Literal.isFloatingLiteral()) {
Chris Lattner525a0502007-09-22 18:29:59 +0000214 QualType Ty;
215 const llvm::fltSemantics *Format;
Chris Lattner525a0502007-09-22 18:29:59 +0000216
217 if (Literal.isFloat) {
218 Ty = Context.FloatTy;
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000219 Format = Context.Target.getFloatFormat();
220 } else if (!Literal.isLong) {
Chris Lattner525a0502007-09-22 18:29:59 +0000221 Ty = Context.DoubleTy;
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000222 Format = Context.Target.getDoubleFormat();
223 } else {
224 Ty = Context.LongDoubleTy;
225 Format = Context.Target.getLongDoubleFormat();
Chris Lattner525a0502007-09-22 18:29:59 +0000226 }
227
Ted Kremenek720c4ec2007-11-29 00:56:49 +0000228 // isExact will be set by GetFloatValue().
229 bool isExact = false;
230
231 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
232 Ty, Tok.getLocation());
233
Chris Lattner5d661452007-08-26 03:42:43 +0000234 } else if (!Literal.isIntegerLiteral()) {
235 return ExprResult(true);
236 } else {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000237 QualType Ty;
Reid Spencer5f016e22007-07-11 17:01:13 +0000238
Neil Boothb9449512007-08-29 22:00:19 +0000239 // long long is a C99 feature.
240 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth79859c32007-08-29 22:13:52 +0000241 Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000242 Diag(Tok.getLocation(), diag::ext_longlong);
243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 // Get the value in the widest-possible width.
Chris Lattner98be4942008-03-05 18:54:05 +0000245 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
247 if (Literal.GetIntegerValue(ResultVal)) {
248 // If this value didn't fit into uintmax_t, warn and force to ull.
249 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattnerf0467b32008-04-02 04:24:33 +0000250 Ty = Context.UnsignedLongLongTy;
251 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner98be4942008-03-05 18:54:05 +0000252 "long long is not intmax_t?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 } else {
254 // If this value fits into a ULL, try to figure out what else it fits into
255 // according to the rules of C99 6.4.4.1p5.
256
257 // Octal, Hexadecimal, and integers with a U suffix are allowed to
258 // be an unsigned int.
259 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
260
261 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000262 unsigned Width = 0;
Chris Lattner97c51562007-08-23 21:58:08 +0000263 if (!Literal.isLong && !Literal.isLongLong) {
264 // Are int/unsigned possibilities?
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000265 unsigned IntSize = Context.Target.getIntWidth();
266
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 // Does it fit in a unsigned int?
268 if (ResultVal.isIntN(IntSize)) {
269 // Does it fit in a signed int?
270 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000271 Ty = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000273 Ty = Context.UnsignedIntTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000274 Width = IntSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 }
277
278 // Are long/unsigned long possibilities?
Chris Lattnerf0467b32008-04-02 04:24:33 +0000279 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000280 unsigned LongSize = Context.Target.getLongWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000281
282 // Does it fit in a unsigned long?
283 if (ResultVal.isIntN(LongSize)) {
284 // Does it fit in a signed long?
285 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000286 Ty = Context.LongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000288 Ty = Context.UnsignedLongTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000289 Width = LongSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000290 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 }
292
293 // Finally, check long long if needed.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000294 if (Ty.isNull()) {
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000295 unsigned LongLongSize = Context.Target.getLongLongWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000296
297 // Does it fit in a unsigned long long?
298 if (ResultVal.isIntN(LongLongSize)) {
299 // Does it fit in a signed long long?
300 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000301 Ty = Context.LongLongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000303 Ty = Context.UnsignedLongLongTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000304 Width = LongLongSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 }
306 }
307
308 // If we still couldn't decide a type, we probably have something that
309 // does not fit in a signed long long, but has no U suffix.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000310 if (Ty.isNull()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattnerf0467b32008-04-02 04:24:33 +0000312 Ty = Context.UnsignedLongLongTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000313 Width = Context.Target.getLongLongWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 }
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000315
316 if (ResultVal.getBitWidth() != Width)
317 ResultVal.trunc(Width);
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 }
319
Chris Lattnerf0467b32008-04-02 04:24:33 +0000320 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 }
Chris Lattner5d661452007-08-26 03:42:43 +0000322
323 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
324 if (Literal.isImaginary)
325 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
326
327 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000328}
329
Steve Narofff69936d2007-09-16 03:34:24 +0000330Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 ExprTy *Val) {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000332 Expr *E = (Expr *)Val;
333 assert((E != 0) && "ActOnParenExpr() missing expr");
334 return new ParenExpr(L, R, E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000335}
336
337/// The UsualUnaryConversions() function is *not* called by this routine.
338/// See C99 6.3.2.1p[2-4] for more details.
339QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
340 SourceLocation OpLoc, bool isSizeof) {
341 // C99 6.5.3.4p1:
342 if (isa<FunctionType>(exprType) && isSizeof)
343 // alignof(function) is allowed.
344 Diag(OpLoc, diag::ext_sizeof_function_type);
345 else if (exprType->isVoidType())
346 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
347 else if (exprType->isIncompleteType()) {
348 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
349 diag::err_alignof_incomplete_type,
350 exprType.getAsString());
351 return QualType(); // error
352 }
353 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
354 return Context.getSizeType();
355}
356
357Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000358ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 SourceLocation LPLoc, TypeTy *Ty,
360 SourceLocation RPLoc) {
361 // If error parsing type, ignore.
362 if (Ty == 0) return true;
363
364 // Verify that this is a valid expression.
365 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
366
367 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
368
369 if (resultType.isNull())
370 return true;
371 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
372}
373
Chris Lattner5d794252007-08-24 21:41:10 +0000374QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattnerdbb36972007-08-24 21:16:53 +0000375 DefaultFunctionArrayConversion(V);
376
Chris Lattnercc26ed72007-08-26 05:39:26 +0000377 // These operators return the element type of a complex type.
Chris Lattnerdbb36972007-08-24 21:16:53 +0000378 if (const ComplexType *CT = V->getType()->getAsComplexType())
379 return CT->getElementType();
Chris Lattnercc26ed72007-08-26 05:39:26 +0000380
381 // Otherwise they pass through real integer and floating point types here.
382 if (V->getType()->isArithmeticType())
383 return V->getType();
384
385 // Reject anything else.
386 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
387 return QualType();
Chris Lattnerdbb36972007-08-24 21:16:53 +0000388}
389
390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391
Steve Narofff69936d2007-09-16 03:34:24 +0000392Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 tok::TokenKind Kind,
394 ExprTy *Input) {
395 UnaryOperator::Opcode Opc;
396 switch (Kind) {
397 default: assert(0 && "Unknown unary op!");
398 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
399 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
400 }
401 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
402 if (result.isNull())
403 return true;
404 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
405}
406
407Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000408ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner727a80d2007-07-15 23:59:53 +0000410 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner12d9ff62007-07-16 00:14:47 +0000411
412 // Perform default conversions.
413 DefaultFunctionArrayConversion(LHSExp);
414 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner727a80d2007-07-15 23:59:53 +0000415
Chris Lattner12d9ff62007-07-16 00:14:47 +0000416 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000419 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 // in the subscript position. As a result, we need to derive the array base
421 // and index from the expression types.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000422 Expr *BaseExpr, *IndexExpr;
423 QualType ResultType;
Chris Lattnerbefee482007-07-31 16:53:04 +0000424 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner12d9ff62007-07-16 00:14:47 +0000425 BaseExpr = LHSExp;
426 IndexExpr = RHSExp;
427 // FIXME: need to deal with const...
428 ResultType = PTy->getPointeeType();
Chris Lattnerbefee482007-07-31 16:53:04 +0000429 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000430 // Handle the uncommon case of "123[Ptr]".
Chris Lattner12d9ff62007-07-16 00:14:47 +0000431 BaseExpr = RHSExp;
432 IndexExpr = LHSExp;
433 // FIXME: need to deal with const...
434 ResultType = PTy->getPointeeType();
Chris Lattnerc8629632007-07-31 19:29:30 +0000435 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
436 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner12d9ff62007-07-16 00:14:47 +0000437 IndexExpr = RHSExp;
Steve Naroff608e0ee2007-08-03 22:40:33 +0000438
439 // Component access limited to variables (reject vec4.rg[1]).
Nate Begeman8a997642008-05-09 06:41:27 +0000440 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
441 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begeman213541a2008-04-18 23:10:10 +0000442 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff608e0ee2007-08-03 22:40:33 +0000443 SourceRange(LLoc, RLoc));
Chris Lattner12d9ff62007-07-16 00:14:47 +0000444 // FIXME: need to deal with const...
445 ResultType = VTy->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 } else {
Chris Lattner727a80d2007-07-15 23:59:53 +0000447 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
448 RHSExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 }
450 // C99 6.5.2.1p1
Chris Lattner12d9ff62007-07-16 00:14:47 +0000451 if (!IndexExpr->getType()->isIntegerType())
452 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
453 IndexExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000454
Chris Lattner12d9ff62007-07-16 00:14:47 +0000455 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
456 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattnerd805bec2008-04-02 06:59:01 +0000457 // void (*)(int)) and pointers to incomplete types. Functions are not
458 // objects in C99.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000459 if (!ResultType->isObjectType())
460 return Diag(BaseExpr->getLocStart(),
461 diag::err_typecheck_subscript_not_object,
462 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
463
464 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465}
466
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000467QualType Sema::
Nate Begeman213541a2008-04-18 23:10:10 +0000468CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000469 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begeman213541a2008-04-18 23:10:10 +0000470 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begeman8a997642008-05-09 06:41:27 +0000471
472 // This flag determines whether or not the component is to be treated as a
473 // special name, or a regular GLSL-style component access.
474 bool SpecialComponent = false;
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000475
476 // The vector accessor can't exceed the number of elements.
477 const char *compStr = CompName.getName();
478 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000479 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000480 baseType.getAsString(), SourceRange(CompLoc));
481 return QualType();
482 }
Nate Begeman8a997642008-05-09 06:41:27 +0000483
484 // Check that we've found one of the special components, or that the component
485 // names must come from the same set.
486 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
487 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
488 SpecialComponent = true;
489 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner88dca042007-08-02 22:33:49 +0000490 do
491 compStr++;
492 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
493 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
494 do
495 compStr++;
496 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
497 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
498 do
499 compStr++;
500 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
501 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000502
Nate Begeman8a997642008-05-09 06:41:27 +0000503 if (!SpecialComponent && *compStr) {
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000504 // We didn't get to the end of the string. This means the component names
505 // didn't come from the same set *or* we encountered an illegal name.
Nate Begeman213541a2008-04-18 23:10:10 +0000506 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000507 std::string(compStr,compStr+1), SourceRange(CompLoc));
508 return QualType();
509 }
510 // Each component accessor can't exceed the vector type.
511 compStr = CompName.getName();
512 while (*compStr) {
513 if (vecType->isAccessorWithinNumElements(*compStr))
514 compStr++;
515 else
516 break;
517 }
Nate Begeman8a997642008-05-09 06:41:27 +0000518 if (!SpecialComponent && *compStr) {
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000519 // We didn't get to the end of the string. This means a component accessor
520 // exceeds the number of elements in the vector.
Nate Begeman213541a2008-04-18 23:10:10 +0000521 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000522 baseType.getAsString(), SourceRange(CompLoc));
523 return QualType();
524 }
Nate Begeman8a997642008-05-09 06:41:27 +0000525
526 // If we have a special component name, verify that the current vector length
527 // is an even number, since all special component names return exactly half
528 // the elements.
529 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
530 return QualType();
531 }
532
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000533 // The component accessor looks fine - now we need to compute the actual type.
534 // The vector type is implied by the component accessor. For example,
535 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begeman8a997642008-05-09 06:41:27 +0000536 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
537 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
538 : strlen(CompName.getName());
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000539 if (CompSize == 1)
540 return vecType->getElementType();
Steve Naroffbea0b342007-07-29 16:33:31 +0000541
Nate Begeman213541a2008-04-18 23:10:10 +0000542 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroffbea0b342007-07-29 16:33:31 +0000543 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begeman213541a2008-04-18 23:10:10 +0000544 // diagostics look bad. We want extended vector types to appear built-in.
545 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
546 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
547 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroffbea0b342007-07-29 16:33:31 +0000548 }
549 return VT; // should never get here (a typedef type should always be found).
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000550}
551
Reid Spencer5f016e22007-07-11 17:01:13 +0000552Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000553ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 tok::TokenKind OpKind, SourceLocation MemberLoc,
555 IdentifierInfo &Member) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000556 Expr *BaseExpr = static_cast<Expr *>(Base);
557 assert(BaseExpr && "no record expression");
Steve Naroff3cc4af82007-12-16 21:42:28 +0000558
559 // Perform default conversions.
560 DefaultFunctionArrayConversion(BaseExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000561
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000562 QualType BaseType = BaseExpr->getType();
563 assert(!BaseType.isNull() && "no type for member expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 if (OpKind == tok::arrow) {
Chris Lattnerbefee482007-07-31 16:53:04 +0000566 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000567 BaseType = PT->getPointeeType();
568 else
569 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
570 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 }
Nate Begeman213541a2008-04-18 23:10:10 +0000572 // The base type is either a record or an ExtVectorType.
Chris Lattnerc8629632007-07-31 19:29:30 +0000573 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000574 RecordDecl *RDecl = RTy->getDecl();
575 if (RTy->isIncompleteType())
576 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
577 BaseExpr->getSourceRange());
578 // The record definition is complete, now make sure the member is valid.
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000579 FieldDecl *MemberDecl = RDecl->getMember(&Member);
580 if (!MemberDecl)
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000581 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
582 SourceRange(MemberLoc));
Eli Friedman51019072008-02-06 22:48:16 +0000583
584 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedman64ec0cc2008-02-07 05:24:51 +0000585 // FIXME: Handle address space modifiers
Eli Friedman51019072008-02-06 22:48:16 +0000586 QualType MemberType = MemberDecl->getType();
587 unsigned combinedQualifiers =
Chris Lattnerf46699c2008-02-20 20:55:12 +0000588 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman51019072008-02-06 22:48:16 +0000589 MemberType = MemberType.getQualifiedType(combinedQualifiers);
590
591 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
592 MemberLoc, MemberType);
Nate Begeman213541a2008-04-18 23:10:10 +0000593 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff608e0ee2007-08-03 22:40:33 +0000594 // Component access limited to variables (reject vec4.rg.g).
Nate Begeman8a997642008-05-09 06:41:27 +0000595 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
596 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begeman213541a2008-04-18 23:10:10 +0000597 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff608e0ee2007-08-03 22:40:33 +0000598 SourceRange(MemberLoc));
Nate Begeman213541a2008-04-18 23:10:10 +0000599 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000600 if (ret.isNull())
601 return true;
Nate Begeman213541a2008-04-18 23:10:10 +0000602 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000603 } else if (BaseType->isObjCInterfaceType()) {
604 ObjCInterfaceDecl *IFace;
605 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
606 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000607 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000608 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
609 ObjCInterfaceDecl *clsDeclared;
610 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000611 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
612 OpKind==tok::arrow);
Steve Naroffae784072008-05-30 00:40:33 +0000613 } else if (isObjCObjectPointerType(BaseType)) {
614 PointerType *pointerType = static_cast<PointerType*>(BaseType.getTypePtr());
615 BaseType = pointerType->getPointeeType();
616 ObjCInterfaceDecl *IFace;
617 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
618 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
619 else
620 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
621 ObjCInterfaceDecl *clsDeclared;
622 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
623 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
624 OpKind==tok::arrow);
625 // Check for properties.
626 if (OpKind==tok::period) {
627 // Before we look for explicit property declarations, we check for
628 // nullary methods (which allow '.' notation).
629 Selector Sel = PP.getSelectorTable().getNullarySelector(&Member);
630 ObjCMethodDecl *MD = IFace->lookupInstanceMethod(Sel);
631 if (MD)
632 return new ObjCPropertyRefExpr(MD, MD->getResultType(),
633 MemberLoc, BaseExpr);
634 // FIXME: Need to deal with setter methods that take 1 argument. E.g.:
635 // @interface NSBundle : NSObject {}
636 // - (NSString *)bundlePath;
637 // - (void)setBundlePath:(NSString *)x;
638 // @end
639 // void someMethod() { frameworkBundle.bundlePath = 0; }
640 //
641 // FIXME: lookup explicit properties...
642 }
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000643 }
644 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
645 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000646}
647
Steve Narofff69936d2007-09-16 03:34:24 +0000648/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +0000649/// This provides the location of the left/right parens and a list of comma
650/// locations.
651Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000652ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner925e60d2007-12-28 05:29:59 +0000653 ExprTy **args, unsigned NumArgs,
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner74c469f2007-07-21 03:03:59 +0000655 Expr *Fn = static_cast<Expr *>(fn);
656 Expr **Args = reinterpret_cast<Expr**>(args);
657 assert(Fn && "no function call expression");
Chris Lattner04421082008-04-08 04:40:51 +0000658 FunctionDecl *FDecl = NULL;
Chris Lattner04421082008-04-08 04:40:51 +0000659
660 // Promote the function operand.
661 UsualUnaryConversions(Fn);
662
663 // If we're directly calling a function, get the declaration for
664 // that function.
665 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
666 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
667 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
668
Chris Lattner925e60d2007-12-28 05:29:59 +0000669 // Make the call expr early, before semantic checks. This guarantees cleanup
670 // of arguments and function on error.
Chris Lattner8123a952008-04-10 02:22:51 +0000671 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner925e60d2007-12-28 05:29:59 +0000672 Context.BoolTy, RParenLoc));
673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
675 // type pointer to function".
Chris Lattner925e60d2007-12-28 05:29:59 +0000676 const PointerType *PT = Fn->getType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 if (PT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000678 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
679 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000680 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
681 if (FuncT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000682 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
683 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000684
685 // We know the result type of the call, set it.
686 TheCall->setType(FuncT->getResultType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000687
Chris Lattner925e60d2007-12-28 05:29:59 +0000688 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
690 // assignment, to the types of the corresponding parameter, ...
Chris Lattner925e60d2007-12-28 05:29:59 +0000691 unsigned NumArgsInProto = Proto->getNumArgs();
692 unsigned NumArgsToCheck = NumArgs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000693
Chris Lattner04421082008-04-08 04:40:51 +0000694 // If too few arguments are available (and we don't have default
695 // arguments for the remaining parameters), don't make the call.
696 if (NumArgs < NumArgsInProto) {
Chris Lattner8123a952008-04-10 02:22:51 +0000697 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner04421082008-04-08 04:40:51 +0000698 // Use default arguments for missing arguments
699 NumArgsToCheck = NumArgsInProto;
Chris Lattner8123a952008-04-10 02:22:51 +0000700 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner04421082008-04-08 04:40:51 +0000701 } else
702 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
703 Fn->getSourceRange());
704 }
705
Chris Lattner925e60d2007-12-28 05:29:59 +0000706 // If too many are passed and not variadic, error on the extras and drop
707 // them.
708 if (NumArgs > NumArgsInProto) {
709 if (!Proto->isVariadic()) {
Chris Lattnerd472b312007-07-21 03:09:58 +0000710 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000711 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnerd472b312007-07-21 03:09:58 +0000712 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner925e60d2007-12-28 05:29:59 +0000713 Args[NumArgs-1]->getLocEnd()));
714 // This deletes the extra arguments.
715 TheCall->setNumArgs(NumArgsInProto);
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 }
717 NumArgsToCheck = NumArgsInProto;
718 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000719
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 // Continue to check argument types (even if we have too few/many args).
Chris Lattner925e60d2007-12-28 05:29:59 +0000721 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner5cf216b2008-01-04 18:04:52 +0000722 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner04421082008-04-08 04:40:51 +0000723
724 Expr *Arg;
725 if (i < NumArgs)
726 Arg = Args[i];
727 else
728 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner5cf216b2008-01-04 18:04:52 +0000729 QualType ArgType = Arg->getType();
Steve Naroff700204c2007-07-24 21:46:40 +0000730
Chris Lattner925e60d2007-12-28 05:29:59 +0000731 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000732 AssignConvertType ConvTy =
733 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner925e60d2007-12-28 05:29:59 +0000734 TheCall->setArg(i, Arg);
735
Chris Lattner5cf216b2008-01-04 18:04:52 +0000736 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
737 ArgType, Arg, "passing"))
738 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000740
741 // If this is a variadic call, handle args passed through "...".
742 if (Proto->isVariadic()) {
Steve Naroffb291ab62007-08-28 23:30:39 +0000743 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner925e60d2007-12-28 05:29:59 +0000744 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
745 Expr *Arg = Args[i];
746 DefaultArgumentPromotion(Arg);
747 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000748 }
Steve Naroffb291ab62007-08-28 23:30:39 +0000749 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000750 } else {
751 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
752
Steve Naroffb291ab62007-08-28 23:30:39 +0000753 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner925e60d2007-12-28 05:29:59 +0000754 for (unsigned i = 0; i != NumArgs; i++) {
755 Expr *Arg = Args[i];
756 DefaultArgumentPromotion(Arg);
757 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000758 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000760
Chris Lattner59907c42007-08-10 20:18:51 +0000761 // Do special checking on direct calls to functions.
Eli Friedmand38617c2008-05-14 19:38:39 +0000762 if (FDecl)
763 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattner59907c42007-08-10 20:18:51 +0000764
Chris Lattner925e60d2007-12-28 05:29:59 +0000765 return TheCall.take();
Reid Spencer5f016e22007-07-11 17:01:13 +0000766}
767
768Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000769ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroffaff1edd2007-07-19 21:32:11 +0000770 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofff69936d2007-09-16 03:34:24 +0000771 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Naroff4aa88f82007-07-19 01:06:55 +0000772 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroffaff1edd2007-07-19 21:32:11 +0000773 // FIXME: put back this assert when initializers are worked out.
Steve Narofff69936d2007-09-16 03:34:24 +0000774 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroffaff1edd2007-07-19 21:32:11 +0000775 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlssond35c8322007-12-05 07:24:19 +0000776
Eli Friedman6223c222008-05-20 05:22:08 +0000777 if (literalType->isArrayType()) {
778 if (literalType->getAsVariableArrayType())
779 return Diag(LParenLoc,
780 diag::err_variable_object_no_init,
781 SourceRange(LParenLoc,
782 literalExpr->getSourceRange().getEnd()));
783 } else if (literalType->isIncompleteType()) {
784 return Diag(LParenLoc,
785 diag::err_typecheck_decl_incomplete_type,
786 literalType.getAsString(),
787 SourceRange(LParenLoc,
788 literalExpr->getSourceRange().getEnd()));
789 }
790
Steve Naroffd0091aa2008-01-10 22:15:12 +0000791 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff58d18212008-01-09 20:58:06 +0000792 return true;
Steve Naroffe9b12192008-01-14 18:19:28 +0000793
794 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
795 if (isFileScope) { // 6.5.2.5p3
Steve Naroffd0091aa2008-01-10 22:15:12 +0000796 if (CheckForConstantInitializer(literalExpr, literalType))
797 return true;
798 }
Steve Naroffe9b12192008-01-14 18:19:28 +0000799 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000800}
801
802Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000803ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000804 SourceLocation RBraceLoc) {
Steve Narofff0090632007-09-02 02:04:30 +0000805 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000806
Steve Naroff08d92e42007-09-15 18:49:24 +0000807 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroffd35005e2007-09-03 01:24:23 +0000808 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000809
Chris Lattnerf0467b32008-04-02 04:24:33 +0000810 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
811 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
812 return E;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000813}
814
Chris Lattnerfe23e212007-12-20 00:44:32 +0000815bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssona64db8f2007-11-27 05:51:55 +0000816 assert(VectorTy->isVectorType() && "Not a vector type!");
817
818 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner98be4942008-03-05 18:54:05 +0000819 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssona64db8f2007-11-27 05:51:55 +0000820 return Diag(R.getBegin(),
821 Ty->isVectorType() ?
822 diag::err_invalid_conversion_between_vectors :
823 diag::err_invalid_conversion_between_vector_and_integer,
824 VectorTy.getAsString().c_str(),
825 Ty.getAsString().c_str(), R);
826 } else
827 return Diag(R.getBegin(),
828 diag::err_invalid_conversion_between_vector_and_scalar,
829 VectorTy.getAsString().c_str(),
830 Ty.getAsString().c_str(), R);
831
832 return false;
833}
834
Steve Naroff4aa88f82007-07-19 01:06:55 +0000835Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000836ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 SourceLocation RParenLoc, ExprTy *Op) {
Steve Narofff69936d2007-09-16 03:34:24 +0000838 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff16beff82007-07-16 23:25:18 +0000839
840 Expr *castExpr = static_cast<Expr*>(Op);
841 QualType castType = QualType::getFromOpaquePtr(Ty);
842
Steve Naroff711602b2007-08-31 00:32:44 +0000843 UsualUnaryConversions(castExpr);
844
Chris Lattner75af4802007-07-18 16:00:06 +0000845 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
846 // type needs to be scalar.
Chris Lattner3da2db42007-10-29 04:26:44 +0000847 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff63564b82008-06-03 12:56:35 +0000848 if (!castType->isScalarType() && !castType->isVectorType()) {
849 // GCC struct/union extension.
850 if (castType == castExpr->getType() &&
Steve Naroff0326e042008-06-03 13:21:30 +0000851 castType->isStructureType() || castType->isUnionType()) {
852 Diag(LParenLoc, diag::ext_typecheck_cast_nonscalar,
853 SourceRange(LParenLoc, RParenLoc));
854 return new CastExpr(castType, castExpr, LParenLoc);
855 } else
Steve Naroff63564b82008-06-03 12:56:35 +0000856 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
857 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
858 }
Steve Naroff9412d682008-01-24 22:55:05 +0000859 if (!castExpr->getType()->isScalarType() &&
860 !castExpr->getType()->isVectorType())
Chris Lattner3da2db42007-10-29 04:26:44 +0000861 return Diag(castExpr->getLocStart(),
862 diag::err_typecheck_expect_scalar_operand,
863 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssona64db8f2007-11-27 05:51:55 +0000864
865 if (castExpr->getType()->isVectorType()) {
866 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
867 castExpr->getType(), castType))
868 return true;
869 } else if (castType->isVectorType()) {
870 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
871 castType, castExpr->getType()))
872 return true;
Chris Lattner3da2db42007-10-29 04:26:44 +0000873 }
Steve Naroff16beff82007-07-16 23:25:18 +0000874 }
875 return new CastExpr(castType, castExpr, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000876}
877
Chris Lattnera21ddb32007-11-26 01:40:58 +0000878/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
879/// In that case, lex = cond.
Reid Spencer5f016e22007-07-11 17:01:13 +0000880inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff49b45262007-07-13 16:58:59 +0000881 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000882 UsualUnaryConversions(cond);
883 UsualUnaryConversions(lex);
884 UsualUnaryConversions(rex);
885 QualType condT = cond->getType();
886 QualType lexT = lex->getType();
887 QualType rexT = rex->getType();
888
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 // first, check the condition.
Steve Naroff49b45262007-07-13 16:58:59 +0000890 if (!condT->isScalarType()) { // C99 6.5.15p2
891 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
892 condT.getAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 return QualType();
894 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000895
896 // Now check the two expressions.
897
898 // If both operands have arithmetic type, do the usual arithmetic conversions
899 // to find a common type: C99 6.5.15p3,5.
900 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffa4332e22007-07-17 00:58:39 +0000901 UsualArithmeticConversions(lex, rex);
902 return lex->getType();
903 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000904
905 // If both operands are the same structure or union type, the result is that
906 // type.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000907 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner70d67a92008-01-06 22:42:25 +0000908 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattnera21ddb32007-11-26 01:40:58 +0000909 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner70d67a92008-01-06 22:42:25 +0000910 // "If both the operands have structure or union type, the result has
911 // that type." This implies that CV qualifiers are dropped.
912 return lexT.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000914
915 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroffe701c0a2008-05-12 21:44:38 +0000916 // The following || allows only one side to be void (a GCC-ism).
917 if (lexT->isVoidType() || rexT->isVoidType()) {
918 if (!lexT->isVoidType())
919 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
920 rex->getSourceRange());
921 if (!rexT->isVoidType())
922 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
923 lex->getSourceRange());
Chris Lattner70d67a92008-01-06 22:42:25 +0000924 return lexT.getUnqualifiedType();
Steve Naroffe701c0a2008-05-12 21:44:38 +0000925 }
Steve Naroffb6d54e52008-01-08 01:11:38 +0000926 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
927 // the type of the other operand."
928 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000929 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000930 return lexT;
931 }
932 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000933 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000934 return rexT;
935 }
Chris Lattnerbd57d362008-01-06 22:50:31 +0000936 // Handle the case where both operands are pointers before we handle null
937 // pointer constants in case both operands are null pointer constants.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000938 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
939 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
940 // get the "pointed to" types
941 QualType lhptee = LHSPT->getPointeeType();
942 QualType rhptee = RHSPT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000943
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000944 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
945 if (lhptee->isVoidType() &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000946 rhptee->isIncompleteOrObjectType()) {
Chris Lattnerf46699c2008-02-20 20:55:12 +0000947 // Figure out necessary qualifiers (C99 6.5.15p6)
948 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmana541d532008-02-10 22:59:36 +0000949 QualType destType = Context.getPointerType(destPointee);
950 ImpCastExprToType(lex, destType); // add qualifiers if necessary
951 ImpCastExprToType(rex, destType); // promote to void*
952 return destType;
953 }
Chris Lattnerd805bec2008-04-02 06:59:01 +0000954 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattnerf46699c2008-02-20 20:55:12 +0000955 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmana541d532008-02-10 22:59:36 +0000956 QualType destType = Context.getPointerType(destPointee);
957 ImpCastExprToType(lex, destType); // add qualifiers if necessary
958 ImpCastExprToType(rex, destType); // promote to void*
959 return destType;
960 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000961
Steve Naroffec0550f2007-10-15 20:41:53 +0000962 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
963 rhptee.getUnqualifiedType())) {
Steve Naroffc0ff1ca2008-02-01 22:44:48 +0000964 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000965 lexT.getAsString(), rexT.getAsString(),
966 lex->getSourceRange(), rex->getSourceRange());
Eli Friedmanb1284ac2008-01-30 17:02:03 +0000967 // In this situation, we assume void* type. No especially good
968 // reason, but this is what gcc does, and we do have to pick
969 // to get a consistent AST.
970 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
971 ImpCastExprToType(lex, voidPtrTy);
972 ImpCastExprToType(rex, voidPtrTy);
973 return voidPtrTy;
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000974 }
975 // The pointer types are compatible.
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000976 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
977 // differently qualified versions of compatible types, the result type is
978 // a pointer to an appropriately qualified version of the *composite*
979 // type.
Eli Friedman5835ea22008-05-16 20:37:07 +0000980 // FIXME: Need to calculate the composite type.
Eli Friedmana541d532008-02-10 22:59:36 +0000981 // FIXME: Need to add qualifiers
Eli Friedman5835ea22008-05-16 20:37:07 +0000982 QualType compositeType = lexT;
983 ImpCastExprToType(lex, compositeType);
984 ImpCastExprToType(rex, compositeType);
985 return compositeType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 }
987 }
Steve Naroffaa73eec2008-05-31 22:33:45 +0000988 // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
989 // evaluates to "struct objc_object *" (and is handled above when comparing
990 // id with statically typed objects). FIXME: Do we need an ImpCastExprToType?
991 if (lexT->isObjCQualifiedIdType() || rexT->isObjCQualifiedIdType()) {
992 if (ObjCQualifiedIdTypesAreCompatible(lexT, rexT, true))
993 return Context.getObjCIdType();
994 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000995 // Otherwise, the operands are not compatible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff49b45262007-07-13 16:58:59 +0000997 lexT.getAsString(), rexT.getAsString(),
998 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 return QualType();
1000}
1001
Steve Narofff69936d2007-09-16 03:34:24 +00001002/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Reid Spencer5f016e22007-07-11 17:01:13 +00001003/// in the case of a the GNU conditional expr extension.
Steve Narofff69936d2007-09-16 03:34:24 +00001004Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 SourceLocation ColonLoc,
1006 ExprTy *Cond, ExprTy *LHS,
1007 ExprTy *RHS) {
Chris Lattner26824902007-07-16 21:39:03 +00001008 Expr *CondExpr = (Expr *) Cond;
1009 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattnera21ddb32007-11-26 01:40:58 +00001010
1011 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
1012 // was the condition.
1013 bool isLHSNull = LHSExpr == 0;
1014 if (isLHSNull)
1015 LHSExpr = CondExpr;
1016
Chris Lattner26824902007-07-16 21:39:03 +00001017 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
1018 RHSExpr, QuestionLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 if (result.isNull())
1020 return true;
Chris Lattnera21ddb32007-11-26 01:40:58 +00001021 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
1022 RHSExpr, result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001023}
1024
Steve Naroffb291ab62007-08-28 23:30:39 +00001025/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffb3c2b882008-01-29 02:42:22 +00001026/// do not have a prototype. Arguments that have type float are promoted to
1027/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner925e60d2007-12-28 05:29:59 +00001028void Sema::DefaultArgumentPromotion(Expr *&Expr) {
1029 QualType Ty = Expr->getType();
1030 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffb291ab62007-08-28 23:30:39 +00001031
Chris Lattner925e60d2007-12-28 05:29:59 +00001032 if (Ty == Context.FloatTy)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001033 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffb3c2b882008-01-29 02:42:22 +00001034 else
1035 UsualUnaryConversions(Expr);
Steve Naroffb291ab62007-08-28 23:30:39 +00001036}
1037
Steve Narofffa2eaab2007-07-15 02:02:06 +00001038/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattnerf0467b32008-04-02 04:24:33 +00001039void Sema::DefaultFunctionArrayConversion(Expr *&E) {
1040 QualType Ty = E->getType();
1041 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendling08ad47c2007-07-17 03:52:31 +00001042
Chris Lattnerf0467b32008-04-02 04:24:33 +00001043 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattner423a3c92008-04-02 17:45:06 +00001044 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattnerf0467b32008-04-02 04:24:33 +00001045 Ty = E->getType();
Bill Wendlingea5e79f2007-07-17 04:16:47 +00001046 }
Chris Lattnerf0467b32008-04-02 04:24:33 +00001047 if (Ty->isFunctionType())
1048 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattnere6327742008-04-02 05:18:44 +00001049 else if (Ty->isArrayType())
1050 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Reid Spencer5f016e22007-07-11 17:01:13 +00001051}
1052
Nate Begemane2ce1d92008-01-17 17:46:27 +00001053/// UsualUnaryConversions - Performs various conversions that are common to most
Reid Spencer5f016e22007-07-11 17:01:13 +00001054/// operators (C99 6.3). The conversions of array and function types are
1055/// sometimes surpressed. For example, the array->pointer conversion doesn't
1056/// apply if the array is an argument to the sizeof or address (&) operators.
1057/// In these instances, this routine should *not* be called.
Chris Lattner925e60d2007-12-28 05:29:59 +00001058Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
1059 QualType Ty = Expr->getType();
1060 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001061
Chris Lattner925e60d2007-12-28 05:29:59 +00001062 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattner423a3c92008-04-02 17:45:06 +00001063 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner925e60d2007-12-28 05:29:59 +00001064 Ty = Expr->getType();
Bill Wendlingea5e79f2007-07-17 04:16:47 +00001065 }
Chris Lattner925e60d2007-12-28 05:29:59 +00001066 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattner1e0a3902008-01-16 19:17:22 +00001067 ImpCastExprToType(Expr, Context.IntTy);
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001068 else
Chris Lattner925e60d2007-12-28 05:29:59 +00001069 DefaultFunctionArrayConversion(Expr);
1070
1071 return Expr;
Reid Spencer5f016e22007-07-11 17:01:13 +00001072}
1073
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001074/// UsualArithmeticConversions - Performs various conversions that are common to
Reid Spencer5f016e22007-07-11 17:01:13 +00001075/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
1076/// routine returns the first non-arithmetic type found. The client is
1077/// responsible for emitting appropriate error diagnostics.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001078/// FIXME: verify the conversion rules for "complex int" are consistent with
1079/// GCC.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001080QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
1081 bool isCompAssign) {
Steve Naroff8702a0f2007-08-25 19:54:59 +00001082 if (!isCompAssign) {
1083 UsualUnaryConversions(lhsExpr);
1084 UsualUnaryConversions(rhsExpr);
1085 }
Steve Naroff3187e202007-10-18 18:55:53 +00001086 // For conversion purposes, we ignore any qualifiers.
1087 // For example, "const float" and "float" are equivalent.
Steve Narofff68a63f2007-11-10 19:45:54 +00001088 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
1089 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001090
1091 // If both types are identical, no conversion is needed.
Steve Naroff3187e202007-10-18 18:55:53 +00001092 if (lhs == rhs)
1093 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001094
1095 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1096 // The caller can deal with this (e.g. pointer + int).
Steve Naroffa4332e22007-07-17 00:58:39 +00001097 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001098 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001099
1100 // At this point, we have two different arithmetic types.
1101
1102 // Handle complex types first (C99 6.3.1.8p1).
1103 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff02f62a92008-01-15 19:36:10 +00001104 // if we have an integer operand, the result is the complex type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001105 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001106 // convert the rhs to the lhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001107 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001108 return lhs;
Steve Naroff02f62a92008-01-15 19:36:10 +00001109 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001110 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001111 // convert the lhs to the rhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001112 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001113 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001114 }
Steve Narofff1448a02007-08-27 01:27:54 +00001115 // This handles complex/complex, complex/float, or float/complex.
1116 // When both operands are complex, the shorter operand is converted to the
1117 // type of the longer, and that is the type of the result. This corresponds
1118 // to what is done when combining two real floating-point operands.
1119 // The fun begins when size promotion occur across type domains.
1120 // From H&S 6.3.4: When one operand is complex and the other is a real
1121 // floating-point type, the less precise type is converted, within it's
1122 // real or complex domain, to the precision of the other type. For example,
1123 // when combining a "long double" with a "double _Complex", the
1124 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnera75cea32008-04-06 23:38:49 +00001125 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Narofffb0d4962007-08-27 15:30:22 +00001126
1127 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff55fe4552007-08-27 21:32:55 +00001128 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1129 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001130 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff55fe4552007-08-27 21:32:55 +00001131 } else if (result < 0) { // The right side is bigger, convert lhs.
1132 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1133 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001134 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff55fe4552007-08-27 21:32:55 +00001135 }
1136 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1137 // domains match. This is a requirement for our implementation, C99
1138 // does not require this promotion.
1139 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1140 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff29960362007-08-27 21:43:43 +00001141 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001142 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff29960362007-08-27 21:43:43 +00001143 return rhs;
Steve Naroff55fe4552007-08-27 21:32:55 +00001144 } else { // handle "_Complex double, double".
Steve Naroff29960362007-08-27 21:43:43 +00001145 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001146 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff29960362007-08-27 21:43:43 +00001147 return lhs;
Steve Naroff55fe4552007-08-27 21:32:55 +00001148 }
Steve Naroffa4332e22007-07-17 00:58:39 +00001149 }
Steve Naroff29960362007-08-27 21:43:43 +00001150 return lhs; // The domain/size match exactly.
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 // Now handle "real" floating types (i.e. float, double, long double).
1153 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1154 // if we have an integer operand, the result is the real floating type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001155 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001156 // convert rhs to the lhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001157 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001158 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001159 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001160 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001161 // convert lhs to the rhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001162 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001163 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001164 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001165 // We have two real floating types, float/complex combos were handled above.
1166 // Convert the smaller operand to the bigger result.
Chris Lattnera75cea32008-04-06 23:38:49 +00001167 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Narofffb0d4962007-08-27 15:30:22 +00001168
1169 if (result > 0) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001170 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001171 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001172 }
Steve Narofffb0d4962007-08-27 15:30:22 +00001173 if (result < 0) { // convert the lhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001174 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Narofffb0d4962007-08-27 15:30:22 +00001175 return rhs;
1176 }
1177 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 }
Steve Naroff02f62a92008-01-15 19:36:10 +00001179 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1180 // Handle GCC complex int extension.
Steve Naroff02f62a92008-01-15 19:36:10 +00001181 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001182 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff02f62a92008-01-15 19:36:10 +00001183
Eli Friedman8e54ad02008-02-08 01:19:44 +00001184 if (lhsComplexInt && rhsComplexInt) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001185 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1186 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman8c4e5db2008-02-08 01:24:30 +00001187 // convert the rhs
1188 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1189 return lhs;
Eli Friedman8e54ad02008-02-08 01:19:44 +00001190 }
1191 if (!isCompAssign)
Eli Friedman8c4e5db2008-02-08 01:24:30 +00001192 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman8e54ad02008-02-08 01:19:44 +00001193 return rhs;
1194 } else if (lhsComplexInt && rhs->isIntegerType()) {
1195 // convert the rhs to the lhs complex type.
1196 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1197 return lhs;
1198 } else if (rhsComplexInt && lhs->isIntegerType()) {
1199 // convert the lhs to the rhs complex type.
1200 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1201 return rhs;
1202 }
Steve Naroff02f62a92008-01-15 19:36:10 +00001203 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001204 // Finally, we have two differing integer types.
Chris Lattner7cfeb082008-04-06 23:55:33 +00001205 if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001206 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001207 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001208 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001209 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001210 return rhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001211}
1212
1213// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1214// being closely modeled after the C99 spec:-). The odd characteristic of this
1215// routine is it effectively iqnores the qualifiers on the top level pointee.
1216// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1217// FIXME: add a couple examples in this comment.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001218Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001219Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1220 QualType lhptee, rhptee;
1221
1222 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner2dcb6bb2007-07-31 21:27:01 +00001223 lhptee = lhsType->getAsPointerType()->getPointeeType();
1224 rhptee = rhsType->getAsPointerType()->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001225
1226 // make sure we operate on the canonical type
1227 lhptee = lhptee.getCanonicalType();
1228 rhptee = rhptee.getCanonicalType();
1229
Chris Lattner5cf216b2008-01-04 18:04:52 +00001230 AssignConvertType ConvTy = Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001231
1232 // C99 6.5.16.1p1: This following citation is common to constraints
1233 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1234 // qualifiers of the type *pointed to* by the right;
Chris Lattnerf46699c2008-02-20 20:55:12 +00001235 // FIXME: Handle ASQualType
1236 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1237 rhptee.getCVRQualifiers())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001238 ConvTy = CompatiblePointerDiscardsQualifiers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001239
1240 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1241 // incomplete type and the other is a pointer to a qualified or unqualified
1242 // version of void...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001243 if (lhptee->isVoidType()) {
Chris Lattnerd805bec2008-04-02 06:59:01 +00001244 if (rhptee->isIncompleteOrObjectType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001245 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001246
1247 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerd805bec2008-04-02 06:59:01 +00001248 assert(rhptee->isFunctionType());
1249 return FunctionVoidPointer;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001250 }
1251
1252 if (rhptee->isVoidType()) {
Chris Lattnerd805bec2008-04-02 06:59:01 +00001253 if (lhptee->isIncompleteOrObjectType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001254 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001255
1256 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerd805bec2008-04-02 06:59:01 +00001257 assert(lhptee->isFunctionType());
1258 return FunctionVoidPointer;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001259 }
1260
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1262 // unqualified versions of compatible types, ...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001263 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1264 rhptee.getUnqualifiedType()))
1265 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner5cf216b2008-01-04 18:04:52 +00001266 return ConvTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001267}
1268
1269/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1270/// has code to accommodate several GCC extensions when type checking
1271/// pointers. Here are some objectionable examples that GCC considers warnings:
1272///
1273/// int a, *pint;
1274/// short *pshort;
1275/// struct foo *pfoo;
1276///
1277/// pint = pshort; // warning: assignment from incompatible pointer type
1278/// a = pint; // warning: assignment makes integer from pointer without a cast
1279/// pint = a; // warning: assignment makes pointer from integer without a cast
1280/// pint = pfoo; // warning: assignment from incompatible pointer type
1281///
1282/// As a result, the code for dealing with pointers is more complex than the
1283/// C99 spec dictates.
Reid Spencer5f016e22007-07-11 17:01:13 +00001284///
Chris Lattner5cf216b2008-01-04 18:04:52 +00001285Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001286Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnerfc144e22008-01-04 23:18:45 +00001287 // Get canonical types. We're not formatting these types, just comparing
1288 // them.
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001289 lhsType = lhsType.getCanonicalType().getUnqualifiedType();
1290 rhsType = rhsType.getCanonicalType().getUnqualifiedType();
1291
1292 if (lhsType == rhsType)
Chris Lattnerd2656dd2008-01-07 17:51:46 +00001293 return Compatible; // Common case: fast path an exact match.
Steve Naroff700204c2007-07-24 21:46:40 +00001294
Anders Carlsson793680e2007-10-12 23:56:29 +00001295 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattner8f8fc7b2008-04-07 06:52:53 +00001296 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlsson793680e2007-10-12 23:56:29 +00001297 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001298 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001299 }
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001300
Chris Lattnereca7be62008-04-07 05:30:13 +00001301 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1302 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001303 return Compatible;
Steve Naroff20373222008-06-03 14:04:54 +00001304 // Relax integer conversions like we do for pointers below.
1305 if (rhsType->isIntegerType())
1306 return IntToPointer;
1307 if (lhsType->isIntegerType())
1308 return PointerToInt;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001309 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001310 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001311
Chris Lattner78eca282008-04-07 06:49:41 +00001312 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begeman213541a2008-04-18 23:10:10 +00001313 // For ExtVector, allow vector splats; float -> <n x float>
1314 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattnere8b3e962008-01-04 23:32:24 +00001315 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1316 return Compatible;
1317 }
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001318
Chris Lattnere8b3e962008-01-04 23:32:24 +00001319 // If LHS and RHS are both vectors of integer or both vectors of floating
1320 // point types, and the total vector length is the same, allow the
1321 // conversion. This is a bitcast; no bits are changed but the result type
1322 // is different.
1323 if (getLangOptions().LaxVectorConversions &&
1324 lhsType->isVectorType() && rhsType->isVectorType()) {
1325 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1326 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner98be4942008-03-05 18:54:05 +00001327 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begeman4119d1a2007-12-30 02:59:45 +00001328 return Compatible;
1329 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001330 }
1331 return Incompatible;
1332 }
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001333
Chris Lattnere8b3e962008-01-04 23:32:24 +00001334 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 return Compatible;
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001336
Chris Lattner78eca282008-04-07 06:49:41 +00001337 if (isa<PointerType>(lhsType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 if (rhsType->isIntegerType())
Chris Lattnerb7b61152008-01-04 18:22:42 +00001339 return IntToPointer;
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001340
Chris Lattner78eca282008-04-07 06:49:41 +00001341 if (isa<PointerType>(rhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001343 return Incompatible;
1344 }
1345
Chris Lattner78eca282008-04-07 06:49:41 +00001346 if (isa<PointerType>(rhsType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001348 if (lhsType == Context.BoolTy)
1349 return Compatible;
1350
1351 if (lhsType->isIntegerType())
Chris Lattnerb7b61152008-01-04 18:22:42 +00001352 return PointerToInt;
Reid Spencer5f016e22007-07-11 17:01:13 +00001353
Chris Lattner78eca282008-04-07 06:49:41 +00001354 if (isa<PointerType>(lhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001356 return Incompatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001357 }
Eli Friedmanf8f873d2008-05-30 18:07:22 +00001358
Chris Lattnerfc144e22008-01-04 23:18:45 +00001359 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner78eca282008-04-07 06:49:41 +00001360 if (Context.typesAreCompatible(lhsType, rhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001361 return Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 }
1363 return Incompatible;
1364}
1365
Chris Lattner5cf216b2008-01-04 18:04:52 +00001366Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001367Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff529a4ad2007-11-27 17:58:44 +00001368 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1369 // a null pointer constant.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001370 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian9d3185e2008-01-03 18:46:52 +00001371 && rExpr->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001372 ImpCastExprToType(rExpr, lhsType);
Steve Naroff529a4ad2007-11-27 17:58:44 +00001373 return Compatible;
1374 }
Chris Lattner943140e2007-10-16 02:55:40 +00001375 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroff90045e82007-07-13 23:32:42 +00001376 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff08d92e42007-09-15 18:49:24 +00001377 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroff90045e82007-07-13 23:32:42 +00001378 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner943140e2007-10-16 02:55:40 +00001379 //
1380 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1381 // are better understood.
1382 if (!lhsType->isReferenceType())
1383 DefaultFunctionArrayConversion(rExpr);
Steve Narofff1120de2007-08-24 22:33:52 +00001384
Chris Lattner5cf216b2008-01-04 18:04:52 +00001385 Sema::AssignConvertType result =
1386 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Narofff1120de2007-08-24 22:33:52 +00001387
1388 // C99 6.5.16.1p2: The value of the right operand is converted to the
1389 // type of the assignment expression.
1390 if (rExpr->getType() != lhsType)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001391 ImpCastExprToType(rExpr, lhsType);
Steve Narofff1120de2007-08-24 22:33:52 +00001392 return result;
Steve Naroff90045e82007-07-13 23:32:42 +00001393}
1394
Chris Lattner5cf216b2008-01-04 18:04:52 +00001395Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001396Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1397 return CheckAssignmentConstraints(lhsType, rhsType);
1398}
1399
Chris Lattnerca5eede2007-12-12 05:47:28 +00001400QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 Diag(loc, diag::err_typecheck_invalid_operands,
1402 lex->getType().getAsString(), rex->getType().getAsString(),
1403 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnerca5eede2007-12-12 05:47:28 +00001404 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001405}
1406
Steve Naroff49b45262007-07-13 16:58:59 +00001407inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1408 Expr *&rex) {
Nate Begeman1330b0e2008-04-04 01:30:25 +00001409 // For conversion purposes, we ignore any qualifiers.
1410 // For example, "const float" and "float" are equivalent.
1411 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1412 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001413
1414 // make sure the vector types are identical.
Nate Begeman1330b0e2008-04-04 01:30:25 +00001415 if (lhsType == rhsType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 return lhsType;
Nate Begeman4119d1a2007-12-30 02:59:45 +00001417
Nate Begeman213541a2008-04-18 23:10:10 +00001418 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begeman4119d1a2007-12-30 02:59:45 +00001419 // promote the rhs to the vector type.
Nate Begeman213541a2008-04-18 23:10:10 +00001420 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begeman4119d1a2007-12-30 02:59:45 +00001421 if (V->getElementType().getCanonicalType().getTypePtr()
1422 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001423 ImpCastExprToType(rex, lhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001424 return lhsType;
1425 }
1426 }
1427
Nate Begeman213541a2008-04-18 23:10:10 +00001428 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begeman4119d1a2007-12-30 02:59:45 +00001429 // promote the lhs to the vector type.
Nate Begeman213541a2008-04-18 23:10:10 +00001430 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begeman4119d1a2007-12-30 02:59:45 +00001431 if (V->getElementType().getCanonicalType().getTypePtr()
1432 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001433 ImpCastExprToType(lex, rhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001434 return rhsType;
1435 }
1436 }
1437
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 // You cannot convert between vector values of different size.
1439 Diag(loc, diag::err_typecheck_vector_not_convertable,
1440 lex->getType().getAsString(), rex->getType().getAsString(),
1441 lex->getSourceRange(), rex->getSourceRange());
1442 return QualType();
1443}
1444
1445inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001446 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001447{
Steve Naroff90045e82007-07-13 23:32:42 +00001448 QualType lhsType = lex->getType(), rhsType = rex->getType();
1449
1450 if (lhsType->isVectorType() || rhsType->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 return CheckVectorOperands(loc, lex, rex);
Steve Naroff49b45262007-07-13 16:58:59 +00001452
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001453 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001454
Steve Naroffa4332e22007-07-17 00:58:39 +00001455 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001456 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001457 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001458}
1459
1460inline QualType Sema::CheckRemainderOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001461 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001462{
Steve Naroff90045e82007-07-13 23:32:42 +00001463 QualType lhsType = lex->getType(), rhsType = rex->getType();
1464
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001465 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001466
Steve Naroffa4332e22007-07-17 00:58:39 +00001467 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001468 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001469 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001470}
1471
1472inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001473 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001474{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001475 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff49b45262007-07-13 16:58:59 +00001476 return CheckVectorOperands(loc, lex, rex);
1477
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001478 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedmand72d16e2008-05-18 18:08:51 +00001479
Reid Spencer5f016e22007-07-11 17:01:13 +00001480 // handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001481 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001482 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001483
Eli Friedmand72d16e2008-05-18 18:08:51 +00001484 // Put any potential pointer into PExp
1485 Expr* PExp = lex, *IExp = rex;
1486 if (IExp->getType()->isPointerType())
1487 std::swap(PExp, IExp);
1488
1489 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1490 if (IExp->getType()->isIntegerType()) {
1491 // Check for arithmetic on pointers to incomplete types
1492 if (!PTy->getPointeeType()->isObjectType()) {
1493 if (PTy->getPointeeType()->isVoidType()) {
1494 Diag(loc, diag::ext_gnu_void_ptr,
1495 lex->getSourceRange(), rex->getSourceRange());
1496 } else {
1497 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1498 lex->getType().getAsString(), lex->getSourceRange());
1499 return QualType();
1500 }
1501 }
1502 return PExp->getType();
1503 }
1504 }
1505
Chris Lattnerca5eede2007-12-12 05:47:28 +00001506 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001507}
1508
Chris Lattnereca7be62008-04-07 05:30:13 +00001509// C99 6.5.6
1510QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1511 SourceLocation loc, bool isCompAssign) {
Steve Naroff3e5e5562007-07-16 22:23:01 +00001512 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001514
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001515 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001516
Chris Lattner6e4ab612007-12-09 21:53:25 +00001517 // Enforce type constraints: C99 6.5.6p3.
1518
1519 // Handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001520 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001521 return compType;
Chris Lattner6e4ab612007-12-09 21:53:25 +00001522
1523 // Either ptr - int or ptr - ptr.
1524 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001525 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001526
Chris Lattner6e4ab612007-12-09 21:53:25 +00001527 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff2565eef2008-01-29 18:58:14 +00001528 if (!lpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001529 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001530 if (lpointee->isVoidType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001531 Diag(loc, diag::ext_gnu_void_ptr,
1532 lex->getSourceRange(), rex->getSourceRange());
1533 } else {
1534 Diag(loc, diag::err_typecheck_sub_ptr_object,
1535 lex->getType().getAsString(), lex->getSourceRange());
1536 return QualType();
1537 }
1538 }
1539
1540 // The result type of a pointer-int computation is the pointer type.
1541 if (rex->getType()->isIntegerType())
1542 return lex->getType();
Steve Naroff3e5e5562007-07-16 22:23:01 +00001543
Chris Lattner6e4ab612007-12-09 21:53:25 +00001544 // Handle pointer-pointer subtractions.
1545 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001546 QualType rpointee = RHSPTy->getPointeeType();
1547
Chris Lattner6e4ab612007-12-09 21:53:25 +00001548 // RHS must be an object type, unless void (GNU).
Steve Naroff2565eef2008-01-29 18:58:14 +00001549 if (!rpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001550 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001551 if (rpointee->isVoidType()) {
1552 if (!lpointee->isVoidType())
Chris Lattner6e4ab612007-12-09 21:53:25 +00001553 Diag(loc, diag::ext_gnu_void_ptr,
1554 lex->getSourceRange(), rex->getSourceRange());
1555 } else {
1556 Diag(loc, diag::err_typecheck_sub_ptr_object,
1557 rex->getType().getAsString(), rex->getSourceRange());
1558 return QualType();
1559 }
1560 }
1561
1562 // Pointee types must be compatible.
Steve Naroff2565eef2008-01-29 18:58:14 +00001563 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1564 rpointee.getUnqualifiedType())) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001565 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1566 lex->getType().getAsString(), rex->getType().getAsString(),
1567 lex->getSourceRange(), rex->getSourceRange());
1568 return QualType();
1569 }
1570
1571 return Context.getPointerDiffType();
1572 }
1573 }
1574
Chris Lattnerca5eede2007-12-12 05:47:28 +00001575 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001576}
1577
Chris Lattnereca7be62008-04-07 05:30:13 +00001578// C99 6.5.7
1579QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1580 bool isCompAssign) {
Chris Lattnerca5eede2007-12-12 05:47:28 +00001581 // C99 6.5.7p2: Each of the operands shall have integer type.
1582 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1583 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001584
Chris Lattnerca5eede2007-12-12 05:47:28 +00001585 // Shifts don't perform usual arithmetic conversions, they just do integer
1586 // promotions on each operand. C99 6.5.7p3
Chris Lattner1dcf2c82007-12-13 07:28:16 +00001587 if (!isCompAssign)
1588 UsualUnaryConversions(lex);
Chris Lattnerca5eede2007-12-12 05:47:28 +00001589 UsualUnaryConversions(rex);
1590
1591 // "The type of the result is that of the promoted left operand."
1592 return lex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001593}
1594
Chris Lattnereca7be62008-04-07 05:30:13 +00001595// C99 6.5.8
1596QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1597 bool isRelational) {
Chris Lattnera5937dd2007-08-26 01:18:55 +00001598 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff30bf7712007-08-10 18:26:40 +00001599 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1600 UsualArithmeticConversions(lex, rex);
1601 else {
1602 UsualUnaryConversions(lex);
1603 UsualUnaryConversions(rex);
1604 }
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001605 QualType lType = lex->getType();
1606 QualType rType = rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001607
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001608 // For non-floating point types, check for self-comparisons of the form
1609 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1610 // often indicate logic errors in the program.
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001611 if (!lType->isFloatingType()) {
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001612 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1613 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001614 if (DRL->getDecl() == DRR->getDecl())
1615 Diag(loc, diag::warn_selfcomparison);
1616 }
1617
Chris Lattnera5937dd2007-08-26 01:18:55 +00001618 if (isRelational) {
1619 if (lType->isRealType() && rType->isRealType())
1620 return Context.IntTy;
1621 } else {
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001622 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001623 if (lType->isFloatingType()) {
1624 assert (rType->isFloatingType());
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001625 CheckFloatComparison(loc,lex,rex);
Ted Kremenek6a261552007-10-29 16:40:01 +00001626 }
1627
Chris Lattnera5937dd2007-08-26 01:18:55 +00001628 if (lType->isArithmeticType() && rType->isArithmeticType())
1629 return Context.IntTy;
1630 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001631
Chris Lattnerd28f8152007-08-26 01:10:14 +00001632 bool LHSIsNull = lex->isNullPointerConstant(Context);
1633 bool RHSIsNull = rex->isNullPointerConstant(Context);
1634
Chris Lattnera5937dd2007-08-26 01:18:55 +00001635 // All of the following pointer related warnings are GCC extensions, except
1636 // when handling null pointer constants. One day, we can consider making them
1637 // errors (when -pedantic-errors is enabled).
Steve Naroff77878cc2007-08-27 04:08:11 +00001638 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattnerbc896f52008-04-03 05:07:25 +00001639 QualType LCanPointeeTy =
1640 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1641 QualType RCanPointeeTy =
1642 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001643
Steve Naroff66296cb2007-11-13 14:57:38 +00001644 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattnerbc896f52008-04-03 05:07:25 +00001645 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1646 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1647 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001648 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1649 lType.getAsString(), rType.getAsString(),
1650 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001652 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001653 return Context.IntTy;
1654 }
Steve Naroff20373222008-06-03 14:04:54 +00001655 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) {
1656 if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
1657 ImpCastExprToType(rex, lType);
1658 return Context.IntTy;
1659 }
Fariborz Jahanian7359f042007-12-20 01:06:58 +00001660 }
Steve Naroff20373222008-06-03 14:04:54 +00001661 if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
1662 rType->isIntegerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001663 if (!RHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001664 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1665 lType.getAsString(), rType.getAsString(),
1666 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001667 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001668 return Context.IntTy;
1669 }
Steve Naroff20373222008-06-03 14:04:54 +00001670 if (lType->isIntegerType() &&
1671 (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001672 if (!LHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001673 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1674 lType.getAsString(), rType.getAsString(),
1675 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001676 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001677 return Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 }
Chris Lattnerca5eede2007-12-12 05:47:28 +00001679 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001680}
1681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682inline QualType Sema::CheckBitwiseOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001683 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001684{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001685 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001687
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001688 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001689
Steve Naroffa4332e22007-07-17 00:58:39 +00001690 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001691 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001692 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001693}
1694
1695inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff49b45262007-07-13 16:58:59 +00001696 Expr *&lex, Expr *&rex, SourceLocation loc)
Reid Spencer5f016e22007-07-11 17:01:13 +00001697{
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001698 UsualUnaryConversions(lex);
1699 UsualUnaryConversions(rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001700
Eli Friedman5773a6c2008-05-13 20:16:47 +00001701 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 return Context.IntTy;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001703 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704}
1705
1706inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Narofff1120de2007-08-24 22:33:52 +00001707 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001708{
1709 QualType lhsType = lex->getType();
1710 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1712
1713 switch (mlval) { // C99 6.5.16p2
Chris Lattner5cf216b2008-01-04 18:04:52 +00001714 case Expr::MLV_Valid:
1715 break;
1716 case Expr::MLV_ConstQualified:
1717 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1718 return QualType();
1719 case Expr::MLV_ArrayType:
1720 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1721 lhsType.getAsString(), lex->getSourceRange());
1722 return QualType();
1723 case Expr::MLV_NotObjectType:
1724 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1725 lhsType.getAsString(), lex->getSourceRange());
1726 return QualType();
1727 case Expr::MLV_InvalidExpression:
1728 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1729 lex->getSourceRange());
1730 return QualType();
1731 case Expr::MLV_IncompleteType:
1732 case Expr::MLV_IncompleteVoidType:
1733 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1734 lhsType.getAsString(), lex->getSourceRange());
1735 return QualType();
1736 case Expr::MLV_DuplicateVectorComponents:
1737 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1738 lex->getSourceRange());
1739 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001741
Chris Lattner5cf216b2008-01-04 18:04:52 +00001742 AssignConvertType ConvTy;
1743 if (compoundType.isNull())
1744 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1745 else
1746 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1747
1748 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1749 rex, "assigning"))
1750 return QualType();
1751
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 // C99 6.5.16p3: The type of an assignment expression is the type of the
1753 // left operand unless the left operand has qualified type, in which case
1754 // it is the unqualified version of the type of the left operand.
1755 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1756 // is converted to the type of the assignment expression (above).
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001757 // C++ 5.17p1: the type of the assignment expression is that of its left
1758 // oprdu.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001759 return lhsType.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001760}
1761
1762inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff49b45262007-07-13 16:58:59 +00001763 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001764 UsualUnaryConversions(rex);
1765 return rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001766}
1767
Steve Naroff49b45262007-07-13 16:58:59 +00001768/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1769/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Reid Spencer5f016e22007-07-11 17:01:13 +00001770QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff49b45262007-07-13 16:58:59 +00001771 QualType resType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 assert(!resType.isNull() && "no type for increment/decrement expression");
1773
Steve Naroff084f9ed2007-08-24 17:20:07 +00001774 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffd848a382007-11-11 14:15:57 +00001775 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedmand72d16e2008-05-18 18:08:51 +00001776 if (pt->getPointeeType()->isVoidType()) {
1777 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1778 } else if (!pt->getPointeeType()->isObjectType()) {
1779 // C99 6.5.2.4p2, 6.5.6p2
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1781 resType.getAsString(), op->getSourceRange());
1782 return QualType();
1783 }
Steve Naroff084f9ed2007-08-24 17:20:07 +00001784 } else if (!resType->isRealType()) {
1785 if (resType->isComplexType())
1786 // C99 does not support ++/-- on complex types.
1787 Diag(OpLoc, diag::ext_integer_increment_complex,
1788 resType.getAsString(), op->getSourceRange());
1789 else {
1790 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1791 resType.getAsString(), op->getSourceRange());
1792 return QualType();
1793 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 }
Steve Naroffdd10e022007-08-23 21:37:33 +00001795 // At this point, we know we have a real, complex or pointer type.
1796 // Now make sure the operand is a modifiable lvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1798 if (mlval != Expr::MLV_Valid) {
1799 // FIXME: emit a more precise diagnostic...
1800 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1801 op->getSourceRange());
1802 return QualType();
1803 }
1804 return resType;
1805}
1806
Anders Carlsson369dee42008-02-01 07:15:58 +00001807/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Reid Spencer5f016e22007-07-11 17:01:13 +00001808/// This routine allows us to typecheck complex/recursive expressions
1809/// where the declaration is needed for type checking. Here are some
1810/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattnerf0467b32008-04-02 04:24:33 +00001811static ValueDecl *getPrimaryDecl(Expr *E) {
1812 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 case Stmt::DeclRefExprClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001814 return cast<DeclRefExpr>(E)->getDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 case Stmt::MemberExprClass:
Chris Lattnerf82228f2007-11-16 17:46:48 +00001816 // Fields cannot be declared with a 'register' storage class.
1817 // &X->f is always ok, even if X is declared register.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001818 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnerf82228f2007-11-16 17:46:48 +00001819 return 0;
Chris Lattnerf0467b32008-04-02 04:24:33 +00001820 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson369dee42008-02-01 07:15:58 +00001821 case Stmt::ArraySubscriptExprClass: {
1822 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1823
Chris Lattnerf0467b32008-04-02 04:24:33 +00001824 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlssonf2a4b842008-02-01 16:01:31 +00001825 if (!VD || VD->getType()->isPointerType())
Anders Carlsson369dee42008-02-01 07:15:58 +00001826 return 0;
1827 else
1828 return VD;
1829 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001830 case Stmt::UnaryOperatorClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001831 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 case Stmt::ParenExprClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001833 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf82228f2007-11-16 17:46:48 +00001834 case Stmt::ImplicitCastExprClass:
1835 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001836 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 default:
1838 return 0;
1839 }
1840}
1841
1842/// CheckAddressOfOperand - The operand of & must be either a function
1843/// designator or an lvalue designating an object. If it is an lvalue, the
1844/// object cannot be declared with storage class register or be a bit field.
1845/// Note: The usual conversions are *not* applied to the operand of the &
1846/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1847QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff08f19672008-01-13 17:10:08 +00001848 if (getLangOptions().C99) {
1849 // Implement C99-only parts of addressof rules.
1850 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1851 if (uOp->getOpcode() == UnaryOperator::Deref)
1852 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1853 // (assuming the deref expression is valid).
1854 return uOp->getSubExpr()->getType();
1855 }
1856 // Technically, there should be a check for array subscript
1857 // expressions here, but the result of one is always an lvalue anyway.
1858 }
Anders Carlsson369dee42008-02-01 07:15:58 +00001859 ValueDecl *dcl = getPrimaryDecl(op);
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 Expr::isLvalueResult lval = op->isLvalue();
1861
1862 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnerf82228f2007-11-16 17:46:48 +00001863 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1864 // FIXME: emit more specific diag...
Reid Spencer5f016e22007-07-11 17:01:13 +00001865 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1866 op->getSourceRange());
1867 return QualType();
1868 }
Steve Naroffbcb2b612008-02-29 23:30:25 +00001869 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1870 if (MemExpr->getMemberDecl()->isBitField()) {
1871 Diag(OpLoc, diag::err_typecheck_address_of,
1872 std::string("bit-field"), op->getSourceRange());
1873 return QualType();
1874 }
1875 // Check for Apple extension for accessing vector components.
1876 } else if (isa<ArraySubscriptExpr>(op) &&
1877 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1878 Diag(OpLoc, diag::err_typecheck_address_of,
1879 std::string("vector"), op->getSourceRange());
1880 return QualType();
1881 } else if (dcl) { // C99 6.5.3.2p1
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 // We have an lvalue with a decl. Make sure the decl is not declared
1883 // with the register storage-class specifier.
1884 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1885 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroffbcb2b612008-02-29 23:30:25 +00001886 Diag(OpLoc, diag::err_typecheck_address_of,
1887 std::string("register variable"), op->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 return QualType();
1889 }
1890 } else
1891 assert(0 && "Unknown/unexpected decl type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 }
1893 // If the operand has type "type", the result has type "pointer to type".
1894 return Context.getPointerType(op->getType());
1895}
1896
1897QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001898 UsualUnaryConversions(op);
1899 QualType qType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001900
Chris Lattnerbefee482007-07-31 16:53:04 +00001901 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff08f19672008-01-13 17:10:08 +00001902 // Note that per both C89 and C99, this is always legal, even
1903 // if ptype is an incomplete type or void.
1904 // It would be possible to warn about dereferencing a
1905 // void pointer, but it's completely well-defined,
1906 // and such a warning is unlikely to catch any mistakes.
1907 return PT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 }
1909 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1910 qType.getAsString(), op->getSourceRange());
1911 return QualType();
1912}
1913
1914static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1915 tok::TokenKind Kind) {
1916 BinaryOperator::Opcode Opc;
1917 switch (Kind) {
1918 default: assert(0 && "Unknown binop!");
1919 case tok::star: Opc = BinaryOperator::Mul; break;
1920 case tok::slash: Opc = BinaryOperator::Div; break;
1921 case tok::percent: Opc = BinaryOperator::Rem; break;
1922 case tok::plus: Opc = BinaryOperator::Add; break;
1923 case tok::minus: Opc = BinaryOperator::Sub; break;
1924 case tok::lessless: Opc = BinaryOperator::Shl; break;
1925 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1926 case tok::lessequal: Opc = BinaryOperator::LE; break;
1927 case tok::less: Opc = BinaryOperator::LT; break;
1928 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1929 case tok::greater: Opc = BinaryOperator::GT; break;
1930 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1931 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1932 case tok::amp: Opc = BinaryOperator::And; break;
1933 case tok::caret: Opc = BinaryOperator::Xor; break;
1934 case tok::pipe: Opc = BinaryOperator::Or; break;
1935 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1936 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1937 case tok::equal: Opc = BinaryOperator::Assign; break;
1938 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1939 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1940 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1941 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1942 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1943 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1944 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1945 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1946 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1947 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1948 case tok::comma: Opc = BinaryOperator::Comma; break;
1949 }
1950 return Opc;
1951}
1952
1953static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1954 tok::TokenKind Kind) {
1955 UnaryOperator::Opcode Opc;
1956 switch (Kind) {
1957 default: assert(0 && "Unknown unary op!");
1958 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1959 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1960 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1961 case tok::star: Opc = UnaryOperator::Deref; break;
1962 case tok::plus: Opc = UnaryOperator::Plus; break;
1963 case tok::minus: Opc = UnaryOperator::Minus; break;
1964 case tok::tilde: Opc = UnaryOperator::Not; break;
1965 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1966 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1967 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1968 case tok::kw___real: Opc = UnaryOperator::Real; break;
1969 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1970 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1971 }
1972 return Opc;
1973}
1974
1975// Binary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001976Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 ExprTy *LHS, ExprTy *RHS) {
1978 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1979 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1980
Steve Narofff69936d2007-09-16 03:34:24 +00001981 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1982 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Reid Spencer5f016e22007-07-11 17:01:13 +00001983
1984 QualType ResultTy; // Result type of the binary operator.
1985 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1986
1987 switch (Opc) {
1988 default:
1989 assert(0 && "Unknown binary expr!");
1990 case BinaryOperator::Assign:
1991 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1992 break;
1993 case BinaryOperator::Mul:
1994 case BinaryOperator::Div:
1995 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1996 break;
1997 case BinaryOperator::Rem:
1998 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1999 break;
2000 case BinaryOperator::Add:
2001 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
2002 break;
2003 case BinaryOperator::Sub:
2004 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
2005 break;
2006 case BinaryOperator::Shl:
2007 case BinaryOperator::Shr:
2008 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
2009 break;
2010 case BinaryOperator::LE:
2011 case BinaryOperator::LT:
2012 case BinaryOperator::GE:
2013 case BinaryOperator::GT:
Chris Lattnera5937dd2007-08-26 01:18:55 +00002014 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 break;
2016 case BinaryOperator::EQ:
2017 case BinaryOperator::NE:
Chris Lattnera5937dd2007-08-26 01:18:55 +00002018 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 break;
2020 case BinaryOperator::And:
2021 case BinaryOperator::Xor:
2022 case BinaryOperator::Or:
2023 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
2024 break;
2025 case BinaryOperator::LAnd:
2026 case BinaryOperator::LOr:
2027 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
2028 break;
2029 case BinaryOperator::MulAssign:
2030 case BinaryOperator::DivAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00002031 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002032 if (!CompTy.isNull())
2033 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2034 break;
2035 case BinaryOperator::RemAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00002036 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 if (!CompTy.isNull())
2038 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2039 break;
2040 case BinaryOperator::AddAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00002041 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 if (!CompTy.isNull())
2043 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2044 break;
2045 case BinaryOperator::SubAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00002046 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 if (!CompTy.isNull())
2048 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2049 break;
2050 case BinaryOperator::ShlAssign:
2051 case BinaryOperator::ShrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00002052 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002053 if (!CompTy.isNull())
2054 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2055 break;
2056 case BinaryOperator::AndAssign:
2057 case BinaryOperator::XorAssign:
2058 case BinaryOperator::OrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00002059 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 if (!CompTy.isNull())
2061 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
2062 break;
2063 case BinaryOperator::Comma:
2064 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
2065 break;
2066 }
2067 if (ResultTy.isNull())
2068 return true;
2069 if (CompTy.isNull())
Chris Lattner17d1b2a2007-08-28 18:36:55 +00002070 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002071 else
Chris Lattner17d1b2a2007-08-28 18:36:55 +00002072 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002073}
2074
2075// Unary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00002076Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Reid Spencer5f016e22007-07-11 17:01:13 +00002077 ExprTy *input) {
2078 Expr *Input = (Expr*)input;
2079 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2080 QualType resultType;
2081 switch (Opc) {
2082 default:
2083 assert(0 && "Unimplemented unary expr!");
2084 case UnaryOperator::PreInc:
2085 case UnaryOperator::PreDec:
2086 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
2087 break;
2088 case UnaryOperator::AddrOf:
2089 resultType = CheckAddressOfOperand(Input, OpLoc);
2090 break;
2091 case UnaryOperator::Deref:
Steve Naroff1ca9b112007-12-18 04:06:57 +00002092 DefaultFunctionArrayConversion(Input);
Reid Spencer5f016e22007-07-11 17:01:13 +00002093 resultType = CheckIndirectionOperand(Input, OpLoc);
2094 break;
2095 case UnaryOperator::Plus:
2096 case UnaryOperator::Minus:
Steve Naroffc80b4ee2007-07-16 21:54:35 +00002097 UsualUnaryConversions(Input);
2098 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002099 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
2100 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2101 resultType.getAsString());
2102 break;
2103 case UnaryOperator::Not: // bitwise complement
Steve Naroffc80b4ee2007-07-16 21:54:35 +00002104 UsualUnaryConversions(Input);
2105 resultType = Input->getType();
Steve Naroff084f9ed2007-08-24 17:20:07 +00002106 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2107 if (!resultType->isIntegerType()) {
2108 if (resultType->isComplexType())
2109 // C99 does not support '~' for complex conjugation.
2110 Diag(OpLoc, diag::ext_integer_complement_complex,
2111 resultType.getAsString());
2112 else
2113 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2114 resultType.getAsString());
2115 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 break;
2117 case UnaryOperator::LNot: // logical negation
2118 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroffc80b4ee2007-07-16 21:54:35 +00002119 DefaultFunctionArrayConversion(Input);
2120 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2122 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2123 resultType.getAsString());
2124 // LNot always has type int. C99 6.5.3.3p5.
2125 resultType = Context.IntTy;
2126 break;
2127 case UnaryOperator::SizeOf:
2128 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2129 break;
2130 case UnaryOperator::AlignOf:
2131 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2132 break;
Chris Lattnerdbb36972007-08-24 21:16:53 +00002133 case UnaryOperator::Real:
Chris Lattnerdbb36972007-08-24 21:16:53 +00002134 case UnaryOperator::Imag:
Chris Lattner5d794252007-08-24 21:41:10 +00002135 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattnerdbb36972007-08-24 21:16:53 +00002136 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002137 case UnaryOperator::Extension:
Reid Spencer5f016e22007-07-11 17:01:13 +00002138 resultType = Input->getType();
2139 break;
2140 }
2141 if (resultType.isNull())
2142 return true;
2143 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2144}
2145
Steve Naroff1b273c42007-09-16 14:56:35 +00002146/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2147Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +00002148 SourceLocation LabLoc,
2149 IdentifierInfo *LabelII) {
2150 // Look up the record for this label identifier.
2151 LabelStmt *&LabelDecl = LabelMap[LabelII];
2152
2153 // If we haven't seen this label yet, create a forward reference.
2154 if (LabelDecl == 0)
2155 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2156
2157 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattner6481a572007-08-03 17:31:20 +00002158 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2159 Context.getPointerType(Context.VoidTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00002160}
2161
Steve Naroff1b273c42007-09-16 14:56:35 +00002162Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattnerab18c4c2007-07-24 16:58:17 +00002163 SourceLocation RPLoc) { // "({..})"
2164 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2165 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2166 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2167
2168 // FIXME: there are a variety of strange constraints to enforce here, for
2169 // example, it is not possible to goto into a stmt expression apparently.
2170 // More semantic analysis is needed.
2171
2172 // FIXME: the last statement in the compount stmt has its value used. We
2173 // should not warn about it being unused.
2174
2175 // If there are sub stmts in the compound stmt, take the type of the last one
2176 // as the type of the stmtexpr.
2177 QualType Ty = Context.VoidTy;
2178
2179 if (!Compound->body_empty())
2180 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2181 Ty = LastExpr->getType();
2182
2183 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2184}
Steve Naroffd34e9152007-08-01 22:05:33 +00002185
Steve Naroff1b273c42007-09-16 14:56:35 +00002186Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002187 SourceLocation TypeLoc,
2188 TypeTy *argty,
2189 OffsetOfComponent *CompPtr,
2190 unsigned NumComponents,
2191 SourceLocation RPLoc) {
2192 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2193 assert(!ArgTy.isNull() && "Missing type argument!");
2194
2195 // We must have at least one component that refers to the type, and the first
2196 // one is known to be a field designator. Verify that the ArgTy represents
2197 // a struct/union/class.
2198 if (!ArgTy->isRecordType())
2199 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2200
2201 // Otherwise, create a compound literal expression as the base, and
2202 // iteratively process the offsetof designators.
Steve Naroffe9b12192008-01-14 18:19:28 +00002203 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002204
Chris Lattner9e2b75c2007-08-31 21:49:13 +00002205 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2206 // GCC extension, diagnose them.
2207 if (NumComponents != 1)
2208 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2209 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2210
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002211 for (unsigned i = 0; i != NumComponents; ++i) {
2212 const OffsetOfComponent &OC = CompPtr[i];
2213 if (OC.isBrackets) {
2214 // Offset of an array sub-field. TODO: Should we allow vector elements?
2215 const ArrayType *AT = Res->getType()->getAsArrayType();
2216 if (!AT) {
2217 delete Res;
2218 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2219 Res->getType().getAsString());
2220 }
2221
Chris Lattner704fe352007-08-30 17:59:59 +00002222 // FIXME: C++: Verify that operator[] isn't overloaded.
2223
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002224 // C99 6.5.2.1p1
2225 Expr *Idx = static_cast<Expr*>(OC.U.E);
2226 if (!Idx->getType()->isIntegerType())
2227 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2228 Idx->getSourceRange());
2229
2230 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2231 continue;
2232 }
2233
2234 const RecordType *RC = Res->getType()->getAsRecordType();
2235 if (!RC) {
2236 delete Res;
2237 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2238 Res->getType().getAsString());
2239 }
2240
2241 // Get the decl corresponding to this.
2242 RecordDecl *RD = RC->getDecl();
2243 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2244 if (!MemberDecl)
2245 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2246 OC.U.IdentInfo->getName(),
2247 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner704fe352007-08-30 17:59:59 +00002248
2249 // FIXME: C++: Verify that MemberDecl isn't a static field.
2250 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman51019072008-02-06 22:48:16 +00002251 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2252 // matter here.
2253 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002254 }
2255
2256 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2257 BuiltinLoc);
2258}
2259
2260
Steve Naroff1b273c42007-09-16 14:56:35 +00002261Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroffd34e9152007-08-01 22:05:33 +00002262 TypeTy *arg1, TypeTy *arg2,
2263 SourceLocation RPLoc) {
2264 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2265 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2266
2267 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2268
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002269 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroffd34e9152007-08-01 22:05:33 +00002270}
2271
Steve Naroff1b273c42007-09-16 14:56:35 +00002272Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroffd04fdd52007-08-03 21:21:27 +00002273 ExprTy *expr1, ExprTy *expr2,
2274 SourceLocation RPLoc) {
2275 Expr *CondExpr = static_cast<Expr*>(cond);
2276 Expr *LHSExpr = static_cast<Expr*>(expr1);
2277 Expr *RHSExpr = static_cast<Expr*>(expr2);
2278
2279 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2280
2281 // The conditional expression is required to be a constant expression.
2282 llvm::APSInt condEval(32);
2283 SourceLocation ExpLoc;
2284 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2285 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2286 CondExpr->getSourceRange());
2287
2288 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2289 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2290 RHSExpr->getType();
2291 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2292}
2293
Nate Begeman67295d02008-01-30 20:50:20 +00002294/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begemane2ce1d92008-01-17 17:46:27 +00002295/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begeman67295d02008-01-30 20:50:20 +00002296/// The number of arguments has already been validated to match the number of
2297/// arguments in FnType.
2298static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begemane2ce1d92008-01-17 17:46:27 +00002299 unsigned NumParams = FnType->getNumArgs();
Nate Begemand6595fa2008-04-18 23:35:14 +00002300 for (unsigned i = 0; i != NumParams; ++i) {
2301 QualType ExprTy = Args[i]->getType().getCanonicalType();
2302 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2303
2304 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begemane2ce1d92008-01-17 17:46:27 +00002305 return false;
Nate Begemand6595fa2008-04-18 23:35:14 +00002306 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002307 return true;
2308}
2309
2310Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2311 SourceLocation *CommaLocs,
2312 SourceLocation BuiltinLoc,
2313 SourceLocation RParenLoc) {
Nate Begeman796ef3d2008-01-31 05:38:29 +00002314 // __builtin_overload requires at least 2 arguments
2315 if (NumArgs < 2)
2316 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2317 SourceRange(BuiltinLoc, RParenLoc));
Nate Begemane2ce1d92008-01-17 17:46:27 +00002318
Nate Begemane2ce1d92008-01-17 17:46:27 +00002319 // The first argument is required to be a constant expression. It tells us
2320 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002321 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begemane2ce1d92008-01-17 17:46:27 +00002322 Expr *NParamsExpr = Args[0];
2323 llvm::APSInt constEval(32);
2324 SourceLocation ExpLoc;
2325 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2326 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2327 NParamsExpr->getSourceRange());
2328
2329 // Verify that the number of parameters is > 0
2330 unsigned NumParams = constEval.getZExtValue();
2331 if (NumParams == 0)
2332 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2333 NParamsExpr->getSourceRange());
2334 // Verify that we have at least 1 + NumParams arguments to the builtin.
2335 if ((NumParams + 1) > NumArgs)
2336 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2337 SourceRange(BuiltinLoc, RParenLoc));
2338
2339 // Figure out the return type, by matching the args to one of the functions
Nate Begeman67295d02008-01-30 20:50:20 +00002340 // listed after the parameters.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002341 OverloadExpr *OE = 0;
Nate Begemane2ce1d92008-01-17 17:46:27 +00002342 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2343 // UsualUnaryConversions will convert the function DeclRefExpr into a
2344 // pointer to function.
2345 Expr *Fn = UsualUnaryConversions(Args[i]);
2346 FunctionTypeProto *FnType = 0;
Nate Begeman67295d02008-01-30 20:50:20 +00002347 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2348 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2349 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2350 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002351
2352 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2353 // parameters, and the number of parameters must match the value passed to
2354 // the builtin.
2355 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begeman67295d02008-01-30 20:50:20 +00002356 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2357 Fn->getSourceRange());
Nate Begemane2ce1d92008-01-17 17:46:27 +00002358
2359 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begeman67295d02008-01-30 20:50:20 +00002360 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begemane2ce1d92008-01-17 17:46:27 +00002361 // If they match, return a new OverloadExpr.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002362 if (ExprsMatchFnType(Args+1, FnType)) {
2363 if (OE)
2364 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2365 OE->getFn()->getSourceRange());
2366 // Remember our match, and continue processing the remaining arguments
2367 // to catch any errors.
2368 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2369 BuiltinLoc, RParenLoc);
2370 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002371 }
Nate Begeman796ef3d2008-01-31 05:38:29 +00002372 // Return the newly created OverloadExpr node, if we succeded in matching
2373 // exactly one of the candidate functions.
2374 if (OE)
2375 return OE;
Nate Begemane2ce1d92008-01-17 17:46:27 +00002376
2377 // If we didn't find a matching function Expr in the __builtin_overload list
2378 // the return an error.
2379 std::string typeNames;
Nate Begeman67295d02008-01-30 20:50:20 +00002380 for (unsigned i = 0; i != NumParams; ++i) {
2381 if (i != 0) typeNames += ", ";
2382 typeNames += Args[i+1]->getType().getAsString();
2383 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002384
2385 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2386 SourceRange(BuiltinLoc, RParenLoc));
2387}
2388
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002389Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2390 ExprTy *expr, TypeTy *type,
Chris Lattner5cf216b2008-01-04 18:04:52 +00002391 SourceLocation RPLoc) {
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002392 Expr *E = static_cast<Expr*>(expr);
2393 QualType T = QualType::getFromOpaquePtr(type);
2394
2395 InitBuiltinVaListType();
2396
Chris Lattner5cf216b2008-01-04 18:04:52 +00002397 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2398 != Compatible)
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002399 return Diag(E->getLocStart(),
2400 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2401 E->getType().getAsString(),
2402 E->getSourceRange());
2403
2404 // FIXME: Warn if a non-POD type is passed in.
2405
2406 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2407}
2408
Chris Lattner5cf216b2008-01-04 18:04:52 +00002409bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2410 SourceLocation Loc,
2411 QualType DstType, QualType SrcType,
2412 Expr *SrcExpr, const char *Flavor) {
2413 // Decode the result (notice that AST's are still created for extensions).
2414 bool isInvalid = false;
2415 unsigned DiagKind;
2416 switch (ConvTy) {
2417 default: assert(0 && "Unknown conversion type");
2418 case Compatible: return false;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002419 case PointerToInt:
Chris Lattner5cf216b2008-01-04 18:04:52 +00002420 DiagKind = diag::ext_typecheck_convert_pointer_int;
2421 break;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002422 case IntToPointer:
2423 DiagKind = diag::ext_typecheck_convert_int_pointer;
2424 break;
Chris Lattner5cf216b2008-01-04 18:04:52 +00002425 case IncompatiblePointer:
2426 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2427 break;
2428 case FunctionVoidPointer:
2429 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2430 break;
2431 case CompatiblePointerDiscardsQualifiers:
2432 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2433 break;
2434 case Incompatible:
2435 DiagKind = diag::err_typecheck_convert_incompatible;
2436 isInvalid = true;
2437 break;
2438 }
2439
2440 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2441 SrcExpr->getSourceRange());
2442 return isInvalid;
2443}
2444
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00002445
2446