blob: 04561f5f629a1c4aac3b903ee772cce50c106b01 [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 Naroff021ca182008-05-29 21:12:08 +000019#include "clang/AST/ExprObjC.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000020#include "clang/Parse/DeclSpec.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000022#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000023#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner08464942007-12-28 05:29:59 +000025#include "llvm/ADT/OwningPtr.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000028using namespace clang;
29
Steve Naroff83895f72007-09-16 03:34:24 +000030/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000031/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
32/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
33/// multiple tokens. However, the common case is that StringToks points to one
34/// string.
35///
36Action::ExprResult
Steve Naroff83895f72007-09-16 03:34:24 +000037Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000038 assert(NumStringToks && "Must have at least one string!");
39
Steve Naroff4f88b312007-03-13 22:37:02 +000040 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
41 if (Literal.hadError)
42 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000043
Chris Lattner23b7eb62007-06-15 23:05:46 +000044 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000045 for (unsigned i = 0; i != NumStringToks; ++i)
46 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattner36fc8792008-02-11 00:02:17 +000047
48 // Verify that pascal strings aren't too large.
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000049 if (Literal.Pascal && Literal.GetStringLength() > 256)
50 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
51 SourceRange(StringToks[0].getLocation(),
52 StringToks[NumStringToks-1].getLocation()));
Steve Narofff1e53692007-03-23 22:27:02 +000053
Chris Lattner36fc8792008-02-11 00:02:17 +000054 QualType StrTy = Context.CharTy;
Eli Friedmana9040872008-05-27 07:57:14 +000055 if (Literal.AnyWide) StrTy = Context.getWcharType();
Chris Lattner36fc8792008-02-11 00:02:17 +000056 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
57
58 // Get an array type for the string, according to C99 6.4.5. This includes
59 // the nul terminator character as well as the string length for pascal
60 // strings.
61 StrTy = Context.getConstantArrayType(StrTy,
62 llvm::APInt(32, Literal.GetStringLength()+1),
63 ArrayType::Normal, 0);
64
Chris Lattner5b183d82006-11-10 05:03:26 +000065 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000066 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattner36fc8792008-02-11 00:02:17 +000067 Literal.AnyWide, StrTy,
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000068 StringToks[0].getLocation(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000069 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000070}
71
Chris Lattnere168f762006-11-10 05:29:30 +000072
Steve Naroff30d242c2007-09-15 18:49:24 +000073/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattnerac18be92006-11-20 06:49:47 +000074/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroff157c4032008-03-19 23:46:26 +000075/// identifier is used in a function call context.
Steve Naroff30d242c2007-09-15 18:49:24 +000076Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +000077 IdentifierInfo &II,
78 bool HasTrailingLParen) {
Chris Lattner59a25942008-03-31 00:36:02 +000079 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff2fc93f52008-04-02 14:35:35 +000080 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattner59a25942008-03-31 00:36:02 +000081
82 // If this reference is in an Objective-C method, then ivar lookup happens as
83 // well.
84 if (CurMethodDecl) {
Steve Naroff257520b2008-04-01 23:04:06 +000085 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattner59a25942008-03-31 00:36:02 +000086 // There are two cases to handle here. 1) scoped lookup could have failed,
87 // in which case we should look for an ivar. 2) scoped lookup could have
88 // found a decl, but that decl is outside the current method (i.e. a global
89 // variable). In these two cases, we do a lookup for an ivar with this
90 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroff257520b2008-04-01 23:04:06 +000091 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Chris Lattner59a25942008-03-31 00:36:02 +000092 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface(), *DeclClass;
93 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, DeclClass)) {
94 // FIXME: This should use a new expr for a direct reference, don't turn
95 // this into Self->ivar, just return a BareIVarExpr or something.
96 IdentifierInfo &II = Context.Idents.get("self");
97 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
98 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
99 static_cast<Expr*>(SelfExpr.Val), true, true);
100 }
101 }
102 }
103
Chris Lattner17ed4872006-11-20 04:58:19 +0000104 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +0000105 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +0000106 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +0000107 if (HasTrailingLParen &&
Chris Lattner59a25942008-03-31 00:36:02 +0000108 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattnerac18be92006-11-20 06:49:47 +0000109 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +0000110 else {
Chris Lattnerac18be92006-11-20 06:49:47 +0000111 // If this name wasn't predeclared and if this is not a function call,
112 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +0000113 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +0000114 }
Chris Lattner17ed4872006-11-20 04:58:19 +0000115 }
Chris Lattner59a25942008-03-31 00:36:02 +0000116
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000117 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner8a8558b2008-02-29 16:48:43 +0000118 // check if referencing an identifier with __attribute__((deprecated)).
119 if (VD->getAttr<DeprecatedAttr>())
120 Diag(Loc, diag::warn_deprecated, VD->getName());
121
Steve Naroffcf871f52007-08-28 18:45:29 +0000122 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +0000123 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000124 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000125 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000126 }
Chris Lattner59a25942008-03-31 00:36:02 +0000127
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000128 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +0000129 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000130 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanianb31a2f22007-12-05 18:16:33 +0000131 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +0000132 if (isa<NamespaceDecl>(D))
133 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Steve Narofff1e53692007-03-23 22:27:02 +0000134
135 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +0000136 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +0000137}
Chris Lattnere168f762006-11-10 05:29:30 +0000138
Steve Naroff83895f72007-09-16 03:34:24 +0000139Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson625bfc82007-07-21 05:21:51 +0000140 tok::TokenKind Kind) {
141 PreDefinedExpr::IdentType IT;
142
Chris Lattnere168f762006-11-10 05:29:30 +0000143 switch (Kind) {
Chris Lattner317e6ba2008-01-12 18:39:25 +0000144 default: assert(0 && "Unknown simple primary expr!");
145 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
146 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
147 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattnere168f762006-11-10 05:29:30 +0000148 }
Chris Lattner317e6ba2008-01-12 18:39:25 +0000149
150 // Verify that this is in a function context.
Chris Lattner0a8c2322008-01-12 19:32:28 +0000151 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattner317e6ba2008-01-12 18:39:25 +0000152 return Diag(Loc, diag::err_predef_outside_function);
Anders Carlsson625bfc82007-07-21 05:21:51 +0000153
Chris Lattnera81a0272008-01-12 08:14:25 +0000154 // Pre-defined identifiers are of type char[x], where x is the length of the
155 // string.
Chris Lattner0a8c2322008-01-12 19:32:28 +0000156 unsigned Length;
157 if (CurFunctionDecl)
158 Length = CurFunctionDecl->getIdentifier()->getLength();
159 else
Fariborz Jahanianad134b92008-01-17 17:37:26 +0000160 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattner317e6ba2008-01-12 18:39:25 +0000161
Chris Lattner0a8c2322008-01-12 19:32:28 +0000162 llvm::APInt LengthI(32, Length + 1);
Chris Lattner317e6ba2008-01-12 18:39:25 +0000163 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattner0a8c2322008-01-12 19:32:28 +0000164 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattnera81a0272008-01-12 08:14:25 +0000165 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000166}
167
Steve Naroff83895f72007-09-16 03:34:24 +0000168Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000169 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000170 CharBuffer.resize(Tok.getLength());
171 const char *ThisTokBegin = &CharBuffer[0];
172 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
173
174 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
175 Tok.getLocation(), PP);
176 if (Literal.hadError())
177 return ExprResult(true);
Chris Lattneref24b382008-03-01 08:32:21 +0000178
179 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
180
181 return new CharacterLiteral(Literal.getValue(), type, Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000182}
183
Steve Naroff83895f72007-09-16 03:34:24 +0000184Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000185 // fast path for a single digit (which is quite common). A single digit
186 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
187 if (Tok.getLength() == 1) {
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000188 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000189
Chris Lattner37e05872008-03-05 18:54:05 +0000190 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000191 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner23b7eb62007-06-15 23:05:46 +0000192 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000193 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000194 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000195 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000196 IntegerBuffer.resize(Tok.getLength());
197 const char *ThisTokBegin = &IntegerBuffer[0];
198
Chris Lattner67ca9252007-05-21 01:08:44 +0000199 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000200 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000201 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000202 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000203 if (Literal.hadError)
204 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000205
Chris Lattner1c20a172007-08-26 03:42:43 +0000206 Expr *Res;
207
208 if (Literal.isFloatingLiteral()) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000209 QualType Ty;
210 const llvm::fltSemantics *Format;
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000211
212 if (Literal.isFloat) {
213 Ty = Context.FloatTy;
Chris Lattner7570e9c2008-03-08 08:52:55 +0000214 Format = Context.Target.getFloatFormat();
215 } else if (!Literal.isLong) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000216 Ty = Context.DoubleTy;
Chris Lattner7570e9c2008-03-08 08:52:55 +0000217 Format = Context.Target.getDoubleFormat();
218 } else {
219 Ty = Context.LongDoubleTy;
220 Format = Context.Target.getLongDoubleFormat();
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000221 }
222
Ted Kremenek3a2c9502007-11-29 00:56:49 +0000223 // isExact will be set by GetFloatValue().
224 bool isExact = false;
225
226 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
227 Ty, Tok.getLocation());
228
Chris Lattner1c20a172007-08-26 03:42:43 +0000229 } else if (!Literal.isIntegerLiteral()) {
230 return ExprResult(true);
231 } else {
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000232 QualType Ty;
Chris Lattner67ca9252007-05-21 01:08:44 +0000233
Neil Boothac582c52007-08-29 22:00:19 +0000234 // long long is a C99 feature.
235 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000236 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000237 Diag(Tok.getLocation(), diag::ext_longlong);
238
Chris Lattner67ca9252007-05-21 01:08:44 +0000239 // Get the value in the widest-possible width.
Chris Lattner37e05872008-03-05 18:54:05 +0000240 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000241
242 if (Literal.GetIntegerValue(ResultVal)) {
243 // If this value didn't fit into uintmax_t, warn and force to ull.
244 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000245 Ty = Context.UnsignedLongLongTy;
246 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner37e05872008-03-05 18:54:05 +0000247 "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000248 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000249 // If this value fits into a ULL, try to figure out what else it fits into
250 // according to the rules of C99 6.4.4.1p5.
251
252 // Octal, Hexadecimal, and integers with a U suffix are allowed to
253 // be an unsigned int.
254 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
255
256 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner55258cf2008-05-09 05:59:00 +0000257 unsigned Width = 0;
Chris Lattner7b939cf2007-08-23 21:58:08 +0000258 if (!Literal.isLong && !Literal.isLongLong) {
259 // Are int/unsigned possibilities?
Chris Lattner55258cf2008-05-09 05:59:00 +0000260 unsigned IntSize = Context.Target.getIntWidth();
261
Chris Lattner67ca9252007-05-21 01:08:44 +0000262 // Does it fit in a unsigned int?
263 if (ResultVal.isIntN(IntSize)) {
264 // Does it fit in a signed int?
265 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000266 Ty = Context.IntTy;
Chris Lattner67ca9252007-05-21 01:08:44 +0000267 else if (AllowUnsigned)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000268 Ty = Context.UnsignedIntTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000269 Width = IntSize;
Chris Lattner67ca9252007-05-21 01:08:44 +0000270 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000271 }
272
273 // Are long/unsigned long possibilities?
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000274 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattner55258cf2008-05-09 05:59:00 +0000275 unsigned LongSize = Context.Target.getLongWidth();
Chris Lattner67ca9252007-05-21 01:08:44 +0000276
277 // Does it fit in a unsigned long?
278 if (ResultVal.isIntN(LongSize)) {
279 // Does it fit in a signed long?
280 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000281 Ty = Context.LongTy;
Chris Lattner67ca9252007-05-21 01:08:44 +0000282 else if (AllowUnsigned)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000283 Ty = Context.UnsignedLongTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000284 Width = LongSize;
Chris Lattner67ca9252007-05-21 01:08:44 +0000285 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000286 }
287
288 // Finally, check long long if needed.
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000289 if (Ty.isNull()) {
Chris Lattner55258cf2008-05-09 05:59:00 +0000290 unsigned LongLongSize = Context.Target.getLongLongWidth();
Chris Lattner67ca9252007-05-21 01:08:44 +0000291
292 // Does it fit in a unsigned long long?
293 if (ResultVal.isIntN(LongLongSize)) {
294 // Does it fit in a signed long long?
295 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000296 Ty = Context.LongLongTy;
Chris Lattner67ca9252007-05-21 01:08:44 +0000297 else if (AllowUnsigned)
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000298 Ty = Context.UnsignedLongLongTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000299 Width = LongLongSize;
Chris Lattner67ca9252007-05-21 01:08:44 +0000300 }
301 }
302
303 // If we still couldn't decide a type, we probably have something that
304 // does not fit in a signed long long, but has no U suffix.
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000305 if (Ty.isNull()) {
Chris Lattner67ca9252007-05-21 01:08:44 +0000306 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000307 Ty = Context.UnsignedLongLongTy;
Chris Lattner55258cf2008-05-09 05:59:00 +0000308 Width = Context.Target.getLongLongWidth();
Chris Lattner67ca9252007-05-21 01:08:44 +0000309 }
Chris Lattner55258cf2008-05-09 05:59:00 +0000310
311 if (ResultVal.getBitWidth() != Width)
312 ResultVal.trunc(Width);
Steve Naroff09ef4742007-03-09 23:16:33 +0000313 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000314
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000315 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000316 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000317
318 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
319 if (Literal.isImaginary)
320 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
321
322 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000323}
324
Steve Naroff83895f72007-09-16 03:34:24 +0000325Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattnere168f762006-11-10 05:29:30 +0000326 ExprTy *Val) {
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000327 Expr *E = (Expr *)Val;
328 assert((E != 0) && "ActOnParenExpr() missing expr");
329 return new ParenExpr(L, R, E);
Chris Lattnere168f762006-11-10 05:29:30 +0000330}
331
Steve Naroff71b59a92007-06-04 22:22:31 +0000332/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000333/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000334QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
335 SourceLocation OpLoc, bool isSizeof) {
336 // C99 6.5.3.4p1:
337 if (isa<FunctionType>(exprType) && isSizeof)
338 // alignof(function) is allowed.
339 Diag(OpLoc, diag::ext_sizeof_function_type);
340 else if (exprType->isVoidType())
341 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
342 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000343 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000344 diag::err_alignof_incomplete_type,
345 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000346 return QualType(); // error
347 }
348 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
349 return Context.getSizeType();
350}
351
Chris Lattnere168f762006-11-10 05:29:30 +0000352Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000353ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000354 SourceLocation LPLoc, TypeTy *Ty,
355 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000356 // If error parsing type, ignore.
357 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000358
359 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000360 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000361
Steve Naroff043d45d2007-05-15 02:32:35 +0000362 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
363
364 if (resultType.isNull())
365 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000366 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000367}
368
Chris Lattner74ed76b2007-08-24 21:41:10 +0000369QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000370 DefaultFunctionArrayConversion(V);
371
Chris Lattnere267f5d2007-08-26 05:39:26 +0000372 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000373 if (const ComplexType *CT = V->getType()->getAsComplexType())
374 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000375
376 // Otherwise they pass through real integer and floating point types here.
377 if (V->getType()->isArithmeticType())
378 return V->getType();
379
380 // Reject anything else.
381 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
382 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000383}
384
385
Chris Lattnere168f762006-11-10 05:29:30 +0000386
Steve Naroff83895f72007-09-16 03:34:24 +0000387Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000388 tok::TokenKind Kind,
389 ExprTy *Input) {
390 UnaryOperator::Opcode Opc;
391 switch (Kind) {
392 default: assert(0 && "Unknown unary op!");
393 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
394 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
395 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000396 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000397 if (result.isNull())
398 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000399 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000400}
401
402Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000403ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000404 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000405 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000406
407 // Perform default conversions.
408 DefaultFunctionArrayConversion(LHSExp);
409 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000410
Chris Lattner36d572b2007-07-16 00:14:47 +0000411 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000412
Steve Naroffc1aadb12007-03-28 21:49:40 +0000413 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000414 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000415 // in the subscript position. As a result, we need to derive the array base
416 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000417 Expr *BaseExpr, *IndexExpr;
418 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000419 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000420 BaseExpr = LHSExp;
421 IndexExpr = RHSExp;
422 // FIXME: need to deal with const...
423 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000424 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000425 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000426 BaseExpr = RHSExp;
427 IndexExpr = LHSExp;
428 // FIXME: need to deal with const...
429 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000430 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
431 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000432 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000433
434 // Component access limited to variables (reject vec4.rg[1]).
Nate Begemanf322eab2008-05-09 06:41:27 +0000435 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
436 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemance4d7fc2008-04-18 23:10:10 +0000437 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff01047312007-08-03 22:40:33 +0000438 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000439 // FIXME: need to deal with const...
440 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000441 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000442 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
443 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000444 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000445 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000446 if (!IndexExpr->getType()->isIntegerType())
447 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
448 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000449
Chris Lattner36d572b2007-07-16 00:14:47 +0000450 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
451 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattnerb3a176d2008-04-02 06:59:01 +0000452 // void (*)(int)) and pointers to incomplete types. Functions are not
453 // objects in C99.
Chris Lattner36d572b2007-07-16 00:14:47 +0000454 if (!ResultType->isObjectType())
455 return Diag(BaseExpr->getLocStart(),
456 diag::err_typecheck_subscript_not_object,
457 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
458
459 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000460}
461
Steve Narofff8fd09e2007-07-27 22:15:19 +0000462QualType Sema::
Nate Begemance4d7fc2008-04-18 23:10:10 +0000463CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000464 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemance4d7fc2008-04-18 23:10:10 +0000465 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begemanf322eab2008-05-09 06:41:27 +0000466
467 // This flag determines whether or not the component is to be treated as a
468 // special name, or a regular GLSL-style component access.
469 bool SpecialComponent = false;
Steve Narofff8fd09e2007-07-27 22:15:19 +0000470
471 // The vector accessor can't exceed the number of elements.
472 const char *compStr = CompName.getName();
473 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +0000474 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000475 baseType.getAsString(), SourceRange(CompLoc));
476 return QualType();
477 }
Nate Begemanf322eab2008-05-09 06:41:27 +0000478
479 // Check that we've found one of the special components, or that the component
480 // names must come from the same set.
481 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
482 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
483 SpecialComponent = true;
484 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner7e152db2007-08-02 22:33:49 +0000485 do
486 compStr++;
487 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
488 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
489 do
490 compStr++;
491 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
492 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
493 do
494 compStr++;
495 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
496 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000497
Nate Begemanf322eab2008-05-09 06:41:27 +0000498 if (!SpecialComponent && *compStr) {
Steve Narofff8fd09e2007-07-27 22:15:19 +0000499 // We didn't get to the end of the string. This means the component names
500 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000501 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000502 std::string(compStr,compStr+1), SourceRange(CompLoc));
503 return QualType();
504 }
505 // Each component accessor can't exceed the vector type.
506 compStr = CompName.getName();
507 while (*compStr) {
508 if (vecType->isAccessorWithinNumElements(*compStr))
509 compStr++;
510 else
511 break;
512 }
Nate Begemanf322eab2008-05-09 06:41:27 +0000513 if (!SpecialComponent && *compStr) {
Steve Narofff8fd09e2007-07-27 22:15:19 +0000514 // We didn't get to the end of the string. This means a component accessor
515 // exceeds the number of elements in the vector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000516 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Narofff8fd09e2007-07-27 22:15:19 +0000517 baseType.getAsString(), SourceRange(CompLoc));
518 return QualType();
519 }
Nate Begemanf322eab2008-05-09 06:41:27 +0000520
521 // If we have a special component name, verify that the current vector length
522 // is an even number, since all special component names return exactly half
523 // the elements.
524 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
525 return QualType();
526 }
527
Steve Narofff8fd09e2007-07-27 22:15:19 +0000528 // The component accessor looks fine - now we need to compute the actual type.
529 // The vector type is implied by the component accessor. For example,
530 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begemanf322eab2008-05-09 06:41:27 +0000531 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
532 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
533 : strlen(CompName.getName());
Steve Narofff8fd09e2007-07-27 22:15:19 +0000534 if (CompSize == 1)
535 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000536
Nate Begemance4d7fc2008-04-18 23:10:10 +0000537 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000538 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemance4d7fc2008-04-18 23:10:10 +0000539 // diagostics look bad. We want extended vector types to appear built-in.
540 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
541 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
542 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000543 }
544 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000545}
546
Chris Lattnere168f762006-11-10 05:29:30 +0000547Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000548ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000549 tok::TokenKind OpKind, SourceLocation MemberLoc,
550 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000551 Expr *BaseExpr = static_cast<Expr *>(Base);
552 assert(BaseExpr && "no record expression");
Steve Naroffeaaae462007-12-16 21:42:28 +0000553
554 // Perform default conversions.
555 DefaultFunctionArrayConversion(BaseExpr);
Steve Naroffca8f7122007-04-01 01:41:35 +0000556
Steve Naroff185616f2007-07-26 03:11:44 +0000557 QualType BaseType = BaseExpr->getType();
558 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000559
Steve Narofff1e53692007-03-23 22:27:02 +0000560 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000561 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000562 BaseType = PT->getPointeeType();
563 else
564 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
565 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000566 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000567 // The base type is either a record or an ExtVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000568 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000569 RecordDecl *RDecl = RTy->getDecl();
570 if (RTy->isIncompleteType())
571 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
572 BaseExpr->getSourceRange());
573 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000574 FieldDecl *MemberDecl = RDecl->getMember(&Member);
575 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000576 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
577 SourceRange(MemberLoc));
Eli Friedman1242fff2008-02-06 22:48:16 +0000578
579 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedman69d56cf2008-02-07 05:24:51 +0000580 // FIXME: Handle address space modifiers
Eli Friedman1242fff2008-02-06 22:48:16 +0000581 QualType MemberType = MemberDecl->getType();
582 unsigned combinedQualifiers =
Chris Lattner445fcab2008-02-20 20:55:12 +0000583 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman1242fff2008-02-06 22:48:16 +0000584 MemberType = MemberType.getQualifiedType(combinedQualifiers);
585
586 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
587 MemberLoc, MemberType);
Nate Begemance4d7fc2008-04-18 23:10:10 +0000588 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000589 // Component access limited to variables (reject vec4.rg.g).
Nate Begemanf322eab2008-05-09 06:41:27 +0000590 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
591 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begemance4d7fc2008-04-18 23:10:10 +0000592 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff01047312007-08-03 22:40:33 +0000593 SourceRange(MemberLoc));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000594 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Narofff8fd09e2007-07-27 22:15:19 +0000595 if (ret.isNull())
596 return true;
Nate Begemance4d7fc2008-04-18 23:10:10 +0000597 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000598 } else if (BaseType->isObjCInterfaceType()) {
599 ObjCInterfaceDecl *IFace;
600 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
601 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000602 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000603 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
604 ObjCInterfaceDecl *clsDeclared;
605 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000606 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
607 OpKind==tok::arrow);
608 }
609 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
610 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000611}
612
Steve Naroff83895f72007-09-16 03:34:24 +0000613/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattnere168f762006-11-10 05:29:30 +0000614/// This provides the location of the left/right parens and a list of comma
615/// locations.
616Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000617ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner08464942007-12-28 05:29:59 +0000618 ExprTy **args, unsigned NumArgs,
Chris Lattnere168f762006-11-10 05:29:30 +0000619 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000620 Expr *Fn = static_cast<Expr *>(fn);
621 Expr **Args = reinterpret_cast<Expr**>(args);
622 assert(Fn && "no function call expression");
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000623 FunctionDecl *FDecl = NULL;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000624
625 // Promote the function operand.
626 UsualUnaryConversions(Fn);
627
628 // If we're directly calling a function, get the declaration for
629 // that function.
630 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
631 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
632 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
633
Chris Lattner08464942007-12-28 05:29:59 +0000634 // Make the call expr early, before semantic checks. This guarantees cleanup
635 // of arguments and function on error.
Chris Lattner58258242008-04-10 02:22:51 +0000636 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner08464942007-12-28 05:29:59 +0000637 Context.BoolTy, RParenLoc));
638
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000639 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
640 // type pointer to function".
Chris Lattner08464942007-12-28 05:29:59 +0000641 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000642 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000643 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
644 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000645 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
646 if (FuncT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000647 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
648 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000649
650 // We know the result type of the call, set it.
651 TheCall->setType(FuncT->getResultType());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000652
Chris Lattner08464942007-12-28 05:29:59 +0000653 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000654 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
655 // assignment, to the types of the corresponding parameter, ...
Chris Lattner08464942007-12-28 05:29:59 +0000656 unsigned NumArgsInProto = Proto->getNumArgs();
657 unsigned NumArgsToCheck = NumArgs;
Steve Naroff17f76e02007-05-03 21:03:48 +0000658
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000659 // If too few arguments are available (and we don't have default
660 // arguments for the remaining parameters), don't make the call.
661 if (NumArgs < NumArgsInProto) {
Chris Lattner58258242008-04-10 02:22:51 +0000662 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000663 // Use default arguments for missing arguments
664 NumArgsToCheck = NumArgsInProto;
Chris Lattner58258242008-04-10 02:22:51 +0000665 TheCall->setNumArgs(NumArgsInProto);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000666 } else
667 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
668 Fn->getSourceRange());
669 }
670
Chris Lattner08464942007-12-28 05:29:59 +0000671 // If too many are passed and not variadic, error on the extras and drop
672 // them.
673 if (NumArgs > NumArgsInProto) {
674 if (!Proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000675 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000676 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000677 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner08464942007-12-28 05:29:59 +0000678 Args[NumArgs-1]->getLocEnd()));
679 // This deletes the extra arguments.
680 TheCall->setNumArgs(NumArgsInProto);
Steve Naroff8563f652007-05-28 19:25:56 +0000681 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000682 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000683 }
Chris Lattner08464942007-12-28 05:29:59 +0000684
Steve Naroff17f76e02007-05-03 21:03:48 +0000685 // Continue to check argument types (even if we have too few/many args).
Chris Lattner08464942007-12-28 05:29:59 +0000686 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner9bad62c2008-01-04 18:04:52 +0000687 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000688
689 Expr *Arg;
690 if (i < NumArgs)
691 Arg = Args[i];
692 else
693 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner9bad62c2008-01-04 18:04:52 +0000694 QualType ArgType = Arg->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000695
Chris Lattner08464942007-12-28 05:29:59 +0000696 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner9bad62c2008-01-04 18:04:52 +0000697 AssignConvertType ConvTy =
698 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner08464942007-12-28 05:29:59 +0000699 TheCall->setArg(i, Arg);
700
Chris Lattner9bad62c2008-01-04 18:04:52 +0000701 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
702 ArgType, Arg, "passing"))
703 return true;
Steve Naroff17f76e02007-05-03 21:03:48 +0000704 }
Chris Lattner08464942007-12-28 05:29:59 +0000705
706 // If this is a variadic call, handle args passed through "...".
707 if (Proto->isVariadic()) {
Steve Naroff0b661582007-08-28 23:30:39 +0000708 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner08464942007-12-28 05:29:59 +0000709 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
710 Expr *Arg = Args[i];
711 DefaultArgumentPromotion(Arg);
712 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000713 }
Steve Naroff0b661582007-08-28 23:30:39 +0000714 }
Chris Lattner08464942007-12-28 05:29:59 +0000715 } else {
716 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
717
Steve Naroff0b661582007-08-28 23:30:39 +0000718 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner08464942007-12-28 05:29:59 +0000719 for (unsigned i = 0; i != NumArgs; i++) {
720 Expr *Arg = Args[i];
721 DefaultArgumentPromotion(Arg);
722 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000723 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000724 }
Chris Lattner08464942007-12-28 05:29:59 +0000725
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000726 // Do special checking on direct calls to functions.
Eli Friedmana1b4ed82008-05-14 19:38:39 +0000727 if (FDecl)
728 return CheckFunctionCall(FDecl, TheCall.take());
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000729
Chris Lattner08464942007-12-28 05:29:59 +0000730 return TheCall.take();
Chris Lattnere168f762006-11-10 05:29:30 +0000731}
732
733Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000734ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000735 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff83895f72007-09-16 03:34:24 +0000736 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Narofffbd09832007-07-19 01:06:55 +0000737 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000738 // FIXME: put back this assert when initializers are worked out.
Steve Naroff83895f72007-09-16 03:34:24 +0000739 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroff57eb2c52007-07-19 21:32:11 +0000740 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000741
Eli Friedman37a186d2008-05-20 05:22:08 +0000742 if (literalType->isArrayType()) {
743 if (literalType->getAsVariableArrayType())
744 return Diag(LParenLoc,
745 diag::err_variable_object_no_init,
746 SourceRange(LParenLoc,
747 literalExpr->getSourceRange().getEnd()));
748 } else if (literalType->isIncompleteType()) {
749 return Diag(LParenLoc,
750 diag::err_typecheck_decl_incomplete_type,
751 literalType.getAsString(),
752 SourceRange(LParenLoc,
753 literalExpr->getSourceRange().getEnd()));
754 }
755
Steve Naroff98f72032008-01-10 22:15:12 +0000756 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff08ddb8c2008-01-09 20:58:06 +0000757 return true;
Steve Naroffd32419d2008-01-14 18:19:28 +0000758
759 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
760 if (isFileScope) { // 6.5.2.5p3
Steve Naroff98f72032008-01-10 22:15:12 +0000761 if (CheckForConstantInitializer(literalExpr, literalType))
762 return true;
763 }
Steve Naroffd32419d2008-01-14 18:19:28 +0000764 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Steve Narofffbd09832007-07-19 01:06:55 +0000765}
766
767Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000768ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson4692db02007-08-31 04:56:16 +0000769 SourceLocation RBraceLoc) {
Steve Naroff2fea1392007-09-02 02:04:30 +0000770 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson4692db02007-08-31 04:56:16 +0000771
Steve Naroff30d242c2007-09-15 18:49:24 +0000772 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000773 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson4692db02007-08-31 04:56:16 +0000774
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000775 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
776 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
777 return E;
Steve Narofffbd09832007-07-19 01:06:55 +0000778}
779
Chris Lattner6c9ffe92007-12-20 00:44:32 +0000780bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonde71adf2007-11-27 05:51:55 +0000781 assert(VectorTy->isVectorType() && "Not a vector type!");
782
783 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner37e05872008-03-05 18:54:05 +0000784 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonde71adf2007-11-27 05:51:55 +0000785 return Diag(R.getBegin(),
786 Ty->isVectorType() ?
787 diag::err_invalid_conversion_between_vectors :
788 diag::err_invalid_conversion_between_vector_and_integer,
789 VectorTy.getAsString().c_str(),
790 Ty.getAsString().c_str(), R);
791 } else
792 return Diag(R.getBegin(),
793 diag::err_invalid_conversion_between_vector_and_scalar,
794 VectorTy.getAsString().c_str(),
795 Ty.getAsString().c_str(), R);
796
797 return false;
798}
799
Steve Narofffbd09832007-07-19 01:06:55 +0000800Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000801ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattnere168f762006-11-10 05:29:30 +0000802 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff83895f72007-09-16 03:34:24 +0000803 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000804
805 Expr *castExpr = static_cast<Expr*>(Op);
806 QualType castType = QualType::getFromOpaquePtr(Ty);
807
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000808 UsualUnaryConversions(castExpr);
809
Chris Lattnerbd270732007-07-18 16:00:06 +0000810 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
811 // type needs to be scalar.
Chris Lattner3bc4d202007-10-29 04:26:44 +0000812 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroffd9d581f2008-01-24 22:55:05 +0000813 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000814 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
815 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Naroffd9d581f2008-01-24 22:55:05 +0000816 if (!castExpr->getType()->isScalarType() &&
817 !castExpr->getType()->isVectorType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000818 return Diag(castExpr->getLocStart(),
819 diag::err_typecheck_expect_scalar_operand,
820 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonde71adf2007-11-27 05:51:55 +0000821
822 if (castExpr->getType()->isVectorType()) {
823 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
824 castExpr->getType(), castType))
825 return true;
826 } else if (castType->isVectorType()) {
827 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
828 castType, castExpr->getType()))
829 return true;
Chris Lattner3bc4d202007-10-29 04:26:44 +0000830 }
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000831 }
832 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000833}
834
Chris Lattner2ab40a62007-11-26 01:40:58 +0000835/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
836/// In that case, lex = cond.
Steve Narofff8a28c52007-05-15 20:29:32 +0000837inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000838 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000839 UsualUnaryConversions(cond);
840 UsualUnaryConversions(lex);
841 UsualUnaryConversions(rex);
842 QualType condT = cond->getType();
843 QualType lexT = lex->getType();
844 QualType rexT = rex->getType();
845
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000846 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000847 if (!condT->isScalarType()) { // C99 6.5.15p2
848 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
849 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000850 return QualType();
851 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000852
853 // Now check the two expressions.
854
855 // If both operands have arithmetic type, do the usual arithmetic conversions
856 // to find a common type: C99 6.5.15p3,5.
857 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffdbd9e892007-07-17 00:58:39 +0000858 UsualArithmeticConversions(lex, rex);
859 return lex->getType();
860 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000861
862 // If both operands are the same structure or union type, the result is that
863 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000864 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattnere2949f42008-01-06 22:42:25 +0000865 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner2ab40a62007-11-26 01:40:58 +0000866 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattnere2949f42008-01-06 22:42:25 +0000867 // "If both the operands have structure or union type, the result has
868 // that type." This implies that CV qualifiers are dropped.
869 return lexT.getUnqualifiedType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000870 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000871
872 // C99 6.5.15p5: "If both operands have void type, the result has void type."
Steve Naroffbf1516c2008-05-12 21:44:38 +0000873 // The following || allows only one side to be void (a GCC-ism).
874 if (lexT->isVoidType() || rexT->isVoidType()) {
875 if (!lexT->isVoidType())
876 Diag(rex->getLocStart(), diag::ext_typecheck_cond_one_void,
877 rex->getSourceRange());
878 if (!rexT->isVoidType())
879 Diag(lex->getLocStart(), diag::ext_typecheck_cond_one_void,
880 lex->getSourceRange());
Chris Lattnere2949f42008-01-06 22:42:25 +0000881 return lexT.getUnqualifiedType();
Steve Naroffbf1516c2008-05-12 21:44:38 +0000882 }
Steve Naroff039ad3c2008-01-08 01:11:38 +0000883 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
884 // the type of the other operand."
885 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +0000886 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff039ad3c2008-01-08 01:11:38 +0000887 return lexT;
888 }
889 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +0000890 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff039ad3c2008-01-08 01:11:38 +0000891 return rexT;
892 }
Chris Lattner4d3b0f52008-01-06 22:50:31 +0000893 // Handle the case where both operands are pointers before we handle null
894 // pointer constants in case both operands are null pointer constants.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000895 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
896 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
897 // get the "pointed to" types
898 QualType lhptee = LHSPT->getPointeeType();
899 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000900
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000901 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
902 if (lhptee->isVoidType() &&
Chris Lattnerb3a176d2008-04-02 06:59:01 +0000903 rhptee->isIncompleteOrObjectType()) {
Chris Lattner445fcab2008-02-20 20:55:12 +0000904 // Figure out necessary qualifiers (C99 6.5.15p6)
905 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedman15888c22008-02-10 22:59:36 +0000906 QualType destType = Context.getPointerType(destPointee);
907 ImpCastExprToType(lex, destType); // add qualifiers if necessary
908 ImpCastExprToType(rex, destType); // promote to void*
909 return destType;
910 }
Chris Lattnerb3a176d2008-04-02 06:59:01 +0000911 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner445fcab2008-02-20 20:55:12 +0000912 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedman15888c22008-02-10 22:59:36 +0000913 QualType destType = Context.getPointerType(destPointee);
914 ImpCastExprToType(lex, destType); // add qualifiers if necessary
915 ImpCastExprToType(rex, destType); // promote to void*
916 return destType;
917 }
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000918
Steve Naroff32e44c02007-10-15 20:41:53 +0000919 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
920 rhptee.getUnqualifiedType())) {
Steve Naroff0dffa442008-02-01 22:44:48 +0000921 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000922 lexT.getAsString(), rexT.getAsString(),
923 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman1bc0fed2008-01-30 17:02:03 +0000924 // In this situation, we assume void* type. No especially good
925 // reason, but this is what gcc does, and we do have to pick
926 // to get a consistent AST.
927 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
928 ImpCastExprToType(lex, voidPtrTy);
929 ImpCastExprToType(rex, voidPtrTy);
930 return voidPtrTy;
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000931 }
932 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000933 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
934 // differently qualified versions of compatible types, the result type is
935 // a pointer to an appropriately qualified version of the *composite*
936 // type.
Eli Friedman928ab4d2008-05-16 20:37:07 +0000937 // FIXME: Need to calculate the composite type.
Eli Friedman15888c22008-02-10 22:59:36 +0000938 // FIXME: Need to add qualifiers
Eli Friedman928ab4d2008-05-16 20:37:07 +0000939 QualType compositeType = lexT;
940 ImpCastExprToType(lex, compositeType);
941 ImpCastExprToType(rex, compositeType);
942 return compositeType;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000943 }
944 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000945
Chris Lattnere2949f42008-01-06 22:42:25 +0000946 // Otherwise, the operands are not compatible.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000947 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000948 lexT.getAsString(), rexT.getAsString(),
949 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000950 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000951}
952
Steve Naroff83895f72007-09-16 03:34:24 +0000953/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattnere168f762006-11-10 05:29:30 +0000954/// in the case of a the GNU conditional expr extension.
Steve Naroff83895f72007-09-16 03:34:24 +0000955Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000956 SourceLocation ColonLoc,
957 ExprTy *Cond, ExprTy *LHS,
958 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000959 Expr *CondExpr = (Expr *) Cond;
960 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000961
962 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
963 // was the condition.
964 bool isLHSNull = LHSExpr == 0;
965 if (isLHSNull)
966 LHSExpr = CondExpr;
967
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000968 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
969 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000970 if (result.isNull())
971 return true;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000972 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
973 RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000974}
975
Steve Naroff0b661582007-08-28 23:30:39 +0000976/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroff31daee12008-01-29 02:42:22 +0000977/// do not have a prototype. Arguments that have type float are promoted to
978/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner08464942007-12-28 05:29:59 +0000979void Sema::DefaultArgumentPromotion(Expr *&Expr) {
980 QualType Ty = Expr->getType();
981 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroff0b661582007-08-28 23:30:39 +0000982
Chris Lattner08464942007-12-28 05:29:59 +0000983 if (Ty == Context.FloatTy)
Chris Lattnera65e1f32008-01-16 19:17:22 +0000984 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroff31daee12008-01-29 02:42:22 +0000985 else
986 UsualUnaryConversions(Expr);
Steve Naroff0b661582007-08-28 23:30:39 +0000987}
988
Steve Naroff81569d22007-07-15 02:02:06 +0000989/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000990void Sema::DefaultFunctionArrayConversion(Expr *&E) {
991 QualType Ty = E->getType();
992 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000993
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000994 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattner182f6602008-04-02 17:45:06 +0000995 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000996 Ty = E->getType();
Bill Wendling354fb262007-07-17 04:16:47 +0000997 }
Chris Lattner24d5bfe2008-04-02 04:24:33 +0000998 if (Ty->isFunctionType())
999 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattnera21ad802008-04-02 05:18:44 +00001000 else if (Ty->isArrayType())
1001 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Steve Naroff1926c832007-04-24 00:23:05 +00001002}
1003
Nate Begeman1e36a852008-01-17 17:46:27 +00001004/// UsualUnaryConversions - Performs various conversions that are common to most
Steve Naroff71b59a92007-06-04 22:22:31 +00001005/// operators (C99 6.3). The conversions of array and function types are
1006/// sometimes surpressed. For example, the array->pointer conversion doesn't
1007/// apply if the array is an argument to the sizeof or address (&) operators.
1008/// In these instances, this routine should *not* be called.
Chris Lattner08464942007-12-28 05:29:59 +00001009Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
1010 QualType Ty = Expr->getType();
1011 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Steve Naroff71b59a92007-06-04 22:22:31 +00001012
Chris Lattner08464942007-12-28 05:29:59 +00001013 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattner182f6602008-04-02 17:45:06 +00001014 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner08464942007-12-28 05:29:59 +00001015 Ty = Expr->getType();
Bill Wendling354fb262007-07-17 04:16:47 +00001016 }
Chris Lattner08464942007-12-28 05:29:59 +00001017 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnera65e1f32008-01-16 19:17:22 +00001018 ImpCastExprToType(Expr, Context.IntTy);
Steve Naroff31090012007-07-16 21:54:35 +00001019 else
Chris Lattner08464942007-12-28 05:29:59 +00001020 DefaultFunctionArrayConversion(Expr);
1021
1022 return Expr;
Steve Naroff71b59a92007-06-04 22:22:31 +00001023}
1024
Chris Lattnerf17bd422007-08-30 17:45:32 +00001025/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +00001026/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
1027/// routine returns the first non-arithmetic type found. The client is
1028/// responsible for emitting appropriate error diagnostics.
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001029/// FIXME: verify the conversion rules for "complex int" are consistent with
1030/// GCC.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001031QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
1032 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +00001033 if (!isCompAssign) {
1034 UsualUnaryConversions(lhsExpr);
1035 UsualUnaryConversions(rhsExpr);
1036 }
Steve Naroff1bb21df2007-10-18 18:55:53 +00001037 // For conversion purposes, we ignore any qualifiers.
1038 // For example, "const float" and "float" are equivalent.
Steve Naroff257b4a22007-11-10 19:45:54 +00001039 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
1040 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff1926c832007-04-24 00:23:05 +00001041
Chris Lattner0c46c5d2007-06-02 23:53:17 +00001042 // If both types are identical, no conversion is needed.
Steve Naroff1bb21df2007-10-18 18:55:53 +00001043 if (lhs == rhs)
1044 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +00001045
1046 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1047 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001048 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001049 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +00001050
Chris Lattner0c46c5d2007-06-02 23:53:17 +00001051 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +00001052
1053 // Handle complex types first (C99 6.3.1.8p1).
1054 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff6fcfd052008-01-15 19:36:10 +00001055 // if we have an integer operand, the result is the complex type.
Steve Naroffabefc392008-01-15 22:21:49 +00001056 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001057 // convert the rhs to the lhs complex type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001058 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001059 return lhs;
Steve Naroff6fcfd052008-01-15 19:36:10 +00001060 }
Steve Naroffabefc392008-01-15 22:21:49 +00001061 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001062 // convert the lhs to the rhs complex type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001063 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001064 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001065 }
Steve Naroff9091ef72007-08-27 01:27:54 +00001066 // This handles complex/complex, complex/float, or float/complex.
1067 // When both operands are complex, the shorter operand is converted to the
1068 // type of the longer, and that is the type of the result. This corresponds
1069 // to what is done when combining two real floating-point operands.
1070 // The fun begins when size promotion occur across type domains.
1071 // From H&S 6.3.4: When one operand is complex and the other is a real
1072 // floating-point type, the less precise type is converted, within it's
1073 // real or complex domain, to the precision of the other type. For example,
1074 // when combining a "long double" with a "double _Complex", the
1075 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnerb90739d2008-04-06 23:38:49 +00001076 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff7af82d42007-08-27 15:30:22 +00001077
1078 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +00001079 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1080 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001081 ImpCastExprToType(rhsExpr, rhs);
Steve Naroffe31313d2007-08-27 21:32:55 +00001082 } else if (result < 0) { // The right side is bigger, convert lhs.
1083 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1084 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001085 ImpCastExprToType(lhsExpr, lhs);
Steve Naroffe31313d2007-08-27 21:32:55 +00001086 }
1087 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1088 // domains match. This is a requirement for our implementation, C99
1089 // does not require this promotion.
1090 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1091 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +00001092 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001093 ImpCastExprToType(lhsExpr, rhs);
Steve Naroffa042db22007-08-27 21:43:43 +00001094 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +00001095 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +00001096 if (!isCompAssign)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001097 ImpCastExprToType(rhsExpr, lhs);
Steve Naroffa042db22007-08-27 21:43:43 +00001098 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +00001099 }
Steve Naroffdbd9e892007-07-17 00:58:39 +00001100 }
Steve Naroffa042db22007-08-27 21:43:43 +00001101 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +00001102 }
Steve Naroffb01bbe32007-04-25 01:22:31 +00001103 // Now handle "real" floating types (i.e. float, double, long double).
1104 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1105 // if we have an integer operand, the result is the real floating type.
Steve Naroffabefc392008-01-15 22:21:49 +00001106 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001107 // convert rhs to the lhs floating point type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001108 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001109 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001110 }
Steve Naroffabefc392008-01-15 22:21:49 +00001111 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001112 // convert lhs to the rhs floating point type.
Chris Lattnera65e1f32008-01-16 19:17:22 +00001113 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001114 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001115 }
Steve Naroff81569d22007-07-15 02:02:06 +00001116 // We have two real floating types, float/complex combos were handled above.
1117 // Convert the smaller operand to the bigger result.
Chris Lattnerb90739d2008-04-06 23:38:49 +00001118 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff7af82d42007-08-27 15:30:22 +00001119
1120 if (result > 0) { // convert the rhs
Chris Lattnera65e1f32008-01-16 19:17:22 +00001121 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001122 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001123 }
Steve Naroff7af82d42007-08-27 15:30:22 +00001124 if (result < 0) { // convert the lhs
Chris Lattnera65e1f32008-01-16 19:17:22 +00001125 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff7af82d42007-08-27 15:30:22 +00001126 return rhs;
1127 }
1128 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +00001129 }
Steve Naroff6fcfd052008-01-15 19:36:10 +00001130 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1131 // Handle GCC complex int extension.
Steve Naroff6fcfd052008-01-15 19:36:10 +00001132 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman1974e532008-02-08 01:19:44 +00001133 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff6fcfd052008-01-15 19:36:10 +00001134
Eli Friedman1974e532008-02-08 01:19:44 +00001135 if (lhsComplexInt && rhsComplexInt) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00001136 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1137 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman113eed52008-02-08 01:24:30 +00001138 // convert the rhs
1139 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1140 return lhs;
Eli Friedman1974e532008-02-08 01:19:44 +00001141 }
1142 if (!isCompAssign)
Eli Friedman113eed52008-02-08 01:24:30 +00001143 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman1974e532008-02-08 01:19:44 +00001144 return rhs;
1145 } else if (lhsComplexInt && rhs->isIntegerType()) {
1146 // convert the rhs to the lhs complex type.
1147 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1148 return lhs;
1149 } else if (rhsComplexInt && lhs->isIntegerType()) {
1150 // convert the lhs to the rhs complex type.
1151 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1152 return rhs;
1153 }
Steve Naroff6fcfd052008-01-15 19:36:10 +00001154 }
Steve Naroff81569d22007-07-15 02:02:06 +00001155 // Finally, we have two differing integer types.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00001156 if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
Chris Lattnera65e1f32008-01-16 19:17:22 +00001157 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001158 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001159 }
Chris Lattnera65e1f32008-01-16 19:17:22 +00001160 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001161 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +00001162}
1163
Steve Naroff3f597292007-05-11 22:18:03 +00001164// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1165// being closely modeled after the C99 spec:-). The odd characteristic of this
1166// routine is it effectively iqnores the qualifiers on the top level pointee.
1167// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1168// FIXME: add a couple examples in this comment.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001169Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001170Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +00001171 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001172
1173 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +00001174 lhptee = lhsType->getAsPointerType()->getPointeeType();
1175 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001176
1177 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +00001178 lhptee = lhptee.getCanonicalType();
1179 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001180
Chris Lattner9bad62c2008-01-04 18:04:52 +00001181 AssignConvertType ConvTy = Compatible;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001182
Steve Naroff3f597292007-05-11 22:18:03 +00001183 // C99 6.5.16.1p1: This following citation is common to constraints
1184 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1185 // qualifiers of the type *pointed to* by the right;
Chris Lattner445fcab2008-02-20 20:55:12 +00001186 // FIXME: Handle ASQualType
1187 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1188 rhptee.getCVRQualifiers())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001189 ConvTy = CompatiblePointerDiscardsQualifiers;
Steve Naroff3f597292007-05-11 22:18:03 +00001190
1191 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1192 // incomplete type and the other is a pointer to a qualified or unqualified
1193 // version of void...
Chris Lattner0a788432008-01-03 22:56:36 +00001194 if (lhptee->isVoidType()) {
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001195 if (rhptee->isIncompleteOrObjectType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001196 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001197
1198 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001199 assert(rhptee->isFunctionType());
1200 return FunctionVoidPointer;
Chris Lattner0a788432008-01-03 22:56:36 +00001201 }
1202
1203 if (rhptee->isVoidType()) {
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001204 if (lhptee->isIncompleteOrObjectType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001205 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001206
1207 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerb3a176d2008-04-02 06:59:01 +00001208 assert(lhptee->isFunctionType());
1209 return FunctionVoidPointer;
Chris Lattner0a788432008-01-03 22:56:36 +00001210 }
1211
Steve Naroff3f597292007-05-11 22:18:03 +00001212 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1213 // unqualified versions of compatible types, ...
Chris Lattner0a788432008-01-03 22:56:36 +00001214 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1215 rhptee.getUnqualifiedType()))
1216 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner9bad62c2008-01-04 18:04:52 +00001217 return ConvTy;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001218}
1219
Steve Naroff98cf3e92007-06-06 18:38:38 +00001220/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +00001221/// has code to accommodate several GCC extensions when type checking
1222/// pointers. Here are some objectionable examples that GCC considers warnings:
1223///
1224/// int a, *pint;
1225/// short *pshort;
1226/// struct foo *pfoo;
1227///
1228/// pint = pshort; // warning: assignment from incompatible pointer type
1229/// a = pint; // warning: assignment makes integer from pointer without a cast
1230/// pint = a; // warning: assignment makes pointer from integer without a cast
1231/// pint = pfoo; // warning: assignment from incompatible pointer type
1232///
1233/// As a result, the code for dealing with pointers is more complex than the
1234/// C99 spec dictates.
Steve Naroff17f76e02007-05-03 21:03:48 +00001235///
Chris Lattner9bad62c2008-01-04 18:04:52 +00001236Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001237Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnera52c2f22008-01-04 23:18:45 +00001238 // Get canonical types. We're not formatting these types, just comparing
1239 // them.
1240 lhsType = lhsType.getCanonicalType();
1241 rhsType = rhsType.getCanonicalType();
1242
1243 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerf5c973d2008-01-07 17:51:46 +00001244 return Compatible; // Common case: fast path an exact match.
Steve Naroff44fd8ff2007-07-24 21:46:40 +00001245
Anders Carlsson24ebce62007-10-12 23:56:29 +00001246 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattner7460fd22008-04-07 06:52:53 +00001247 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlsson24ebce62007-10-12 23:56:29 +00001248 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001249 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001250 }
Chris Lattnera52c2f22008-01-04 23:18:45 +00001251
Chris Lattner2a3569b2008-04-07 05:30:13 +00001252 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1253 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001254 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001255 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001256 }
Chris Lattner881a2122008-01-04 23:32:24 +00001257
Chris Lattnerec646832008-04-07 06:49:41 +00001258 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001259 // For ExtVector, allow vector splats; float -> <n x float>
1260 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattner881a2122008-01-04 23:32:24 +00001261 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1262 return Compatible;
1263 }
1264
1265 // If LHS and RHS are both vectors of integer or both vectors of floating
1266 // point types, and the total vector length is the same, allow the
1267 // conversion. This is a bitcast; no bits are changed but the result type
1268 // is different.
1269 if (getLangOptions().LaxVectorConversions &&
1270 lhsType->isVectorType() && rhsType->isVectorType()) {
1271 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1272 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner37e05872008-03-05 18:54:05 +00001273 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begeman330aaa72007-12-30 02:59:45 +00001274 return Compatible;
1275 }
Chris Lattner881a2122008-01-04 23:32:24 +00001276 }
1277 return Incompatible;
1278 }
1279
1280 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001281 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001282
Chris Lattnerec646832008-04-07 06:49:41 +00001283 if (isa<PointerType>(lhsType)) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001284 if (rhsType->isIntegerType())
Chris Lattner940cfeb2008-01-04 18:22:42 +00001285 return IntToPointer;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001286
Chris Lattnerec646832008-04-07 06:49:41 +00001287 if (isa<PointerType>(rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001288 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001289 return Incompatible;
1290 }
1291
Chris Lattnerec646832008-04-07 06:49:41 +00001292 if (isa<PointerType>(rhsType)) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001293 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Chris Lattnerec646832008-04-07 06:49:41 +00001294 if (lhsType->isIntegerType() && lhsType != Context.BoolTy)
Chris Lattner940cfeb2008-01-04 18:22:42 +00001295 return PointerToInt;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001296
Chris Lattnerec646832008-04-07 06:49:41 +00001297 if (isa<PointerType>(lhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001298 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001299 return Incompatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001300 }
1301
1302 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattnerec646832008-04-07 06:49:41 +00001303 if (Context.typesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001304 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001305 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001306 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001307}
1308
Chris Lattner9bad62c2008-01-04 18:04:52 +00001309Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001310Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001311 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1312 // a null pointer constant.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001313 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian5cc21a72008-01-03 18:46:52 +00001314 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001315 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001316 return Compatible;
1317 }
Chris Lattnere6dcd502007-10-16 02:55:40 +00001318 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001319 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff30d242c2007-09-15 18:49:24 +00001320 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001321 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattnere6dcd502007-10-16 02:55:40 +00001322 //
1323 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1324 // are better understood.
1325 if (!lhsType->isReferenceType())
1326 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001327
Chris Lattner9bad62c2008-01-04 18:04:52 +00001328 Sema::AssignConvertType result =
1329 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001330
1331 // C99 6.5.16.1p2: The value of the right operand is converted to the
1332 // type of the assignment expression.
1333 if (rExpr->getType() != lhsType)
Chris Lattnera65e1f32008-01-16 19:17:22 +00001334 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001335 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001336}
1337
Chris Lattner9bad62c2008-01-04 18:04:52 +00001338Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001339Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1340 return CheckAssignmentConstraints(lhsType, rhsType);
1341}
1342
Chris Lattner5c11c412007-12-12 05:47:28 +00001343QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001344 Diag(loc, diag::err_typecheck_invalid_operands,
1345 lex->getType().getAsString(), rex->getType().getAsString(),
1346 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner5c11c412007-12-12 05:47:28 +00001347 return QualType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001348}
1349
Steve Naroff7a5af782007-07-13 16:58:59 +00001350inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1351 Expr *&rex) {
Nate Begeman002e4bd2008-04-04 01:30:25 +00001352 // For conversion purposes, we ignore any qualifiers.
1353 // For example, "const float" and "float" are equivalent.
1354 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1355 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff84ff4b42007-07-09 21:31:10 +00001356
1357 // make sure the vector types are identical.
Nate Begeman002e4bd2008-04-04 01:30:25 +00001358 if (lhsType == rhsType)
Steve Naroff84ff4b42007-07-09 21:31:10 +00001359 return lhsType;
Nate Begeman330aaa72007-12-30 02:59:45 +00001360
Nate Begemance4d7fc2008-04-18 23:10:10 +00001361 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begeman330aaa72007-12-30 02:59:45 +00001362 // promote the rhs to the vector type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001363 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begeman330aaa72007-12-30 02:59:45 +00001364 if (V->getElementType().getCanonicalType().getTypePtr()
1365 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001366 ImpCastExprToType(rex, lhsType);
Nate Begeman330aaa72007-12-30 02:59:45 +00001367 return lhsType;
1368 }
1369 }
1370
Nate Begemance4d7fc2008-04-18 23:10:10 +00001371 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begeman330aaa72007-12-30 02:59:45 +00001372 // promote the lhs to the vector type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001373 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begeman330aaa72007-12-30 02:59:45 +00001374 if (V->getElementType().getCanonicalType().getTypePtr()
1375 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001376 ImpCastExprToType(lex, rhsType);
Nate Begeman330aaa72007-12-30 02:59:45 +00001377 return rhsType;
1378 }
1379 }
1380
Steve Naroff84ff4b42007-07-09 21:31:10 +00001381 // You cannot convert between vector values of different size.
1382 Diag(loc, diag::err_typecheck_vector_not_convertable,
1383 lex->getType().getAsString(), rex->getType().getAsString(),
1384 lex->getSourceRange(), rex->getSourceRange());
1385 return QualType();
1386}
1387
Steve Naroff218bc2b2007-05-04 21:54:46 +00001388inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001389 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001390{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001391 QualType lhsType = lex->getType(), rhsType = rex->getType();
1392
1393 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001394 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001395
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001396 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001397
Steve Naroffdbd9e892007-07-17 00:58:39 +00001398 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001399 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001400 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001401}
1402
Steve Naroff218bc2b2007-05-04 21:54:46 +00001403inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001404 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001405{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001406 QualType lhsType = lex->getType(), rhsType = rex->getType();
1407
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001408 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001409
Steve Naroffdbd9e892007-07-17 00:58:39 +00001410 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001411 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001412 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001413}
1414
1415inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001416 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001417{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001418 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001419 return CheckVectorOperands(loc, lex, rex);
1420
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001421 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Eli Friedman8e122982008-05-18 18:08:51 +00001422
Steve Naroffe4718892007-04-27 18:30:00 +00001423 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001424 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001425 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001426
Eli Friedman8e122982008-05-18 18:08:51 +00001427 // Put any potential pointer into PExp
1428 Expr* PExp = lex, *IExp = rex;
1429 if (IExp->getType()->isPointerType())
1430 std::swap(PExp, IExp);
1431
1432 if (const PointerType* PTy = PExp->getType()->getAsPointerType()) {
1433 if (IExp->getType()->isIntegerType()) {
1434 // Check for arithmetic on pointers to incomplete types
1435 if (!PTy->getPointeeType()->isObjectType()) {
1436 if (PTy->getPointeeType()->isVoidType()) {
1437 Diag(loc, diag::ext_gnu_void_ptr,
1438 lex->getSourceRange(), rex->getSourceRange());
1439 } else {
1440 Diag(loc, diag::err_typecheck_arithmetic_incomplete_type,
1441 lex->getType().getAsString(), lex->getSourceRange());
1442 return QualType();
1443 }
1444 }
1445 return PExp->getType();
1446 }
1447 }
1448
Chris Lattner5c11c412007-12-12 05:47:28 +00001449 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001450}
1451
Chris Lattner2a3569b2008-04-07 05:30:13 +00001452// C99 6.5.6
1453QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1454 SourceLocation loc, bool isCompAssign) {
Steve Naroff94a5aca2007-07-16 22:23:01 +00001455 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001456 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001457
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001458 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001459
Chris Lattner4d62f422007-12-09 21:53:25 +00001460 // Enforce type constraints: C99 6.5.6p3.
1461
1462 // Handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001463 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001464 return compType;
Chris Lattner4d62f422007-12-09 21:53:25 +00001465
1466 // Either ptr - int or ptr - ptr.
1467 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroffddb1dd82008-01-29 18:58:14 +00001468 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman1974e532008-02-08 01:19:44 +00001469
Chris Lattner4d62f422007-12-09 21:53:25 +00001470 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001471 if (!lpointee->isObjectType()) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001472 // Handle the GNU void* extension.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001473 if (lpointee->isVoidType()) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001474 Diag(loc, diag::ext_gnu_void_ptr,
1475 lex->getSourceRange(), rex->getSourceRange());
1476 } else {
1477 Diag(loc, diag::err_typecheck_sub_ptr_object,
1478 lex->getType().getAsString(), lex->getSourceRange());
1479 return QualType();
1480 }
1481 }
1482
1483 // The result type of a pointer-int computation is the pointer type.
1484 if (rex->getType()->isIntegerType())
1485 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001486
Chris Lattner4d62f422007-12-09 21:53:25 +00001487 // Handle pointer-pointer subtractions.
1488 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman1974e532008-02-08 01:19:44 +00001489 QualType rpointee = RHSPTy->getPointeeType();
1490
Chris Lattner4d62f422007-12-09 21:53:25 +00001491 // RHS must be an object type, unless void (GNU).
Steve Naroffddb1dd82008-01-29 18:58:14 +00001492 if (!rpointee->isObjectType()) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001493 // Handle the GNU void* extension.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001494 if (rpointee->isVoidType()) {
1495 if (!lpointee->isVoidType())
Chris Lattner4d62f422007-12-09 21:53:25 +00001496 Diag(loc, diag::ext_gnu_void_ptr,
1497 lex->getSourceRange(), rex->getSourceRange());
1498 } else {
1499 Diag(loc, diag::err_typecheck_sub_ptr_object,
1500 rex->getType().getAsString(), rex->getSourceRange());
1501 return QualType();
1502 }
1503 }
1504
1505 // Pointee types must be compatible.
Steve Naroffddb1dd82008-01-29 18:58:14 +00001506 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1507 rpointee.getUnqualifiedType())) {
Chris Lattner4d62f422007-12-09 21:53:25 +00001508 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1509 lex->getType().getAsString(), rex->getType().getAsString(),
1510 lex->getSourceRange(), rex->getSourceRange());
1511 return QualType();
1512 }
1513
1514 return Context.getPointerDiffType();
1515 }
1516 }
1517
Chris Lattner5c11c412007-12-12 05:47:28 +00001518 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001519}
1520
Chris Lattner2a3569b2008-04-07 05:30:13 +00001521// C99 6.5.7
1522QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1523 bool isCompAssign) {
Chris Lattner5c11c412007-12-12 05:47:28 +00001524 // C99 6.5.7p2: Each of the operands shall have integer type.
1525 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1526 return InvalidOperands(loc, lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001527
Chris Lattner5c11c412007-12-12 05:47:28 +00001528 // Shifts don't perform usual arithmetic conversions, they just do integer
1529 // promotions on each operand. C99 6.5.7p3
Chris Lattner3c133402007-12-13 07:28:16 +00001530 if (!isCompAssign)
1531 UsualUnaryConversions(lex);
Chris Lattner5c11c412007-12-12 05:47:28 +00001532 UsualUnaryConversions(rex);
1533
1534 // "The type of the result is that of the promoted left operand."
1535 return lex->getType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001536}
1537
Chris Lattner2a3569b2008-04-07 05:30:13 +00001538// C99 6.5.8
1539QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1540 bool isRelational) {
Chris Lattnerb620c342007-08-26 01:18:55 +00001541 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001542 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1543 UsualArithmeticConversions(lex, rex);
1544 else {
1545 UsualUnaryConversions(lex);
1546 UsualUnaryConversions(rex);
1547 }
Steve Naroff31090012007-07-16 21:54:35 +00001548 QualType lType = lex->getType();
1549 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001550
Ted Kremeneke2763b02007-10-29 17:13:39 +00001551 // For non-floating point types, check for self-comparisons of the form
1552 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1553 // often indicate logic errors in the program.
Ted Kremeneke451eae2007-10-29 16:58:49 +00001554 if (!lType->isFloatingType()) {
Ted Kremenekfff70962008-01-17 16:57:34 +00001555 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1556 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremeneke451eae2007-10-29 16:58:49 +00001557 if (DRL->getDecl() == DRR->getDecl())
1558 Diag(loc, diag::warn_selfcomparison);
1559 }
1560
Chris Lattnerb620c342007-08-26 01:18:55 +00001561 if (isRelational) {
1562 if (lType->isRealType() && rType->isRealType())
1563 return Context.IntTy;
1564 } else {
Ted Kremeneke2763b02007-10-29 17:13:39 +00001565 // Check for comparisons of floating point operands using != and ==.
Ted Kremeneke2763b02007-10-29 17:13:39 +00001566 if (lType->isFloatingType()) {
1567 assert (rType->isFloatingType());
Ted Kremenek43fb8b02007-11-25 00:58:00 +00001568 CheckFloatComparison(loc,lex,rex);
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001569 }
1570
Chris Lattnerb620c342007-08-26 01:18:55 +00001571 if (lType->isArithmeticType() && rType->isArithmeticType())
1572 return Context.IntTy;
1573 }
Steve Naroffe4718892007-04-27 18:30:00 +00001574
Chris Lattner1895e582007-08-26 01:10:14 +00001575 bool LHSIsNull = lex->isNullPointerConstant(Context);
1576 bool RHSIsNull = rex->isNullPointerConstant(Context);
1577
Chris Lattnerb620c342007-08-26 01:18:55 +00001578 // All of the following pointer related warnings are GCC extensions, except
1579 // when handling null pointer constants. One day, we can consider making them
1580 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001581 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner3a0702e2008-04-03 05:07:25 +00001582 QualType LCanPointeeTy =
1583 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1584 QualType RCanPointeeTy =
1585 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman1974e532008-02-08 01:19:44 +00001586
Steve Naroffb6666252007-11-13 14:57:38 +00001587 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner3a0702e2008-04-03 05:07:25 +00001588 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1589 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1590 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001591 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1592 lType.getAsString(), rType.getAsString(),
1593 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001594 }
Chris Lattnera65e1f32008-01-16 19:17:22 +00001595 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001596 return Context.IntTy;
1597 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001598 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
Chris Lattner2a3569b2008-04-07 05:30:13 +00001599 && ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattnera65e1f32008-01-16 19:17:22 +00001600 ImpCastExprToType(rex, lType);
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001601 return Context.IntTy;
1602 }
Steve Naroffcdee44c2007-08-16 21:48:38 +00001603 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001604 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001605 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1606 lType.getAsString(), rType.getAsString(),
1607 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnera65e1f32008-01-16 19:17:22 +00001608 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001609 return Context.IntTy;
1610 }
1611 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001612 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001613 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1614 lType.getAsString(), rType.getAsString(),
1615 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnera65e1f32008-01-16 19:17:22 +00001616 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001617 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001618 }
Chris Lattner5c11c412007-12-12 05:47:28 +00001619 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001620}
1621
Steve Naroff218bc2b2007-05-04 21:54:46 +00001622inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001623 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001624{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001625 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001626 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001627
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001628 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001629
Steve Naroffdbd9e892007-07-17 00:58:39 +00001630 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001631 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001632 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001633}
1634
Steve Naroff218bc2b2007-05-04 21:54:46 +00001635inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001636 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001637{
Steve Naroff31090012007-07-16 21:54:35 +00001638 UsualUnaryConversions(lex);
1639 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001640
Eli Friedman58639e52008-05-13 20:16:47 +00001641 if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001642 return Context.IntTy;
Chris Lattner5c11c412007-12-12 05:47:28 +00001643 return InvalidOperands(loc, lex, rex);
Steve Naroffae4143e2007-04-26 20:39:23 +00001644}
1645
Steve Naroff35d85152007-05-07 00:24:15 +00001646inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001647 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001648{
Steve Naroff0af91202007-04-27 21:51:21 +00001649 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001650 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff9358c712007-05-27 23:58:33 +00001651 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1652
1653 switch (mlval) { // C99 6.5.16p2
Chris Lattner9bad62c2008-01-04 18:04:52 +00001654 case Expr::MLV_Valid:
1655 break;
1656 case Expr::MLV_ConstQualified:
1657 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1658 return QualType();
1659 case Expr::MLV_ArrayType:
1660 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1661 lhsType.getAsString(), lex->getSourceRange());
1662 return QualType();
1663 case Expr::MLV_NotObjectType:
1664 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1665 lhsType.getAsString(), lex->getSourceRange());
1666 return QualType();
1667 case Expr::MLV_InvalidExpression:
1668 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1669 lex->getSourceRange());
1670 return QualType();
1671 case Expr::MLV_IncompleteType:
1672 case Expr::MLV_IncompleteVoidType:
1673 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1674 lhsType.getAsString(), lex->getSourceRange());
1675 return QualType();
1676 case Expr::MLV_DuplicateVectorComponents:
1677 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1678 lex->getSourceRange());
1679 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001680 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001681
Chris Lattner9bad62c2008-01-04 18:04:52 +00001682 AssignConvertType ConvTy;
1683 if (compoundType.isNull())
1684 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1685 else
1686 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1687
1688 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1689 rex, "assigning"))
1690 return QualType();
1691
Steve Naroff98cf3e92007-06-06 18:38:38 +00001692 // C99 6.5.16p3: The type of an assignment expression is the type of the
1693 // left operand unless the left operand has qualified type, in which case
1694 // it is the unqualified version of the type of the left operand.
1695 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1696 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001697 // C++ 5.17p1: the type of the assignment expression is that of its left
1698 // oprdu.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001699 return lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001700}
1701
Steve Naroff218bc2b2007-05-04 21:54:46 +00001702inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001703 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001704 UsualUnaryConversions(rex);
1705 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001706}
1707
Steve Naroff7a5af782007-07-13 16:58:59 +00001708/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1709/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001710QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001711 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001712 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001713
Steve Naroff9d139172007-08-24 17:20:07 +00001714 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff5f9ae642007-11-11 14:15:57 +00001715 if (const PointerType *pt = resType->getAsPointerType()) {
Eli Friedman8e122982008-05-18 18:08:51 +00001716 if (pt->getPointeeType()->isVoidType()) {
1717 Diag(OpLoc, diag::ext_gnu_void_ptr, op->getSourceRange());
1718 } else if (!pt->getPointeeType()->isObjectType()) {
1719 // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001720 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1721 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001722 return QualType();
1723 }
Steve Naroff9d139172007-08-24 17:20:07 +00001724 } else if (!resType->isRealType()) {
1725 if (resType->isComplexType())
1726 // C99 does not support ++/-- on complex types.
1727 Diag(OpLoc, diag::ext_integer_increment_complex,
1728 resType.getAsString(), op->getSourceRange());
1729 else {
1730 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1731 resType.getAsString(), op->getSourceRange());
1732 return QualType();
1733 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001734 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001735 // At this point, we know we have a real, complex or pointer type.
1736 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001737 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1738 if (mlval != Expr::MLV_Valid) {
1739 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001740 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001741 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001742 return QualType();
1743 }
1744 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001745}
1746
Anders Carlsson806700f2008-02-01 07:15:58 +00001747/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001748/// This routine allows us to typecheck complex/recursive expressions
1749/// where the declaration is needed for type checking. Here are some
1750/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001751static ValueDecl *getPrimaryDecl(Expr *E) {
1752 switch (E->getStmtClass()) {
Steve Naroff47500512007-04-19 23:00:49 +00001753 case Stmt::DeclRefExprClass:
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001754 return cast<DeclRefExpr>(E)->getDecl();
Steve Naroff47500512007-04-19 23:00:49 +00001755 case Stmt::MemberExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001756 // Fields cannot be declared with a 'register' storage class.
1757 // &X->f is always ok, even if X is declared register.
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001758 if (cast<MemberExpr>(E)->isArrow())
Chris Lattner48d52842007-11-16 17:46:48 +00001759 return 0;
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001760 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson806700f2008-02-01 07:15:58 +00001761 case Stmt::ArraySubscriptExprClass: {
1762 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1763
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001764 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson59435b22008-02-01 16:01:31 +00001765 if (!VD || VD->getType()->isPointerType())
Anders Carlsson806700f2008-02-01 07:15:58 +00001766 return 0;
1767 else
1768 return VD;
1769 }
Steve Naroff47500512007-04-19 23:00:49 +00001770 case Stmt::UnaryOperatorClass:
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001771 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001772 case Stmt::ParenExprClass:
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001773 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner48d52842007-11-16 17:46:48 +00001774 case Stmt::ImplicitCastExprClass:
1775 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner24d5bfe2008-04-02 04:24:33 +00001776 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001777 default:
1778 return 0;
1779 }
1780}
1781
1782/// CheckAddressOfOperand - The operand of & must be either a function
1783/// designator or an lvalue designating an object. If it is an lvalue, the
1784/// object cannot be declared with storage class register or be a bit field.
1785/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001786/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001787QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff826e91a2008-01-13 17:10:08 +00001788 if (getLangOptions().C99) {
1789 // Implement C99-only parts of addressof rules.
1790 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1791 if (uOp->getOpcode() == UnaryOperator::Deref)
1792 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1793 // (assuming the deref expression is valid).
1794 return uOp->getSubExpr()->getType();
1795 }
1796 // Technically, there should be a check for array subscript
1797 // expressions here, but the result of one is always an lvalue anyway.
1798 }
Anders Carlsson806700f2008-02-01 07:15:58 +00001799 ValueDecl *dcl = getPrimaryDecl(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001800 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001801
Steve Naroff9358c712007-05-27 23:58:33 +00001802 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattner48d52842007-11-16 17:46:48 +00001803 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1804 // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001805 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1806 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001807 return QualType();
1808 }
Steve Naroffb96e4ab62008-02-29 23:30:25 +00001809 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1810 if (MemExpr->getMemberDecl()->isBitField()) {
1811 Diag(OpLoc, diag::err_typecheck_address_of,
1812 std::string("bit-field"), op->getSourceRange());
1813 return QualType();
1814 }
1815 // Check for Apple extension for accessing vector components.
1816 } else if (isa<ArraySubscriptExpr>(op) &&
1817 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1818 Diag(OpLoc, diag::err_typecheck_address_of,
1819 std::string("vector"), op->getSourceRange());
1820 return QualType();
1821 } else if (dcl) { // C99 6.5.3.2p1
Steve Naroff47500512007-04-19 23:00:49 +00001822 // We have an lvalue with a decl. Make sure the decl is not declared
1823 // with the register storage-class specifier.
1824 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001825 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroffb96e4ab62008-02-29 23:30:25 +00001826 Diag(OpLoc, diag::err_typecheck_address_of,
1827 std::string("register variable"), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001828 return QualType();
1829 }
Steve Narofff633d092007-04-25 19:01:39 +00001830 } else
1831 assert(0 && "Unknown/unexpected decl type");
Steve Naroff47500512007-04-19 23:00:49 +00001832 }
1833 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001834 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001835}
1836
Steve Naroff35d85152007-05-07 00:24:15 +00001837QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001838 UsualUnaryConversions(op);
1839 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001840
Chris Lattnerc996b172007-07-31 16:53:04 +00001841 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff826e91a2008-01-13 17:10:08 +00001842 // Note that per both C89 and C99, this is always legal, even
1843 // if ptype is an incomplete type or void.
1844 // It would be possible to warn about dereferencing a
1845 // void pointer, but it's completely well-defined,
1846 // and such a warning is unlikely to catch any mistakes.
1847 return PT->getPointeeType();
Steve Naroff758ada12007-05-28 16:15:57 +00001848 }
1849 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001850 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001851 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001852}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001853
1854static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1855 tok::TokenKind Kind) {
1856 BinaryOperator::Opcode Opc;
1857 switch (Kind) {
1858 default: assert(0 && "Unknown binop!");
1859 case tok::star: Opc = BinaryOperator::Mul; break;
1860 case tok::slash: Opc = BinaryOperator::Div; break;
1861 case tok::percent: Opc = BinaryOperator::Rem; break;
1862 case tok::plus: Opc = BinaryOperator::Add; break;
1863 case tok::minus: Opc = BinaryOperator::Sub; break;
1864 case tok::lessless: Opc = BinaryOperator::Shl; break;
1865 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1866 case tok::lessequal: Opc = BinaryOperator::LE; break;
1867 case tok::less: Opc = BinaryOperator::LT; break;
1868 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1869 case tok::greater: Opc = BinaryOperator::GT; break;
1870 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1871 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1872 case tok::amp: Opc = BinaryOperator::And; break;
1873 case tok::caret: Opc = BinaryOperator::Xor; break;
1874 case tok::pipe: Opc = BinaryOperator::Or; break;
1875 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1876 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1877 case tok::equal: Opc = BinaryOperator::Assign; break;
1878 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1879 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1880 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1881 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1882 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1883 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1884 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1885 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1886 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1887 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1888 case tok::comma: Opc = BinaryOperator::Comma; break;
1889 }
1890 return Opc;
1891}
1892
Steve Naroff35d85152007-05-07 00:24:15 +00001893static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1894 tok::TokenKind Kind) {
1895 UnaryOperator::Opcode Opc;
1896 switch (Kind) {
1897 default: assert(0 && "Unknown unary op!");
1898 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1899 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1900 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1901 case tok::star: Opc = UnaryOperator::Deref; break;
1902 case tok::plus: Opc = UnaryOperator::Plus; break;
1903 case tok::minus: Opc = UnaryOperator::Minus; break;
1904 case tok::tilde: Opc = UnaryOperator::Not; break;
1905 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1906 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1907 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1908 case tok::kw___real: Opc = UnaryOperator::Real; break;
1909 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001910 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001911 }
1912 return Opc;
1913}
1914
Steve Naroff218bc2b2007-05-04 21:54:46 +00001915// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001916Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Steve Naroff218bc2b2007-05-04 21:54:46 +00001917 ExprTy *LHS, ExprTy *RHS) {
1918 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1919 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1920
Steve Naroff83895f72007-09-16 03:34:24 +00001921 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1922 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Steve Naroff218bc2b2007-05-04 21:54:46 +00001923
Chris Lattner256c21b2007-06-28 03:53:10 +00001924 QualType ResultTy; // Result type of the binary operator.
1925 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001926
1927 switch (Opc) {
1928 default:
1929 assert(0 && "Unknown binary expr!");
1930 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001931 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001932 break;
1933 case BinaryOperator::Mul:
1934 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001935 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001936 break;
1937 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001938 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001939 break;
1940 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001941 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001942 break;
1943 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001944 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001945 break;
1946 case BinaryOperator::Shl:
1947 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001948 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001949 break;
1950 case BinaryOperator::LE:
1951 case BinaryOperator::LT:
1952 case BinaryOperator::GE:
1953 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001954 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001955 break;
1956 case BinaryOperator::EQ:
1957 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001958 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001959 break;
1960 case BinaryOperator::And:
1961 case BinaryOperator::Xor:
1962 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001963 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001964 break;
1965 case BinaryOperator::LAnd:
1966 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001967 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001968 break;
1969 case BinaryOperator::MulAssign:
1970 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001971 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001972 if (!CompTy.isNull())
1973 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001974 break;
1975 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001976 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001977 if (!CompTy.isNull())
1978 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001979 break;
1980 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001981 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001982 if (!CompTy.isNull())
1983 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001984 break;
1985 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001986 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001987 if (!CompTy.isNull())
1988 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001989 break;
1990 case BinaryOperator::ShlAssign:
1991 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001992 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001993 if (!CompTy.isNull())
1994 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001995 break;
1996 case BinaryOperator::AndAssign:
1997 case BinaryOperator::XorAssign:
1998 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001999 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00002000 if (!CompTy.isNull())
2001 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002002 break;
2003 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00002004 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002005 break;
2006 }
Chris Lattner256c21b2007-06-28 03:53:10 +00002007 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00002008 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00002009 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00002010 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00002011 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00002012 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002013}
2014
Steve Naroff35d85152007-05-07 00:24:15 +00002015// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00002016Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00002017 ExprTy *input) {
2018 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00002019 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
2020 QualType resultType;
2021 switch (Opc) {
2022 default:
2023 assert(0 && "Unimplemented unary expr!");
2024 case UnaryOperator::PreInc:
2025 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00002026 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002027 break;
2028 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00002029 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002030 break;
2031 case UnaryOperator::Deref:
Steve Naroffb7235642007-12-18 04:06:57 +00002032 DefaultFunctionArrayConversion(Input);
Chris Lattner86554282007-06-08 22:32:33 +00002033 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002034 break;
2035 case UnaryOperator::Plus:
2036 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00002037 UsualUnaryConversions(Input);
2038 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00002039 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00002040 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2041 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00002042 break;
2043 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00002044 UsualUnaryConversions(Input);
2045 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00002046 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2047 if (!resultType->isIntegerType()) {
2048 if (resultType->isComplexType())
2049 // C99 does not support '~' for complex conjugation.
2050 Diag(OpLoc, diag::ext_integer_complement_complex,
2051 resultType.getAsString());
2052 else
2053 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2054 resultType.getAsString());
2055 }
Steve Naroff35d85152007-05-07 00:24:15 +00002056 break;
2057 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00002058 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00002059 DefaultFunctionArrayConversion(Input);
2060 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00002061 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00002062 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2063 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00002064 // LNot always has type int. C99 6.5.3.3p5.
2065 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00002066 break;
2067 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00002068 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00002069 break;
Steve Naroff35d85152007-05-07 00:24:15 +00002070 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00002071 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2072 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00002073 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00002074 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00002075 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00002076 break;
Chris Lattner86554282007-06-08 22:32:33 +00002077 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00002078 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00002079 break;
Steve Naroff35d85152007-05-07 00:24:15 +00002080 }
2081 if (resultType.isNull())
2082 return true;
Chris Lattner86554282007-06-08 22:32:33 +00002083 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00002084}
Chris Lattnereefa10e2007-05-28 06:56:27 +00002085
Steve Naroff66356bd2007-09-16 14:56:35 +00002086/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2087Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattnereefa10e2007-05-28 06:56:27 +00002088 SourceLocation LabLoc,
2089 IdentifierInfo *LabelII) {
2090 // Look up the record for this label identifier.
2091 LabelStmt *&LabelDecl = LabelMap[LabelII];
2092
2093 // If we haven't seen this label yet, create a forward reference.
2094 if (LabelDecl == 0)
2095 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2096
2097 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00002098 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2099 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00002100}
2101
Steve Naroff66356bd2007-09-16 14:56:35 +00002102Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner366727f2007-07-24 16:58:17 +00002103 SourceLocation RPLoc) { // "({..})"
2104 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2105 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2106 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2107
2108 // FIXME: there are a variety of strange constraints to enforce here, for
2109 // example, it is not possible to goto into a stmt expression apparently.
2110 // More semantic analysis is needed.
2111
2112 // FIXME: the last statement in the compount stmt has its value used. We
2113 // should not warn about it being unused.
2114
2115 // If there are sub stmts in the compound stmt, take the type of the last one
2116 // as the type of the stmtexpr.
2117 QualType Ty = Context.VoidTy;
2118
2119 if (!Compound->body_empty())
2120 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2121 Ty = LastExpr->getType();
2122
2123 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2124}
Steve Naroff78864672007-08-01 22:05:33 +00002125
Steve Naroff66356bd2007-09-16 14:56:35 +00002126Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattnerf17bd422007-08-30 17:45:32 +00002127 SourceLocation TypeLoc,
2128 TypeTy *argty,
2129 OffsetOfComponent *CompPtr,
2130 unsigned NumComponents,
2131 SourceLocation RPLoc) {
2132 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2133 assert(!ArgTy.isNull() && "Missing type argument!");
2134
2135 // We must have at least one component that refers to the type, and the first
2136 // one is known to be a field designator. Verify that the ArgTy represents
2137 // a struct/union/class.
2138 if (!ArgTy->isRecordType())
2139 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2140
2141 // Otherwise, create a compound literal expression as the base, and
2142 // iteratively process the offsetof designators.
Steve Naroffd32419d2008-01-14 18:19:28 +00002143 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattnerf17bd422007-08-30 17:45:32 +00002144
Chris Lattner78502cf2007-08-31 21:49:13 +00002145 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2146 // GCC extension, diagnose them.
2147 if (NumComponents != 1)
2148 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2149 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2150
Chris Lattnerf17bd422007-08-30 17:45:32 +00002151 for (unsigned i = 0; i != NumComponents; ++i) {
2152 const OffsetOfComponent &OC = CompPtr[i];
2153 if (OC.isBrackets) {
2154 // Offset of an array sub-field. TODO: Should we allow vector elements?
2155 const ArrayType *AT = Res->getType()->getAsArrayType();
2156 if (!AT) {
2157 delete Res;
2158 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2159 Res->getType().getAsString());
2160 }
2161
Chris Lattner98dbf0a2007-08-30 17:59:59 +00002162 // FIXME: C++: Verify that operator[] isn't overloaded.
2163
Chris Lattnerf17bd422007-08-30 17:45:32 +00002164 // C99 6.5.2.1p1
2165 Expr *Idx = static_cast<Expr*>(OC.U.E);
2166 if (!Idx->getType()->isIntegerType())
2167 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2168 Idx->getSourceRange());
2169
2170 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2171 continue;
2172 }
2173
2174 const RecordType *RC = Res->getType()->getAsRecordType();
2175 if (!RC) {
2176 delete Res;
2177 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2178 Res->getType().getAsString());
2179 }
2180
2181 // Get the decl corresponding to this.
2182 RecordDecl *RD = RC->getDecl();
2183 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2184 if (!MemberDecl)
2185 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2186 OC.U.IdentInfo->getName(),
2187 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00002188
2189 // FIXME: C++: Verify that MemberDecl isn't a static field.
2190 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman1242fff2008-02-06 22:48:16 +00002191 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2192 // matter here.
2193 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattnerf17bd422007-08-30 17:45:32 +00002194 }
2195
2196 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2197 BuiltinLoc);
2198}
2199
2200
Steve Naroff66356bd2007-09-16 14:56:35 +00002201Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00002202 TypeTy *arg1, TypeTy *arg2,
2203 SourceLocation RPLoc) {
2204 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2205 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2206
2207 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2208
Chris Lattnerf17bd422007-08-30 17:45:32 +00002209 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00002210}
2211
Steve Naroff66356bd2007-09-16 14:56:35 +00002212Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff9efdabc2007-08-03 21:21:27 +00002213 ExprTy *expr1, ExprTy *expr2,
2214 SourceLocation RPLoc) {
2215 Expr *CondExpr = static_cast<Expr*>(cond);
2216 Expr *LHSExpr = static_cast<Expr*>(expr1);
2217 Expr *RHSExpr = static_cast<Expr*>(expr2);
2218
2219 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2220
2221 // The conditional expression is required to be a constant expression.
2222 llvm::APSInt condEval(32);
2223 SourceLocation ExpLoc;
2224 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2225 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2226 CondExpr->getSourceRange());
2227
2228 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2229 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2230 RHSExpr->getType();
2231 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2232}
2233
Nate Begeman936b2072008-01-30 20:50:20 +00002234/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman1e36a852008-01-17 17:46:27 +00002235/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begeman936b2072008-01-30 20:50:20 +00002236/// The number of arguments has already been validated to match the number of
2237/// arguments in FnType.
2238static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman1e36a852008-01-17 17:46:27 +00002239 unsigned NumParams = FnType->getNumArgs();
Nate Begeman46bd0372008-04-18 23:35:14 +00002240 for (unsigned i = 0; i != NumParams; ++i) {
2241 QualType ExprTy = Args[i]->getType().getCanonicalType();
2242 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2243
2244 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman1e36a852008-01-17 17:46:27 +00002245 return false;
Nate Begeman46bd0372008-04-18 23:35:14 +00002246 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002247 return true;
2248}
2249
2250Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2251 SourceLocation *CommaLocs,
2252 SourceLocation BuiltinLoc,
2253 SourceLocation RParenLoc) {
Nate Begeman4cd66892008-01-31 05:38:29 +00002254 // __builtin_overload requires at least 2 arguments
2255 if (NumArgs < 2)
2256 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2257 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman1e36a852008-01-17 17:46:27 +00002258
Nate Begeman1e36a852008-01-17 17:46:27 +00002259 // The first argument is required to be a constant expression. It tells us
2260 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begeman4cd66892008-01-31 05:38:29 +00002261 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman1e36a852008-01-17 17:46:27 +00002262 Expr *NParamsExpr = Args[0];
2263 llvm::APSInt constEval(32);
2264 SourceLocation ExpLoc;
2265 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2266 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2267 NParamsExpr->getSourceRange());
2268
2269 // Verify that the number of parameters is > 0
2270 unsigned NumParams = constEval.getZExtValue();
2271 if (NumParams == 0)
2272 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2273 NParamsExpr->getSourceRange());
2274 // Verify that we have at least 1 + NumParams arguments to the builtin.
2275 if ((NumParams + 1) > NumArgs)
2276 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2277 SourceRange(BuiltinLoc, RParenLoc));
2278
2279 // Figure out the return type, by matching the args to one of the functions
Nate Begeman936b2072008-01-30 20:50:20 +00002280 // listed after the parameters.
Nate Begeman4cd66892008-01-31 05:38:29 +00002281 OverloadExpr *OE = 0;
Nate Begeman1e36a852008-01-17 17:46:27 +00002282 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2283 // UsualUnaryConversions will convert the function DeclRefExpr into a
2284 // pointer to function.
2285 Expr *Fn = UsualUnaryConversions(Args[i]);
2286 FunctionTypeProto *FnType = 0;
Nate Begeman936b2072008-01-30 20:50:20 +00002287 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2288 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2289 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2290 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002291
2292 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2293 // parameters, and the number of parameters must match the value passed to
2294 // the builtin.
2295 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begeman936b2072008-01-30 20:50:20 +00002296 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2297 Fn->getSourceRange());
Nate Begeman1e36a852008-01-17 17:46:27 +00002298
2299 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begeman936b2072008-01-30 20:50:20 +00002300 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman1e36a852008-01-17 17:46:27 +00002301 // If they match, return a new OverloadExpr.
Nate Begeman4cd66892008-01-31 05:38:29 +00002302 if (ExprsMatchFnType(Args+1, FnType)) {
2303 if (OE)
2304 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2305 OE->getFn()->getSourceRange());
2306 // Remember our match, and continue processing the remaining arguments
2307 // to catch any errors.
2308 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2309 BuiltinLoc, RParenLoc);
2310 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002311 }
Nate Begeman4cd66892008-01-31 05:38:29 +00002312 // Return the newly created OverloadExpr node, if we succeded in matching
2313 // exactly one of the candidate functions.
2314 if (OE)
2315 return OE;
Nate Begeman1e36a852008-01-17 17:46:27 +00002316
2317 // If we didn't find a matching function Expr in the __builtin_overload list
2318 // the return an error.
2319 std::string typeNames;
Nate Begeman936b2072008-01-30 20:50:20 +00002320 for (unsigned i = 0; i != NumParams; ++i) {
2321 if (i != 0) typeNames += ", ";
2322 typeNames += Args[i+1]->getType().getAsString();
2323 }
Nate Begeman1e36a852008-01-17 17:46:27 +00002324
2325 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2326 SourceRange(BuiltinLoc, RParenLoc));
2327}
2328
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002329Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2330 ExprTy *expr, TypeTy *type,
Chris Lattner9bad62c2008-01-04 18:04:52 +00002331 SourceLocation RPLoc) {
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002332 Expr *E = static_cast<Expr*>(expr);
2333 QualType T = QualType::getFromOpaquePtr(type);
2334
2335 InitBuiltinVaListType();
2336
Chris Lattner9bad62c2008-01-04 18:04:52 +00002337 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2338 != Compatible)
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002339 return Diag(E->getLocStart(),
2340 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2341 E->getType().getAsString(),
2342 E->getSourceRange());
2343
2344 // FIXME: Warn if a non-POD type is passed in.
2345
2346 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2347}
2348
Chris Lattner9bad62c2008-01-04 18:04:52 +00002349bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2350 SourceLocation Loc,
2351 QualType DstType, QualType SrcType,
2352 Expr *SrcExpr, const char *Flavor) {
2353 // Decode the result (notice that AST's are still created for extensions).
2354 bool isInvalid = false;
2355 unsigned DiagKind;
2356 switch (ConvTy) {
2357 default: assert(0 && "Unknown conversion type");
2358 case Compatible: return false;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002359 case PointerToInt:
Chris Lattner9bad62c2008-01-04 18:04:52 +00002360 DiagKind = diag::ext_typecheck_convert_pointer_int;
2361 break;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002362 case IntToPointer:
2363 DiagKind = diag::ext_typecheck_convert_int_pointer;
2364 break;
Chris Lattner9bad62c2008-01-04 18:04:52 +00002365 case IncompatiblePointer:
2366 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2367 break;
2368 case FunctionVoidPointer:
2369 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2370 break;
2371 case CompatiblePointerDiscardsQualifiers:
2372 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2373 break;
2374 case Incompatible:
2375 DiagKind = diag::err_typecheck_convert_incompatible;
2376 isInvalid = true;
2377 break;
2378 }
2379
2380 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2381 SrcExpr->getSourceRange());
2382 return isInvalid;
2383}
2384
Argyrios Kyrtzidis08114892008-04-27 13:50:30 +00002385
2386