blob: 6a6cd25575a00b855cf3377ac0072dc116ed0368 [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 Naroff563477d2007-09-18 23:55:05 +000019#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/LiteralSupport.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner925e60d2007-12-28 05:29:59 +000024#include "llvm/ADT/OwningPtr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/SmallString.h"
Chris Lattner59907c42007-08-10 20:18:51 +000026#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
Steve Narofff69936d2007-09-16 03:34:24 +000029/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Reid Spencer5f016e22007-07-11 17:01:13 +000030/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
31/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
32/// multiple tokens. However, the common case is that StringToks points to one
33/// string.
34///
35Action::ExprResult
Steve Narofff69936d2007-09-16 03:34:24 +000036Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +000037 assert(NumStringToks && "Must have at least one string!");
38
39 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
40 if (Literal.hadError)
41 return ExprResult(true);
42
43 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
44 for (unsigned i = 0; i != NumStringToks; ++i)
45 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattnera7ad98f2008-02-11 00:02:17 +000046
47 // Verify that pascal strings aren't too large.
Anders Carlssonee98ac52007-10-15 02:50:23 +000048 if (Literal.Pascal && Literal.GetStringLength() > 256)
49 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
50 SourceRange(StringToks[0].getLocation(),
51 StringToks[NumStringToks-1].getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +000052
Chris Lattnera7ad98f2008-02-11 00:02:17 +000053 QualType StrTy = Context.CharTy;
54 // FIXME: handle wchar_t
55 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
56
57 // Get an array type for the string, according to C99 6.4.5. This includes
58 // the nul terminator character as well as the string length for pascal
59 // strings.
60 StrTy = Context.getConstantArrayType(StrTy,
61 llvm::APInt(32, Literal.GetStringLength()+1),
62 ArrayType::Normal, 0);
63
Reid Spencer5f016e22007-07-11 17:01:13 +000064 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
65 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera7ad98f2008-02-11 00:02:17 +000066 Literal.AnyWide, StrTy,
Anders Carlssonee98ac52007-10-15 02:50:23 +000067 StringToks[0].getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +000068 StringToks[NumStringToks-1].getLocation());
69}
70
71
Steve Naroff08d92e42007-09-15 18:49:24 +000072/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Reid Spencer5f016e22007-07-11 17:01:13 +000073/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroff0d755ad2008-03-19 23:46:26 +000074/// identifier is used in a function call context.
Steve Naroff08d92e42007-09-15 18:49:24 +000075Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +000076 IdentifierInfo &II,
77 bool HasTrailingLParen) {
Chris Lattner8a934232008-03-31 00:36:02 +000078 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroffb327ce02008-04-02 14:35:35 +000079 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattner8a934232008-03-31 00:36:02 +000080
81 // If this reference is in an Objective-C method, then ivar lookup happens as
82 // well.
83 if (CurMethodDecl) {
Steve Naroffe8043c32008-04-01 23:04:06 +000084 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattner8a934232008-03-31 00:36:02 +000085 // There are two cases to handle here. 1) scoped lookup could have failed,
86 // in which case we should look for an ivar. 2) scoped lookup could have
87 // found a decl, but that decl is outside the current method (i.e. a global
88 // variable). In these two cases, we do a lookup for an ivar with this
89 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroffe8043c32008-04-01 23:04:06 +000090 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Chris Lattner8a934232008-03-31 00:36:02 +000091 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface(), *DeclClass;
92 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, DeclClass)) {
93 // FIXME: This should use a new expr for a direct reference, don't turn
94 // this into Self->ivar, just return a BareIVarExpr or something.
95 IdentifierInfo &II = Context.Idents.get("self");
96 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
97 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
98 static_cast<Expr*>(SelfExpr.Val), true, true);
99 }
100 }
101 }
102
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 if (D == 0) {
104 // Otherwise, this could be an implicitly declared function reference (legal
105 // in C90, extension in C99).
106 if (HasTrailingLParen &&
Chris Lattner8a934232008-03-31 00:36:02 +0000107 !getLangOptions().CPlusPlus) // Not in C++.
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 D = ImplicitlyDefineFunction(Loc, II, S);
109 else {
110 // If this name wasn't predeclared and if this is not a function call,
111 // diagnose the problem.
112 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
113 }
114 }
Chris Lattner8a934232008-03-31 00:36:02 +0000115
Steve Naroffe1223f72007-08-28 03:03:08 +0000116 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner7e669b22008-02-29 16:48:43 +0000117 // check if referencing an identifier with __attribute__((deprecated)).
118 if (VD->getAttr<DeprecatedAttr>())
119 Diag(Loc, diag::warn_deprecated, VD->getName());
120
Steve Naroff53a32342007-08-28 18:45:29 +0000121 // Only create DeclRefExpr's for valid Decl's.
Steve Naroff5912a352007-08-28 20:14:24 +0000122 if (VD->isInvalidDecl())
Steve Naroffe1223f72007-08-28 03:03:08 +0000123 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroffe1223f72007-08-28 03:03:08 +0000125 }
Chris Lattner8a934232008-03-31 00:36:02 +0000126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 if (isa<TypedefDecl>(D))
128 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000129 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian5ef404f2007-12-05 18:16:33 +0000130 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
132 assert(0 && "Invalid decl");
Chris Lattnereddbe032007-07-21 04:57:45 +0000133 abort();
Reid Spencer5f016e22007-07-11 17:01:13 +0000134}
135
Steve Narofff69936d2007-09-16 03:34:24 +0000136Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson22742662007-07-21 05:21:51 +0000137 tok::TokenKind Kind) {
138 PreDefinedExpr::IdentType IT;
139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 switch (Kind) {
Chris Lattner1423ea42008-01-12 18:39:25 +0000141 default: assert(0 && "Unknown simple primary expr!");
142 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
143 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
144 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
Chris Lattner1423ea42008-01-12 18:39:25 +0000146
147 // Verify that this is in a function context.
Chris Lattner8f978d52008-01-12 19:32:28 +0000148 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattner1423ea42008-01-12 18:39:25 +0000149 return Diag(Loc, diag::err_predef_outside_function);
Anders Carlsson22742662007-07-21 05:21:51 +0000150
Chris Lattnerfa28b302008-01-12 08:14:25 +0000151 // Pre-defined identifiers are of type char[x], where x is the length of the
152 // string.
Chris Lattner8f978d52008-01-12 19:32:28 +0000153 unsigned Length;
154 if (CurFunctionDecl)
155 Length = CurFunctionDecl->getIdentifier()->getLength();
156 else
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000157 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattner1423ea42008-01-12 18:39:25 +0000158
Chris Lattner8f978d52008-01-12 19:32:28 +0000159 llvm::APInt LengthI(32, Length + 1);
Chris Lattner1423ea42008-01-12 18:39:25 +0000160 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattner8f978d52008-01-12 19:32:28 +0000161 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattnerfa28b302008-01-12 08:14:25 +0000162 return new PreDefinedExpr(Loc, ResTy, IT);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
Steve Narofff69936d2007-09-16 03:34:24 +0000165Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 llvm::SmallString<16> CharBuffer;
167 CharBuffer.resize(Tok.getLength());
168 const char *ThisTokBegin = &CharBuffer[0];
169 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
170
171 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
172 Tok.getLocation(), PP);
173 if (Literal.hadError())
174 return ExprResult(true);
Chris Lattnerfc62bfd2008-03-01 08:32:21 +0000175
176 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
177
178 return new CharacterLiteral(Literal.getValue(), type, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Steve Narofff69936d2007-09-16 03:34:24 +0000181Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 // fast path for a single digit (which is quite common). A single digit
183 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
184 if (Tok.getLength() == 1) {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000185 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000186
Chris Lattner98be4942008-03-05 18:54:05 +0000187 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattnerf0467b32008-04-02 04:24:33 +0000188 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 Context.IntTy,
190 Tok.getLocation()));
191 }
192 llvm::SmallString<512> IntegerBuffer;
193 IntegerBuffer.resize(Tok.getLength());
194 const char *ThisTokBegin = &IntegerBuffer[0];
195
196 // Get the spelling of the token, which eliminates trigraphs, etc.
197 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
198 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
199 Tok.getLocation(), PP);
200 if (Literal.hadError)
201 return ExprResult(true);
202
Chris Lattner5d661452007-08-26 03:42:43 +0000203 Expr *Res;
204
205 if (Literal.isFloatingLiteral()) {
Chris Lattner525a0502007-09-22 18:29:59 +0000206 QualType Ty;
207 const llvm::fltSemantics *Format;
Chris Lattner525a0502007-09-22 18:29:59 +0000208
209 if (Literal.isFloat) {
210 Ty = Context.FloatTy;
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000211 Format = Context.Target.getFloatFormat();
212 } else if (!Literal.isLong) {
Chris Lattner525a0502007-09-22 18:29:59 +0000213 Ty = Context.DoubleTy;
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000214 Format = Context.Target.getDoubleFormat();
215 } else {
216 Ty = Context.LongDoubleTy;
217 Format = Context.Target.getLongDoubleFormat();
Chris Lattner525a0502007-09-22 18:29:59 +0000218 }
219
Ted Kremenek720c4ec2007-11-29 00:56:49 +0000220 // isExact will be set by GetFloatValue().
221 bool isExact = false;
222
223 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
224 Ty, Tok.getLocation());
225
Chris Lattner5d661452007-08-26 03:42:43 +0000226 } else if (!Literal.isIntegerLiteral()) {
227 return ExprResult(true);
228 } else {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000229 QualType Ty;
Reid Spencer5f016e22007-07-11 17:01:13 +0000230
Neil Boothb9449512007-08-29 22:00:19 +0000231 // long long is a C99 feature.
232 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth79859c32007-08-29 22:13:52 +0000233 Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000234 Diag(Tok.getLocation(), diag::ext_longlong);
235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // Get the value in the widest-possible width.
Chris Lattner98be4942008-03-05 18:54:05 +0000237 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238
239 if (Literal.GetIntegerValue(ResultVal)) {
240 // If this value didn't fit into uintmax_t, warn and force to ull.
241 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattnerf0467b32008-04-02 04:24:33 +0000242 Ty = Context.UnsignedLongLongTy;
243 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner98be4942008-03-05 18:54:05 +0000244 "long long is not intmax_t?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 } else {
246 // If this value fits into a ULL, try to figure out what else it fits into
247 // according to the rules of C99 6.4.4.1p5.
248
249 // Octal, Hexadecimal, and integers with a U suffix are allowed to
250 // be an unsigned int.
251 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
252
253 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner97c51562007-08-23 21:58:08 +0000254 if (!Literal.isLong && !Literal.isLongLong) {
255 // Are int/unsigned possibilities?
Chris Lattner98be4942008-03-05 18:54:05 +0000256 unsigned IntSize =
257 static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 // Does it fit in a unsigned int?
259 if (ResultVal.isIntN(IntSize)) {
260 // Does it fit in a signed int?
261 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000262 Ty = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000264 Ty = Context.UnsignedIntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
266
Chris Lattnerf0467b32008-04-02 04:24:33 +0000267 if (!Ty.isNull())
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 ResultVal.trunc(IntSize);
269 }
270
271 // Are long/unsigned long possibilities?
Chris Lattnerf0467b32008-04-02 04:24:33 +0000272 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattner98be4942008-03-05 18:54:05 +0000273 unsigned LongSize =
274 static_cast<unsigned>(Context.getTypeSize(Context.LongTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000275
276 // Does it fit in a unsigned long?
277 if (ResultVal.isIntN(LongSize)) {
278 // Does it fit in a signed long?
279 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000280 Ty = Context.LongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000282 Ty = Context.UnsignedLongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 }
Chris Lattnerf0467b32008-04-02 04:24:33 +0000284 if (!Ty.isNull())
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 ResultVal.trunc(LongSize);
286 }
287
288 // Finally, check long long if needed.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000289 if (Ty.isNull()) {
Chris Lattner98be4942008-03-05 18:54:05 +0000290 unsigned LongLongSize =
291 static_cast<unsigned>(Context.getTypeSize(Context.LongLongTy));
Reid Spencer5f016e22007-07-11 17:01:13 +0000292
293 // Does it fit in a unsigned long long?
294 if (ResultVal.isIntN(LongLongSize)) {
295 // Does it fit in a signed long long?
296 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000297 Ty = Context.LongLongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000299 Ty = Context.UnsignedLongLongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 }
301 }
302
303 // If we still couldn't decide a type, we probably have something that
304 // does not fit in a signed long long, but has no U suffix.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000305 if (Ty.isNull()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattnerf0467b32008-04-02 04:24:33 +0000307 Ty = Context.UnsignedLongLongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 }
309 }
310
Chris Lattnerf0467b32008-04-02 04:24:33 +0000311 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 }
Chris Lattner5d661452007-08-26 03:42:43 +0000313
314 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
315 if (Literal.isImaginary)
316 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
317
318 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000319}
320
Steve Narofff69936d2007-09-16 03:34:24 +0000321Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 ExprTy *Val) {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000323 Expr *E = (Expr *)Val;
324 assert((E != 0) && "ActOnParenExpr() missing expr");
325 return new ParenExpr(L, R, E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327
328/// The UsualUnaryConversions() function is *not* called by this routine.
329/// See C99 6.3.2.1p[2-4] for more details.
330QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
331 SourceLocation OpLoc, bool isSizeof) {
332 // C99 6.5.3.4p1:
333 if (isa<FunctionType>(exprType) && isSizeof)
334 // alignof(function) is allowed.
335 Diag(OpLoc, diag::ext_sizeof_function_type);
336 else if (exprType->isVoidType())
337 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
338 else if (exprType->isIncompleteType()) {
339 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
340 diag::err_alignof_incomplete_type,
341 exprType.getAsString());
342 return QualType(); // error
343 }
344 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
345 return Context.getSizeType();
346}
347
348Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000349ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 SourceLocation LPLoc, TypeTy *Ty,
351 SourceLocation RPLoc) {
352 // If error parsing type, ignore.
353 if (Ty == 0) return true;
354
355 // Verify that this is a valid expression.
356 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
357
358 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
359
360 if (resultType.isNull())
361 return true;
362 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
363}
364
Chris Lattner5d794252007-08-24 21:41:10 +0000365QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattnerdbb36972007-08-24 21:16:53 +0000366 DefaultFunctionArrayConversion(V);
367
Chris Lattnercc26ed72007-08-26 05:39:26 +0000368 // These operators return the element type of a complex type.
Chris Lattnerdbb36972007-08-24 21:16:53 +0000369 if (const ComplexType *CT = V->getType()->getAsComplexType())
370 return CT->getElementType();
Chris Lattnercc26ed72007-08-26 05:39:26 +0000371
372 // Otherwise they pass through real integer and floating point types here.
373 if (V->getType()->isArithmeticType())
374 return V->getType();
375
376 // Reject anything else.
377 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
378 return QualType();
Chris Lattnerdbb36972007-08-24 21:16:53 +0000379}
380
381
Reid Spencer5f016e22007-07-11 17:01:13 +0000382
Steve Narofff69936d2007-09-16 03:34:24 +0000383Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 tok::TokenKind Kind,
385 ExprTy *Input) {
386 UnaryOperator::Opcode Opc;
387 switch (Kind) {
388 default: assert(0 && "Unknown unary op!");
389 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
390 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
391 }
392 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
393 if (result.isNull())
394 return true;
395 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
396}
397
398Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000399ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner727a80d2007-07-15 23:59:53 +0000401 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner12d9ff62007-07-16 00:14:47 +0000402
403 // Perform default conversions.
404 DefaultFunctionArrayConversion(LHSExp);
405 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner727a80d2007-07-15 23:59:53 +0000406
Chris Lattner12d9ff62007-07-16 00:14:47 +0000407 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000408
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000410 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // in the subscript position. As a result, we need to derive the array base
412 // and index from the expression types.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000413 Expr *BaseExpr, *IndexExpr;
414 QualType ResultType;
Chris Lattnerbefee482007-07-31 16:53:04 +0000415 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner12d9ff62007-07-16 00:14:47 +0000416 BaseExpr = LHSExp;
417 IndexExpr = RHSExp;
418 // FIXME: need to deal with const...
419 ResultType = PTy->getPointeeType();
Chris Lattnerbefee482007-07-31 16:53:04 +0000420 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000421 // Handle the uncommon case of "123[Ptr]".
Chris Lattner12d9ff62007-07-16 00:14:47 +0000422 BaseExpr = RHSExp;
423 IndexExpr = LHSExp;
424 // FIXME: need to deal with const...
425 ResultType = PTy->getPointeeType();
Chris Lattnerc8629632007-07-31 19:29:30 +0000426 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
427 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner12d9ff62007-07-16 00:14:47 +0000428 IndexExpr = RHSExp;
Steve Naroff608e0ee2007-08-03 22:40:33 +0000429
430 // Component access limited to variables (reject vec4.rg[1]).
Nate Begeman60c9b182008-02-19 01:11:03 +0000431 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr))
Steve Naroff608e0ee2007-08-03 22:40:33 +0000432 return Diag(LLoc, diag::err_ocuvector_component_access,
433 SourceRange(LLoc, RLoc));
Chris Lattner12d9ff62007-07-16 00:14:47 +0000434 // FIXME: need to deal with const...
435 ResultType = VTy->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 } else {
Chris Lattner727a80d2007-07-15 23:59:53 +0000437 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
438 RHSExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 }
440 // C99 6.5.2.1p1
Chris Lattner12d9ff62007-07-16 00:14:47 +0000441 if (!IndexExpr->getType()->isIntegerType())
442 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
443 IndexExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000444
Chris Lattner12d9ff62007-07-16 00:14:47 +0000445 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
446 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattnerd805bec2008-04-02 06:59:01 +0000447 // void (*)(int)) and pointers to incomplete types. Functions are not
448 // objects in C99.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000449 if (!ResultType->isObjectType())
450 return Diag(BaseExpr->getLocStart(),
451 diag::err_typecheck_subscript_not_object,
452 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
453
454 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000455}
456
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000457QualType Sema::
458CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
459 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattnerc8629632007-07-31 19:29:30 +0000460 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000461
462 // The vector accessor can't exceed the number of elements.
463 const char *compStr = CompName.getName();
464 if (strlen(compStr) > vecType->getNumElements()) {
465 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
466 baseType.getAsString(), SourceRange(CompLoc));
467 return QualType();
468 }
469 // The component names must come from the same set.
Chris Lattner88dca042007-08-02 22:33:49 +0000470 if (vecType->getPointAccessorIdx(*compStr) != -1) {
471 do
472 compStr++;
473 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
474 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
475 do
476 compStr++;
477 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
478 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
479 do
480 compStr++;
481 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
482 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000483
484 if (*compStr) {
485 // We didn't get to the end of the string. This means the component names
486 // didn't come from the same set *or* we encountered an illegal name.
487 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
488 std::string(compStr,compStr+1), SourceRange(CompLoc));
489 return QualType();
490 }
491 // Each component accessor can't exceed the vector type.
492 compStr = CompName.getName();
493 while (*compStr) {
494 if (vecType->isAccessorWithinNumElements(*compStr))
495 compStr++;
496 else
497 break;
498 }
499 if (*compStr) {
500 // We didn't get to the end of the string. This means a component accessor
501 // exceeds the number of elements in the vector.
502 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
503 baseType.getAsString(), SourceRange(CompLoc));
504 return QualType();
505 }
506 // The component accessor looks fine - now we need to compute the actual type.
507 // The vector type is implied by the component accessor. For example,
508 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
509 unsigned CompSize = strlen(CompName.getName());
510 if (CompSize == 1)
511 return vecType->getElementType();
Steve Naroffbea0b342007-07-29 16:33:31 +0000512
513 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
514 // Now look up the TypeDefDecl from the vector type. Without this,
515 // diagostics look bad. We want OCU vector types to appear built-in.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000516 for (unsigned i = 0, E = OCUVectorDecls.size(); i != E; ++i) {
Steve Naroffbea0b342007-07-29 16:33:31 +0000517 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
518 return Context.getTypedefType(OCUVectorDecls[i]);
519 }
520 return VT; // should never get here (a typedef type should always be found).
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000521}
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000524ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 tok::TokenKind OpKind, SourceLocation MemberLoc,
526 IdentifierInfo &Member) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000527 Expr *BaseExpr = static_cast<Expr *>(Base);
528 assert(BaseExpr && "no record expression");
Steve Naroff3cc4af82007-12-16 21:42:28 +0000529
530 // Perform default conversions.
531 DefaultFunctionArrayConversion(BaseExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000532
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000533 QualType BaseType = BaseExpr->getType();
534 assert(!BaseType.isNull() && "no type for member expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000535
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 if (OpKind == tok::arrow) {
Chris Lattnerbefee482007-07-31 16:53:04 +0000537 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000538 BaseType = PT->getPointeeType();
539 else
540 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
541 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000543 // The base type is either a record or an OCUVectorType.
Chris Lattnerc8629632007-07-31 19:29:30 +0000544 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000545 RecordDecl *RDecl = RTy->getDecl();
546 if (RTy->isIncompleteType())
547 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
548 BaseExpr->getSourceRange());
549 // The record definition is complete, now make sure the member is valid.
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000550 FieldDecl *MemberDecl = RDecl->getMember(&Member);
551 if (!MemberDecl)
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000552 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
553 SourceRange(MemberLoc));
Eli Friedman51019072008-02-06 22:48:16 +0000554
555 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedman64ec0cc2008-02-07 05:24:51 +0000556 // FIXME: Handle address space modifiers
Eli Friedman51019072008-02-06 22:48:16 +0000557 QualType MemberType = MemberDecl->getType();
558 unsigned combinedQualifiers =
Chris Lattnerf46699c2008-02-20 20:55:12 +0000559 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman51019072008-02-06 22:48:16 +0000560 MemberType = MemberType.getQualifiedType(combinedQualifiers);
561
562 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
563 MemberLoc, MemberType);
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000564 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff608e0ee2007-08-03 22:40:33 +0000565 // Component access limited to variables (reject vec4.rg.g).
Nate Begeman9c167112008-03-17 17:22:18 +0000566 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr))
Steve Naroff608e0ee2007-08-03 22:40:33 +0000567 return Diag(OpLoc, diag::err_ocuvector_component_access,
568 SourceRange(MemberLoc));
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000569 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
570 if (ret.isNull())
571 return true;
Chris Lattner6481a572007-08-03 17:31:20 +0000572 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573 } else if (BaseType->isObjCInterfaceType()) {
574 ObjCInterfaceDecl *IFace;
575 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
576 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000577 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000578 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
579 ObjCInterfaceDecl *clsDeclared;
580 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000581 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
582 OpKind==tok::arrow);
583 }
584 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
585 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000586}
587
Steve Narofff69936d2007-09-16 03:34:24 +0000588/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +0000589/// This provides the location of the left/right parens and a list of comma
590/// locations.
591Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000592ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner925e60d2007-12-28 05:29:59 +0000593 ExprTy **args, unsigned NumArgs,
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner74c469f2007-07-21 03:03:59 +0000595 Expr *Fn = static_cast<Expr *>(fn);
596 Expr **Args = reinterpret_cast<Expr**>(args);
597 assert(Fn && "no function call expression");
Chris Lattner04421082008-04-08 04:40:51 +0000598 FunctionDecl *FDecl = NULL;
599 unsigned NumArgsPassed = NumArgs;
600
601 // Promote the function operand.
602 UsualUnaryConversions(Fn);
603
604 // If we're directly calling a function, get the declaration for
605 // that function.
606 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
607 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
608 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
609
Chris Lattner925e60d2007-12-28 05:29:59 +0000610 // Make the call expr early, before semantic checks. This guarantees cleanup
611 // of arguments and function on error.
Chris Lattner04421082008-04-08 04:40:51 +0000612 if (getLangOptions().CPlusPlus && FDecl && NumArgs < FDecl->getNumParams())
613 NumArgsPassed = FDecl->getNumParams();
614 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgsPassed,
Chris Lattner925e60d2007-12-28 05:29:59 +0000615 Context.BoolTy, RParenLoc));
616
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
618 // type pointer to function".
Chris Lattner925e60d2007-12-28 05:29:59 +0000619 const PointerType *PT = Fn->getType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 if (PT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000621 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
622 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000623 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
624 if (FuncT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000625 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
626 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000627
628 // We know the result type of the call, set it.
629 TheCall->setType(FuncT->getResultType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000630
Chris Lattner925e60d2007-12-28 05:29:59 +0000631 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
633 // assignment, to the types of the corresponding parameter, ...
Chris Lattner925e60d2007-12-28 05:29:59 +0000634 unsigned NumArgsInProto = Proto->getNumArgs();
635 unsigned NumArgsToCheck = NumArgs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000636
Chris Lattner04421082008-04-08 04:40:51 +0000637 // If too few arguments are available (and we don't have default
638 // arguments for the remaining parameters), don't make the call.
639 if (NumArgs < NumArgsInProto) {
640 if (getLangOptions().CPlusPlus &&
641 FDecl &&
642 FDecl->getParamDecl(NumArgs)->getDefaultArg()) {
643 // Use default arguments for missing arguments
644 NumArgsToCheck = NumArgsInProto;
645 } else
646 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
647 Fn->getSourceRange());
648 }
649
Chris Lattner925e60d2007-12-28 05:29:59 +0000650 // If too many are passed and not variadic, error on the extras and drop
651 // them.
652 if (NumArgs > NumArgsInProto) {
653 if (!Proto->isVariadic()) {
Chris Lattnerd472b312007-07-21 03:09:58 +0000654 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000655 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnerd472b312007-07-21 03:09:58 +0000656 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner925e60d2007-12-28 05:29:59 +0000657 Args[NumArgs-1]->getLocEnd()));
658 // This deletes the extra arguments.
659 TheCall->setNumArgs(NumArgsInProto);
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 }
661 NumArgsToCheck = NumArgsInProto;
662 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000663
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 // Continue to check argument types (even if we have too few/many args).
Chris Lattner925e60d2007-12-28 05:29:59 +0000665 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner5cf216b2008-01-04 18:04:52 +0000666 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner04421082008-04-08 04:40:51 +0000667
668 Expr *Arg;
669 if (i < NumArgs)
670 Arg = Args[i];
671 else
672 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner5cf216b2008-01-04 18:04:52 +0000673 QualType ArgType = Arg->getType();
Steve Naroff700204c2007-07-24 21:46:40 +0000674
Chris Lattner925e60d2007-12-28 05:29:59 +0000675 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000676 AssignConvertType ConvTy =
677 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner925e60d2007-12-28 05:29:59 +0000678 TheCall->setArg(i, Arg);
679
Chris Lattner5cf216b2008-01-04 18:04:52 +0000680 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
681 ArgType, Arg, "passing"))
682 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000684
685 // If this is a variadic call, handle args passed through "...".
686 if (Proto->isVariadic()) {
Steve Naroffb291ab62007-08-28 23:30:39 +0000687 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner925e60d2007-12-28 05:29:59 +0000688 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
689 Expr *Arg = Args[i];
690 DefaultArgumentPromotion(Arg);
691 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000692 }
Steve Naroffb291ab62007-08-28 23:30:39 +0000693 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000694 } else {
695 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
696
Steve Naroffb291ab62007-08-28 23:30:39 +0000697 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner925e60d2007-12-28 05:29:59 +0000698 for (unsigned i = 0; i != NumArgs; i++) {
699 Expr *Arg = Args[i];
700 DefaultArgumentPromotion(Arg);
701 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000702 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000704
Chris Lattner59907c42007-08-10 20:18:51 +0000705 // Do special checking on direct calls to functions.
Chris Lattner04421082008-04-08 04:40:51 +0000706 if (FDecl && CheckFunctionCall(FDecl, TheCall.get()))
707 return true;
Chris Lattner59907c42007-08-10 20:18:51 +0000708
Chris Lattner925e60d2007-12-28 05:29:59 +0000709 return TheCall.take();
Reid Spencer5f016e22007-07-11 17:01:13 +0000710}
711
712Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000713ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroffaff1edd2007-07-19 21:32:11 +0000714 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofff69936d2007-09-16 03:34:24 +0000715 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Naroff4aa88f82007-07-19 01:06:55 +0000716 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroffaff1edd2007-07-19 21:32:11 +0000717 // FIXME: put back this assert when initializers are worked out.
Steve Narofff69936d2007-09-16 03:34:24 +0000718 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroffaff1edd2007-07-19 21:32:11 +0000719 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlssond35c8322007-12-05 07:24:19 +0000720
Steve Naroff2fdc3742007-12-10 22:44:33 +0000721 // FIXME: add more semantic analysis (C99 6.5.2.5).
Steve Naroffd0091aa2008-01-10 22:15:12 +0000722 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff58d18212008-01-09 20:58:06 +0000723 return true;
Steve Naroffe9b12192008-01-14 18:19:28 +0000724
725 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
726 if (isFileScope) { // 6.5.2.5p3
Steve Naroffd0091aa2008-01-10 22:15:12 +0000727 if (CheckForConstantInitializer(literalExpr, literalType))
728 return true;
729 }
Steve Naroffe9b12192008-01-14 18:19:28 +0000730 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000731}
732
733Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000734ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000735 SourceLocation RBraceLoc) {
Steve Narofff0090632007-09-02 02:04:30 +0000736 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000737
Steve Naroff08d92e42007-09-15 18:49:24 +0000738 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroffd35005e2007-09-03 01:24:23 +0000739 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000740
Chris Lattnerf0467b32008-04-02 04:24:33 +0000741 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
742 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
743 return E;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000744}
745
Chris Lattnerfe23e212007-12-20 00:44:32 +0000746bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssona64db8f2007-11-27 05:51:55 +0000747 assert(VectorTy->isVectorType() && "Not a vector type!");
748
749 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner98be4942008-03-05 18:54:05 +0000750 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssona64db8f2007-11-27 05:51:55 +0000751 return Diag(R.getBegin(),
752 Ty->isVectorType() ?
753 diag::err_invalid_conversion_between_vectors :
754 diag::err_invalid_conversion_between_vector_and_integer,
755 VectorTy.getAsString().c_str(),
756 Ty.getAsString().c_str(), R);
757 } else
758 return Diag(R.getBegin(),
759 diag::err_invalid_conversion_between_vector_and_scalar,
760 VectorTy.getAsString().c_str(),
761 Ty.getAsString().c_str(), R);
762
763 return false;
764}
765
Steve Naroff4aa88f82007-07-19 01:06:55 +0000766Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000767ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 SourceLocation RParenLoc, ExprTy *Op) {
Steve Narofff69936d2007-09-16 03:34:24 +0000769 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff16beff82007-07-16 23:25:18 +0000770
771 Expr *castExpr = static_cast<Expr*>(Op);
772 QualType castType = QualType::getFromOpaquePtr(Ty);
773
Steve Naroff711602b2007-08-31 00:32:44 +0000774 UsualUnaryConversions(castExpr);
775
Chris Lattner75af4802007-07-18 16:00:06 +0000776 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
777 // type needs to be scalar.
Chris Lattner3da2db42007-10-29 04:26:44 +0000778 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff9412d682008-01-24 22:55:05 +0000779 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattner3da2db42007-10-29 04:26:44 +0000780 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
781 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Naroff9412d682008-01-24 22:55:05 +0000782 if (!castExpr->getType()->isScalarType() &&
783 !castExpr->getType()->isVectorType())
Chris Lattner3da2db42007-10-29 04:26:44 +0000784 return Diag(castExpr->getLocStart(),
785 diag::err_typecheck_expect_scalar_operand,
786 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssona64db8f2007-11-27 05:51:55 +0000787
788 if (castExpr->getType()->isVectorType()) {
789 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
790 castExpr->getType(), castType))
791 return true;
792 } else if (castType->isVectorType()) {
793 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
794 castType, castExpr->getType()))
795 return true;
Chris Lattner3da2db42007-10-29 04:26:44 +0000796 }
Steve Naroff16beff82007-07-16 23:25:18 +0000797 }
798 return new CastExpr(castType, castExpr, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799}
800
Chris Lattnera21ddb32007-11-26 01:40:58 +0000801/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
802/// In that case, lex = cond.
Reid Spencer5f016e22007-07-11 17:01:13 +0000803inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff49b45262007-07-13 16:58:59 +0000804 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000805 UsualUnaryConversions(cond);
806 UsualUnaryConversions(lex);
807 UsualUnaryConversions(rex);
808 QualType condT = cond->getType();
809 QualType lexT = lex->getType();
810 QualType rexT = rex->getType();
811
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 // first, check the condition.
Steve Naroff49b45262007-07-13 16:58:59 +0000813 if (!condT->isScalarType()) { // C99 6.5.15p2
814 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
815 condT.getAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 return QualType();
817 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000818
819 // Now check the two expressions.
820
821 // If both operands have arithmetic type, do the usual arithmetic conversions
822 // to find a common type: C99 6.5.15p3,5.
823 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffa4332e22007-07-17 00:58:39 +0000824 UsualArithmeticConversions(lex, rex);
825 return lex->getType();
826 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000827
828 // If both operands are the same structure or union type, the result is that
829 // type.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000830 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner70d67a92008-01-06 22:42:25 +0000831 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattnera21ddb32007-11-26 01:40:58 +0000832 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner70d67a92008-01-06 22:42:25 +0000833 // "If both the operands have structure or union type, the result has
834 // that type." This implies that CV qualifiers are dropped.
835 return lexT.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000837
838 // C99 6.5.15p5: "If both operands have void type, the result has void type."
839 if (lexT->isVoidType() && rexT->isVoidType())
840 return lexT.getUnqualifiedType();
Steve Naroffb6d54e52008-01-08 01:11:38 +0000841
842 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
843 // the type of the other operand."
844 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000845 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000846 return lexT;
847 }
848 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000849 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000850 return rexT;
851 }
Chris Lattnerbd57d362008-01-06 22:50:31 +0000852 // Handle the case where both operands are pointers before we handle null
853 // pointer constants in case both operands are null pointer constants.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000854 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
855 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
856 // get the "pointed to" types
857 QualType lhptee = LHSPT->getPointeeType();
858 QualType rhptee = RHSPT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000859
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000860 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
861 if (lhptee->isVoidType() &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000862 rhptee->isIncompleteOrObjectType()) {
Chris Lattnerf46699c2008-02-20 20:55:12 +0000863 // Figure out necessary qualifiers (C99 6.5.15p6)
864 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmana541d532008-02-10 22:59:36 +0000865 QualType destType = Context.getPointerType(destPointee);
866 ImpCastExprToType(lex, destType); // add qualifiers if necessary
867 ImpCastExprToType(rex, destType); // promote to void*
868 return destType;
869 }
Chris Lattnerd805bec2008-04-02 06:59:01 +0000870 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattnerf46699c2008-02-20 20:55:12 +0000871 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmana541d532008-02-10 22:59:36 +0000872 QualType destType = Context.getPointerType(destPointee);
873 ImpCastExprToType(lex, destType); // add qualifiers if necessary
874 ImpCastExprToType(rex, destType); // promote to void*
875 return destType;
876 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000877
Steve Naroffec0550f2007-10-15 20:41:53 +0000878 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
879 rhptee.getUnqualifiedType())) {
Steve Naroffc0ff1ca2008-02-01 22:44:48 +0000880 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000881 lexT.getAsString(), rexT.getAsString(),
882 lex->getSourceRange(), rex->getSourceRange());
Eli Friedmanb1284ac2008-01-30 17:02:03 +0000883 // In this situation, we assume void* type. No especially good
884 // reason, but this is what gcc does, and we do have to pick
885 // to get a consistent AST.
886 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
887 ImpCastExprToType(lex, voidPtrTy);
888 ImpCastExprToType(rex, voidPtrTy);
889 return voidPtrTy;
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000890 }
891 // The pointer types are compatible.
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000892 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
893 // differently qualified versions of compatible types, the result type is
894 // a pointer to an appropriately qualified version of the *composite*
895 // type.
Chris Lattnerbd57d362008-01-06 22:50:31 +0000896 // FIXME: Need to return the composite type.
Eli Friedmana541d532008-02-10 22:59:36 +0000897 // FIXME: Need to add qualifiers
Chris Lattnerbd57d362008-01-06 22:50:31 +0000898 return lexT;
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 }
900 }
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000901
Chris Lattner70d67a92008-01-06 22:42:25 +0000902 // Otherwise, the operands are not compatible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff49b45262007-07-13 16:58:59 +0000904 lexT.getAsString(), rexT.getAsString(),
905 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 return QualType();
907}
908
Steve Narofff69936d2007-09-16 03:34:24 +0000909/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Reid Spencer5f016e22007-07-11 17:01:13 +0000910/// in the case of a the GNU conditional expr extension.
Steve Narofff69936d2007-09-16 03:34:24 +0000911Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 SourceLocation ColonLoc,
913 ExprTy *Cond, ExprTy *LHS,
914 ExprTy *RHS) {
Chris Lattner26824902007-07-16 21:39:03 +0000915 Expr *CondExpr = (Expr *) Cond;
916 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattnera21ddb32007-11-26 01:40:58 +0000917
918 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
919 // was the condition.
920 bool isLHSNull = LHSExpr == 0;
921 if (isLHSNull)
922 LHSExpr = CondExpr;
923
Chris Lattner26824902007-07-16 21:39:03 +0000924 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
925 RHSExpr, QuestionLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 if (result.isNull())
927 return true;
Chris Lattnera21ddb32007-11-26 01:40:58 +0000928 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
929 RHSExpr, result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000930}
931
Steve Naroffb291ab62007-08-28 23:30:39 +0000932/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffb3c2b882008-01-29 02:42:22 +0000933/// do not have a prototype. Arguments that have type float are promoted to
934/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner925e60d2007-12-28 05:29:59 +0000935void Sema::DefaultArgumentPromotion(Expr *&Expr) {
936 QualType Ty = Expr->getType();
937 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffb291ab62007-08-28 23:30:39 +0000938
Chris Lattner925e60d2007-12-28 05:29:59 +0000939 if (Ty == Context.FloatTy)
Chris Lattner1e0a3902008-01-16 19:17:22 +0000940 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffb3c2b882008-01-29 02:42:22 +0000941 else
942 UsualUnaryConversions(Expr);
Steve Naroffb291ab62007-08-28 23:30:39 +0000943}
944
Steve Narofffa2eaab2007-07-15 02:02:06 +0000945/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattnerf0467b32008-04-02 04:24:33 +0000946void Sema::DefaultFunctionArrayConversion(Expr *&E) {
947 QualType Ty = E->getType();
948 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendling08ad47c2007-07-17 03:52:31 +0000949
Chris Lattnerf0467b32008-04-02 04:24:33 +0000950 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattner423a3c92008-04-02 17:45:06 +0000951 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattnerf0467b32008-04-02 04:24:33 +0000952 Ty = E->getType();
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000953 }
Chris Lattnerf0467b32008-04-02 04:24:33 +0000954 if (Ty->isFunctionType())
955 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattnere6327742008-04-02 05:18:44 +0000956 else if (Ty->isArrayType())
957 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Reid Spencer5f016e22007-07-11 17:01:13 +0000958}
959
Nate Begemane2ce1d92008-01-17 17:46:27 +0000960/// UsualUnaryConversions - Performs various conversions that are common to most
Reid Spencer5f016e22007-07-11 17:01:13 +0000961/// operators (C99 6.3). The conversions of array and function types are
962/// sometimes surpressed. For example, the array->pointer conversion doesn't
963/// apply if the array is an argument to the sizeof or address (&) operators.
964/// In these instances, this routine should *not* be called.
Chris Lattner925e60d2007-12-28 05:29:59 +0000965Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
966 QualType Ty = Expr->getType();
967 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000968
Chris Lattner925e60d2007-12-28 05:29:59 +0000969 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattner423a3c92008-04-02 17:45:06 +0000970 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner925e60d2007-12-28 05:29:59 +0000971 Ty = Expr->getType();
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000972 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000973 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattner1e0a3902008-01-16 19:17:22 +0000974 ImpCastExprToType(Expr, Context.IntTy);
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000975 else
Chris Lattner925e60d2007-12-28 05:29:59 +0000976 DefaultFunctionArrayConversion(Expr);
977
978 return Expr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000979}
980
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000981/// UsualArithmeticConversions - Performs various conversions that are common to
Reid Spencer5f016e22007-07-11 17:01:13 +0000982/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
983/// routine returns the first non-arithmetic type found. The client is
984/// responsible for emitting appropriate error diagnostics.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000985/// FIXME: verify the conversion rules for "complex int" are consistent with
986/// GCC.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +0000987QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
988 bool isCompAssign) {
Steve Naroff8702a0f2007-08-25 19:54:59 +0000989 if (!isCompAssign) {
990 UsualUnaryConversions(lhsExpr);
991 UsualUnaryConversions(rhsExpr);
992 }
Steve Naroff3187e202007-10-18 18:55:53 +0000993 // For conversion purposes, we ignore any qualifiers.
994 // For example, "const float" and "float" are equivalent.
Steve Narofff68a63f2007-11-10 19:45:54 +0000995 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
996 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000997
998 // If both types are identical, no conversion is needed.
Steve Naroff3187e202007-10-18 18:55:53 +0000999 if (lhs == rhs)
1000 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001001
1002 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1003 // The caller can deal with this (e.g. pointer + int).
Steve Naroffa4332e22007-07-17 00:58:39 +00001004 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001005 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001006
1007 // At this point, we have two different arithmetic types.
1008
1009 // Handle complex types first (C99 6.3.1.8p1).
1010 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff02f62a92008-01-15 19:36:10 +00001011 // if we have an integer operand, the result is the complex type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001012 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001013 // convert the rhs to the lhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001014 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001015 return lhs;
Steve Naroff02f62a92008-01-15 19:36:10 +00001016 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001017 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001018 // convert the lhs to the rhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001019 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001020 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001021 }
Steve Narofff1448a02007-08-27 01:27:54 +00001022 // This handles complex/complex, complex/float, or float/complex.
1023 // When both operands are complex, the shorter operand is converted to the
1024 // type of the longer, and that is the type of the result. This corresponds
1025 // to what is done when combining two real floating-point operands.
1026 // The fun begins when size promotion occur across type domains.
1027 // From H&S 6.3.4: When one operand is complex and the other is a real
1028 // floating-point type, the less precise type is converted, within it's
1029 // real or complex domain, to the precision of the other type. For example,
1030 // when combining a "long double" with a "double _Complex", the
1031 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnera75cea32008-04-06 23:38:49 +00001032 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Narofffb0d4962007-08-27 15:30:22 +00001033
1034 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff55fe4552007-08-27 21:32:55 +00001035 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1036 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001037 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff55fe4552007-08-27 21:32:55 +00001038 } else if (result < 0) { // The right side is bigger, convert lhs.
1039 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1040 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001041 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff55fe4552007-08-27 21:32:55 +00001042 }
1043 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1044 // domains match. This is a requirement for our implementation, C99
1045 // does not require this promotion.
1046 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1047 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff29960362007-08-27 21:43:43 +00001048 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001049 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff29960362007-08-27 21:43:43 +00001050 return rhs;
Steve Naroff55fe4552007-08-27 21:32:55 +00001051 } else { // handle "_Complex double, double".
Steve Naroff29960362007-08-27 21:43:43 +00001052 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001053 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff29960362007-08-27 21:43:43 +00001054 return lhs;
Steve Naroff55fe4552007-08-27 21:32:55 +00001055 }
Steve Naroffa4332e22007-07-17 00:58:39 +00001056 }
Steve Naroff29960362007-08-27 21:43:43 +00001057 return lhs; // The domain/size match exactly.
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 // Now handle "real" floating types (i.e. float, double, long double).
1060 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1061 // if we have an integer operand, the result is the real floating type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001062 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001063 // convert rhs to the lhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001064 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001065 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001066 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001067 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001068 // convert lhs to the rhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001069 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001070 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001071 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001072 // We have two real floating types, float/complex combos were handled above.
1073 // Convert the smaller operand to the bigger result.
Chris Lattnera75cea32008-04-06 23:38:49 +00001074 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Narofffb0d4962007-08-27 15:30:22 +00001075
1076 if (result > 0) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001077 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001078 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001079 }
Steve Narofffb0d4962007-08-27 15:30:22 +00001080 if (result < 0) { // convert the lhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001081 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Narofffb0d4962007-08-27 15:30:22 +00001082 return rhs;
1083 }
1084 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 }
Steve Naroff02f62a92008-01-15 19:36:10 +00001086 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1087 // Handle GCC complex int extension.
Steve Naroff02f62a92008-01-15 19:36:10 +00001088 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001089 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff02f62a92008-01-15 19:36:10 +00001090
Eli Friedman8e54ad02008-02-08 01:19:44 +00001091 if (lhsComplexInt && rhsComplexInt) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001092 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1093 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman8c4e5db2008-02-08 01:24:30 +00001094 // convert the rhs
1095 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1096 return lhs;
Eli Friedman8e54ad02008-02-08 01:19:44 +00001097 }
1098 if (!isCompAssign)
Eli Friedman8c4e5db2008-02-08 01:24:30 +00001099 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman8e54ad02008-02-08 01:19:44 +00001100 return rhs;
1101 } else if (lhsComplexInt && rhs->isIntegerType()) {
1102 // convert the rhs to the lhs complex type.
1103 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1104 return lhs;
1105 } else if (rhsComplexInt && lhs->isIntegerType()) {
1106 // convert the lhs to the rhs complex type.
1107 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1108 return rhs;
1109 }
Steve Naroff02f62a92008-01-15 19:36:10 +00001110 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001111 // Finally, we have two differing integer types.
Chris Lattner7cfeb082008-04-06 23:55:33 +00001112 if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001113 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001114 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001115 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001116 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001117 return rhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001118}
1119
1120// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1121// being closely modeled after the C99 spec:-). The odd characteristic of this
1122// routine is it effectively iqnores the qualifiers on the top level pointee.
1123// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1124// FIXME: add a couple examples in this comment.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001125Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001126Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1127 QualType lhptee, rhptee;
1128
1129 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner2dcb6bb2007-07-31 21:27:01 +00001130 lhptee = lhsType->getAsPointerType()->getPointeeType();
1131 rhptee = rhsType->getAsPointerType()->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001132
1133 // make sure we operate on the canonical type
1134 lhptee = lhptee.getCanonicalType();
1135 rhptee = rhptee.getCanonicalType();
1136
Chris Lattner5cf216b2008-01-04 18:04:52 +00001137 AssignConvertType ConvTy = Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
1139 // C99 6.5.16.1p1: This following citation is common to constraints
1140 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1141 // qualifiers of the type *pointed to* by the right;
Chris Lattnerf46699c2008-02-20 20:55:12 +00001142 // FIXME: Handle ASQualType
1143 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1144 rhptee.getCVRQualifiers())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001145 ConvTy = CompatiblePointerDiscardsQualifiers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001146
1147 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1148 // incomplete type and the other is a pointer to a qualified or unqualified
1149 // version of void...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001150 if (lhptee->isVoidType()) {
Chris Lattnerd805bec2008-04-02 06:59:01 +00001151 if (rhptee->isIncompleteOrObjectType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001152 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001153
1154 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerd805bec2008-04-02 06:59:01 +00001155 assert(rhptee->isFunctionType());
1156 return FunctionVoidPointer;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001157 }
1158
1159 if (rhptee->isVoidType()) {
Chris Lattnerd805bec2008-04-02 06:59:01 +00001160 if (lhptee->isIncompleteOrObjectType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001161 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001162
1163 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerd805bec2008-04-02 06:59:01 +00001164 assert(lhptee->isFunctionType());
1165 return FunctionVoidPointer;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001166 }
1167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1169 // unqualified versions of compatible types, ...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001170 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1171 rhptee.getUnqualifiedType()))
1172 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner5cf216b2008-01-04 18:04:52 +00001173 return ConvTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001174}
1175
1176/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1177/// has code to accommodate several GCC extensions when type checking
1178/// pointers. Here are some objectionable examples that GCC considers warnings:
1179///
1180/// int a, *pint;
1181/// short *pshort;
1182/// struct foo *pfoo;
1183///
1184/// pint = pshort; // warning: assignment from incompatible pointer type
1185/// a = pint; // warning: assignment makes integer from pointer without a cast
1186/// pint = a; // warning: assignment makes pointer from integer without a cast
1187/// pint = pfoo; // warning: assignment from incompatible pointer type
1188///
1189/// As a result, the code for dealing with pointers is more complex than the
1190/// C99 spec dictates.
Reid Spencer5f016e22007-07-11 17:01:13 +00001191///
Chris Lattner5cf216b2008-01-04 18:04:52 +00001192Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001193Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnerfc144e22008-01-04 23:18:45 +00001194 // Get canonical types. We're not formatting these types, just comparing
1195 // them.
1196 lhsType = lhsType.getCanonicalType();
1197 rhsType = rhsType.getCanonicalType();
1198
1199 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerd2656dd2008-01-07 17:51:46 +00001200 return Compatible; // Common case: fast path an exact match.
Steve Naroff700204c2007-07-24 21:46:40 +00001201
Anders Carlsson793680e2007-10-12 23:56:29 +00001202 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattner8f8fc7b2008-04-07 06:52:53 +00001203 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlsson793680e2007-10-12 23:56:29 +00001204 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001205 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001206 }
Chris Lattnerfc144e22008-01-04 23:18:45 +00001207
Chris Lattnereca7be62008-04-07 05:30:13 +00001208 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1209 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001210 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001211 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001212 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001213
Chris Lattner78eca282008-04-07 06:49:41 +00001214 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Chris Lattnere8b3e962008-01-04 23:32:24 +00001215 // For OCUVector, allow vector splats; float -> <n x float>
Chris Lattner78eca282008-04-07 06:49:41 +00001216 if (const OCUVectorType *LV = dyn_cast<OCUVectorType>(lhsType)) {
Chris Lattnere8b3e962008-01-04 23:32:24 +00001217 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1218 return Compatible;
1219 }
1220
1221 // If LHS and RHS are both vectors of integer or both vectors of floating
1222 // point types, and the total vector length is the same, allow the
1223 // conversion. This is a bitcast; no bits are changed but the result type
1224 // is different.
1225 if (getLangOptions().LaxVectorConversions &&
1226 lhsType->isVectorType() && rhsType->isVectorType()) {
1227 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1228 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner98be4942008-03-05 18:54:05 +00001229 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begeman4119d1a2007-12-30 02:59:45 +00001230 return Compatible;
1231 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001232 }
1233 return Incompatible;
1234 }
1235
1236 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001238
Chris Lattner78eca282008-04-07 06:49:41 +00001239 if (isa<PointerType>(lhsType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 if (rhsType->isIntegerType())
Chris Lattnerb7b61152008-01-04 18:22:42 +00001241 return IntToPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +00001242
Chris Lattner78eca282008-04-07 06:49:41 +00001243 if (isa<PointerType>(rhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001245 return Incompatible;
1246 }
1247
Chris Lattner78eca282008-04-07 06:49:41 +00001248 if (isa<PointerType>(rhsType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Chris Lattner78eca282008-04-07 06:49:41 +00001250 if (lhsType->isIntegerType() && lhsType != Context.BoolTy)
Chris Lattnerb7b61152008-01-04 18:22:42 +00001251 return PointerToInt;
Reid Spencer5f016e22007-07-11 17:01:13 +00001252
Chris Lattner78eca282008-04-07 06:49:41 +00001253 if (isa<PointerType>(lhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001255 return Incompatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001256 }
1257
1258 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner78eca282008-04-07 06:49:41 +00001259 if (Context.typesAreCompatible(lhsType, rhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 return Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 }
1262 return Incompatible;
1263}
1264
Chris Lattner5cf216b2008-01-04 18:04:52 +00001265Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001266Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff529a4ad2007-11-27 17:58:44 +00001267 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1268 // a null pointer constant.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001269 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian9d3185e2008-01-03 18:46:52 +00001270 && rExpr->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001271 ImpCastExprToType(rExpr, lhsType);
Steve Naroff529a4ad2007-11-27 17:58:44 +00001272 return Compatible;
1273 }
Chris Lattner943140e2007-10-16 02:55:40 +00001274 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroff90045e82007-07-13 23:32:42 +00001275 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff08d92e42007-09-15 18:49:24 +00001276 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroff90045e82007-07-13 23:32:42 +00001277 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner943140e2007-10-16 02:55:40 +00001278 //
1279 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1280 // are better understood.
1281 if (!lhsType->isReferenceType())
1282 DefaultFunctionArrayConversion(rExpr);
Steve Narofff1120de2007-08-24 22:33:52 +00001283
Chris Lattner5cf216b2008-01-04 18:04:52 +00001284 Sema::AssignConvertType result =
1285 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Narofff1120de2007-08-24 22:33:52 +00001286
1287 // C99 6.5.16.1p2: The value of the right operand is converted to the
1288 // type of the assignment expression.
1289 if (rExpr->getType() != lhsType)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001290 ImpCastExprToType(rExpr, lhsType);
Steve Narofff1120de2007-08-24 22:33:52 +00001291 return result;
Steve Naroff90045e82007-07-13 23:32:42 +00001292}
1293
Chris Lattner5cf216b2008-01-04 18:04:52 +00001294Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001295Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1296 return CheckAssignmentConstraints(lhsType, rhsType);
1297}
1298
Chris Lattnerca5eede2007-12-12 05:47:28 +00001299QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 Diag(loc, diag::err_typecheck_invalid_operands,
1301 lex->getType().getAsString(), rex->getType().getAsString(),
1302 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnerca5eede2007-12-12 05:47:28 +00001303 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001304}
1305
Steve Naroff49b45262007-07-13 16:58:59 +00001306inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1307 Expr *&rex) {
Nate Begeman1330b0e2008-04-04 01:30:25 +00001308 // For conversion purposes, we ignore any qualifiers.
1309 // For example, "const float" and "float" are equivalent.
1310 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1311 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001312
1313 // make sure the vector types are identical.
Nate Begeman1330b0e2008-04-04 01:30:25 +00001314 if (lhsType == rhsType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 return lhsType;
Nate Begeman4119d1a2007-12-30 02:59:45 +00001316
1317 // if the lhs is an ocu vector and the rhs is a scalar of the same type,
1318 // promote the rhs to the vector type.
1319 if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) {
1320 if (V->getElementType().getCanonicalType().getTypePtr()
1321 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001322 ImpCastExprToType(rex, lhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001323 return lhsType;
1324 }
1325 }
1326
1327 // if the rhs is an ocu vector and the lhs is a scalar of the same type,
1328 // promote the lhs to the vector type.
1329 if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) {
1330 if (V->getElementType().getCanonicalType().getTypePtr()
1331 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001332 ImpCastExprToType(lex, rhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001333 return rhsType;
1334 }
1335 }
1336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 // You cannot convert between vector values of different size.
1338 Diag(loc, diag::err_typecheck_vector_not_convertable,
1339 lex->getType().getAsString(), rex->getType().getAsString(),
1340 lex->getSourceRange(), rex->getSourceRange());
1341 return QualType();
1342}
1343
1344inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001345 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001346{
Steve Naroff90045e82007-07-13 23:32:42 +00001347 QualType lhsType = lex->getType(), rhsType = rex->getType();
1348
1349 if (lhsType->isVectorType() || rhsType->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 return CheckVectorOperands(loc, lex, rex);
Steve Naroff49b45262007-07-13 16:58:59 +00001351
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001352 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001353
Steve Naroffa4332e22007-07-17 00:58:39 +00001354 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001355 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001356 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001357}
1358
1359inline QualType Sema::CheckRemainderOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001360 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001361{
Steve Naroff90045e82007-07-13 23:32:42 +00001362 QualType lhsType = lex->getType(), rhsType = rex->getType();
1363
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001364 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001365
Steve Naroffa4332e22007-07-17 00:58:39 +00001366 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001367 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001368 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001369}
1370
1371inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001372 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001373{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001374 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff49b45262007-07-13 16:58:59 +00001375 return CheckVectorOperands(loc, lex, rex);
1376
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001377 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff3e5e5562007-07-16 22:23:01 +00001378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 // handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001380 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001381 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001382
Steve Naroffa4332e22007-07-17 00:58:39 +00001383 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1384 return lex->getType();
1385 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1386 return rex->getType();
Chris Lattnerca5eede2007-12-12 05:47:28 +00001387 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001388}
1389
Chris Lattnereca7be62008-04-07 05:30:13 +00001390// C99 6.5.6
1391QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1392 SourceLocation loc, bool isCompAssign) {
Steve Naroff3e5e5562007-07-16 22:23:01 +00001393 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001395
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001396 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001397
Chris Lattner6e4ab612007-12-09 21:53:25 +00001398 // Enforce type constraints: C99 6.5.6p3.
1399
1400 // Handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001401 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001402 return compType;
Chris Lattner6e4ab612007-12-09 21:53:25 +00001403
1404 // Either ptr - int or ptr - ptr.
1405 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001406 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001407
Chris Lattner6e4ab612007-12-09 21:53:25 +00001408 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff2565eef2008-01-29 18:58:14 +00001409 if (!lpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001410 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001411 if (lpointee->isVoidType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001412 Diag(loc, diag::ext_gnu_void_ptr,
1413 lex->getSourceRange(), rex->getSourceRange());
1414 } else {
1415 Diag(loc, diag::err_typecheck_sub_ptr_object,
1416 lex->getType().getAsString(), lex->getSourceRange());
1417 return QualType();
1418 }
1419 }
1420
1421 // The result type of a pointer-int computation is the pointer type.
1422 if (rex->getType()->isIntegerType())
1423 return lex->getType();
Steve Naroff3e5e5562007-07-16 22:23:01 +00001424
Chris Lattner6e4ab612007-12-09 21:53:25 +00001425 // Handle pointer-pointer subtractions.
1426 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001427 QualType rpointee = RHSPTy->getPointeeType();
1428
Chris Lattner6e4ab612007-12-09 21:53:25 +00001429 // RHS must be an object type, unless void (GNU).
Steve Naroff2565eef2008-01-29 18:58:14 +00001430 if (!rpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001431 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001432 if (rpointee->isVoidType()) {
1433 if (!lpointee->isVoidType())
Chris Lattner6e4ab612007-12-09 21:53:25 +00001434 Diag(loc, diag::ext_gnu_void_ptr,
1435 lex->getSourceRange(), rex->getSourceRange());
1436 } else {
1437 Diag(loc, diag::err_typecheck_sub_ptr_object,
1438 rex->getType().getAsString(), rex->getSourceRange());
1439 return QualType();
1440 }
1441 }
1442
1443 // Pointee types must be compatible.
Steve Naroff2565eef2008-01-29 18:58:14 +00001444 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1445 rpointee.getUnqualifiedType())) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001446 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1447 lex->getType().getAsString(), rex->getType().getAsString(),
1448 lex->getSourceRange(), rex->getSourceRange());
1449 return QualType();
1450 }
1451
1452 return Context.getPointerDiffType();
1453 }
1454 }
1455
Chris Lattnerca5eede2007-12-12 05:47:28 +00001456 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001457}
1458
Chris Lattnereca7be62008-04-07 05:30:13 +00001459// C99 6.5.7
1460QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1461 bool isCompAssign) {
Chris Lattnerca5eede2007-12-12 05:47:28 +00001462 // C99 6.5.7p2: Each of the operands shall have integer type.
1463 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1464 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001465
Chris Lattnerca5eede2007-12-12 05:47:28 +00001466 // Shifts don't perform usual arithmetic conversions, they just do integer
1467 // promotions on each operand. C99 6.5.7p3
Chris Lattner1dcf2c82007-12-13 07:28:16 +00001468 if (!isCompAssign)
1469 UsualUnaryConversions(lex);
Chris Lattnerca5eede2007-12-12 05:47:28 +00001470 UsualUnaryConversions(rex);
1471
1472 // "The type of the result is that of the promoted left operand."
1473 return lex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001474}
1475
Chris Lattnereca7be62008-04-07 05:30:13 +00001476// C99 6.5.8
1477QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1478 bool isRelational) {
Chris Lattnera5937dd2007-08-26 01:18:55 +00001479 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff30bf7712007-08-10 18:26:40 +00001480 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1481 UsualArithmeticConversions(lex, rex);
1482 else {
1483 UsualUnaryConversions(lex);
1484 UsualUnaryConversions(rex);
1485 }
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001486 QualType lType = lex->getType();
1487 QualType rType = rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001488
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001489 // For non-floating point types, check for self-comparisons of the form
1490 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1491 // often indicate logic errors in the program.
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001492 if (!lType->isFloatingType()) {
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001493 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1494 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001495 if (DRL->getDecl() == DRR->getDecl())
1496 Diag(loc, diag::warn_selfcomparison);
1497 }
1498
Chris Lattnera5937dd2007-08-26 01:18:55 +00001499 if (isRelational) {
1500 if (lType->isRealType() && rType->isRealType())
1501 return Context.IntTy;
1502 } else {
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001503 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001504 if (lType->isFloatingType()) {
1505 assert (rType->isFloatingType());
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001506 CheckFloatComparison(loc,lex,rex);
Ted Kremenek6a261552007-10-29 16:40:01 +00001507 }
1508
Chris Lattnera5937dd2007-08-26 01:18:55 +00001509 if (lType->isArithmeticType() && rType->isArithmeticType())
1510 return Context.IntTy;
1511 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001512
Chris Lattnerd28f8152007-08-26 01:10:14 +00001513 bool LHSIsNull = lex->isNullPointerConstant(Context);
1514 bool RHSIsNull = rex->isNullPointerConstant(Context);
1515
Chris Lattnera5937dd2007-08-26 01:18:55 +00001516 // All of the following pointer related warnings are GCC extensions, except
1517 // when handling null pointer constants. One day, we can consider making them
1518 // errors (when -pedantic-errors is enabled).
Steve Naroff77878cc2007-08-27 04:08:11 +00001519 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattnerbc896f52008-04-03 05:07:25 +00001520 QualType LCanPointeeTy =
1521 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1522 QualType RCanPointeeTy =
1523 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001524
Steve Naroff66296cb2007-11-13 14:57:38 +00001525 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattnerbc896f52008-04-03 05:07:25 +00001526 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1527 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1528 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001529 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1530 lType.getAsString(), rType.getAsString(),
1531 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001533 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001534 return Context.IntTy;
1535 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001536 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
Chris Lattnereca7be62008-04-07 05:30:13 +00001537 && ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001538 ImpCastExprToType(rex, lType);
Fariborz Jahanian7359f042007-12-20 01:06:58 +00001539 return Context.IntTy;
1540 }
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001541 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001542 if (!RHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001543 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1544 lType.getAsString(), rType.getAsString(),
1545 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001546 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001547 return Context.IntTy;
1548 }
1549 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001550 if (!LHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001551 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1552 lType.getAsString(), rType.getAsString(),
1553 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001554 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001555 return Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 }
Chris Lattnerca5eede2007-12-12 05:47:28 +00001557 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001558}
1559
Reid Spencer5f016e22007-07-11 17:01:13 +00001560inline QualType Sema::CheckBitwiseOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001561 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001562{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001563 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001565
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001566 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001567
Steve Naroffa4332e22007-07-17 00:58:39 +00001568 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001569 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001570 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001571}
1572
1573inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff49b45262007-07-13 16:58:59 +00001574 Expr *&lex, Expr *&rex, SourceLocation loc)
Reid Spencer5f016e22007-07-11 17:01:13 +00001575{
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001576 UsualUnaryConversions(lex);
1577 UsualUnaryConversions(rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001578
Steve Naroffa4332e22007-07-17 00:58:39 +00001579 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 return Context.IntTy;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001581 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001582}
1583
1584inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Narofff1120de2007-08-24 22:33:52 +00001585 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001586{
1587 QualType lhsType = lex->getType();
1588 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001589 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1590
1591 switch (mlval) { // C99 6.5.16p2
Chris Lattner5cf216b2008-01-04 18:04:52 +00001592 case Expr::MLV_Valid:
1593 break;
1594 case Expr::MLV_ConstQualified:
1595 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1596 return QualType();
1597 case Expr::MLV_ArrayType:
1598 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1599 lhsType.getAsString(), lex->getSourceRange());
1600 return QualType();
1601 case Expr::MLV_NotObjectType:
1602 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1603 lhsType.getAsString(), lex->getSourceRange());
1604 return QualType();
1605 case Expr::MLV_InvalidExpression:
1606 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1607 lex->getSourceRange());
1608 return QualType();
1609 case Expr::MLV_IncompleteType:
1610 case Expr::MLV_IncompleteVoidType:
1611 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1612 lhsType.getAsString(), lex->getSourceRange());
1613 return QualType();
1614 case Expr::MLV_DuplicateVectorComponents:
1615 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1616 lex->getSourceRange());
1617 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001619
Chris Lattner5cf216b2008-01-04 18:04:52 +00001620 AssignConvertType ConvTy;
1621 if (compoundType.isNull())
1622 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1623 else
1624 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1625
1626 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1627 rex, "assigning"))
1628 return QualType();
1629
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 // C99 6.5.16p3: The type of an assignment expression is the type of the
1631 // left operand unless the left operand has qualified type, in which case
1632 // it is the unqualified version of the type of the left operand.
1633 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1634 // is converted to the type of the assignment expression (above).
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001635 // C++ 5.17p1: the type of the assignment expression is that of its left
1636 // oprdu.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001637 return lhsType.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001638}
1639
1640inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff49b45262007-07-13 16:58:59 +00001641 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001642 UsualUnaryConversions(rex);
1643 return rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001644}
1645
Steve Naroff49b45262007-07-13 16:58:59 +00001646/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1647/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Reid Spencer5f016e22007-07-11 17:01:13 +00001648QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff49b45262007-07-13 16:58:59 +00001649 QualType resType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 assert(!resType.isNull() && "no type for increment/decrement expression");
1651
Steve Naroff084f9ed2007-08-24 17:20:07 +00001652 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffd848a382007-11-11 14:15:57 +00001653 if (const PointerType *pt = resType->getAsPointerType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1655 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1656 resType.getAsString(), op->getSourceRange());
1657 return QualType();
1658 }
Steve Naroff084f9ed2007-08-24 17:20:07 +00001659 } else if (!resType->isRealType()) {
1660 if (resType->isComplexType())
1661 // C99 does not support ++/-- on complex types.
1662 Diag(OpLoc, diag::ext_integer_increment_complex,
1663 resType.getAsString(), op->getSourceRange());
1664 else {
1665 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1666 resType.getAsString(), op->getSourceRange());
1667 return QualType();
1668 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 }
Steve Naroffdd10e022007-08-23 21:37:33 +00001670 // At this point, we know we have a real, complex or pointer type.
1671 // Now make sure the operand is a modifiable lvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1673 if (mlval != Expr::MLV_Valid) {
1674 // FIXME: emit a more precise diagnostic...
1675 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1676 op->getSourceRange());
1677 return QualType();
1678 }
1679 return resType;
1680}
1681
Anders Carlsson369dee42008-02-01 07:15:58 +00001682/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Reid Spencer5f016e22007-07-11 17:01:13 +00001683/// This routine allows us to typecheck complex/recursive expressions
1684/// where the declaration is needed for type checking. Here are some
1685/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattnerf0467b32008-04-02 04:24:33 +00001686static ValueDecl *getPrimaryDecl(Expr *E) {
1687 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 case Stmt::DeclRefExprClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001689 return cast<DeclRefExpr>(E)->getDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001690 case Stmt::MemberExprClass:
Chris Lattnerf82228f2007-11-16 17:46:48 +00001691 // Fields cannot be declared with a 'register' storage class.
1692 // &X->f is always ok, even if X is declared register.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001693 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnerf82228f2007-11-16 17:46:48 +00001694 return 0;
Chris Lattnerf0467b32008-04-02 04:24:33 +00001695 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson369dee42008-02-01 07:15:58 +00001696 case Stmt::ArraySubscriptExprClass: {
1697 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1698
Chris Lattnerf0467b32008-04-02 04:24:33 +00001699 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlssonf2a4b842008-02-01 16:01:31 +00001700 if (!VD || VD->getType()->isPointerType())
Anders Carlsson369dee42008-02-01 07:15:58 +00001701 return 0;
1702 else
1703 return VD;
1704 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 case Stmt::UnaryOperatorClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001706 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 case Stmt::ParenExprClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001708 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf82228f2007-11-16 17:46:48 +00001709 case Stmt::ImplicitCastExprClass:
1710 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001711 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 default:
1713 return 0;
1714 }
1715}
1716
1717/// CheckAddressOfOperand - The operand of & must be either a function
1718/// designator or an lvalue designating an object. If it is an lvalue, the
1719/// object cannot be declared with storage class register or be a bit field.
1720/// Note: The usual conversions are *not* applied to the operand of the &
1721/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1722QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff08f19672008-01-13 17:10:08 +00001723 if (getLangOptions().C99) {
1724 // Implement C99-only parts of addressof rules.
1725 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1726 if (uOp->getOpcode() == UnaryOperator::Deref)
1727 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1728 // (assuming the deref expression is valid).
1729 return uOp->getSubExpr()->getType();
1730 }
1731 // Technically, there should be a check for array subscript
1732 // expressions here, but the result of one is always an lvalue anyway.
1733 }
Anders Carlsson369dee42008-02-01 07:15:58 +00001734 ValueDecl *dcl = getPrimaryDecl(op);
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 Expr::isLvalueResult lval = op->isLvalue();
1736
1737 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnerf82228f2007-11-16 17:46:48 +00001738 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1739 // FIXME: emit more specific diag...
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1741 op->getSourceRange());
1742 return QualType();
1743 }
Steve Naroffbcb2b612008-02-29 23:30:25 +00001744 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1745 if (MemExpr->getMemberDecl()->isBitField()) {
1746 Diag(OpLoc, diag::err_typecheck_address_of,
1747 std::string("bit-field"), op->getSourceRange());
1748 return QualType();
1749 }
1750 // Check for Apple extension for accessing vector components.
1751 } else if (isa<ArraySubscriptExpr>(op) &&
1752 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1753 Diag(OpLoc, diag::err_typecheck_address_of,
1754 std::string("vector"), op->getSourceRange());
1755 return QualType();
1756 } else if (dcl) { // C99 6.5.3.2p1
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 // We have an lvalue with a decl. Make sure the decl is not declared
1758 // with the register storage-class specifier.
1759 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1760 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroffbcb2b612008-02-29 23:30:25 +00001761 Diag(OpLoc, diag::err_typecheck_address_of,
1762 std::string("register variable"), op->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 return QualType();
1764 }
1765 } else
1766 assert(0 && "Unknown/unexpected decl type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 }
1768 // If the operand has type "type", the result has type "pointer to type".
1769 return Context.getPointerType(op->getType());
1770}
1771
1772QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001773 UsualUnaryConversions(op);
1774 QualType qType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001775
Chris Lattnerbefee482007-07-31 16:53:04 +00001776 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff08f19672008-01-13 17:10:08 +00001777 // Note that per both C89 and C99, this is always legal, even
1778 // if ptype is an incomplete type or void.
1779 // It would be possible to warn about dereferencing a
1780 // void pointer, but it's completely well-defined,
1781 // and such a warning is unlikely to catch any mistakes.
1782 return PT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 }
1784 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1785 qType.getAsString(), op->getSourceRange());
1786 return QualType();
1787}
1788
1789static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1790 tok::TokenKind Kind) {
1791 BinaryOperator::Opcode Opc;
1792 switch (Kind) {
1793 default: assert(0 && "Unknown binop!");
1794 case tok::star: Opc = BinaryOperator::Mul; break;
1795 case tok::slash: Opc = BinaryOperator::Div; break;
1796 case tok::percent: Opc = BinaryOperator::Rem; break;
1797 case tok::plus: Opc = BinaryOperator::Add; break;
1798 case tok::minus: Opc = BinaryOperator::Sub; break;
1799 case tok::lessless: Opc = BinaryOperator::Shl; break;
1800 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1801 case tok::lessequal: Opc = BinaryOperator::LE; break;
1802 case tok::less: Opc = BinaryOperator::LT; break;
1803 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1804 case tok::greater: Opc = BinaryOperator::GT; break;
1805 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1806 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1807 case tok::amp: Opc = BinaryOperator::And; break;
1808 case tok::caret: Opc = BinaryOperator::Xor; break;
1809 case tok::pipe: Opc = BinaryOperator::Or; break;
1810 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1811 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1812 case tok::equal: Opc = BinaryOperator::Assign; break;
1813 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1814 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1815 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1816 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1817 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1818 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1819 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1820 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1821 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1822 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1823 case tok::comma: Opc = BinaryOperator::Comma; break;
1824 }
1825 return Opc;
1826}
1827
1828static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1829 tok::TokenKind Kind) {
1830 UnaryOperator::Opcode Opc;
1831 switch (Kind) {
1832 default: assert(0 && "Unknown unary op!");
1833 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1834 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1835 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1836 case tok::star: Opc = UnaryOperator::Deref; break;
1837 case tok::plus: Opc = UnaryOperator::Plus; break;
1838 case tok::minus: Opc = UnaryOperator::Minus; break;
1839 case tok::tilde: Opc = UnaryOperator::Not; break;
1840 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1841 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1842 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1843 case tok::kw___real: Opc = UnaryOperator::Real; break;
1844 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1845 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1846 }
1847 return Opc;
1848}
1849
1850// Binary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001851Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 ExprTy *LHS, ExprTy *RHS) {
1853 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1854 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1855
Steve Narofff69936d2007-09-16 03:34:24 +00001856 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1857 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Reid Spencer5f016e22007-07-11 17:01:13 +00001858
1859 QualType ResultTy; // Result type of the binary operator.
1860 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1861
1862 switch (Opc) {
1863 default:
1864 assert(0 && "Unknown binary expr!");
1865 case BinaryOperator::Assign:
1866 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1867 break;
1868 case BinaryOperator::Mul:
1869 case BinaryOperator::Div:
1870 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1871 break;
1872 case BinaryOperator::Rem:
1873 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1874 break;
1875 case BinaryOperator::Add:
1876 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1877 break;
1878 case BinaryOperator::Sub:
1879 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1880 break;
1881 case BinaryOperator::Shl:
1882 case BinaryOperator::Shr:
1883 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1884 break;
1885 case BinaryOperator::LE:
1886 case BinaryOperator::LT:
1887 case BinaryOperator::GE:
1888 case BinaryOperator::GT:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001889 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 break;
1891 case BinaryOperator::EQ:
1892 case BinaryOperator::NE:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001893 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 break;
1895 case BinaryOperator::And:
1896 case BinaryOperator::Xor:
1897 case BinaryOperator::Or:
1898 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1899 break;
1900 case BinaryOperator::LAnd:
1901 case BinaryOperator::LOr:
1902 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1903 break;
1904 case BinaryOperator::MulAssign:
1905 case BinaryOperator::DivAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001906 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001907 if (!CompTy.isNull())
1908 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1909 break;
1910 case BinaryOperator::RemAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001911 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 if (!CompTy.isNull())
1913 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1914 break;
1915 case BinaryOperator::AddAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001916 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 if (!CompTy.isNull())
1918 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1919 break;
1920 case BinaryOperator::SubAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001921 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 if (!CompTy.isNull())
1923 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1924 break;
1925 case BinaryOperator::ShlAssign:
1926 case BinaryOperator::ShrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001927 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001928 if (!CompTy.isNull())
1929 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1930 break;
1931 case BinaryOperator::AndAssign:
1932 case BinaryOperator::XorAssign:
1933 case BinaryOperator::OrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001934 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001935 if (!CompTy.isNull())
1936 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1937 break;
1938 case BinaryOperator::Comma:
1939 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1940 break;
1941 }
1942 if (ResultTy.isNull())
1943 return true;
1944 if (CompTy.isNull())
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001945 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001946 else
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001947 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001948}
1949
1950// Unary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001951Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 ExprTy *input) {
1953 Expr *Input = (Expr*)input;
1954 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1955 QualType resultType;
1956 switch (Opc) {
1957 default:
1958 assert(0 && "Unimplemented unary expr!");
1959 case UnaryOperator::PreInc:
1960 case UnaryOperator::PreDec:
1961 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1962 break;
1963 case UnaryOperator::AddrOf:
1964 resultType = CheckAddressOfOperand(Input, OpLoc);
1965 break;
1966 case UnaryOperator::Deref:
Steve Naroff1ca9b112007-12-18 04:06:57 +00001967 DefaultFunctionArrayConversion(Input);
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 resultType = CheckIndirectionOperand(Input, OpLoc);
1969 break;
1970 case UnaryOperator::Plus:
1971 case UnaryOperator::Minus:
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001972 UsualUnaryConversions(Input);
1973 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001974 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1975 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1976 resultType.getAsString());
1977 break;
1978 case UnaryOperator::Not: // bitwise complement
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001979 UsualUnaryConversions(Input);
1980 resultType = Input->getType();
Steve Naroff084f9ed2007-08-24 17:20:07 +00001981 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1982 if (!resultType->isIntegerType()) {
1983 if (resultType->isComplexType())
1984 // C99 does not support '~' for complex conjugation.
1985 Diag(OpLoc, diag::ext_integer_complement_complex,
1986 resultType.getAsString());
1987 else
1988 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1989 resultType.getAsString());
1990 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 break;
1992 case UnaryOperator::LNot: // logical negation
1993 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001994 DefaultFunctionArrayConversion(Input);
1995 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1997 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1998 resultType.getAsString());
1999 // LNot always has type int. C99 6.5.3.3p5.
2000 resultType = Context.IntTy;
2001 break;
2002 case UnaryOperator::SizeOf:
2003 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2004 break;
2005 case UnaryOperator::AlignOf:
2006 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2007 break;
Chris Lattnerdbb36972007-08-24 21:16:53 +00002008 case UnaryOperator::Real:
Chris Lattnerdbb36972007-08-24 21:16:53 +00002009 case UnaryOperator::Imag:
Chris Lattner5d794252007-08-24 21:41:10 +00002010 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattnerdbb36972007-08-24 21:16:53 +00002011 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002012 case UnaryOperator::Extension:
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 resultType = Input->getType();
2014 break;
2015 }
2016 if (resultType.isNull())
2017 return true;
2018 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2019}
2020
Steve Naroff1b273c42007-09-16 14:56:35 +00002021/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2022Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 SourceLocation LabLoc,
2024 IdentifierInfo *LabelII) {
2025 // Look up the record for this label identifier.
2026 LabelStmt *&LabelDecl = LabelMap[LabelII];
2027
2028 // If we haven't seen this label yet, create a forward reference.
2029 if (LabelDecl == 0)
2030 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2031
2032 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattner6481a572007-08-03 17:31:20 +00002033 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2034 Context.getPointerType(Context.VoidTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00002035}
2036
Steve Naroff1b273c42007-09-16 14:56:35 +00002037Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattnerab18c4c2007-07-24 16:58:17 +00002038 SourceLocation RPLoc) { // "({..})"
2039 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2040 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2041 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2042
2043 // FIXME: there are a variety of strange constraints to enforce here, for
2044 // example, it is not possible to goto into a stmt expression apparently.
2045 // More semantic analysis is needed.
2046
2047 // FIXME: the last statement in the compount stmt has its value used. We
2048 // should not warn about it being unused.
2049
2050 // If there are sub stmts in the compound stmt, take the type of the last one
2051 // as the type of the stmtexpr.
2052 QualType Ty = Context.VoidTy;
2053
2054 if (!Compound->body_empty())
2055 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2056 Ty = LastExpr->getType();
2057
2058 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2059}
Steve Naroffd34e9152007-08-01 22:05:33 +00002060
Steve Naroff1b273c42007-09-16 14:56:35 +00002061Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002062 SourceLocation TypeLoc,
2063 TypeTy *argty,
2064 OffsetOfComponent *CompPtr,
2065 unsigned NumComponents,
2066 SourceLocation RPLoc) {
2067 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2068 assert(!ArgTy.isNull() && "Missing type argument!");
2069
2070 // We must have at least one component that refers to the type, and the first
2071 // one is known to be a field designator. Verify that the ArgTy represents
2072 // a struct/union/class.
2073 if (!ArgTy->isRecordType())
2074 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2075
2076 // Otherwise, create a compound literal expression as the base, and
2077 // iteratively process the offsetof designators.
Steve Naroffe9b12192008-01-14 18:19:28 +00002078 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002079
Chris Lattner9e2b75c2007-08-31 21:49:13 +00002080 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2081 // GCC extension, diagnose them.
2082 if (NumComponents != 1)
2083 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2084 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2085
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002086 for (unsigned i = 0; i != NumComponents; ++i) {
2087 const OffsetOfComponent &OC = CompPtr[i];
2088 if (OC.isBrackets) {
2089 // Offset of an array sub-field. TODO: Should we allow vector elements?
2090 const ArrayType *AT = Res->getType()->getAsArrayType();
2091 if (!AT) {
2092 delete Res;
2093 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2094 Res->getType().getAsString());
2095 }
2096
Chris Lattner704fe352007-08-30 17:59:59 +00002097 // FIXME: C++: Verify that operator[] isn't overloaded.
2098
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002099 // C99 6.5.2.1p1
2100 Expr *Idx = static_cast<Expr*>(OC.U.E);
2101 if (!Idx->getType()->isIntegerType())
2102 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2103 Idx->getSourceRange());
2104
2105 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2106 continue;
2107 }
2108
2109 const RecordType *RC = Res->getType()->getAsRecordType();
2110 if (!RC) {
2111 delete Res;
2112 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2113 Res->getType().getAsString());
2114 }
2115
2116 // Get the decl corresponding to this.
2117 RecordDecl *RD = RC->getDecl();
2118 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2119 if (!MemberDecl)
2120 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2121 OC.U.IdentInfo->getName(),
2122 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner704fe352007-08-30 17:59:59 +00002123
2124 // FIXME: C++: Verify that MemberDecl isn't a static field.
2125 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman51019072008-02-06 22:48:16 +00002126 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2127 // matter here.
2128 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002129 }
2130
2131 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2132 BuiltinLoc);
2133}
2134
2135
Steve Naroff1b273c42007-09-16 14:56:35 +00002136Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroffd34e9152007-08-01 22:05:33 +00002137 TypeTy *arg1, TypeTy *arg2,
2138 SourceLocation RPLoc) {
2139 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2140 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2141
2142 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2143
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002144 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroffd34e9152007-08-01 22:05:33 +00002145}
2146
Steve Naroff1b273c42007-09-16 14:56:35 +00002147Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroffd04fdd52007-08-03 21:21:27 +00002148 ExprTy *expr1, ExprTy *expr2,
2149 SourceLocation RPLoc) {
2150 Expr *CondExpr = static_cast<Expr*>(cond);
2151 Expr *LHSExpr = static_cast<Expr*>(expr1);
2152 Expr *RHSExpr = static_cast<Expr*>(expr2);
2153
2154 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2155
2156 // The conditional expression is required to be a constant expression.
2157 llvm::APSInt condEval(32);
2158 SourceLocation ExpLoc;
2159 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2160 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2161 CondExpr->getSourceRange());
2162
2163 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2164 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2165 RHSExpr->getType();
2166 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2167}
2168
Nate Begeman67295d02008-01-30 20:50:20 +00002169/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begemane2ce1d92008-01-17 17:46:27 +00002170/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begeman67295d02008-01-30 20:50:20 +00002171/// The number of arguments has already been validated to match the number of
2172/// arguments in FnType.
2173static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begemane2ce1d92008-01-17 17:46:27 +00002174 unsigned NumParams = FnType->getNumArgs();
2175 for (unsigned i = 0; i != NumParams; ++i)
Nate Begeman67295d02008-01-30 20:50:20 +00002176 if (Args[i]->getType().getCanonicalType() !=
2177 FnType->getArgType(i).getCanonicalType())
Nate Begemane2ce1d92008-01-17 17:46:27 +00002178 return false;
2179 return true;
2180}
2181
2182Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2183 SourceLocation *CommaLocs,
2184 SourceLocation BuiltinLoc,
2185 SourceLocation RParenLoc) {
Nate Begeman796ef3d2008-01-31 05:38:29 +00002186 // __builtin_overload requires at least 2 arguments
2187 if (NumArgs < 2)
2188 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2189 SourceRange(BuiltinLoc, RParenLoc));
Nate Begemane2ce1d92008-01-17 17:46:27 +00002190
Nate Begemane2ce1d92008-01-17 17:46:27 +00002191 // The first argument is required to be a constant expression. It tells us
2192 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002193 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begemane2ce1d92008-01-17 17:46:27 +00002194 Expr *NParamsExpr = Args[0];
2195 llvm::APSInt constEval(32);
2196 SourceLocation ExpLoc;
2197 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2198 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2199 NParamsExpr->getSourceRange());
2200
2201 // Verify that the number of parameters is > 0
2202 unsigned NumParams = constEval.getZExtValue();
2203 if (NumParams == 0)
2204 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2205 NParamsExpr->getSourceRange());
2206 // Verify that we have at least 1 + NumParams arguments to the builtin.
2207 if ((NumParams + 1) > NumArgs)
2208 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2209 SourceRange(BuiltinLoc, RParenLoc));
2210
2211 // Figure out the return type, by matching the args to one of the functions
Nate Begeman67295d02008-01-30 20:50:20 +00002212 // listed after the parameters.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002213 OverloadExpr *OE = 0;
Nate Begemane2ce1d92008-01-17 17:46:27 +00002214 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2215 // UsualUnaryConversions will convert the function DeclRefExpr into a
2216 // pointer to function.
2217 Expr *Fn = UsualUnaryConversions(Args[i]);
2218 FunctionTypeProto *FnType = 0;
Nate Begeman67295d02008-01-30 20:50:20 +00002219 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2220 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2221 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2222 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002223
2224 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2225 // parameters, and the number of parameters must match the value passed to
2226 // the builtin.
2227 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begeman67295d02008-01-30 20:50:20 +00002228 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2229 Fn->getSourceRange());
Nate Begemane2ce1d92008-01-17 17:46:27 +00002230
2231 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begeman67295d02008-01-30 20:50:20 +00002232 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begemane2ce1d92008-01-17 17:46:27 +00002233 // If they match, return a new OverloadExpr.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002234 if (ExprsMatchFnType(Args+1, FnType)) {
2235 if (OE)
2236 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2237 OE->getFn()->getSourceRange());
2238 // Remember our match, and continue processing the remaining arguments
2239 // to catch any errors.
2240 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2241 BuiltinLoc, RParenLoc);
2242 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002243 }
Nate Begeman796ef3d2008-01-31 05:38:29 +00002244 // Return the newly created OverloadExpr node, if we succeded in matching
2245 // exactly one of the candidate functions.
2246 if (OE)
2247 return OE;
Nate Begemane2ce1d92008-01-17 17:46:27 +00002248
2249 // If we didn't find a matching function Expr in the __builtin_overload list
2250 // the return an error.
2251 std::string typeNames;
Nate Begeman67295d02008-01-30 20:50:20 +00002252 for (unsigned i = 0; i != NumParams; ++i) {
2253 if (i != 0) typeNames += ", ";
2254 typeNames += Args[i+1]->getType().getAsString();
2255 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002256
2257 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2258 SourceRange(BuiltinLoc, RParenLoc));
2259}
2260
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002261Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2262 ExprTy *expr, TypeTy *type,
Chris Lattner5cf216b2008-01-04 18:04:52 +00002263 SourceLocation RPLoc) {
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002264 Expr *E = static_cast<Expr*>(expr);
2265 QualType T = QualType::getFromOpaquePtr(type);
2266
2267 InitBuiltinVaListType();
2268
Chris Lattner5cf216b2008-01-04 18:04:52 +00002269 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2270 != Compatible)
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002271 return Diag(E->getLocStart(),
2272 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2273 E->getType().getAsString(),
2274 E->getSourceRange());
2275
2276 // FIXME: Warn if a non-POD type is passed in.
2277
2278 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2279}
2280
Chris Lattner5cf216b2008-01-04 18:04:52 +00002281bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2282 SourceLocation Loc,
2283 QualType DstType, QualType SrcType,
2284 Expr *SrcExpr, const char *Flavor) {
2285 // Decode the result (notice that AST's are still created for extensions).
2286 bool isInvalid = false;
2287 unsigned DiagKind;
2288 switch (ConvTy) {
2289 default: assert(0 && "Unknown conversion type");
2290 case Compatible: return false;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002291 case PointerToInt:
Chris Lattner5cf216b2008-01-04 18:04:52 +00002292 DiagKind = diag::ext_typecheck_convert_pointer_int;
2293 break;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002294 case IntToPointer:
2295 DiagKind = diag::ext_typecheck_convert_int_pointer;
2296 break;
Chris Lattner5cf216b2008-01-04 18:04:52 +00002297 case IncompatiblePointer:
2298 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2299 break;
2300 case FunctionVoidPointer:
2301 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2302 break;
2303 case CompatiblePointerDiscardsQualifiers:
2304 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2305 break;
2306 case Incompatible:
2307 DiagKind = diag::err_typecheck_convert_incompatible;
2308 isInvalid = true;
2309 break;
2310 }
2311
2312 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2313 SrcExpr->getSourceRange());
2314 return isInvalid;
2315}
2316