blob: 832493876aebfef38212abb6d1a1b2849d218eb9 [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000015#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000016#include "clang/AST/Decl.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
18#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000019#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000022#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/TargetInfo.h"
24#include "llvm/ADT/SmallString.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000025using namespace llvm;
26using namespace clang;
27
Steve Naroffe4718892007-04-27 18:30:00 +000028// Sema.h avoids including Expr.h. As a result, all the Check* functions take
29// an unsigned which is really an enum. These typedefs provide a short hand
30// notiation for casting (to keep the lines within 80 columns:-)
31typedef BinaryOperator::Opcode BOP;
32typedef UnaryOperator::Opcode UOP;
33
Steve Naroffdf7855b2007-02-21 23:46:25 +000034/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000035/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
36/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
37/// multiple tokens. However, the common case is that StringToks points to one
38/// string.
39///
40Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000041Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000042 assert(NumStringToks && "Must have at least one string!");
43
Steve Naroff4f88b312007-03-13 22:37:02 +000044 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
45 if (Literal.hadError)
46 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000047
Chris Lattner5b183d82006-11-10 05:03:26 +000048 SmallVector<SourceLocation, 4> StringTokLocs;
49 for (unsigned i = 0; i != NumStringToks; ++i)
50 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000051
52 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000053 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000054
Chris Lattner5b183d82006-11-10 05:03:26 +000055 // FIXME: use factory.
Chris Lattner5b183d82006-11-10 05:03:26 +000056 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000057 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Narofff1e53692007-03-23 22:27:02 +000058 Literal.AnyWide, t);
Chris Lattner5b183d82006-11-10 05:03:26 +000059}
60
Chris Lattnere168f762006-11-10 05:29:30 +000061
Chris Lattnerac18be92006-11-20 06:49:47 +000062/// ParseIdentifierExpr - The parser read an identifier in expression context,
63/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
64/// identifier is used in an function call context.
65Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
66 IdentifierInfo &II,
67 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000068 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000069 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000070 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000071 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000072 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000073 if (HasTrailingLParen &&
74 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000075 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000076 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000077 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000078 // If this name wasn't predeclared and if this is not a function call,
79 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000080 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000081 }
Chris Lattner17ed4872006-11-20 04:58:19 +000082 }
83
Steve Naroff46ba1eb2007-04-03 23:13:13 +000084 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
85 return new DeclRefExpr(VD, VD->getType());
86 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000087 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
88
89 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000090}
Chris Lattnere168f762006-11-10 05:29:30 +000091
Chris Lattner17ed4872006-11-20 04:58:19 +000092Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
93 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000094 switch (Kind) {
95 default:
96 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000097 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000098 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
99 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
100 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +0000101 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +0000102 }
103}
104
Steve Naroffae4143e2007-04-26 20:39:23 +0000105Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
106 SmallString<16> CharBuffer;
107 CharBuffer.resize(Tok.getLength());
108 const char *ThisTokBegin = &CharBuffer[0];
109 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
110
111 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
112 Tok.getLocation(), PP);
113 if (Literal.hadError())
114 return ExprResult(true);
115 return new CharacterLiteral(Literal.getValue(), Context.IntTy);
116}
117
Steve Naroff8160ea22007-03-06 01:09:46 +0000118Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000119 // fast path for a single digit (which is quite common). A single digit
120 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
121 if (Tok.getLength() == 1) {
122 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
123 return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy));
124 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000125 SmallString<512> IntegerBuffer;
126 IntegerBuffer.resize(Tok.getLength());
127 const char *ThisTokBegin = &IntegerBuffer[0];
128
129 // Get the spelling of the token, which eliminates trigraphs, etc. Notes:
130 // - We know that ThisTokBuf points to a buffer that is big enough for the
131 // whole token and 'spelled' tokens can only shrink.
132 // - In practice, the local buffer is only used when the spelling doesn't
133 // match the original token (which is rare). The common case simply returns
134 // a pointer to a *constant* buffer (avoiding a copy).
135
136 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000137 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000138 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000139 if (Literal.hadError)
140 return ExprResult(true);
141
Steve Naroff09ef4742007-03-09 23:16:33 +0000142 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000143 QualType t;
Steve Naroff09ef4742007-03-09 23:16:33 +0000144 if (Literal.hasSuffix()) {
145 if (Literal.isLong)
146 t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
147 else if (Literal.isLongLong)
148 t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
149 else
150 t = Context.UnsignedIntTy;
151 } else {
152 t = Context.IntTy; // implicit type is "int"
153 }
Steve Naroff451d8f162007-03-12 23:22:38 +0000154 uintmax_t val;
155 if (Literal.GetIntegerValue(val)) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000156 return new IntegerLiteral(val, t);
Steve Naroff09ef4742007-03-09 23:16:33 +0000157 }
158 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000159 // FIXME: fill in the value and compute the real type...
160 return new FloatingLiteral(7.7, Context.FloatTy);
Steve Naroff09ef4742007-03-09 23:16:33 +0000161 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000162 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000163}
164
165Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
166 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000167 Expr *e = (Expr *)Val;
168 assert((e != 0) && "ParseParenExpr() missing expr");
169 return e;
Chris Lattnere168f762006-11-10 05:29:30 +0000170}
171
172
173// Unary Operators. 'Tok' is the token for the operator.
174Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
175 ExprTy *Input) {
176 UnaryOperator::Opcode Opc;
177 switch (Op) {
178 default: assert(0 && "Unknown unary op!");
179 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
180 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
181 case tok::amp: Opc = UnaryOperator::AddrOf; break;
182 case tok::star: Opc = UnaryOperator::Deref; break;
183 case tok::plus: Opc = UnaryOperator::Plus; break;
184 case tok::minus: Opc = UnaryOperator::Minus; break;
185 case tok::tilde: Opc = UnaryOperator::Not; break;
186 case tok::exclaim: Opc = UnaryOperator::LNot; break;
187 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
188 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
189 case tok::kw___real: Opc = UnaryOperator::Real; break;
190 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
191 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
192 case tok::kw___extension__:
193 return Input;
194 //Opc = UnaryOperator::Extension;
195 //break;
196 }
Steve Naroff95af0132007-03-30 23:47:58 +0000197 if (Opc == UnaryOperator::PreInc || Opc == UnaryOperator::PreDec)
198 return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
Steve Naroff47500512007-04-19 23:00:49 +0000199 else if (Opc == UnaryOperator::AddrOf)
Steve Narofff633d092007-04-25 19:01:39 +0000200 return CheckAddressOfOperand((Expr *)Input, OpLoc);
Steve Naroff47500512007-04-19 23:00:49 +0000201 else if (Opc == UnaryOperator::Deref)
Steve Narofff633d092007-04-25 19:01:39 +0000202 return CheckIndirectionOperand((Expr *)Input, OpLoc);
Steve Naroff1926c832007-04-24 00:23:05 +0000203 else if (UnaryOperator::isArithmeticOp(Opc))
204 return CheckArithmeticOperand((Expr *)Input, OpLoc, Opc);
205
206 // will go away when all cases are handled...
207 return new UnaryOperator((Expr *)Input, Opc, QualType());
Chris Lattnere168f762006-11-10 05:29:30 +0000208}
209
210Action::ExprResult Sema::
211ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
212 SourceLocation LParenLoc, TypeTy *Ty,
213 SourceLocation RParenLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000214 // If error parsing type, ignore.
215 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000216
217 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000218 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000219
220 if (isa<FunctionType>(ArgTy) && isSizeof) {
221 // alignof(function) is allowed.
222 Diag(OpLoc, diag::ext_sizeof_function_type);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000223 return new IntegerLiteral(1, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000224 } else if (ArgTy->isVoidType()) {
225 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
226 } else if (ArgTy->isIncompleteType()) {
227 std::string TypeName;
228 ArgTy->getAsString(TypeName);
229 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
230 diag::err_alignof_incomplete_type, TypeName);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000231 return new IntegerLiteral(0, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000232 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000233 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
234 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, Context.getSizeType());
Chris Lattnere168f762006-11-10 05:29:30 +0000235}
236
237
238Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
239 tok::TokenKind Kind,
240 ExprTy *Input) {
241 UnaryOperator::Opcode Opc;
242 switch (Kind) {
243 default: assert(0 && "Unknown unary op!");
244 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
245 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
246 }
Steve Naroff95af0132007-03-30 23:47:58 +0000247 return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
Chris Lattnere168f762006-11-10 05:29:30 +0000248}
249
250Action::ExprResult Sema::
251ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
252 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000253 QualType t1 = ((Expr *)Base)->getType();
254 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000255
256 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000257 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000258
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000259 QualType canonT1 = t1.getCanonicalType();
260 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000261
Steve Naroffc1aadb12007-03-28 21:49:40 +0000262 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
263 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000264 // in the subscript position. As a result, we need to derive the array base
265 // and index from the expression types.
266
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000267 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000268 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
269 baseType = canonT1;
270 indexType = canonT2;
271 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
272 baseType = canonT2;
273 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000274 } else
275 return Diag(LLoc, diag::err_typecheck_subscript_value);
276
Steve Naroffc1aadb12007-03-28 21:49:40 +0000277 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000278 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000279 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000280
Steve Naroff95af0132007-03-30 23:47:58 +0000281 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000282 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000283 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
284 resultType = ary->getElementType();
285 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
286 resultType = ary->getPointeeType();
287 // in practice, the following check catches trying to index a pointer
288 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000289 if (!resultType->isObjectType())
290 return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000291 }
292 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000293}
294
295Action::ExprResult Sema::
296ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
297 tok::TokenKind OpKind, SourceLocation MemberLoc,
298 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000299 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000300
301 assert(!qualifiedType.isNull() && "no type for member expression");
302
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000303 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000304
305 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000306 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
307 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000308 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000309 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000310 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
311 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000312 if (!isa<RecordType>(canonType))
313 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
314
315 // get the struct/union definition from the type.
316 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000317
Steve Naroff92e30f82007-04-02 22:35:25 +0000318 if (canonType->isIncompleteType())
319 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000320
Steve Naroff92e30f82007-04-02 22:35:25 +0000321 FieldDecl *MemberDecl = RD->getMember(&Member);
322 if (!MemberDecl)
323 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
324
325 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000326}
327
328/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
329/// This provides the location of the left/right parens and a list of comma
330/// locations.
331Action::ExprResult Sema::
332ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
333 ExprTy **Args, unsigned NumArgs,
334 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000335 QualType qType = ((Expr *)Fn)->getType();
336
337 assert(!qType.isNull() && "no type for function call expression");
338
339 QualType canonType = qType.getCanonicalType();
340 QualType resultType;
341
342 if (const FunctionType *funcT = dyn_cast<FunctionType>(canonType)) {
343 resultType = funcT->getResultType();
344 }
Steve Naroffb891de32007-05-02 23:51:10 +0000345 // C99 6.5.2.2p7 - If we have a prototype, the arguments are implicitly
346 // converted, as if by assignment, to the types of the corresponding
347 // parameters, taking the type of each parameter to be the unqualified...
348 //
349 // QualType result = UsualAssignmentConversions(lhsType, rhsType, rex, loc);
Steve Naroffae4143e2007-04-26 20:39:23 +0000350 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000351}
352
353Action::ExprResult Sema::
354ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
355 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000356 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000357 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000358 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
Chris Lattnere168f762006-11-10 05:29:30 +0000359}
360
361
362
363// Binary Operators. 'Tok' is the token for the operator.
364Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
365 ExprTy *LHS, ExprTy *RHS) {
366 BinaryOperator::Opcode Opc;
367 switch (Kind) {
368 default: assert(0 && "Unknown binop!");
369 case tok::star: Opc = BinaryOperator::Mul; break;
370 case tok::slash: Opc = BinaryOperator::Div; break;
371 case tok::percent: Opc = BinaryOperator::Rem; break;
372 case tok::plus: Opc = BinaryOperator::Add; break;
373 case tok::minus: Opc = BinaryOperator::Sub; break;
374 case tok::lessless: Opc = BinaryOperator::Shl; break;
375 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
376 case tok::lessequal: Opc = BinaryOperator::LE; break;
377 case tok::less: Opc = BinaryOperator::LT; break;
378 case tok::greaterequal: Opc = BinaryOperator::GE; break;
379 case tok::greater: Opc = BinaryOperator::GT; break;
380 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
381 case tok::equalequal: Opc = BinaryOperator::EQ; break;
382 case tok::amp: Opc = BinaryOperator::And; break;
383 case tok::caret: Opc = BinaryOperator::Xor; break;
384 case tok::pipe: Opc = BinaryOperator::Or; break;
385 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
386 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
387 case tok::equal: Opc = BinaryOperator::Assign; break;
388 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
389 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
390 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
391 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
392 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
393 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
394 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
395 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
396 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
397 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
398 case tok::comma: Opc = BinaryOperator::Comma; break;
399 }
Steve Narofff1e53692007-03-23 22:27:02 +0000400
Steve Naroff1926c832007-04-24 00:23:05 +0000401 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
Steve Naroffae4143e2007-04-26 20:39:23 +0000402
403 assert((lhs != 0) && "ParseBinOp(): missing left expression");
404 assert((rhs != 0) && "ParseBinOp(): missing right expression");
405
Steve Naroff26c8ea52007-03-21 21:08:52 +0000406 if (BinaryOperator::isMultiplicativeOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000407 return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000408 else if (BinaryOperator::isAdditiveOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000409 return CheckAdditiveOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000410 else if (BinaryOperator::isShiftOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000411 return CheckShiftOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000412 else if (BinaryOperator::isRelationalOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000413 return CheckRelationalOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000414 else if (BinaryOperator::isEqualityOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000415 return CheckEqualityOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000416 else if (BinaryOperator::isBitwiseOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000417 return CheckBitwiseOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000418 else if (BinaryOperator::isLogicalOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000419 return CheckLogicalOperands(lhs, rhs, TokLoc, Opc);
Steve Naroffae4143e2007-04-26 20:39:23 +0000420 else if (BinaryOperator::isAssignmentOp(Opc))
421 return CheckAssignmentOperands(lhs, rhs, TokLoc, Opc);
422 else if (Opc == BinaryOperator::Comma)
423 return CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroffe4718892007-04-27 18:30:00 +0000424
Steve Naroffae4143e2007-04-26 20:39:23 +0000425 assert(0 && "ParseBinOp(): illegal binary op");
Chris Lattnere168f762006-11-10 05:29:30 +0000426}
427
428/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
429/// in the case of a the GNU conditional expr extension.
430Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
431 SourceLocation ColonLoc,
432 ExprTy *Cond, ExprTy *LHS,
433 ExprTy *RHS) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000434 QualType lhs = ((Expr *)LHS)->getType();
435 QualType rhs = ((Expr *)RHS)->getType();
436
437 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
438 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
439
Steve Naroffe4718892007-04-27 18:30:00 +0000440 QualType canonType = rhs.getCanonicalType(); // FIXME
Steve Naroffae4143e2007-04-26 20:39:23 +0000441 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, canonType);
Chris Lattnere168f762006-11-10 05:29:30 +0000442}
443
Steve Naroff1926c832007-04-24 00:23:05 +0000444/// UsualUnaryConversion - Performs various conversions that are common to most
445/// operators (C99 6.3). The conversions of array and function types are
446/// sometimes surpressed. For example, the array->pointer conversion doesn't
447/// apply if the array is an argument to the sizeof or address (&) operators.
448/// In these instances, this routine should *not* be called.
449QualType Sema::UsualUnaryConversion(QualType t) {
450 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000451
Steve Naroff1926c832007-04-24 00:23:05 +0000452 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
453 return Context.IntTy;
454 else if (t->isFunctionType()) // C99 6.3.2.1p4
455 return Context.getPointerType(t);
456 else if (t->isArrayType()) // C99 6.3.2.1p3
457 return Context.getPointerType(cast<ArrayType>(t)->getElementType());
458 return t;
459}
460
461/// UsualArithmeticConversions - Performs various conversions that are common to
462/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
463/// routine returns the first non-arithmetic type found. The client is
464/// responsible for emitting appropriate error diagnostics.
465QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000466 QualType lhs = UsualUnaryConversion(t1);
467 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000468
469 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000470 if (!lhs->isArithmeticType())
471 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000472 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000473 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000474
Steve Narofff633d092007-04-25 19:01:39 +0000475 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000476 if (lhs == rhs)
477 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000478
Steve Naroffb01bbe32007-04-25 01:22:31 +0000479 // at this point, we have two different arithmetic types.
480
481 // Handle complex types first (C99 6.3.1.8p1).
482 if (lhs->isComplexType() || rhs->isComplexType()) {
483 // if we have an integer operand, the result is the complex type.
484 if (rhs->isIntegerType())
485 return lhs;
486 if (lhs->isIntegerType())
487 return rhs;
488
Steve Naroffe4718892007-04-27 18:30:00 +0000489 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000490 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000491 // Now handle "real" floating types (i.e. float, double, long double).
492 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
493 // if we have an integer operand, the result is the real floating type.
494 if (rhs->isIntegerType())
495 return lhs;
496 if (lhs->isIntegerType())
497 return rhs;
498
499 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000500 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000501 }
Steve Naroffe4718892007-04-27 18:30:00 +0000502 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000503}
504
Steve Naroff9eb24652007-05-02 21:58:15 +0000505QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
506 Expr *rex, SourceLocation loc) {
Steve Naroffb891de32007-05-02 23:51:10 +0000507 // this check seems unnatural, however it necessary to insure the proper
508 // conversion of functions/arrays. If the conversion where done for all
509 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
510 // unary expressions that surpress this implicit conversion (&, sizeof).
511 if (rhsType->isFunctionType() || rhsType->isArrayType())
512 rhsType = UsualUnaryConversion(rhsType);
513
Steve Naroff9eb24652007-05-02 21:58:15 +0000514 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
515 return lhsType;
516 else if (lhsType->isPointerType()) {
517 if (rhsType->isIntegerType()) {
518 // check for null pointer constant (C99 6.3.2.3p3)
519 const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(rex);
520 if (!constant || constant->getValue() != 0)
521 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroffb891de32007-05-02 23:51:10 +0000522 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000523 }
524 // FIXME: make sure the qualifier are matching
525 if (rhsType->isPointerType()) {
526 if (!Type::pointerTypesAreCompatible(lhsType, rhsType))
527 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroffb891de32007-05-02 23:51:10 +0000528 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000529 }
530 } else if (rhsType->isPointerType()) {
531 if (lhsType->isIntegerType()) {
532 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
533 if (lhsType != Context.BoolTy)
534 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroffb891de32007-05-02 23:51:10 +0000535 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000536 }
537 // - both operands are pointers to qualified or unqualified versions of
538 // compatible types, and the type pointed to by the left has *all* the
539 // qualifiers of the type pointed to by the right;
540 if (lhsType->isPointerType()) {
541 if (!Type::pointerTypesAreCompatible(lhsType, rhsType))
542 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroffb891de32007-05-02 23:51:10 +0000543 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000544 }
Steve Naroff9eb24652007-05-02 21:58:15 +0000545 } else if (lhsType->isStructureType() && rhsType->isStructureType()) {
546 if (Type::structureTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000547 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000548 } else if (lhsType->isUnionType() && rhsType->isUnionType()) {
549 if (Type::unionTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000550 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000551 }
552 return QualType(); // incompatible
553}
554
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000555Action::ExprResult Sema::CheckMultiplicativeOperands(
Steve Naroff1926c832007-04-24 00:23:05 +0000556 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000557{
Steve Naroff1926c832007-04-24 00:23:05 +0000558 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000559
Steve Naroffe4718892007-04-27 18:30:00 +0000560 if ((BOP)code == BinaryOperator::Rem) {
Steve Naroff1926c832007-04-24 00:23:05 +0000561 if (!resType->isIntegerType())
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000562 return Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff1926c832007-04-24 00:23:05 +0000563 } else { // *, /
564 if (!resType->isArithmeticType())
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000565 return Diag(loc, diag::err_typecheck_invalid_operands);
566 }
Steve Naroffe4718892007-04-27 18:30:00 +0000567 return new BinaryOperator(lex, rex, (BOP)code, resType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000568}
569
Steve Naroff1926c832007-04-24 00:23:05 +0000570Action::ExprResult Sema::CheckAdditiveOperands( // C99 6.5.6
571 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
572{
Steve Naroffe4718892007-04-27 18:30:00 +0000573 QualType lhsType = lex->getType(), rhsType = rex->getType();
574 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
575
576 // handle the common case first (both operands are arithmetic).
577 if (resType->isArithmeticType())
578 return new BinaryOperator(lex, rex, (BOP)code, resType);
579 else {
580 if ((BOP)code == BinaryOperator::Add) {
581 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
582 (lhsType->isIntegerType() && rhsType->isPointerType()))
583 return new BinaryOperator(lex, rex, (BOP)code, resType);
584 } else { // -
585 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
586 (lhsType->isPointerType() && rhsType->isPointerType()))
587 return new BinaryOperator(lex, rex, (BOP)code, resType);
588 }
589 }
590 return Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000591}
592
Steve Naroff1926c832007-04-24 00:23:05 +0000593Action::ExprResult Sema::CheckShiftOperands( // C99 6.5.7
594 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
595{
596 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
597
598 if (!resType->isIntegerType())
599 return Diag(loc, diag::err_typecheck_invalid_operands);
600
Steve Naroffe4718892007-04-27 18:30:00 +0000601 return new BinaryOperator(lex, rex, (BOP)code, resType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000602}
603
Steve Naroff1926c832007-04-24 00:23:05 +0000604Action::ExprResult Sema::CheckRelationalOperands( // C99 6.5.8
605 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
606{
607 QualType lType = lex->getType(), rType = rex->getType();
608
609 if (lType->isRealType() && rType->isRealType())
Steve Naroffe4718892007-04-27 18:30:00 +0000610 return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy);
611
612 if (lType->isPointerType() && rType->isPointerType())
613 return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy);
614
615 if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension.
616 return Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
617 return Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000618}
619
Steve Naroff1926c832007-04-24 00:23:05 +0000620Action::ExprResult Sema::CheckEqualityOperands( // C99 6.5.9
621 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
622{
623 QualType lType = lex->getType(), rType = rex->getType();
624
625 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroffe4718892007-04-27 18:30:00 +0000626 return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy);
627
628 if (lType->isPointerType() && rType->isPointerType())
629 return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy);
630
631 if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension.
632 return Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
633 return Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000634}
635
Steve Naroff1926c832007-04-24 00:23:05 +0000636Action::ExprResult Sema::CheckBitwiseOperands(
637 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
638{
639 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
640
641 if (!resType->isIntegerType())
642 return Diag(loc, diag::err_typecheck_invalid_operands);
643
Steve Naroffe4718892007-04-27 18:30:00 +0000644 return new BinaryOperator(lex, rex, (BOP)code, resType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000645}
646
Steve Naroff1926c832007-04-24 00:23:05 +0000647Action::ExprResult Sema::CheckLogicalOperands( // C99 6.5.[13,14]
648 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
649{
Steve Naroffe4718892007-04-27 18:30:00 +0000650 QualType lhsType = UsualUnaryConversion(lex->getType());
651 QualType rhsType = UsualUnaryConversion(rex->getType());
652
653 if (!lhsType->isScalarType() || !rhsType->isScalarType())
654 return Diag(loc, diag::err_typecheck_invalid_operands);
655
656 return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy);
Steve Naroffae4143e2007-04-26 20:39:23 +0000657}
658
Steve Naroffdd92b932007-05-02 02:47:33 +0000659/// CheckAssignmentOperands (C99 6.5.16) - This routine currently
660/// has code to accommodate several GCC extensions when type checking
661/// pointers. Here are some objectionable examples that GCC considers warnings:
662///
663/// int a, *pint;
664/// short *pshort;
665/// struct foo *pfoo;
666///
667/// pint = pshort; // warning: assignment from incompatible pointer type
668/// a = pint; // warning: assignment makes integer from pointer without a cast
669/// pint = a; // warning: assignment makes pointer from integer without a cast
670/// pint = pfoo; // warning: assignment from incompatible pointer type
671///
672/// As a result, the code for dealing with pointers is more complex than the
673/// C99 spec dictates.
674/// Note: the warning above turn into errors when -pedantic-errors is enabled.
675///
676Action::ExprResult Sema::CheckAssignmentOperands(
Steve Naroffae4143e2007-04-26 20:39:23 +0000677 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
678{
Steve Naroff0af91202007-04-27 21:51:21 +0000679 QualType lhsType = lex->getType();
680 QualType rhsType = rex->getType();
681
Steve Naroffdd92b932007-05-02 02:47:33 +0000682 if ((BOP)code == BinaryOperator::Assign) { // C99 6.5.16.1
Steve Naroffb891de32007-05-02 23:51:10 +0000683 if (!lex->isLvalue())
684 return Diag(loc, diag::ext_typecheck_assign_non_lvalue);
Steve Naroffdd92b932007-05-02 02:47:33 +0000685 if (lhsType.isConstQualified())
686 return Diag(loc, diag::err_typecheck_assign_const);
687
Steve Naroff9eb24652007-05-02 21:58:15 +0000688 if (lhsType == rhsType) // common case, fast path...
Steve Naroffdd92b932007-05-02 02:47:33 +0000689 return new BinaryOperator(lex, rex, (BOP)code, lhsType);
Steve Naroff9eb24652007-05-02 21:58:15 +0000690
691 QualType result = UsualAssignmentConversions(lhsType, rhsType, rex, loc);
692 if (result.isNull())
693 return Diag(loc, diag::err_typecheck_assign_incompatible);
694 else
695 return new BinaryOperator(lex, rex, (BOP)code, result);
Steve Naroffdd92b932007-05-02 02:47:33 +0000696 }
Steve Naroffdd92b932007-05-02 02:47:33 +0000697 // FIXME: type check compound assignments...
Steve Naroffe4718892007-04-27 18:30:00 +0000698 return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy);
Steve Naroffae4143e2007-04-26 20:39:23 +0000699}
700
701Action::ExprResult Sema::CheckCommaOperands( // C99 6.5.17
702 Expr *lex, Expr *rex, SourceLocation loc)
703{
Steve Naroffe4718892007-04-27 18:30:00 +0000704 QualType rhsType = UsualUnaryConversion(rex->getType());
705 return new BinaryOperator(lex, rex, BinaryOperator::Comma, rhsType);
Steve Naroff95af0132007-03-30 23:47:58 +0000706}
707
708Action::ExprResult
709Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc,
Steve Naroff78403362007-04-02 22:55:05 +0000710 unsigned OpCode) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000711 QualType qType = op->getType();
Steve Naroff95af0132007-03-30 23:47:58 +0000712
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000713 assert(!qType.isNull() && "no type for increment/decrement expression");
714
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000715 QualType canonType = qType.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000716
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000717 // C99 6.5.2.4p1
718 if (const PointerType *pt = dyn_cast<PointerType>(canonType)) {
719 if (!pt->getPointeeType()->isObjectType()) // C99 6.5.2.4p2, 6.5.6p2
720 return Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, qType);
721 } else if (!canonType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000722 // FIXME: Allow Complex as a GCC extension.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000723 return Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, qType);
724 }
Steve Naroff95af0132007-03-30 23:47:58 +0000725 // At this point, we know we have a real or pointer type. As a result, the
726 // following predicate is overkill (i.e. it will check for types we know we
727 // don't have in this context). Nevertheless, we model the C99 spec closely.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000728 if (!canonType.isModifiableLvalue())
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000729 return Diag(OpLoc, diag::err_typecheck_not_modifiable, qType);
Steve Naroff95af0132007-03-30 23:47:58 +0000730
Steve Naroffe4718892007-04-27 18:30:00 +0000731 return new UnaryOperator(op, (UOP)OpCode, qType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000732}
733
Steve Naroff1926c832007-04-24 00:23:05 +0000734/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000735/// This routine allows us to typecheck complex/recursive expressions
736/// where the declaration is needed for type checking. Here are some
737/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000738static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000739 switch (e->getStmtClass()) {
740 case Stmt::DeclRefExprClass:
741 return cast<DeclRefExpr>(e)->getDecl();
742 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000743 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000744 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000745 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000746 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000747 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000748 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000749 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000750 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000751 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000752 default:
753 return 0;
754 }
755}
756
757/// CheckAddressOfOperand - The operand of & must be either a function
758/// designator or an lvalue designating an object. If it is an lvalue, the
759/// object cannot be declared with storage class register or be a bit field.
760/// Note: The usual conversions are *not* applied to the operand of the &
761/// operator, and its result is never an lvalue.
762Action::ExprResult
Steve Narofff633d092007-04-25 19:01:39 +0000763Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000764 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000765
766 if (!op->isLvalue()) {
767 if (dcl && isa<FunctionDecl>(dcl))
768 ; // C99 6.5.3.2p1: Allow function designators.
769 else
770 return Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
771 } else if (dcl) {
772 // We have an lvalue with a decl. Make sure the decl is not declared
773 // with the register storage-class specifier.
774 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
775 if (vd->getStorageClass() == VarDecl::Register)
776 return Diag(OpLoc, diag::err_typecheck_address_of_register);
Steve Narofff633d092007-04-25 19:01:39 +0000777 } else
778 assert(0 && "Unknown/unexpected decl type");
779
Steve Naroff47500512007-04-19 23:00:49 +0000780 // FIXME: add check for bitfields!
781 }
782 // If the operand has type "type", the result has type "pointer to type".
Steve Narofff633d092007-04-25 19:01:39 +0000783 return new UnaryOperator(op, UnaryOperator::AddrOf,
Steve Naroff47500512007-04-19 23:00:49 +0000784 Context.getPointerType(op->getType()));
785}
786
787Action::ExprResult
Steve Narofff633d092007-04-25 19:01:39 +0000788Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff47500512007-04-19 23:00:49 +0000789 QualType qType = op->getType();
790
791 assert(!qType.isNull() && "no type for * expression");
792
793 QualType canonType = qType.getCanonicalType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000794
795 // FIXME: add type checking and fix result type
Steve Naroff47500512007-04-19 23:00:49 +0000796
Steve Naroffae4143e2007-04-26 20:39:23 +0000797 return new UnaryOperator(op, UnaryOperator::Deref, Context.IntTy);
Steve Naroff47500512007-04-19 23:00:49 +0000798}
Steve Naroff1926c832007-04-24 00:23:05 +0000799
800/// CheckArithmeticOperand - Check the arithmetic unary operators (C99 6.5.3.3).
801Action::ExprResult
802Sema::CheckArithmeticOperand(Expr *op, SourceLocation OpLoc, unsigned Opc) {
803 QualType resultType = UsualUnaryConversion(op->getType());
804
805 switch (Opc) {
806 case UnaryOperator::Plus:
807 case UnaryOperator::Minus:
808 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
809 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
810 break;
811 case UnaryOperator::Not: // bitwise complement
812 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
813 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
814 break;
815 case UnaryOperator::LNot: // logical negation
816 if (!resultType->isScalarType()) // C99 6.5.3.3p1
817 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
818 break;
819 }
Steve Naroffe4718892007-04-27 18:30:00 +0000820 return new UnaryOperator(op, (UOP)Opc, resultType);
Steve Naroff1926c832007-04-24 00:23:05 +0000821}