blob: e85ad03694aa54d55387fdd3c486668353e6d395 [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner5b183d82006-11-10 05:03:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek43fb8b02007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
Chris Lattneraa9c7ae2008-04-08 04:40:51 +000018#include "clang/AST/ExprCXX.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000020#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000021#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000022#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner08464942007-12-28 05:29:59 +000024#include "llvm/ADT/OwningPtr.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000025#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000027using namespace clang;
28
Steve Naroff83895f72007-09-16 03:34:24 +000029/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +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 Naroff83895f72007-09-16 03:34:24 +000036Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000037 assert(NumStringToks && "Must have at least one string!");
38
Steve Naroff4f88b312007-03-13 22:37:02 +000039 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
40 if (Literal.hadError)
41 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000042
Chris Lattner23b7eb62007-06-15 23:05:46 +000043 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000044 for (unsigned i = 0; i != NumStringToks; ++i)
45 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattner36fc8792008-02-11 00:02:17 +000046
47 // Verify that pascal strings aren't too large.
Anders Carlssoncbfc4b82007-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()));
Steve Narofff1e53692007-03-23 22:27:02 +000052
Chris Lattner36fc8792008-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
Chris Lattner5b183d82006-11-10 05:03:26 +000064 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000065 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattner36fc8792008-02-11 00:02:17 +000066 Literal.AnyWide, StrTy,
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000067 StringToks[0].getLocation(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000068 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000069}
70
Chris Lattnere168f762006-11-10 05:29:30 +000071
Steve Naroff30d242c2007-09-15 18:49:24 +000072/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattnerac18be92006-11-20 06:49:47 +000073/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroff157c4032008-03-19 23:46:26 +000074/// identifier is used in a function call context.
Steve Naroff30d242c2007-09-15 18:49:24 +000075Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +000076 IdentifierInfo &II,
77 bool HasTrailingLParen) {
Chris Lattner59a25942008-03-31 00:36:02 +000078 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff2fc93f52008-04-02 14:35:35 +000079 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattner59a25942008-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 Naroff257520b2008-04-01 23:04:06 +000084 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattner59a25942008-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 Naroff257520b2008-04-01 23:04:06 +000090 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Chris Lattner59a25942008-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
Chris Lattner17ed4872006-11-20 04:58:19 +0000103 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +0000104 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +0000105 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +0000106 if (HasTrailingLParen &&
Chris Lattner59a25942008-03-31 00:36:02 +0000107 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattnerac18be92006-11-20 06:49:47 +0000108 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +0000109 else {
Chris Lattnerac18be92006-11-20 06:49:47 +0000110 // If this name wasn't predeclared and if this is not a function call,
111 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +0000112 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +0000113 }
Chris Lattner17ed4872006-11-20 04:58:19 +0000114 }
Chris Lattner59a25942008-03-31 00:36:02 +0000115
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000116 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner8a8558b2008-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 Naroffcf871f52007-08-28 18:45:29 +0000121 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +0000122 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000123 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000124 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000125 }
Chris Lattner59a25942008-03-31 00:36:02 +0000126
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000127 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +0000128 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000129 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanianb31a2f22007-12-05 18:16:33 +0000130 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000131 if (isa<NamespaceDecl>(D))
132 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Steve Narofff1e53692007-03-23 22:27:02 +0000133
134 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +0000135 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +0000136}
Chris Lattnere168f762006-11-10 05:29:30 +0000137
Steve Naroff83895f72007-09-16 03:34:24 +0000138Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson625bfc82007-07-21 05:21:51 +0000139 tok::TokenKind Kind) {
140 PreDefinedExpr::IdentType IT;
141
Chris Lattnere168f762006-11-10 05:29:30 +0000142 switch (Kind) {
Chris Lattner317e6ba2008-01-12 18:39:25 +0000143 default: assert(0 && "Unknown simple primary expr!");
144 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
145 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
146 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattnere168f762006-11-10 05:29:30 +0000147 }
Chris Lattner317e6ba2008-01-12 18:39:25 +0000148
149 // Verify that this is in a function context.
Chris Lattner0a8c2322008-01-12 19:32:28 +0000150 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattner317e6ba2008-01-12 18:39:25 +0000151 return Diag(Loc, diag::err_predef_outside_function);
Anders Carlsson625bfc82007-07-21 05:21:51 +0000152
Chris Lattnera81a0272008-01-12 08:14:25 +0000153 // Pre-defined identifiers are of type char[x], where x is the length of the
154 // string.
Chris Lattner0a8c2322008-01-12 19:32:28 +0000155 unsigned Length;
156 if (CurFunctionDecl)
157 Length = CurFunctionDecl->getIdentifier()->getLength();
158 else
Fariborz Jahanianad134b92008-01-17 17:37:26 +0000159 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattner317e6ba2008-01-12 18:39:25 +0000160
Chris Lattner0a8c2322008-01-12 19:32:28 +0000161 llvm::APInt LengthI(32, Length + 1);
Chris Lattner317e6ba2008-01-12 18:39:25 +0000162 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattner0a8c2322008-01-12 19:32:28 +0000163 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattnera81a0272008-01-12 08:14:25 +0000164 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000165}
166
Steve Naroff83895f72007-09-16 03:34:24 +0000167Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000168 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000169 CharBuffer.resize(Tok.getLength());
170 const char *ThisTokBegin = &CharBuffer[0];
171 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
172
173 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
174 Tok.getLocation(), PP);
175 if (Literal.hadError())
176 return ExprResult(true);
Chris Lattneref24b382008-03-01 08:32:21 +0000177
178 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
179
180 return new CharacterLiteral(Literal.getValue(), type, Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000181}
182
Steve Naroff83895f72007-09-16 03:34:24 +0000183Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000184 // fast path for a single digit (which is quite common). A single digit
185 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
186 if (Tok.getLength() == 1) {
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000187 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000188
Chris Lattner37e05872008-03-05 18:54:05 +0000189 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000190 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner23b7eb62007-06-15 23:05:46 +0000191 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000192 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000193 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000194 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000195 IntegerBuffer.resize(Tok.getLength());
196 const char *ThisTokBegin = &IntegerBuffer[0];
197
Chris Lattner67ca9252007-05-21 01:08:44 +0000198 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000199 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000200 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000201 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000202 if (Literal.hadError)
203 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000204
Chris Lattner1c20a172007-08-26 03:42:43 +0000205 Expr *Res;
206
207 if (Literal.isFloatingLiteral()) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000208 QualType Ty;
209 const llvm::fltSemantics *Format;
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000210
211 if (Literal.isFloat) {
212 Ty = Context.FloatTy;
Chris Lattner7570e9c2008-03-08 08:52:55 +0000213 Format = Context.Target.getFloatFormat();
214 } else if (!Literal.isLong) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000215 Ty = Context.DoubleTy;
Chris Lattner7570e9c2008-03-08 08:52:55 +0000216 Format = Context.Target.getDoubleFormat();
217 } else {
218 Ty = Context.LongDoubleTy;
219 Format = Context.Target.getLongDoubleFormat();
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000220 }
221
Ted Kremenek3a2c9502007-11-29 00:56:49 +0000222 // isExact will be set by GetFloatValue().
223 bool isExact = false;
224
225 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
226 Ty, Tok.getLocation());
227
Chris Lattner1c20a172007-08-26 03:42:43 +0000228 } else if (!Literal.isIntegerLiteral()) {
229 return ExprResult(true);
230 } else {
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000231 QualType Ty;
Chris Lattner67ca9252007-05-21 01:08:44 +0000232
Neil Boothac582c52007-08-29 22:00:19 +0000233 // long long is a C99 feature.
234 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000235 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000236 Diag(Tok.getLocation(), diag::ext_longlong);
237
Chris Lattner67ca9252007-05-21 01:08:44 +0000238 // Get the value in the widest-possible width.
Chris Lattner37e05872008-03-05 18:54:05 +0000239 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000240
241 if (Literal.GetIntegerValue(ResultVal)) {
242 // If this value didn't fit into uintmax_t, warn and force to ull.
243 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000244 Ty = Context.UnsignedLongLongTy;
245 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner37e05872008-03-05 18:54:05 +0000246 "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000247 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000248 // If this value fits into a ULL, try to figure out what else it fits into
249 // according to the rules of C99 6.4.4.1p5.
250
251 // Octal, Hexadecimal, and integers with a U suffix are allowed to
252 // be an unsigned int.
253 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
254
255 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner55258cf2008-05-09 05:59:00 +0000256 unsigned Width = 0;
Chris Lattner7b939cf2007-08-23 21:58:08 +0000257 if (!Literal.isLong && !Literal.isLongLong) {
258 // Are int/unsigned possibilities?
Chris Lattner55258cf2008-05-09 05:59:00 +0000259 unsigned IntSize = Context.Target.getIntWidth();
260
Chris Lattner67ca9252007-05-21 01:08:44 +0000261 // Does it fit in a unsigned int?
262 if (ResultVal.isIntN(IntSize)) {
263 // Does it fit in a signed int?
264 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000265 Ty = Context.IntTy;
Chris Lattner67ca9252007-05-21 01:08:44 +0000266 else if (AllowUnsigned)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000267 Ty = Context.UnsignedIntTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000268 Width = IntSize;
Chris Lattner67ca9252007-05-21 01:08:44 +0000269 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000270 }
271
272 // Are long/unsigned long possibilities?
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000273 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattner55258cf2008-05-09 05:59:00 +0000274 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner67ca9252007-05-21 01:08:44 +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 Lattner24d5bfe2008-04-02 04:24:33 +0000280 Ty = Context.LongTy;
Chris Lattner67ca9252007-05-21 01:08:44 +0000281 else if (AllowUnsigned)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000282 Ty = Context.UnsignedLongTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000283 Width = LongSize;
Chris Lattner67ca9252007-05-21 01:08:44 +0000284 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000285 }
286
287 // Finally, check long long if needed.
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000288 if (Ty.isNull()) {
Chris Lattner55258cf2008-05-09 05:59:00 +0000289 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner67ca9252007-05-21 01:08:44 +0000290
291 // Does it fit in a unsigned long long?
292 if (ResultVal.isIntN(LongLongSize)) {
293 // Does it fit in a signed long long?
294 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000295 Ty = Context.LongLongTy;
Chris Lattner67ca9252007-05-21 01:08:44 +0000296 else if (AllowUnsigned)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000297 Ty = Context.UnsignedLongLongTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000298 Width = LongLongSize;
Chris Lattner67ca9252007-05-21 01:08:44 +0000299 }
300 }
301
302 // If we still couldn't decide a type, we probably have something that
303 // does not fit in a signed long long, but has no U suffix.
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000304 if (Ty.isNull()) {
Chris Lattner67ca9252007-05-21 01:08:44 +0000305 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000306 Ty = Context.UnsignedLongLongTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000307 Width = Context.Target.getLongLongWidth();
Chris Lattner67ca9252007-05-21 01:08:44 +0000308 }
Chris Lattner55258cf2008-05-09 05:59:00 +0000309
310 if (ResultVal.getBitWidth() != Width)
311 ResultVal.trunc(Width);
Steve Naroff09ef4742007-03-09 23:16:33 +0000312 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000313
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000314 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000315 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000316
317 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
318 if (Literal.isImaginary)
319 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
320
321 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000322}
323
Steve Naroff83895f72007-09-16 03:34:24 +0000324Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattnere168f762006-11-10 05:29:30 +0000325 ExprTy *Val) {
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000326 Expr *E = (Expr *)Val;
327 assert((E != 0) && "ActOnParenExpr() missing expr");
328 return new ParenExpr(L, R, E);
Chris Lattnere168f762006-11-10 05:29:30 +0000329}
330
Steve Naroff71b59a92007-06-04 22:22:31 +0000331/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000332/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000333QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
334 SourceLocation OpLoc, bool isSizeof) {
335 // C99 6.5.3.4p1:
336 if (isa<FunctionType>(exprType) && isSizeof)
337 // alignof(function) is allowed.
338 Diag(OpLoc, diag::ext_sizeof_function_type);
339 else if (exprType->isVoidType())
340 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
341 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000342 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000343 diag::err_alignof_incomplete_type,
344 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000345 return QualType(); // error
346 }
347 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
348 return Context.getSizeType();
349}
350
Chris Lattnere168f762006-11-10 05:29:30 +0000351Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000352ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000353 SourceLocation LPLoc, TypeTy *Ty,
354 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000355 // If error parsing type, ignore.
356 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000357
358 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000359 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000360
Steve Naroff043d45d2007-05-15 02:32:35 +0000361 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
362
363 if (resultType.isNull())
364 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000365 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000366}
367
Chris Lattner74ed76b2007-08-24 21:41:10 +0000368QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000369 DefaultFunctionArrayConversion(V);
370
Chris Lattnere267f5d2007-08-26 05:39:26 +0000371 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000372 if (const ComplexType *CT = V->getType()->getAsComplexType())
373 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000374
375 // Otherwise they pass through real integer and floating point types here.
376 if (V->getType()->isArithmeticType())
377 return V->getType();
378
379 // Reject anything else.
380 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
381 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000382}
383
384
Chris Lattnere168f762006-11-10 05:29:30 +0000385
Steve Naroff83895f72007-09-16 03:34:24 +0000386Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000387 tok::TokenKind Kind,
388 ExprTy *Input) {
389 UnaryOperator::Opcode Opc;
390 switch (Kind) {
391 default: assert(0 && "Unknown unary op!");
392 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
393 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
394 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000395 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000396 if (result.isNull())
397 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000398 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000399}
400
401Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000402ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000403 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000404 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000405
406 // Perform default conversions.
407 DefaultFunctionArrayConversion(LHSExp);
408 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000409
Chris Lattner36d572b2007-07-16 00:14:47 +0000410 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000411
Steve Naroffc1aadb12007-03-28 21:49:40 +0000412 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000413 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000414 // in the subscript position. As a result, we need to derive the array base
415 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000416 Expr *BaseExpr, *IndexExpr;
417 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000418 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000419 BaseExpr = LHSExp;
420 IndexExpr = RHSExp;
421 // FIXME: need to deal with const...
422 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000423 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000424 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000425 BaseExpr = RHSExp;
426 IndexExpr = LHSExp;
427 // FIXME: need to deal with const...
428 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000429 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
430 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000431 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000432
433 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanf322eab2008-05-09 06:41:27 +0000434 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
435 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemance4d7fc2008-04-18 23:10:10 +0000436 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff01047312007-08-03 22:40:33 +0000437 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000438 // FIXME: need to deal with const...
439 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000440 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000441 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
442 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000443 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000444 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000445 if (!IndexExpr->getType()->isIntegerType())
446 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
447 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000448
Chris Lattner36d572b2007-07-16 00:14:47 +0000449 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
450 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattnerb3a176d2008-04-02 06:59:01 +0000451 // void (*)(int)) and pointers to incomplete types. Functions are not
452 // objects in C99.
Chris Lattner36d572b2007-07-16 00:14:47 +0000453 if (!ResultType->isObjectType())
454 return Diag(BaseExpr->getLocStart(),
455 diag::err_typecheck_subscript_not_object,
456 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
457
458 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000459}
460
Steve Narofff8fd09e2007-07-27 22:15:19 +0000461QualType Sema::
Nate Begemance4d7fc2008-04-18 23:10:10 +0000462CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000463 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemance4d7fc2008-04-18 23:10:10 +0000464 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanf322eab2008-05-09 06:41:27 +0000465
466 // This flag determines whether or not the component is to be treated as a
467 // special name, or a regular GLSL-style component access.
468 bool SpecialComponent = false;
Steve Narofff8fd09e2007-07-27 22:15:19 +0000469
470 // The vector accessor can't exceed the number of elements.
471 const char *compStr = CompName.getName();
472 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +0000473 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000474 baseType.getAsString(), SourceRange(CompLoc));
475 return QualType();
476 }
Nate Begemanf322eab2008-05-09 06:41:27 +0000477
478 // Check that we've found one of the special components, or that the component
479 // names must come from the same set.
480 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
481 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
482 SpecialComponent = true;
483 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner7e152db2007-08-02 22:33:49 +0000484 do
485 compStr++;
486 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
487 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
488 do
489 compStr++;
490 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
491 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
492 do
493 compStr++;
494 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
495 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000496
Nate Begemanf322eab2008-05-09 06:41:27 +0000497 if (!SpecialComponent && *compStr) {
Steve Narofff8fd09e2007-07-27 22:15:19 +0000498 // We didn't get to the end of the string. This means the component names
499 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000500 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000501 std::string(compStr,compStr+1), SourceRange(CompLoc));
502 return QualType();
503 }
504 // Each component accessor can't exceed the vector type.
505 compStr = CompName.getName();
506 while (*compStr) {
507 if (vecType->isAccessorWithinNumElements(*compStr))
508 compStr++;
509 else
510 break;
511 }
Nate Begemanf322eab2008-05-09 06:41:27 +0000512 if (!SpecialComponent && *compStr) {
Steve Narofff8fd09e2007-07-27 22:15:19 +0000513 // We didn't get to the end of the string. This means a component accessor
514 // exceeds the number of elements in the vector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000515 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000516 baseType.getAsString(), SourceRange(CompLoc));
517 return QualType();
518 }
Nate Begemanf322eab2008-05-09 06:41:27 +0000519
520 // If we have a special component name, verify that the current vector length
521 // is an even number, since all special component names return exactly half
522 // the elements.
523 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
524 return QualType();
525 }
526
Steve Narofff8fd09e2007-07-27 22:15:19 +0000527 // The component accessor looks fine - now we need to compute the actual type.
528 // The vector type is implied by the component accessor. For example,
529 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanf322eab2008-05-09 06:41:27 +0000530 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
531 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
532 : strlen(CompName.getName());
Steve Narofff8fd09e2007-07-27 22:15:19 +0000533 if (CompSize == 1)
534 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000535
Nate Begemance4d7fc2008-04-18 23:10:10 +0000536 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000537 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemance4d7fc2008-04-18 23:10:10 +0000538 // diagostics look bad. We want extended vector types to appear built-in.
539 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
540 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
541 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000542 }
543 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000544}
545
Chris Lattnere168f762006-11-10 05:29:30 +0000546Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000547ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000548 tok::TokenKind OpKind, SourceLocation MemberLoc,
549 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000550 Expr *BaseExpr = static_cast<Expr *>(Base);
551 assert(BaseExpr && "no record expression");
Steve Naroffeaaae462007-12-16 21:42:28 +0000552
553 // Perform default conversions.
554 DefaultFunctionArrayConversion(BaseExpr);
Steve Naroffca8f7122007-04-01 01:41:35 +0000555
Steve Naroff185616f2007-07-26 03:11:44 +0000556 QualType BaseType = BaseExpr->getType();
557 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000558
Steve Narofff1e53692007-03-23 22:27:02 +0000559 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000560 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000561 BaseType = PT->getPointeeType();
562 else
563 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
564 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000565 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000566 // The base type is either a record or an ExtVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000567 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000568 RecordDecl *RDecl = RTy->getDecl();
569 if (RTy->isIncompleteType())
570 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
571 BaseExpr->getSourceRange());
572 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000573 FieldDecl *MemberDecl = RDecl->getMember(&Member);
574 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000575 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
576 SourceRange(MemberLoc));
Eli Friedman1242fff2008-02-06 22:48:16 +0000577
578 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedman69d56cf2008-02-07 05:24:51 +0000579 // FIXME: Handle address space modifiers
Eli Friedman1242fff2008-02-06 22:48:16 +0000580 QualType MemberType = MemberDecl->getType();
581 unsigned combinedQualifiers =
Chris Lattner445fcab2008-02-20 20:55:12 +0000582 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman1242fff2008-02-06 22:48:16 +0000583 MemberType = MemberType.getQualifiedType(combinedQualifiers);
584
585 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
586 MemberLoc, MemberType);
Nate Begemance4d7fc2008-04-18 23:10:10 +0000587 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000588 // Component access limited to variables (reject vec4.rg.g).
Nate Begemanf322eab2008-05-09 06:41:27 +0000589 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
590 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemance4d7fc2008-04-18 23:10:10 +0000591 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff01047312007-08-03 22:40:33 +0000592 SourceRange(MemberLoc));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000593 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Narofff8fd09e2007-07-27 22:15:19 +0000594 if (ret.isNull())
595 return true;
Nate Begemance4d7fc2008-04-18 23:10:10 +0000596 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000597 } else if (BaseType->isObjCInterfaceType()) {
598 ObjCInterfaceDecl *IFace;
599 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
600 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000601 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000602 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
603 ObjCInterfaceDecl *clsDeclared;
604 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000605 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
606 OpKind==tok::arrow);
607 }
608 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
609 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000610}
611
Steve Naroff83895f72007-09-16 03:34:24 +0000612/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattnere168f762006-11-10 05:29:30 +0000613/// This provides the location of the left/right parens and a list of comma
614/// locations.
615Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000616ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner08464942007-12-28 05:29:59 +0000617 ExprTy **args, unsigned NumArgs,
Chris Lattnere168f762006-11-10 05:29:30 +0000618 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000619 Expr *Fn = static_cast<Expr *>(fn);
620 Expr **Args = reinterpret_cast<Expr**>(args);
621 assert(Fn && "no function call expression");
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000622 FunctionDecl *FDecl = NULL;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000623
624 // Promote the function operand.
625 UsualUnaryConversions(Fn);
626
627 // If we're directly calling a function, get the declaration for
628 // that function.
629 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
630 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
631 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
632
Chris Lattner08464942007-12-28 05:29:59 +0000633 // Make the call expr early, before semantic checks. This guarantees cleanup
634 // of arguments and function on error.
Chris Lattner58258242008-04-10 02:22:51 +0000635 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner08464942007-12-28 05:29:59 +0000636 Context.BoolTy, RParenLoc));
637
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000638 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
639 // type pointer to function".
Chris Lattner08464942007-12-28 05:29:59 +0000640 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000641 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000642 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
643 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000644 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
645 if (FuncT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000646 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
647 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000648
649 // We know the result type of the call, set it.
650 TheCall->setType(FuncT->getResultType());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000651
Chris Lattner08464942007-12-28 05:29:59 +0000652 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000653 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
654 // assignment, to the types of the corresponding parameter, ...
Chris Lattner08464942007-12-28 05:29:59 +0000655 unsigned NumArgsInProto = Proto->getNumArgs();
656 unsigned NumArgsToCheck = NumArgs;
Steve Naroff17f76e02007-05-03 21:03:48 +0000657
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000658 // If too few arguments are available (and we don't have default
659 // arguments for the remaining parameters), don't make the call.
660 if (NumArgs < NumArgsInProto) {
Chris Lattner58258242008-04-10 02:22:51 +0000661 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000662 // Use default arguments for missing arguments
663 NumArgsToCheck = NumArgsInProto;
Chris Lattner58258242008-04-10 02:22:51 +0000664 TheCall->setNumArgs(NumArgsInProto);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000665 } else
666 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
667 Fn->getSourceRange());
668 }
669
Chris Lattner08464942007-12-28 05:29:59 +0000670 // If too many are passed and not variadic, error on the extras and drop
671 // them.
672 if (NumArgs > NumArgsInProto) {
673 if (!Proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000674 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000675 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000676 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner08464942007-12-28 05:29:59 +0000677 Args[NumArgs-1]->getLocEnd()));
678 // This deletes the extra arguments.
679 TheCall->setNumArgs(NumArgsInProto);
Steve Naroff8563f652007-05-28 19:25:56 +0000680 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000681 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000682 }
Chris Lattner08464942007-12-28 05:29:59 +0000683
Steve Naroff17f76e02007-05-03 21:03:48 +0000684 // Continue to check argument types (even if we have too few/many args).
Chris Lattner08464942007-12-28 05:29:59 +0000685 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner9bad62c2008-01-04 18:04:52 +0000686 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000687
688 Expr *Arg;
689 if (i < NumArgs)
690 Arg = Args[i];
691 else
692 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner9bad62c2008-01-04 18:04:52 +0000693 QualType ArgType = Arg->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000694
Chris Lattner08464942007-12-28 05:29:59 +0000695 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner9bad62c2008-01-04 18:04:52 +0000696 AssignConvertType ConvTy =
697 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner08464942007-12-28 05:29:59 +0000698 TheCall->setArg(i, Arg);
699
Chris Lattner9bad62c2008-01-04 18:04:52 +0000700 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
701 ArgType, Arg, "passing"))
702 return true;
Steve Naroff17f76e02007-05-03 21:03:48 +0000703 }
Chris Lattner08464942007-12-28 05:29:59 +0000704
705 // If this is a variadic call, handle args passed through "...".
706 if (Proto->isVariadic()) {
Steve Naroff0b661582007-08-28 23:30:39 +0000707 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner08464942007-12-28 05:29:59 +0000708 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
709 Expr *Arg = Args[i];
710 DefaultArgumentPromotion(Arg);
711 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000712 }
Steve Naroff0b661582007-08-28 23:30:39 +0000713 }
Chris Lattner08464942007-12-28 05:29:59 +0000714 } else {
715 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
716
Steve Naroff0b661582007-08-28 23:30:39 +0000717 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner08464942007-12-28 05:29:59 +0000718 for (unsigned i = 0; i != NumArgs; i++) {
719 Expr *Arg = Args[i];
720 DefaultArgumentPromotion(Arg);
721 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000722 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000723 }
Chris Lattner08464942007-12-28 05:29:59 +0000724
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000725 // Do special checking on direct calls to functions.
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000726 if (FDecl)
727 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000728
Chris Lattner08464942007-12-28 05:29:59 +0000729 return TheCall.take();
Chris Lattnere168f762006-11-10 05:29:30 +0000730}
731
732Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000733ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000734 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff83895f72007-09-16 03:34:24 +0000735 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Narofffbd09832007-07-19 01:06:55 +0000736 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000737 // FIXME: put back this assert when initializers are worked out.
Steve Naroff83895f72007-09-16 03:34:24 +0000738 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroff57eb2c52007-07-19 21:32:11 +0000739 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000740
Eli Friedman37a186d2008-05-20 05:22:08 +0000741 if (literalType->isArrayType()) {
742 if (literalType->getAsVariableArrayType())
743 return Diag(LParenLoc,
744 diag::err_variable_object_no_init,
745 SourceRange(LParenLoc,
746 literalExpr->getSourceRange().getEnd()));
747 } else if (literalType->isIncompleteType()) {
748 return Diag(LParenLoc,
749 diag::err_typecheck_decl_incomplete_type,
750 literalType.getAsString(),
751 SourceRange(LParenLoc,
752 literalExpr->getSourceRange().getEnd()));
753 }
754
Steve Naroff98f72032008-01-10 22:15:12 +0000755 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff08ddb8c2008-01-09 20:58:06 +0000756 return true;
Steve Naroffd32419d2008-01-14 18:19:28 +0000757
758 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
759 if (isFileScope) { // 6.5.2.5p3
Steve Naroff98f72032008-01-10 22:15:12 +0000760 if (CheckForConstantInitializer(literalExpr, literalType))
761 return true;
762 }
Steve Naroffd32419d2008-01-14 18:19:28 +0000763 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Steve Narofffbd09832007-07-19 01:06:55 +0000764}
765
766Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000767ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson4692db02007-08-31 04:56:16 +0000768 SourceLocation RBraceLoc) {
Steve Naroff2fea1392007-09-02 02:04:30 +0000769 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson4692db02007-08-31 04:56:16 +0000770
Steve Naroff30d242c2007-09-15 18:49:24 +0000771 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000772 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson4692db02007-08-31 04:56:16 +0000773
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000774 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
775 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
776 return E;
Steve Narofffbd09832007-07-19 01:06:55 +0000777}
778
Chris Lattner6c9ffe92007-12-20 00:44:32 +0000779bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonde71adf2007-11-27 05:51:55 +0000780 assert(VectorTy->isVectorType() && "Not a vector type!");
781
782 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner37e05872008-03-05 18:54:05 +0000783 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonde71adf2007-11-27 05:51:55 +0000784 return Diag(R.getBegin(),
785 Ty->isVectorType() ?
786 diag::err_invalid_conversion_between_vectors :
787 diag::err_invalid_conversion_between_vector_and_integer,
788 VectorTy.getAsString().c_str(),
789 Ty.getAsString().c_str(), R);
790 } else
791 return Diag(R.getBegin(),
792 diag::err_invalid_conversion_between_vector_and_scalar,
793 VectorTy.getAsString().c_str(),
794 Ty.getAsString().c_str(), R);
795
796 return false;
797}
798
Steve Narofffbd09832007-07-19 01:06:55 +0000799Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000800ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattnere168f762006-11-10 05:29:30 +0000801 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff83895f72007-09-16 03:34:24 +0000802 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000803
804 Expr *castExpr = static_cast<Expr*>(Op);
805 QualType castType = QualType::getFromOpaquePtr(Ty);
806
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000807 UsualUnaryConversions(castExpr);
808
Chris Lattnerbd270732007-07-18 16:00:06 +0000809 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
810 // type needs to be scalar.
Chris Lattner3bc4d202007-10-29 04:26:44 +0000811 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroffd9d581f2008-01-24 22:55:05 +0000812 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000813 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
814 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Naroffd9d581f2008-01-24 22:55:05 +0000815 if (!castExpr->getType()->isScalarType() &&
816 !castExpr->getType()->isVectorType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000817 return Diag(castExpr->getLocStart(),
818 diag::err_typecheck_expect_scalar_operand,
819 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonde71adf2007-11-27 05:51:55 +0000820
821 if (castExpr->getType()->isVectorType()) {
822 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
823 castExpr->getType(), castType))
824 return true;
825 } else if (castType->isVectorType()) {
826 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
827 castType, castExpr->getType()))
828 return true;
Chris Lattner3bc4d202007-10-29 04:26:44 +0000829 }
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000830 }
831 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000832}
833
Chris Lattner2ab40a62007-11-26 01:40:58 +0000834/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
835/// In that case, lex = cond.
Steve Narofff8a28c52007-05-15 20:29:32 +0000836inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000837 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000838 UsualUnaryConversions(cond);
839 UsualUnaryConversions(lex);
840 UsualUnaryConversions(rex);
841 QualType condT = cond->getType();
842 QualType lexT = lex->getType();
843 QualType rexT = rex->getType();
844
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000845 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000846 if (!condT->isScalarType()) { // C99 6.5.15p2
847 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
848 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000849 return QualType();
850 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000851
852 // Now check the two expressions.
853
854 // If both operands have arithmetic type, do the usual arithmetic conversions
855 // to find a common type: C99 6.5.15p3,5.
856 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffdbd9e892007-07-17 00:58:39 +0000857 UsualArithmeticConversions(lex, rex);
858 return lex->getType();
859 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000860
861 // If both operands are the same structure or union type, the result is that
862 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000863 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattnere2949f42008-01-06 22:42:25 +0000864 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner2ab40a62007-11-26 01:40:58 +0000865 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattnere2949f42008-01-06 22:42:25 +0000866 // "If both the operands have structure or union type, the result has
867 // that type." This implies that CV qualifiers are dropped.
868 return lexT.getUnqualifiedType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000869 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000870
871 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroffbf1516c2008-05-12 21:44:38 +0000872 // The following || allows only one side to be void (a GCC-ism).
873 if (lexT->isVoidType() || rexT->isVoidType()) {
874 if (!lexT->isVoidType())
875 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
876 rex->getSourceRange());
877 if (!rexT->isVoidType())
878 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
879 lex->getSourceRange());
Chris Lattnere2949f42008-01-06 22:42:25 +0000880 return lexT.getUnqualifiedType();
Steve Naroffbf1516c2008-05-12 21:44:38 +0000881 }
Steve Naroff039ad3c2008-01-08 01:11:38 +0000882 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
883 // the type of the other operand."
884 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +0000885 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff039ad3c2008-01-08 01:11:38 +0000886 return lexT;
887 }
888 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +0000889 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff039ad3c2008-01-08 01:11:38 +0000890 return rexT;
891 }
Chris Lattner4d3b0f52008-01-06 22:50:31 +0000892 // Handle the case where both operands are pointers before we handle null
893 // pointer constants in case both operands are null pointer constants.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000894 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
895 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
896 // get the "pointed to" types
897 QualType lhptee = LHSPT->getPointeeType();
898 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000899
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000900 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
901 if (lhptee->isVoidType() &&
Chris Lattnerb3a176d2008-04-02 06:59:01 +0000902 rhptee->isIncompleteOrObjectType()) {
Chris Lattner445fcab2008-02-20 20:55:12 +0000903 // Figure out necessary qualifiers (C99 6.5.15p6)
904 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedman15888c22008-02-10 22:59:36 +0000905 QualType destType = Context.getPointerType(destPointee);
906 ImpCastExprToType(lex, destType); // add qualifiers if necessary
907 ImpCastExprToType(rex, destType); // promote to void*
908 return destType;
909 }
Chris Lattnerb3a176d2008-04-02 06:59:01 +0000910 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner445fcab2008-02-20 20:55:12 +0000911 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedman15888c22008-02-10 22:59:36 +0000912 QualType destType = Context.getPointerType(destPointee);
913 ImpCastExprToType(lex, destType); // add qualifiers if necessary
914 ImpCastExprToType(rex, destType); // promote to void*
915 return destType;
916 }
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000917
Steve Naroff32e44c02007-10-15 20:41:53 +0000918 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
919 rhptee.getUnqualifiedType())) {
Steve Naroff0dffa442008-02-01 22:44:48 +0000920 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000921 lexT.getAsString(), rexT.getAsString(),
922 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman1bc0fed2008-01-30 17:02:03 +0000923 // In this situation, we assume void* type. No especially good
924 // reason, but this is what gcc does, and we do have to pick
925 // to get a consistent AST.
926 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
927 ImpCastExprToType(lex, voidPtrTy);
928 ImpCastExprToType(rex, voidPtrTy);
929 return voidPtrTy;
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000930 }
931 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000932 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
933 // differently qualified versions of compatible types, the result type is
934 // a pointer to an appropriately qualified version of the *composite*
935 // type.
Eli Friedman928ab4d2008-05-16 20:37:07 +0000936 // FIXME: Need to calculate the composite type.
Eli Friedman15888c22008-02-10 22:59:36 +0000937 // FIXME: Need to add qualifiers
Eli Friedman928ab4d2008-05-16 20:37:07 +0000938 QualType compositeType = lexT;
939 ImpCastExprToType(lex, compositeType);
940 ImpCastExprToType(rex, compositeType);
941 return compositeType;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000942 }
943 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000944
Chris Lattnere2949f42008-01-06 22:42:25 +0000945 // Otherwise, the operands are not compatible.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000946 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000947 lexT.getAsString(), rexT.getAsString(),
948 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000949 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000950}
951
Steve Naroff83895f72007-09-16 03:34:24 +0000952/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattnere168f762006-11-10 05:29:30 +0000953/// in the case of a the GNU conditional expr extension.
Steve Naroff83895f72007-09-16 03:34:24 +0000954Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000955 SourceLocation ColonLoc,
956 ExprTy *Cond, ExprTy *LHS,
957 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000958 Expr *CondExpr = (Expr *) Cond;
959 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000960
961 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
962 // was the condition.
963 bool isLHSNull = LHSExpr == 0;
964 if (isLHSNull)
965 LHSExpr = CondExpr;
966
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000967 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
968 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000969 if (result.isNull())
970 return true;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000971 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
972 RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000973}
974
Steve Naroff0b661582007-08-28 23:30:39 +0000975/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroff31daee12008-01-29 02:42:22 +0000976/// do not have a prototype. Arguments that have type float are promoted to
977/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner08464942007-12-28 05:29:59 +0000978void Sema::DefaultArgumentPromotion(Expr *&Expr) {
979 QualType Ty = Expr->getType();
980 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroff0b661582007-08-28 23:30:39 +0000981
Chris Lattner08464942007-12-28 05:29:59 +0000982 if (Ty == Context.FloatTy)
Chris Lattnera65e1f32008-01-16 19:17:22 +0000983 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroff31daee12008-01-29 02:42:22 +0000984 else
985 UsualUnaryConversions(Expr);
Steve Naroff0b661582007-08-28 23:30:39 +0000986}
987
Steve Naroff81569d22007-07-15 02:02:06 +0000988/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000989void Sema::DefaultFunctionArrayConversion(Expr *&E) {
990 QualType Ty = E->getType();
991 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000992
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000993 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattner182f6602008-04-02 17:45:06 +0000994 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000995 Ty = E->getType();
Bill Wendling354fb262007-07-17 04:16:47 +0000996 }
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000997 if (Ty->isFunctionType())
998 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattnera21ad802008-04-02 05:18:44 +0000999 else if (Ty->isArrayType())
1000 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Steve Naroff1926c832007-04-24 00:23:05 +00001001}
1002
Nate Begeman1e36a852008-01-17 17:46:27 +00001003/// UsualUnaryConversions - Performs various conversions that are common to most
Steve Naroff71b59a92007-06-04 22:22:31 +00001004/// operators (C99 6.3). The conversions of array and function types are
1005/// sometimes surpressed. For example, the array->pointer conversion doesn't
1006/// apply if the array is an argument to the sizeof or address (&) operators.
1007/// In these instances, this routine should *not* be called.
Chris Lattner08464942007-12-28 05:29:59 +00001008Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
1009 QualType Ty = Expr->getType();
1010 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Steve Naroff71b59a92007-06-04 22:22:31 +00001011
Chris Lattner08464942007-12-28 05:29:59 +00001012 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattner182f6602008-04-02 17:45:06 +00001013 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner08464942007-12-28 05:29:59 +00001014 Ty = Expr->getType();
Bill Wendling354fb262007-07-17 04:16:47 +00001015 }
Chris Lattner08464942007-12-28 05:29:59 +00001016 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnera65e1f32008-01-16 19:17:22 +00001017 ImpCastExprToType(Expr, Context.IntTy);
Steve Naroff31090012007-07-16 21:54:35 +00001018 else
Chris Lattner08464942007-12-28 05:29:59 +00001019 DefaultFunctionArrayConversion(Expr);
1020
1021 return Expr;
Steve Naroff71b59a92007-06-04 22:22:31 +00001022}
1023
Chris Lattnerf17bd422007-08-30 17:45:32 +00001024/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +00001025/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
1026/// routine returns the first non-arithmetic type found. The client is
1027/// responsible for emitting appropriate error diagnostics.
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001028/// FIXME: verify the conversion rules for "complex int" are consistent with
1029/// GCC.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001030QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
1031 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +00001032 if (!isCompAssign) {
1033 UsualUnaryConversions(lhsExpr);
1034 UsualUnaryConversions(rhsExpr);
1035 }
Steve Naroff1bb21df2007-10-18 18:55:53 +00001036 // For conversion purposes, we ignore any qualifiers.
1037 // For example, "const float" and "float" are equivalent.
Steve Naroff257b4a22007-11-10 19:45:54 +00001038 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
1039 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff1926c832007-04-24 00:23:05 +00001040
Chris Lattner0c46c5d2007-06-02 23:53:17 +00001041 // If both types are identical, no conversion is needed.
Steve Naroff1bb21df2007-10-18 18:55:53 +00001042 if (lhs == rhs)
1043 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +00001044
1045 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1046 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001047 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001048 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +00001049
Chris Lattner0c46c5d2007-06-02 23:53:17 +00001050 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +00001051
1052 // Handle complex types first (C99 6.3.1.8p1).
1053 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff6fcfd052008-01-15 19:36:10 +00001054 // if we have an integer operand, the result is the complex type.
Steve Naroffabefc392008-01-15 22:21:49 +00001055 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001056 // convert the rhs to the lhs complex type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001057 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001058 return lhs;
Steve Naroff6fcfd052008-01-15 19:36:10 +00001059 }
Steve Naroffabefc392008-01-15 22:21:49 +00001060 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001061 // convert the lhs to the rhs complex type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001062 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001063 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001064 }
Steve Naroff9091ef72007-08-27 01:27:54 +00001065 // This handles complex/complex, complex/float, or float/complex.
1066 // When both operands are complex, the shorter operand is converted to the
1067 // type of the longer, and that is the type of the result. This corresponds
1068 // to what is done when combining two real floating-point operands.
1069 // The fun begins when size promotion occur across type domains.
1070 // From H&S 6.3.4: When one operand is complex and the other is a real
1071 // floating-point type, the less precise type is converted, within it's
1072 // real or complex domain, to the precision of the other type. For example,
1073 // when combining a "long double" with a "double _Complex", the
1074 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnerb90739d2008-04-06 23:38:49 +00001075 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff7af82d42007-08-27 15:30:22 +00001076
1077 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +00001078 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1079 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001080 ImpCastExprToType(rhsExpr, rhs);
Steve Naroffe31313d2007-08-27 21:32:55 +00001081 } else if (result < 0) { // The right side is bigger, convert lhs.
1082 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1083 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001084 ImpCastExprToType(lhsExpr, lhs);
Steve Naroffe31313d2007-08-27 21:32:55 +00001085 }
1086 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1087 // domains match. This is a requirement for our implementation, C99
1088 // does not require this promotion.
1089 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1090 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +00001091 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001092 ImpCastExprToType(lhsExpr, rhs);
Steve Naroffa042db22007-08-27 21:43:43 +00001093 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +00001094 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +00001095 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001096 ImpCastExprToType(rhsExpr, lhs);
Steve Naroffa042db22007-08-27 21:43:43 +00001097 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +00001098 }
Steve Naroffdbd9e892007-07-17 00:58:39 +00001099 }
Steve Naroffa042db22007-08-27 21:43:43 +00001100 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +00001101 }
Steve Naroffb01bbe32007-04-25 01:22:31 +00001102 // Now handle "real" floating types (i.e. float, double, long double).
1103 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1104 // if we have an integer operand, the result is the real floating type.
Steve Naroffabefc392008-01-15 22:21:49 +00001105 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001106 // convert rhs to the lhs floating point type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001107 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001108 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001109 }
Steve Naroffabefc392008-01-15 22:21:49 +00001110 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001111 // convert lhs to the rhs floating point type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001112 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001113 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001114 }
Steve Naroff81569d22007-07-15 02:02:06 +00001115 // We have two real floating types, float/complex combos were handled above.
1116 // Convert the smaller operand to the bigger result.
Chris Lattnerb90739d2008-04-06 23:38:49 +00001117 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff7af82d42007-08-27 15:30:22 +00001118
1119 if (result > 0) { // convert the rhs
Chris Lattnera65e1f32008-01-16 19:17:22 +00001120 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001121 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001122 }
Steve Naroff7af82d42007-08-27 15:30:22 +00001123 if (result < 0) { // convert the lhs
Chris Lattnera65e1f32008-01-16 19:17:22 +00001124 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff7af82d42007-08-27 15:30:22 +00001125 return rhs;
1126 }
1127 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +00001128 }
Steve Naroff6fcfd052008-01-15 19:36:10 +00001129 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1130 // Handle GCC complex int extension.
Steve Naroff6fcfd052008-01-15 19:36:10 +00001131 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman1974e532008-02-08 01:19:44 +00001132 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff6fcfd052008-01-15 19:36:10 +00001133
Eli Friedman1974e532008-02-08 01:19:44 +00001134 if (lhsComplexInt && rhsComplexInt) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00001135 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1136 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman113eed52008-02-08 01:24:30 +00001137 // convert the rhs
1138 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1139 return lhs;
Eli Friedman1974e532008-02-08 01:19:44 +00001140 }
1141 if (!isCompAssign)
Eli Friedman113eed52008-02-08 01:24:30 +00001142 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman1974e532008-02-08 01:19:44 +00001143 return rhs;
1144 } else if (lhsComplexInt && rhs->isIntegerType()) {
1145 // convert the rhs to the lhs complex type.
1146 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1147 return lhs;
1148 } else if (rhsComplexInt && lhs->isIntegerType()) {
1149 // convert the lhs to the rhs complex type.
1150 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1151 return rhs;
1152 }
Steve Naroff6fcfd052008-01-15 19:36:10 +00001153 }
Steve Naroff81569d22007-07-15 02:02:06 +00001154 // Finally, we have two differing integer types.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00001155 if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
Chris Lattnera65e1f32008-01-16 19:17:22 +00001156 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001157 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001158 }
Chris Lattnera65e1f32008-01-16 19:17:22 +00001159 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001160 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +00001161}
1162
Steve Naroff3f597292007-05-11 22:18:03 +00001163// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1164// being closely modeled after the C99 spec:-). The odd characteristic of this
1165// routine is it effectively iqnores the qualifiers on the top level pointee.
1166// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1167// FIXME: add a couple examples in this comment.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001168Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001169Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +00001170 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001171
1172 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +00001173 lhptee = lhsType->getAsPointerType()->getPointeeType();
1174 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001175
1176 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +00001177 lhptee = lhptee.getCanonicalType();
1178 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001179
Chris Lattner9bad62c2008-01-04 18:04:52 +00001180 AssignConvertType ConvTy = Compatible;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001181
Steve Naroff3f597292007-05-11 22:18:03 +00001182 // C99 6.5.16.1p1: This following citation is common to constraints
1183 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1184 // qualifiers of the type *pointed to* by the right;
Chris Lattner445fcab2008-02-20 20:55:12 +00001185 // FIXME: Handle ASQualType
1186 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1187 rhptee.getCVRQualifiers())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001188 ConvTy = CompatiblePointerDiscardsQualifiers;
Steve Naroff3f597292007-05-11 22:18:03 +00001189
1190 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1191 // incomplete type and the other is a pointer to a qualified or unqualified
1192 // version of void...
Chris Lattner0a788432008-01-03 22:56:36 +00001193 if (lhptee->isVoidType()) {
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001194 if (rhptee->isIncompleteOrObjectType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001195 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001196
1197 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001198 assert(rhptee->isFunctionType());
1199 return FunctionVoidPointer;
Chris Lattner0a788432008-01-03 22:56:36 +00001200 }
1201
1202 if (rhptee->isVoidType()) {
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001203 if (lhptee->isIncompleteOrObjectType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001204 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001205
1206 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001207 assert(lhptee->isFunctionType());
1208 return FunctionVoidPointer;
Chris Lattner0a788432008-01-03 22:56:36 +00001209 }
1210
Steve Naroff3f597292007-05-11 22:18:03 +00001211 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1212 // unqualified versions of compatible types, ...
Chris Lattner0a788432008-01-03 22:56:36 +00001213 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1214 rhptee.getUnqualifiedType()))
1215 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner9bad62c2008-01-04 18:04:52 +00001216 return ConvTy;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001217}
1218
Steve Naroff98cf3e92007-06-06 18:38:38 +00001219/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +00001220/// has code to accommodate several GCC extensions when type checking
1221/// pointers. Here are some objectionable examples that GCC considers warnings:
1222///
1223/// int a, *pint;
1224/// short *pshort;
1225/// struct foo *pfoo;
1226///
1227/// pint = pshort; // warning: assignment from incompatible pointer type
1228/// a = pint; // warning: assignment makes integer from pointer without a cast
1229/// pint = a; // warning: assignment makes pointer from integer without a cast
1230/// pint = pfoo; // warning: assignment from incompatible pointer type
1231///
1232/// As a result, the code for dealing with pointers is more complex than the
1233/// C99 spec dictates.
Steve Naroff17f76e02007-05-03 21:03:48 +00001234///
Chris Lattner9bad62c2008-01-04 18:04:52 +00001235Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001236Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnera52c2f22008-01-04 23:18:45 +00001237 // Get canonical types. We're not formatting these types, just comparing
1238 // them.
1239 lhsType = lhsType.getCanonicalType();
1240 rhsType = rhsType.getCanonicalType();
1241
1242 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerf5c973d2008-01-07 17:51:46 +00001243 return Compatible; // Common case: fast path an exact match.
Steve Naroff44fd8ff2007-07-24 21:46:40 +00001244
Anders Carlsson24ebce62007-10-12 23:56:29 +00001245 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattner7460fd22008-04-07 06:52:53 +00001246 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlsson24ebce62007-10-12 23:56:29 +00001247 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001248 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001249 }
Chris Lattnera52c2f22008-01-04 23:18:45 +00001250
Chris Lattner2a3569b2008-04-07 05:30:13 +00001251 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1252 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001253 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001254 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001255 }
Chris Lattner881a2122008-01-04 23:32:24 +00001256
Chris Lattnerec646832008-04-07 06:49:41 +00001257 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001258 // For ExtVector, allow vector splats; float -> <n x float>
1259 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattner881a2122008-01-04 23:32:24 +00001260 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1261 return Compatible;
1262 }
1263
1264 // If LHS and RHS are both vectors of integer or both vectors of floating
1265 // point types, and the total vector length is the same, allow the
1266 // conversion. This is a bitcast; no bits are changed but the result type
1267 // is different.
1268 if (getLangOptions().LaxVectorConversions &&
1269 lhsType->isVectorType() && rhsType->isVectorType()) {
1270 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1271 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner37e05872008-03-05 18:54:05 +00001272 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begeman330aaa72007-12-30 02:59:45 +00001273 return Compatible;
1274 }
Chris Lattner881a2122008-01-04 23:32:24 +00001275 }
1276 return Incompatible;
1277 }
1278
1279 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001280 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001281
Chris Lattnerec646832008-04-07 06:49:41 +00001282 if (isa<PointerType>(lhsType)) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001283 if (rhsType->isIntegerType())
Chris Lattner940cfeb2008-01-04 18:22:42 +00001284 return IntToPointer;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001285
Chris Lattnerec646832008-04-07 06:49:41 +00001286 if (isa<PointerType>(rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001287 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001288 return Incompatible;
1289 }
1290
Chris Lattnerec646832008-04-07 06:49:41 +00001291 if (isa<PointerType>(rhsType)) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001292 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Chris Lattnerec646832008-04-07 06:49:41 +00001293 if (lhsType->isIntegerType() && lhsType != Context.BoolTy)
Chris Lattner940cfeb2008-01-04 18:22:42 +00001294 return PointerToInt;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001295
Chris Lattnerec646832008-04-07 06:49:41 +00001296 if (isa<PointerType>(lhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001297 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001298 return Incompatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001299 }
1300
1301 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattnerec646832008-04-07 06:49:41 +00001302 if (Context.typesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001303 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001304 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001305 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001306}
1307
Chris Lattner9bad62c2008-01-04 18:04:52 +00001308Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001309Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001310 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1311 // a null pointer constant.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001312 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian5cc21a72008-01-03 18:46:52 +00001313 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001314 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001315 return Compatible;
1316 }
Chris Lattnere6dcd502007-10-16 02:55:40 +00001317 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001318 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff30d242c2007-09-15 18:49:24 +00001319 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001320 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattnere6dcd502007-10-16 02:55:40 +00001321 //
1322 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1323 // are better understood.
1324 if (!lhsType->isReferenceType())
1325 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001326
Chris Lattner9bad62c2008-01-04 18:04:52 +00001327 Sema::AssignConvertType result =
1328 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001329
1330 // C99 6.5.16.1p2: The value of the right operand is converted to the
1331 // type of the assignment expression.
1332 if (rExpr->getType() != lhsType)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001333 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001334 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001335}
1336
Chris Lattner9bad62c2008-01-04 18:04:52 +00001337Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001338Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1339 return CheckAssignmentConstraints(lhsType, rhsType);
1340}
1341
Chris Lattner5c11c412007-12-12 05:47:28 +00001342QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001343 Diag(loc, diag::err_typecheck_invalid_operands,
1344 lex->getType().getAsString(), rex->getType().getAsString(),
1345 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner5c11c412007-12-12 05:47:28 +00001346 return QualType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001347}
1348
Steve Naroff7a5af782007-07-13 16:58:59 +00001349inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1350 Expr *&rex) {
Nate Begeman002e4bd2008-04-04 01:30:25 +00001351 // For conversion purposes, we ignore any qualifiers.
1352 // For example, "const float" and "float" are equivalent.
1353 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1354 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff84ff4b42007-07-09 21:31:10 +00001355
1356 // make sure the vector types are identical.
Nate Begeman002e4bd2008-04-04 01:30:25 +00001357 if (lhsType == rhsType)
Steve Naroff84ff4b42007-07-09 21:31:10 +00001358 return lhsType;
Nate Begeman330aaa72007-12-30 02:59:45 +00001359
Nate Begemance4d7fc2008-04-18 23:10:10 +00001360 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begeman330aaa72007-12-30 02:59:45 +00001361 // promote the rhs to the vector type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001362 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begeman330aaa72007-12-30 02:59:45 +00001363 if (V->getElementType().getCanonicalType().getTypePtr()
1364 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001365 ImpCastExprToType(rex, lhsType);
Nate Begeman330aaa72007-12-30 02:59:45 +00001366 return lhsType;
1367 }
1368 }
1369
Nate Begemance4d7fc2008-04-18 23:10:10 +00001370 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begeman330aaa72007-12-30 02:59:45 +00001371 // promote the lhs to the vector type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001372 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begeman330aaa72007-12-30 02:59:45 +00001373 if (V->getElementType().getCanonicalType().getTypePtr()
1374 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001375 ImpCastExprToType(lex, rhsType);
Nate Begeman330aaa72007-12-30 02:59:45 +00001376 return rhsType;
1377 }
1378 }
1379
Steve Naroff84ff4b42007-07-09 21:31:10 +00001380 // You cannot convert between vector values of different size.
1381 Diag(loc, diag::err_typecheck_vector_not_convertable,
1382 lex->getType().getAsString(), rex->getType().getAsString(),
1383 lex->getSourceRange(), rex->getSourceRange());
1384 return QualType();
1385}
1386
Steve Naroff218bc2b2007-05-04 21:54:46 +00001387inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001388 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001389{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001390 QualType lhsType = lex->getType(), rhsType = rex->getType();
1391
1392 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001393 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001394
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001395 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001396
Steve Naroffdbd9e892007-07-17 00:58:39 +00001397 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001398 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001399 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001400}
1401
Steve Naroff218bc2b2007-05-04 21:54:46 +00001402inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001403 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001404{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001405 QualType lhsType = lex->getType(), rhsType = rex->getType();
1406
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001407 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001408
Steve Naroffdbd9e892007-07-17 00:58:39 +00001409 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001410 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001411 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001412}
1413
1414inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001415 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001416{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001417 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001418 return CheckVectorOperands(loc, lex, rex);
1419
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001420 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedman8e122982008-05-18 18:08:51 +00001421
Steve Naroffe4718892007-04-27 18:30:00 +00001422 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001423 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001424 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001425
Eli Friedman8e122982008-05-18 18:08:51 +00001426 // Put any potential pointer into PExp
1427 Expr* PExp = lex, *IExp = rex;
1428 if (IExp->getType()->isPointerType())
1429 std::swap(PExp, IExp);
1430
1431 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1432 if (IExp->getType()->isIntegerType()) {
1433 // Check for arithmetic on pointers to incomplete types
1434 if (!PTy->getPointeeType()->isObjectType()) {
1435 if (PTy->getPointeeType()->isVoidType()) {
1436 Diag(loc, diag::ext_gnu_void_ptr,
1437 lex->getSourceRange(), rex->getSourceRange());
1438 } else {
1439 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1440 lex->getType().getAsString(), lex->getSourceRange());
1441 return QualType();
1442 }
1443 }
1444 return PExp->getType();
1445 }
1446 }
1447
Chris Lattner5c11c412007-12-12 05:47:28 +00001448 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001449}
1450
Chris Lattner2a3569b2008-04-07 05:30:13 +00001451// C99 6.5.6
1452QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1453 SourceLocation loc, bool isCompAssign) {
Steve Naroff94a5aca2007-07-16 22:23:01 +00001454 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001455 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001456
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001457 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001458
Chris Lattner4d62f422007-12-09 21:53:25 +00001459 // Enforce type constraints: C99 6.5.6p3.
1460
1461 // Handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001462 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001463 return compType;
Chris Lattner4d62f422007-12-09 21:53:25 +00001464
1465 // Either ptr - int or ptr - ptr.
1466 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroffddb1dd82008-01-29 18:58:14 +00001467 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman1974e532008-02-08 01:19:44 +00001468
Chris Lattner4d62f422007-12-09 21:53:25 +00001469 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001470 if (!lpointee->isObjectType()) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001471 // Handle the GNU void* extension.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001472 if (lpointee->isVoidType()) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001473 Diag(loc, diag::ext_gnu_void_ptr,
1474 lex->getSourceRange(), rex->getSourceRange());
1475 } else {
1476 Diag(loc, diag::err_typecheck_sub_ptr_object,
1477 lex->getType().getAsString(), lex->getSourceRange());
1478 return QualType();
1479 }
1480 }
1481
1482 // The result type of a pointer-int computation is the pointer type.
1483 if (rex->getType()->isIntegerType())
1484 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001485
Chris Lattner4d62f422007-12-09 21:53:25 +00001486 // Handle pointer-pointer subtractions.
1487 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001488 QualType rpointee = RHSPTy->getPointeeType();
1489
Chris Lattner4d62f422007-12-09 21:53:25 +00001490 // RHS must be an object type, unless void (GNU).
Steve Naroffddb1dd82008-01-29 18:58:14 +00001491 if (!rpointee->isObjectType()) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001492 // Handle the GNU void* extension.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001493 if (rpointee->isVoidType()) {
1494 if (!lpointee->isVoidType())
Chris Lattner4d62f422007-12-09 21:53:25 +00001495 Diag(loc, diag::ext_gnu_void_ptr,
1496 lex->getSourceRange(), rex->getSourceRange());
1497 } else {
1498 Diag(loc, diag::err_typecheck_sub_ptr_object,
1499 rex->getType().getAsString(), rex->getSourceRange());
1500 return QualType();
1501 }
1502 }
1503
1504 // Pointee types must be compatible.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001505 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1506 rpointee.getUnqualifiedType())) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001507 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1508 lex->getType().getAsString(), rex->getType().getAsString(),
1509 lex->getSourceRange(), rex->getSourceRange());
1510 return QualType();
1511 }
1512
1513 return Context.getPointerDiffType();
1514 }
1515 }
1516
Chris Lattner5c11c412007-12-12 05:47:28 +00001517 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001518}
1519
Chris Lattner2a3569b2008-04-07 05:30:13 +00001520// C99 6.5.7
1521QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1522 bool isCompAssign) {
Chris Lattner5c11c412007-12-12 05:47:28 +00001523 // C99 6.5.7p2: Each of the operands shall have integer type.
1524 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1525 return InvalidOperands(loc, lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001526
Chris Lattner5c11c412007-12-12 05:47:28 +00001527 // Shifts don't perform usual arithmetic conversions, they just do integer
1528 // promotions on each operand. C99 6.5.7p3
Chris Lattner3c133402007-12-13 07:28:16 +00001529 if (!isCompAssign)
1530 UsualUnaryConversions(lex);
Chris Lattner5c11c412007-12-12 05:47:28 +00001531 UsualUnaryConversions(rex);
1532
1533 // "The type of the result is that of the promoted left operand."
1534 return lex->getType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001535}
1536
Chris Lattner2a3569b2008-04-07 05:30:13 +00001537// C99 6.5.8
1538QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1539 bool isRelational) {
Chris Lattnerb620c342007-08-26 01:18:55 +00001540 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001541 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1542 UsualArithmeticConversions(lex, rex);
1543 else {
1544 UsualUnaryConversions(lex);
1545 UsualUnaryConversions(rex);
1546 }
Steve Naroff31090012007-07-16 21:54:35 +00001547 QualType lType = lex->getType();
1548 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001549
Ted Kremeneke2763b02007-10-29 17:13:39 +00001550 // For non-floating point types, check for self-comparisons of the form
1551 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1552 // often indicate logic errors in the program.
Ted Kremeneke451eae2007-10-29 16:58:49 +00001553 if (!lType->isFloatingType()) {
Ted Kremenekfff70962008-01-17 16:57:34 +00001554 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1555 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremeneke451eae2007-10-29 16:58:49 +00001556 if (DRL->getDecl() == DRR->getDecl())
1557 Diag(loc, diag::warn_selfcomparison);
1558 }
1559
Chris Lattnerb620c342007-08-26 01:18:55 +00001560 if (isRelational) {
1561 if (lType->isRealType() && rType->isRealType())
1562 return Context.IntTy;
1563 } else {
Ted Kremeneke2763b02007-10-29 17:13:39 +00001564 // Check for comparisons of floating point operands using != and ==.
Ted Kremeneke2763b02007-10-29 17:13:39 +00001565 if (lType->isFloatingType()) {
1566 assert (rType->isFloatingType());
Ted Kremenek43fb8b02007-11-25 00:58:00 +00001567 CheckFloatComparison(loc,lex,rex);
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001568 }
1569
Chris Lattnerb620c342007-08-26 01:18:55 +00001570 if (lType->isArithmeticType() && rType->isArithmeticType())
1571 return Context.IntTy;
1572 }
Steve Naroffe4718892007-04-27 18:30:00 +00001573
Chris Lattner1895e582007-08-26 01:10:14 +00001574 bool LHSIsNull = lex->isNullPointerConstant(Context);
1575 bool RHSIsNull = rex->isNullPointerConstant(Context);
1576
Chris Lattnerb620c342007-08-26 01:18:55 +00001577 // All of the following pointer related warnings are GCC extensions, except
1578 // when handling null pointer constants. One day, we can consider making them
1579 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001580 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner3a0702e2008-04-03 05:07:25 +00001581 QualType LCanPointeeTy =
1582 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1583 QualType RCanPointeeTy =
1584 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman1974e532008-02-08 01:19:44 +00001585
Steve Naroffb6666252007-11-13 14:57:38 +00001586 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner3a0702e2008-04-03 05:07:25 +00001587 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1588 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1589 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001590 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1591 lType.getAsString(), rType.getAsString(),
1592 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001593 }
Chris Lattnera65e1f32008-01-16 19:17:22 +00001594 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001595 return Context.IntTy;
1596 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001597 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
Chris Lattner2a3569b2008-04-07 05:30:13 +00001598 && ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001599 ImpCastExprToType(rex, lType);
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001600 return Context.IntTy;
1601 }
Steve Naroffcdee44c2007-08-16 21:48:38 +00001602 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001603 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001604 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1605 lType.getAsString(), rType.getAsString(),
1606 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnera65e1f32008-01-16 19:17:22 +00001607 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001608 return Context.IntTy;
1609 }
1610 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001611 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001612 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1613 lType.getAsString(), rType.getAsString(),
1614 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnera65e1f32008-01-16 19:17:22 +00001615 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001616 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001617 }
Chris Lattner5c11c412007-12-12 05:47:28 +00001618 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001619}
1620
Steve Naroff218bc2b2007-05-04 21:54:46 +00001621inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001622 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001623{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001624 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001625 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001626
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001627 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001628
Steve Naroffdbd9e892007-07-17 00:58:39 +00001629 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001630 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001631 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001632}
1633
Steve Naroff218bc2b2007-05-04 21:54:46 +00001634inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001635 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001636{
Steve Naroff31090012007-07-16 21:54:35 +00001637 UsualUnaryConversions(lex);
1638 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001639
Eli Friedman58639e52008-05-13 20:16:47 +00001640 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001641 return Context.IntTy;
Chris Lattner5c11c412007-12-12 05:47:28 +00001642 return InvalidOperands(loc, lex, rex);
Steve Naroffae4143e2007-04-26 20:39:23 +00001643}
1644
Steve Naroff35d85152007-05-07 00:24:15 +00001645inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001646 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001647{
Steve Naroff0af91202007-04-27 21:51:21 +00001648 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001649 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff9358c712007-05-27 23:58:33 +00001650 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1651
1652 switch (mlval) { // C99 6.5.16p2
Chris Lattner9bad62c2008-01-04 18:04:52 +00001653 case Expr::MLV_Valid:
1654 break;
1655 case Expr::MLV_ConstQualified:
1656 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1657 return QualType();
1658 case Expr::MLV_ArrayType:
1659 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1660 lhsType.getAsString(), lex->getSourceRange());
1661 return QualType();
1662 case Expr::MLV_NotObjectType:
1663 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1664 lhsType.getAsString(), lex->getSourceRange());
1665 return QualType();
1666 case Expr::MLV_InvalidExpression:
1667 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1668 lex->getSourceRange());
1669 return QualType();
1670 case Expr::MLV_IncompleteType:
1671 case Expr::MLV_IncompleteVoidType:
1672 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1673 lhsType.getAsString(), lex->getSourceRange());
1674 return QualType();
1675 case Expr::MLV_DuplicateVectorComponents:
1676 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1677 lex->getSourceRange());
1678 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001679 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001680
Chris Lattner9bad62c2008-01-04 18:04:52 +00001681 AssignConvertType ConvTy;
1682 if (compoundType.isNull())
1683 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1684 else
1685 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1686
1687 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1688 rex, "assigning"))
1689 return QualType();
1690
Steve Naroff98cf3e92007-06-06 18:38:38 +00001691 // C99 6.5.16p3: The type of an assignment expression is the type of the
1692 // left operand unless the left operand has qualified type, in which case
1693 // it is the unqualified version of the type of the left operand.
1694 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1695 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001696 // C++ 5.17p1: the type of the assignment expression is that of its left
1697 // oprdu.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001698 return lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001699}
1700
Steve Naroff218bc2b2007-05-04 21:54:46 +00001701inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001702 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001703 UsualUnaryConversions(rex);
1704 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001705}
1706
Steve Naroff7a5af782007-07-13 16:58:59 +00001707/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1708/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001709QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001710 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001711 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001712
Steve Naroff9d139172007-08-24 17:20:07 +00001713 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff5f9ae642007-11-11 14:15:57 +00001714 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedman8e122982008-05-18 18:08:51 +00001715 if (pt->getPointeeType()->isVoidType()) {
1716 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1717 } else if (!pt->getPointeeType()->isObjectType()) {
1718 // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001719 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1720 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001721 return QualType();
1722 }
Steve Naroff9d139172007-08-24 17:20:07 +00001723 } else if (!resType->isRealType()) {
1724 if (resType->isComplexType())
1725 // C99 does not support ++/-- on complex types.
1726 Diag(OpLoc, diag::ext_integer_increment_complex,
1727 resType.getAsString(), op->getSourceRange());
1728 else {
1729 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1730 resType.getAsString(), op->getSourceRange());
1731 return QualType();
1732 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001733 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001734 // At this point, we know we have a real, complex or pointer type.
1735 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001736 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1737 if (mlval != Expr::MLV_Valid) {
1738 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001739 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001740 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001741 return QualType();
1742 }
1743 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001744}
1745
Anders Carlsson806700f2008-02-01 07:15:58 +00001746/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001747/// This routine allows us to typecheck complex/recursive expressions
1748/// where the declaration is needed for type checking. Here are some
1749/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001750static ValueDecl *getPrimaryDecl(Expr *E) {
1751 switch (E->getStmtClass()) {
Steve Naroff47500512007-04-19 23:00:49 +00001752 case Stmt::DeclRefExprClass:
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001753 return cast<DeclRefExpr>(E)->getDecl();
Steve Naroff47500512007-04-19 23:00:49 +00001754 case Stmt::MemberExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001755 // Fields cannot be declared with a 'register' storage class.
1756 // &X->f is always ok, even if X is declared register.
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001757 if (cast<MemberExpr>(E)->isArrow())
Chris Lattner48d52842007-11-16 17:46:48 +00001758 return 0;
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001759 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson806700f2008-02-01 07:15:58 +00001760 case Stmt::ArraySubscriptExprClass: {
1761 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1762
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001763 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson59435b22008-02-01 16:01:31 +00001764 if (!VD || VD->getType()->isPointerType())
Anders Carlsson806700f2008-02-01 07:15:58 +00001765 return 0;
1766 else
1767 return VD;
1768 }
Steve Naroff47500512007-04-19 23:00:49 +00001769 case Stmt::UnaryOperatorClass:
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001770 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001771 case Stmt::ParenExprClass:
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001772 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner48d52842007-11-16 17:46:48 +00001773 case Stmt::ImplicitCastExprClass:
1774 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001775 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001776 default:
1777 return 0;
1778 }
1779}
1780
1781/// CheckAddressOfOperand - The operand of & must be either a function
1782/// designator or an lvalue designating an object. If it is an lvalue, the
1783/// object cannot be declared with storage class register or be a bit field.
1784/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001785/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001786QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff826e91a2008-01-13 17:10:08 +00001787 if (getLangOptions().C99) {
1788 // Implement C99-only parts of addressof rules.
1789 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1790 if (uOp->getOpcode() == UnaryOperator::Deref)
1791 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1792 // (assuming the deref expression is valid).
1793 return uOp->getSubExpr()->getType();
1794 }
1795 // Technically, there should be a check for array subscript
1796 // expressions here, but the result of one is always an lvalue anyway.
1797 }
Anders Carlsson806700f2008-02-01 07:15:58 +00001798 ValueDecl *dcl = getPrimaryDecl(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001799 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001800
Steve Naroff9358c712007-05-27 23:58:33 +00001801 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattner48d52842007-11-16 17:46:48 +00001802 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1803 // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001804 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1805 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001806 return QualType();
1807 }
Steve Naroffb96e4ab62008-02-29 23:30:25 +00001808 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1809 if (MemExpr->getMemberDecl()->isBitField()) {
1810 Diag(OpLoc, diag::err_typecheck_address_of,
1811 std::string("bit-field"), op->getSourceRange());
1812 return QualType();
1813 }
1814 // Check for Apple extension for accessing vector components.
1815 } else if (isa<ArraySubscriptExpr>(op) &&
1816 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1817 Diag(OpLoc, diag::err_typecheck_address_of,
1818 std::string("vector"), op->getSourceRange());
1819 return QualType();
1820 } else if (dcl) { // C99 6.5.3.2p1
Steve Naroff47500512007-04-19 23:00:49 +00001821 // We have an lvalue with a decl. Make sure the decl is not declared
1822 // with the register storage-class specifier.
1823 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001824 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroffb96e4ab62008-02-29 23:30:25 +00001825 Diag(OpLoc, diag::err_typecheck_address_of,
1826 std::string("register variable"), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001827 return QualType();
1828 }
Steve Narofff633d092007-04-25 19:01:39 +00001829 } else
1830 assert(0 && "Unknown/unexpected decl type");
Steve Naroff47500512007-04-19 23:00:49 +00001831 }
1832 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001833 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001834}
1835
Steve Naroff35d85152007-05-07 00:24:15 +00001836QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001837 UsualUnaryConversions(op);
1838 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001839
Chris Lattnerc996b172007-07-31 16:53:04 +00001840 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff826e91a2008-01-13 17:10:08 +00001841 // Note that per both C89 and C99, this is always legal, even
1842 // if ptype is an incomplete type or void.
1843 // It would be possible to warn about dereferencing a
1844 // void pointer, but it's completely well-defined,
1845 // and such a warning is unlikely to catch any mistakes.
1846 return PT->getPointeeType();
Steve Naroff758ada12007-05-28 16:15:57 +00001847 }
1848 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001849 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001850 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001851}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001852
1853static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1854 tok::TokenKind Kind) {
1855 BinaryOperator::Opcode Opc;
1856 switch (Kind) {
1857 default: assert(0 && "Unknown binop!");
1858 case tok::star: Opc = BinaryOperator::Mul; break;
1859 case tok::slash: Opc = BinaryOperator::Div; break;
1860 case tok::percent: Opc = BinaryOperator::Rem; break;
1861 case tok::plus: Opc = BinaryOperator::Add; break;
1862 case tok::minus: Opc = BinaryOperator::Sub; break;
1863 case tok::lessless: Opc = BinaryOperator::Shl; break;
1864 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1865 case tok::lessequal: Opc = BinaryOperator::LE; break;
1866 case tok::less: Opc = BinaryOperator::LT; break;
1867 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1868 case tok::greater: Opc = BinaryOperator::GT; break;
1869 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1870 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1871 case tok::amp: Opc = BinaryOperator::And; break;
1872 case tok::caret: Opc = BinaryOperator::Xor; break;
1873 case tok::pipe: Opc = BinaryOperator::Or; break;
1874 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1875 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1876 case tok::equal: Opc = BinaryOperator::Assign; break;
1877 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1878 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1879 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1880 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1881 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1882 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1883 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1884 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1885 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1886 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1887 case tok::comma: Opc = BinaryOperator::Comma; break;
1888 }
1889 return Opc;
1890}
1891
Steve Naroff35d85152007-05-07 00:24:15 +00001892static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1893 tok::TokenKind Kind) {
1894 UnaryOperator::Opcode Opc;
1895 switch (Kind) {
1896 default: assert(0 && "Unknown unary op!");
1897 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1898 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1899 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1900 case tok::star: Opc = UnaryOperator::Deref; break;
1901 case tok::plus: Opc = UnaryOperator::Plus; break;
1902 case tok::minus: Opc = UnaryOperator::Minus; break;
1903 case tok::tilde: Opc = UnaryOperator::Not; break;
1904 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1905 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1906 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1907 case tok::kw___real: Opc = UnaryOperator::Real; break;
1908 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001909 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001910 }
1911 return Opc;
1912}
1913
Steve Naroff218bc2b2007-05-04 21:54:46 +00001914// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001915Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Steve Naroff218bc2b2007-05-04 21:54:46 +00001916 ExprTy *LHS, ExprTy *RHS) {
1917 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1918 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1919
Steve Naroff83895f72007-09-16 03:34:24 +00001920 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1921 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Steve Naroff218bc2b2007-05-04 21:54:46 +00001922
Chris Lattner256c21b2007-06-28 03:53:10 +00001923 QualType ResultTy; // Result type of the binary operator.
1924 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001925
1926 switch (Opc) {
1927 default:
1928 assert(0 && "Unknown binary expr!");
1929 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001930 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001931 break;
1932 case BinaryOperator::Mul:
1933 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001934 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001935 break;
1936 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001937 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001938 break;
1939 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001940 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001941 break;
1942 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001943 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001944 break;
1945 case BinaryOperator::Shl:
1946 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001947 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001948 break;
1949 case BinaryOperator::LE:
1950 case BinaryOperator::LT:
1951 case BinaryOperator::GE:
1952 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001953 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001954 break;
1955 case BinaryOperator::EQ:
1956 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001957 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001958 break;
1959 case BinaryOperator::And:
1960 case BinaryOperator::Xor:
1961 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001962 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001963 break;
1964 case BinaryOperator::LAnd:
1965 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001966 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001967 break;
1968 case BinaryOperator::MulAssign:
1969 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001970 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001971 if (!CompTy.isNull())
1972 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001973 break;
1974 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001975 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001976 if (!CompTy.isNull())
1977 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001978 break;
1979 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001980 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001981 if (!CompTy.isNull())
1982 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001983 break;
1984 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001985 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001986 if (!CompTy.isNull())
1987 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001988 break;
1989 case BinaryOperator::ShlAssign:
1990 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001991 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001992 if (!CompTy.isNull())
1993 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001994 break;
1995 case BinaryOperator::AndAssign:
1996 case BinaryOperator::XorAssign:
1997 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001998 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001999 if (!CompTy.isNull())
2000 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002001 break;
2002 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00002003 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002004 break;
2005 }
Chris Lattner256c21b2007-06-28 03:53:10 +00002006 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00002007 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00002008 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00002009 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00002010 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00002011 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002012}
2013
Steve Naroff35d85152007-05-07 00:24:15 +00002014// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00002015Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00002016 ExprTy *input) {
2017 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00002018 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2019 QualType resultType;
2020 switch (Opc) {
2021 default:
2022 assert(0 && "Unimplemented unary expr!");
2023 case UnaryOperator::PreInc:
2024 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00002025 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002026 break;
2027 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00002028 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002029 break;
2030 case UnaryOperator::Deref:
Steve Naroffb7235642007-12-18 04:06:57 +00002031 DefaultFunctionArrayConversion(Input);
Chris Lattner86554282007-06-08 22:32:33 +00002032 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002033 break;
2034 case UnaryOperator::Plus:
2035 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00002036 UsualUnaryConversions(Input);
2037 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00002038 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00002039 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2040 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00002041 break;
2042 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00002043 UsualUnaryConversions(Input);
2044 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00002045 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2046 if (!resultType->isIntegerType()) {
2047 if (resultType->isComplexType())
2048 // C99 does not support '~' for complex conjugation.
2049 Diag(OpLoc, diag::ext_integer_complement_complex,
2050 resultType.getAsString());
2051 else
2052 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2053 resultType.getAsString());
2054 }
Steve Naroff35d85152007-05-07 00:24:15 +00002055 break;
2056 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00002057 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00002058 DefaultFunctionArrayConversion(Input);
2059 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00002060 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00002061 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2062 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00002063 // LNot always has type int. C99 6.5.3.3p5.
2064 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00002065 break;
2066 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00002067 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00002068 break;
Steve Naroff35d85152007-05-07 00:24:15 +00002069 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00002070 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2071 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00002072 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00002073 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00002074 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00002075 break;
Chris Lattner86554282007-06-08 22:32:33 +00002076 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00002077 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00002078 break;
Steve Naroff35d85152007-05-07 00:24:15 +00002079 }
2080 if (resultType.isNull())
2081 return true;
Chris Lattner86554282007-06-08 22:32:33 +00002082 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002083}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002084
Steve Naroff66356bd2007-09-16 14:56:35 +00002085/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2086Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattnereefa10e2007-05-28 06:56:27 +00002087 SourceLocation LabLoc,
2088 IdentifierInfo *LabelII) {
2089 // Look up the record for this label identifier.
2090 LabelStmt *&LabelDecl = LabelMap[LabelII];
2091
2092 // If we haven't seen this label yet, create a forward reference.
2093 if (LabelDecl == 0)
2094 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2095
2096 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00002097 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2098 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00002099}
2100
Steve Naroff66356bd2007-09-16 14:56:35 +00002101Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner366727f2007-07-24 16:58:17 +00002102 SourceLocation RPLoc) { // "({..})"
2103 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2104 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2105 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2106
2107 // FIXME: there are a variety of strange constraints to enforce here, for
2108 // example, it is not possible to goto into a stmt expression apparently.
2109 // More semantic analysis is needed.
2110
2111 // FIXME: the last statement in the compount stmt has its value used. We
2112 // should not warn about it being unused.
2113
2114 // If there are sub stmts in the compound stmt, take the type of the last one
2115 // as the type of the stmtexpr.
2116 QualType Ty = Context.VoidTy;
2117
2118 if (!Compound->body_empty())
2119 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2120 Ty = LastExpr->getType();
2121
2122 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2123}
Steve Naroff78864672007-08-01 22:05:33 +00002124
Steve Naroff66356bd2007-09-16 14:56:35 +00002125Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattnerf17bd422007-08-30 17:45:32 +00002126 SourceLocation TypeLoc,
2127 TypeTy *argty,
2128 OffsetOfComponent *CompPtr,
2129 unsigned NumComponents,
2130 SourceLocation RPLoc) {
2131 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2132 assert(!ArgTy.isNull() && "Missing type argument!");
2133
2134 // We must have at least one component that refers to the type, and the first
2135 // one is known to be a field designator. Verify that the ArgTy represents
2136 // a struct/union/class.
2137 if (!ArgTy->isRecordType())
2138 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2139
2140 // Otherwise, create a compound literal expression as the base, and
2141 // iteratively process the offsetof designators.
Steve Naroffd32419d2008-01-14 18:19:28 +00002142 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattnerf17bd422007-08-30 17:45:32 +00002143
Chris Lattner78502cf2007-08-31 21:49:13 +00002144 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2145 // GCC extension, diagnose them.
2146 if (NumComponents != 1)
2147 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2148 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2149
Chris Lattnerf17bd422007-08-30 17:45:32 +00002150 for (unsigned i = 0; i != NumComponents; ++i) {
2151 const OffsetOfComponent &OC = CompPtr[i];
2152 if (OC.isBrackets) {
2153 // Offset of an array sub-field. TODO: Should we allow vector elements?
2154 const ArrayType *AT = Res->getType()->getAsArrayType();
2155 if (!AT) {
2156 delete Res;
2157 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2158 Res->getType().getAsString());
2159 }
2160
Chris Lattner98dbf0a2007-08-30 17:59:59 +00002161 // FIXME: C++: Verify that operator[] isn't overloaded.
2162
Chris Lattnerf17bd422007-08-30 17:45:32 +00002163 // C99 6.5.2.1p1
2164 Expr *Idx = static_cast<Expr*>(OC.U.E);
2165 if (!Idx->getType()->isIntegerType())
2166 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2167 Idx->getSourceRange());
2168
2169 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2170 continue;
2171 }
2172
2173 const RecordType *RC = Res->getType()->getAsRecordType();
2174 if (!RC) {
2175 delete Res;
2176 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2177 Res->getType().getAsString());
2178 }
2179
2180 // Get the decl corresponding to this.
2181 RecordDecl *RD = RC->getDecl();
2182 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2183 if (!MemberDecl)
2184 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2185 OC.U.IdentInfo->getName(),
2186 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00002187
2188 // FIXME: C++: Verify that MemberDecl isn't a static field.
2189 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman1242fff2008-02-06 22:48:16 +00002190 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2191 // matter here.
2192 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattnerf17bd422007-08-30 17:45:32 +00002193 }
2194
2195 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2196 BuiltinLoc);
2197}
2198
2199
Steve Naroff66356bd2007-09-16 14:56:35 +00002200Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00002201 TypeTy *arg1, TypeTy *arg2,
2202 SourceLocation RPLoc) {
2203 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2204 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2205
2206 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2207
Chris Lattnerf17bd422007-08-30 17:45:32 +00002208 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00002209}
2210
Steve Naroff66356bd2007-09-16 14:56:35 +00002211Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff9efdabc2007-08-03 21:21:27 +00002212 ExprTy *expr1, ExprTy *expr2,
2213 SourceLocation RPLoc) {
2214 Expr *CondExpr = static_cast<Expr*>(cond);
2215 Expr *LHSExpr = static_cast<Expr*>(expr1);
2216 Expr *RHSExpr = static_cast<Expr*>(expr2);
2217
2218 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2219
2220 // The conditional expression is required to be a constant expression.
2221 llvm::APSInt condEval(32);
2222 SourceLocation ExpLoc;
2223 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2224 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2225 CondExpr->getSourceRange());
2226
2227 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2228 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2229 RHSExpr->getType();
2230 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2231}
2232
Nate Begeman936b2072008-01-30 20:50:20 +00002233/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman1e36a852008-01-17 17:46:27 +00002234/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begeman936b2072008-01-30 20:50:20 +00002235/// The number of arguments has already been validated to match the number of
2236/// arguments in FnType.
2237static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman1e36a852008-01-17 17:46:27 +00002238 unsigned NumParams = FnType->getNumArgs();
Nate Begeman46bd0372008-04-18 23:35:14 +00002239 for (unsigned i = 0; i != NumParams; ++i) {
2240 QualType ExprTy = Args[i]->getType().getCanonicalType();
2241 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2242
2243 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman1e36a852008-01-17 17:46:27 +00002244 return false;
Nate Begeman46bd0372008-04-18 23:35:14 +00002245 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002246 return true;
2247}
2248
2249Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2250 SourceLocation *CommaLocs,
2251 SourceLocation BuiltinLoc,
2252 SourceLocation RParenLoc) {
Nate Begeman4cd66892008-01-31 05:38:29 +00002253 // __builtin_overload requires at least 2 arguments
2254 if (NumArgs < 2)
2255 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2256 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman1e36a852008-01-17 17:46:27 +00002257
Nate Begeman1e36a852008-01-17 17:46:27 +00002258 // The first argument is required to be a constant expression. It tells us
2259 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begeman4cd66892008-01-31 05:38:29 +00002260 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman1e36a852008-01-17 17:46:27 +00002261 Expr *NParamsExpr = Args[0];
2262 llvm::APSInt constEval(32);
2263 SourceLocation ExpLoc;
2264 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2265 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2266 NParamsExpr->getSourceRange());
2267
2268 // Verify that the number of parameters is > 0
2269 unsigned NumParams = constEval.getZExtValue();
2270 if (NumParams == 0)
2271 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2272 NParamsExpr->getSourceRange());
2273 // Verify that we have at least 1 + NumParams arguments to the builtin.
2274 if ((NumParams + 1) > NumArgs)
2275 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2276 SourceRange(BuiltinLoc, RParenLoc));
2277
2278 // Figure out the return type, by matching the args to one of the functions
Nate Begeman936b2072008-01-30 20:50:20 +00002279 // listed after the parameters.
Nate Begeman4cd66892008-01-31 05:38:29 +00002280 OverloadExpr *OE = 0;
Nate Begeman1e36a852008-01-17 17:46:27 +00002281 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2282 // UsualUnaryConversions will convert the function DeclRefExpr into a
2283 // pointer to function.
2284 Expr *Fn = UsualUnaryConversions(Args[i]);
2285 FunctionTypeProto *FnType = 0;
Nate Begeman936b2072008-01-30 20:50:20 +00002286 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2287 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2288 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2289 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002290
2291 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2292 // parameters, and the number of parameters must match the value passed to
2293 // the builtin.
2294 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begeman936b2072008-01-30 20:50:20 +00002295 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2296 Fn->getSourceRange());
Nate Begeman1e36a852008-01-17 17:46:27 +00002297
2298 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begeman936b2072008-01-30 20:50:20 +00002299 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman1e36a852008-01-17 17:46:27 +00002300 // If they match, return a new OverloadExpr.
Nate Begeman4cd66892008-01-31 05:38:29 +00002301 if (ExprsMatchFnType(Args+1, FnType)) {
2302 if (OE)
2303 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2304 OE->getFn()->getSourceRange());
2305 // Remember our match, and continue processing the remaining arguments
2306 // to catch any errors.
2307 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2308 BuiltinLoc, RParenLoc);
2309 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002310 }
Nate Begeman4cd66892008-01-31 05:38:29 +00002311 // Return the newly created OverloadExpr node, if we succeded in matching
2312 // exactly one of the candidate functions.
2313 if (OE)
2314 return OE;
Nate Begeman1e36a852008-01-17 17:46:27 +00002315
2316 // If we didn't find a matching function Expr in the __builtin_overload list
2317 // the return an error.
2318 std::string typeNames;
Nate Begeman936b2072008-01-30 20:50:20 +00002319 for (unsigned i = 0; i != NumParams; ++i) {
2320 if (i != 0) typeNames += ", ";
2321 typeNames += Args[i+1]->getType().getAsString();
2322 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002323
2324 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2325 SourceRange(BuiltinLoc, RParenLoc));
2326}
2327
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002328Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2329 ExprTy *expr, TypeTy *type,
Chris Lattner9bad62c2008-01-04 18:04:52 +00002330 SourceLocation RPLoc) {
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002331 Expr *E = static_cast<Expr*>(expr);
2332 QualType T = QualType::getFromOpaquePtr(type);
2333
2334 InitBuiltinVaListType();
2335
Chris Lattner9bad62c2008-01-04 18:04:52 +00002336 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2337 != Compatible)
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002338 return Diag(E->getLocStart(),
2339 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2340 E->getType().getAsString(),
2341 E->getSourceRange());
2342
2343 // FIXME: Warn if a non-POD type is passed in.
2344
2345 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2346}
2347
Chris Lattner9bad62c2008-01-04 18:04:52 +00002348bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2349 SourceLocation Loc,
2350 QualType DstType, QualType SrcType,
2351 Expr *SrcExpr, const char *Flavor) {
2352 // Decode the result (notice that AST's are still created for extensions).
2353 bool isInvalid = false;
2354 unsigned DiagKind;
2355 switch (ConvTy) {
2356 default: assert(0 && "Unknown conversion type");
2357 case Compatible: return false;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002358 case PointerToInt:
Chris Lattner9bad62c2008-01-04 18:04:52 +00002359 DiagKind = diag::ext_typecheck_convert_pointer_int;
2360 break;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002361 case IntToPointer:
2362 DiagKind = diag::ext_typecheck_convert_int_pointer;
2363 break;
Chris Lattner9bad62c2008-01-04 18:04:52 +00002364 case IncompatiblePointer:
2365 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2366 break;
2367 case FunctionVoidPointer:
2368 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2369 break;
2370 case CompatiblePointerDiscardsQualifiers:
2371 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2372 break;
2373 case Incompatible:
2374 DiagKind = diag::err_typecheck_convert_incompatible;
2375 isInvalid = true;
2376 break;
2377 }
2378
2379 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2380 SrcExpr->getSourceRange());
2381 return isInvalid;
2382}
2383
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +00002384
2385