blob: 48bccf16e2a074a44e935e65ac08fe0d032d6a76 [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 Naroffdf7855b2007-02-21 23:46:25 +000028/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000029/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
30/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
31/// multiple tokens. However, the common case is that StringToks points to one
32/// string.
33///
34Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000035Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000036 assert(NumStringToks && "Must have at least one string!");
37
Steve Naroff4f88b312007-03-13 22:37:02 +000038 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000041
Chris Lattner5b183d82006-11-10 05:03:26 +000042 SmallVector<SourceLocation, 4> StringTokLocs;
43 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000045
46 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000047 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000048
Chris Lattner5b183d82006-11-10 05:03:26 +000049 // FIXME: use factory.
Chris Lattner5b183d82006-11-10 05:03:26 +000050 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000051 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Narofff1e53692007-03-23 22:27:02 +000052 Literal.AnyWide, t);
Chris Lattner5b183d82006-11-10 05:03:26 +000053}
54
Chris Lattnere168f762006-11-10 05:29:30 +000055
Chris Lattnerac18be92006-11-20 06:49:47 +000056/// ParseIdentifierExpr - The parser read an identifier in expression context,
57/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
58/// identifier is used in an function call context.
59Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
60 IdentifierInfo &II,
61 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000062 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000063 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000064 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000065 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000066 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000067 if (HasTrailingLParen &&
68 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000069 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000070 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000071 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000072 // If this name wasn't predeclared and if this is not a function call,
73 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000074 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000075 }
Chris Lattner17ed4872006-11-20 04:58:19 +000076 }
77
Steve Naroff46ba1eb2007-04-03 23:13:13 +000078 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
79 return new DeclRefExpr(VD, VD->getType());
80 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000081 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
82
83 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000084}
Chris Lattnere168f762006-11-10 05:29:30 +000085
Chris Lattner17ed4872006-11-20 04:58:19 +000086Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
87 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000088 switch (Kind) {
89 default:
90 assert(0 && "Unknown simple primary expr!");
Chris Lattnere168f762006-11-10 05:29:30 +000091 case tok::char_constant: // constant: character-constant
Chris Lattner17ed4872006-11-20 04:58:19 +000092 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000093 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
94 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
95 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +000096 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +000097 }
98}
99
Steve Naroffae4143e2007-04-26 20:39:23 +0000100Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
101 SmallString<16> CharBuffer;
102 CharBuffer.resize(Tok.getLength());
103 const char *ThisTokBegin = &CharBuffer[0];
104 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
105
106 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
107 Tok.getLocation(), PP);
108 if (Literal.hadError())
109 return ExprResult(true);
110 return new CharacterLiteral(Literal.getValue(), Context.IntTy);
111}
112
Steve Naroff8160ea22007-03-06 01:09:46 +0000113Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000114 // fast path for a single digit (which is quite common). A single digit
115 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
116 if (Tok.getLength() == 1) {
117 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
118 return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy));
119 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000120 SmallString<512> IntegerBuffer;
121 IntegerBuffer.resize(Tok.getLength());
122 const char *ThisTokBegin = &IntegerBuffer[0];
123
124 // Get the spelling of the token, which eliminates trigraphs, etc. Notes:
125 // - We know that ThisTokBuf points to a buffer that is big enough for the
126 // whole token and 'spelled' tokens can only shrink.
127 // - In practice, the local buffer is only used when the spelling doesn't
128 // match the original token (which is rare). The common case simply returns
129 // a pointer to a *constant* buffer (avoiding a copy).
130
131 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000132 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000133 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000134 if (Literal.hadError)
135 return ExprResult(true);
136
Steve Naroff09ef4742007-03-09 23:16:33 +0000137 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000138 QualType t;
Steve Naroff09ef4742007-03-09 23:16:33 +0000139 if (Literal.hasSuffix()) {
140 if (Literal.isLong)
141 t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
142 else if (Literal.isLongLong)
143 t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
144 else
145 t = Context.UnsignedIntTy;
146 } else {
147 t = Context.IntTy; // implicit type is "int"
148 }
Steve Naroff451d8f162007-03-12 23:22:38 +0000149 uintmax_t val;
150 if (Literal.GetIntegerValue(val)) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000151 return new IntegerLiteral(val, t);
Steve Naroff09ef4742007-03-09 23:16:33 +0000152 }
153 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000154 // FIXME: fill in the value and compute the real type...
155 return new FloatingLiteral(7.7, Context.FloatTy);
Steve Naroff09ef4742007-03-09 23:16:33 +0000156 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000157 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000158}
159
160Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
161 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000162 Expr *e = (Expr *)Val;
163 assert((e != 0) && "ParseParenExpr() missing expr");
164 return e;
Chris Lattnere168f762006-11-10 05:29:30 +0000165}
166
167
168// Unary Operators. 'Tok' is the token for the operator.
169Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
170 ExprTy *Input) {
171 UnaryOperator::Opcode Opc;
172 switch (Op) {
173 default: assert(0 && "Unknown unary op!");
174 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
175 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
176 case tok::amp: Opc = UnaryOperator::AddrOf; break;
177 case tok::star: Opc = UnaryOperator::Deref; break;
178 case tok::plus: Opc = UnaryOperator::Plus; break;
179 case tok::minus: Opc = UnaryOperator::Minus; break;
180 case tok::tilde: Opc = UnaryOperator::Not; break;
181 case tok::exclaim: Opc = UnaryOperator::LNot; break;
182 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
183 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
184 case tok::kw___real: Opc = UnaryOperator::Real; break;
185 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
186 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
187 case tok::kw___extension__:
188 return Input;
189 //Opc = UnaryOperator::Extension;
190 //break;
191 }
Steve Naroff95af0132007-03-30 23:47:58 +0000192 if (Opc == UnaryOperator::PreInc || Opc == UnaryOperator::PreDec)
193 return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
Steve Naroff47500512007-04-19 23:00:49 +0000194 else if (Opc == UnaryOperator::AddrOf)
Steve Narofff633d092007-04-25 19:01:39 +0000195 return CheckAddressOfOperand((Expr *)Input, OpLoc);
Steve Naroff47500512007-04-19 23:00:49 +0000196 else if (Opc == UnaryOperator::Deref)
Steve Narofff633d092007-04-25 19:01:39 +0000197 return CheckIndirectionOperand((Expr *)Input, OpLoc);
Steve Naroff1926c832007-04-24 00:23:05 +0000198 else if (UnaryOperator::isArithmeticOp(Opc))
199 return CheckArithmeticOperand((Expr *)Input, OpLoc, Opc);
200
201 // will go away when all cases are handled...
202 return new UnaryOperator((Expr *)Input, Opc, QualType());
Chris Lattnere168f762006-11-10 05:29:30 +0000203}
204
205Action::ExprResult Sema::
206ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
207 SourceLocation LParenLoc, TypeTy *Ty,
208 SourceLocation RParenLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000209 // If error parsing type, ignore.
210 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000211
212 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000213 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000214
215 if (isa<FunctionType>(ArgTy) && isSizeof) {
216 // alignof(function) is allowed.
217 Diag(OpLoc, diag::ext_sizeof_function_type);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000218 return new IntegerLiteral(1, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000219 } else if (ArgTy->isVoidType()) {
220 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
221 } else if (ArgTy->isIncompleteType()) {
222 std::string TypeName;
223 ArgTy->getAsString(TypeName);
224 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
225 diag::err_alignof_incomplete_type, TypeName);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000226 return new IntegerLiteral(0, Context.IntTy);
Chris Lattner6531c102007-01-23 22:29:49 +0000227 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000228 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
229 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, Context.getSizeType());
Chris Lattnere168f762006-11-10 05:29:30 +0000230}
231
232
233Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
234 tok::TokenKind Kind,
235 ExprTy *Input) {
236 UnaryOperator::Opcode Opc;
237 switch (Kind) {
238 default: assert(0 && "Unknown unary op!");
239 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
240 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
241 }
Steve Naroff95af0132007-03-30 23:47:58 +0000242 return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc);
Chris Lattnere168f762006-11-10 05:29:30 +0000243}
244
245Action::ExprResult Sema::
246ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
247 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000248 QualType t1 = ((Expr *)Base)->getType();
249 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000250
251 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000252 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000253
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000254 QualType canonT1 = t1.getCanonicalType();
255 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000256
Steve Naroffc1aadb12007-03-28 21:49:40 +0000257 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
258 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000259 // in the subscript position. As a result, we need to derive the array base
260 // and index from the expression types.
261
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000262 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000263 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
264 baseType = canonT1;
265 indexType = canonT2;
266 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
267 baseType = canonT2;
268 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000269 } else
270 return Diag(LLoc, diag::err_typecheck_subscript_value);
271
Steve Naroffc1aadb12007-03-28 21:49:40 +0000272 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000273 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000274 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000275
Steve Naroff95af0132007-03-30 23:47:58 +0000276 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000277 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000278 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
279 resultType = ary->getElementType();
280 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
281 resultType = ary->getPointeeType();
282 // in practice, the following check catches trying to index a pointer
283 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000284 if (!resultType->isObjectType())
285 return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000286 }
287 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000288}
289
290Action::ExprResult Sema::
291ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
292 tok::TokenKind OpKind, SourceLocation MemberLoc,
293 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000294 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000295
296 assert(!qualifiedType.isNull() && "no type for member expression");
297
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000298 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000299
300 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000301 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
302 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000303 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000304 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000305 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
306 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000307 if (!isa<RecordType>(canonType))
308 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
309
310 // get the struct/union definition from the type.
311 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000312
Steve Naroff92e30f82007-04-02 22:35:25 +0000313 if (canonType->isIncompleteType())
314 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000315
Steve Naroff92e30f82007-04-02 22:35:25 +0000316 FieldDecl *MemberDecl = RD->getMember(&Member);
317 if (!MemberDecl)
318 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
319
320 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000321}
322
323/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
324/// This provides the location of the left/right parens and a list of comma
325/// locations.
326Action::ExprResult Sema::
327ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
328 ExprTy **Args, unsigned NumArgs,
329 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000330 QualType qType = ((Expr *)Fn)->getType();
331
332 assert(!qType.isNull() && "no type for function call expression");
333
334 QualType canonType = qType.getCanonicalType();
335 QualType resultType;
336
337 if (const FunctionType *funcT = dyn_cast<FunctionType>(canonType)) {
338 resultType = funcT->getResultType();
339 }
340 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs, resultType);
Chris Lattnere168f762006-11-10 05:29:30 +0000341}
342
343Action::ExprResult Sema::
344ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
345 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000346 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000347 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000348 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op);
Chris Lattnere168f762006-11-10 05:29:30 +0000349}
350
351
352
353// Binary Operators. 'Tok' is the token for the operator.
354Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
355 ExprTy *LHS, ExprTy *RHS) {
356 BinaryOperator::Opcode Opc;
357 switch (Kind) {
358 default: assert(0 && "Unknown binop!");
359 case tok::star: Opc = BinaryOperator::Mul; break;
360 case tok::slash: Opc = BinaryOperator::Div; break;
361 case tok::percent: Opc = BinaryOperator::Rem; break;
362 case tok::plus: Opc = BinaryOperator::Add; break;
363 case tok::minus: Opc = BinaryOperator::Sub; break;
364 case tok::lessless: Opc = BinaryOperator::Shl; break;
365 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
366 case tok::lessequal: Opc = BinaryOperator::LE; break;
367 case tok::less: Opc = BinaryOperator::LT; break;
368 case tok::greaterequal: Opc = BinaryOperator::GE; break;
369 case tok::greater: Opc = BinaryOperator::GT; break;
370 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
371 case tok::equalequal: Opc = BinaryOperator::EQ; break;
372 case tok::amp: Opc = BinaryOperator::And; break;
373 case tok::caret: Opc = BinaryOperator::Xor; break;
374 case tok::pipe: Opc = BinaryOperator::Or; break;
375 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
376 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
377 case tok::equal: Opc = BinaryOperator::Assign; break;
378 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
379 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
380 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
381 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
382 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
383 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
384 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
385 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
386 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
387 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
388 case tok::comma: Opc = BinaryOperator::Comma; break;
389 }
Steve Narofff1e53692007-03-23 22:27:02 +0000390
Steve Naroff1926c832007-04-24 00:23:05 +0000391 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
Steve Naroffae4143e2007-04-26 20:39:23 +0000392
393 assert((lhs != 0) && "ParseBinOp(): missing left expression");
394 assert((rhs != 0) && "ParseBinOp(): missing right expression");
395
Steve Naroff26c8ea52007-03-21 21:08:52 +0000396 if (BinaryOperator::isMultiplicativeOp(Opc))
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000397 return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000398 else if (BinaryOperator::isAdditiveOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000399 return CheckAdditiveOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000400 else if (BinaryOperator::isShiftOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000401 return CheckShiftOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000402 else if (BinaryOperator::isRelationalOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000403 return CheckRelationalOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000404 else if (BinaryOperator::isEqualityOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000405 return CheckEqualityOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000406 else if (BinaryOperator::isBitwiseOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000407 return CheckBitwiseOperands(lhs, rhs, TokLoc, Opc);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000408 else if (BinaryOperator::isLogicalOp(Opc))
Steve Naroff1926c832007-04-24 00:23:05 +0000409 return CheckLogicalOperands(lhs, rhs, TokLoc, Opc);
Steve Naroffae4143e2007-04-26 20:39:23 +0000410 else if (BinaryOperator::isAssignmentOp(Opc))
411 return CheckAssignmentOperands(lhs, rhs, TokLoc, Opc);
412 else if (Opc == BinaryOperator::Comma)
413 return CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff1926c832007-04-24 00:23:05 +0000414
Steve Naroffae4143e2007-04-26 20:39:23 +0000415 assert(0 && "ParseBinOp(): illegal binary op");
Chris Lattnere168f762006-11-10 05:29:30 +0000416}
417
418/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
419/// in the case of a the GNU conditional expr extension.
420Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
421 SourceLocation ColonLoc,
422 ExprTy *Cond, ExprTy *LHS,
423 ExprTy *RHS) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000424 QualType lhs = ((Expr *)LHS)->getType();
425 QualType rhs = ((Expr *)RHS)->getType();
426
427 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
428 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
429
430 QualType canonType = rhs.getCanonicalType(); // TEMPORARY
431 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, canonType);
Chris Lattnere168f762006-11-10 05:29:30 +0000432}
433
Steve Naroff1926c832007-04-24 00:23:05 +0000434/// UsualUnaryConversion - Performs various conversions that are common to most
435/// operators (C99 6.3). The conversions of array and function types are
436/// sometimes surpressed. For example, the array->pointer conversion doesn't
437/// apply if the array is an argument to the sizeof or address (&) operators.
438/// In these instances, this routine should *not* be called.
439QualType Sema::UsualUnaryConversion(QualType t) {
440 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000441
Steve Naroff1926c832007-04-24 00:23:05 +0000442 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
443 return Context.IntTy;
444 else if (t->isFunctionType()) // C99 6.3.2.1p4
445 return Context.getPointerType(t);
446 else if (t->isArrayType()) // C99 6.3.2.1p3
447 return Context.getPointerType(cast<ArrayType>(t)->getElementType());
448 return t;
449}
450
Steve Naroff82ceca52007-04-25 21:10:52 +0000451/// GetIntegerRank - Helper function for UsualArithmeticConversions().
452static inline int GetIntegerRank(QualType t) {
453 if (const BuiltinType *BT = dyn_cast<BuiltinType>(t.getCanonicalType())) {
454 switch (BT->getKind()) {
455 case BuiltinType::SChar:
456 case BuiltinType::UChar:
457 return 1;
458 case BuiltinType::Short:
459 case BuiltinType::UShort:
460 return 2;
461 case BuiltinType::Int:
462 case BuiltinType::UInt:
463 return 3;
464 case BuiltinType::Long:
465 case BuiltinType::ULong:
466 return 4;
467 case BuiltinType::LongLong:
468 case BuiltinType::ULongLong:
469 return 5;
470 default:
471 assert(0 && "getFloatingPointRank(): not a floating type");
472 }
473 }
474 return 0;
475}
476
477static inline QualType ConvertSignedWithGreaterRankThanUnsigned(
478 QualType signedType, QualType unsignedType) {
479 // FIXME: Need to check if the signed type can represent all values of the
480 // unsigned type. If it can, then the result is the signed type. If it can't,
481 // then the result is the unsigned version of the signed type.
482 return signedType;
483}
484
Steve Naroffb01bbe32007-04-25 01:22:31 +0000485/// GetFloatingRank - Helper function for UsualArithmeticConversions().
486static inline int GetFloatingRank(QualType t) {
487 if (const BuiltinType *BT = dyn_cast<BuiltinType>(t.getCanonicalType())) {
488 switch (BT->getKind()) {
489 case BuiltinType::Float:
490 case BuiltinType::FloatComplex:
491 return 1;
492 case BuiltinType::Double:
493 case BuiltinType::DoubleComplex:
494 return 2;
495 case BuiltinType::LongDouble:
496 case BuiltinType::LongDoubleComplex:
497 return 3;
498 default:
499 assert(0 && "getFloatingPointRank(): not a floating type");
500 }
501 }
502 return 0;
503}
504
505/// ConvertFloatingRankToComplexType - Another helper for converting floats.
506static inline QualType ConvertFloatingRankToComplexType(int rank,
Steve Narofff633d092007-04-25 19:01:39 +0000507 ASTContext &C) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000508 switch (rank) {
509 case 1:
510 return C.FloatComplexTy;
511 case 2:
512 return C.DoubleComplexTy;
513 case 3:
514 return C.LongDoubleComplexTy;
515 default:
516 assert(0 && "convertRankToComplex(): illegal value for rank");
517 }
518}
519
Steve Naroff1926c832007-04-24 00:23:05 +0000520/// UsualArithmeticConversions - Performs various conversions that are common to
521/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
522/// routine returns the first non-arithmetic type found. The client is
523/// responsible for emitting appropriate error diagnostics.
524QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000525 QualType lhs = UsualUnaryConversion(t1);
526 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000527
528 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000529 if (!lhs->isArithmeticType())
530 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000531 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000532 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000533
Steve Narofff633d092007-04-25 19:01:39 +0000534 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000535 if (lhs == rhs)
536 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000537
Steve Naroffb01bbe32007-04-25 01:22:31 +0000538 // at this point, we have two different arithmetic types.
539
540 // Handle complex types first (C99 6.3.1.8p1).
541 if (lhs->isComplexType() || rhs->isComplexType()) {
542 // if we have an integer operand, the result is the complex type.
543 if (rhs->isIntegerType())
544 return lhs;
545 if (lhs->isIntegerType())
546 return rhs;
547
548 // the following code handles three different combinations:
Steve Narofff633d092007-04-25 19:01:39 +0000549 // complex/complex, complex/float, float/complex. When both operands
550 // are complex, the shorter operand is converted to the type of the longer,
551 // and that is the type of the result. This corresponds to what is done
552 // when combining two real floating-point operands. The fun begins when
553 // size promotion occur across type domains. GetFloatingRank &
554 // ConvertFloatingRankToComplexType handle this without enumerating all
555 // permutations. It also allows us to add new types without breakage.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000556 int lhsRank = GetFloatingRank(lhs);
557 int rhsRank = GetFloatingRank(rhs);
558
Steve Narofff633d092007-04-25 19:01:39 +0000559 // From H&S 6.3.4: When one operand is complex and the other is a real
560 // floating-point type, the less precise type is converted, within it's
561 // real or complex domain, to the precision of the other type. For example,
562 // when combining a "long double" with a "double _Complex", the
563 // "double _Complex" is promoted to "long double _Complex".
564 return ConvertFloatingRankToComplexType(std::max(lhsRank,rhsRank), Context);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000565 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000566 // Now handle "real" floating types (i.e. float, double, long double).
567 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
568 // if we have an integer operand, the result is the real floating type.
569 if (rhs->isIntegerType())
570 return lhs;
571 if (lhs->isIntegerType())
572 return rhs;
573
574 // we have two real floating types, float/complex combos were handled above.
Steve Narofff633d092007-04-25 19:01:39 +0000575 return GetFloatingRank(lhs) >= GetFloatingRank(rhs) ? lhs : rhs;
Steve Naroffb01bbe32007-04-25 01:22:31 +0000576 }
Steve Naroff82ceca52007-04-25 21:10:52 +0000577 // Lastly, handle two integers (C99 6.3.1.8p1)
Steve Naroffb01bbe32007-04-25 01:22:31 +0000578 bool t1Unsigned = lhs->isUnsignedIntegerType();
579 bool t2Unsigned = rhs->isUnsignedIntegerType();
Steve Naroff1926c832007-04-24 00:23:05 +0000580
Steve Naroff82ceca52007-04-25 21:10:52 +0000581 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
582 return GetIntegerRank(lhs) >= GetIntegerRank(rhs) ? lhs : rhs;
583
584 // We have two integer types with differing signs
585 QualType unsignedType = t1Unsigned ? lhs : rhs;
586 QualType signedType = t1Unsigned ? rhs : lhs;
587
588 if (GetIntegerRank(unsignedType) >= GetIntegerRank(signedType))
589 return unsignedType;
Steve Naroff1926c832007-04-24 00:23:05 +0000590 else
Steve Naroff82ceca52007-04-25 21:10:52 +0000591 return ConvertSignedWithGreaterRankThanUnsigned(signedType, unsignedType);
Steve Narofff1e53692007-03-23 22:27:02 +0000592}
593
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000594Action::ExprResult Sema::CheckMultiplicativeOperands(
Steve Naroff1926c832007-04-24 00:23:05 +0000595 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000596{
Steve Naroff1926c832007-04-24 00:23:05 +0000597 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000598
599 if ((BinaryOperator::Opcode)code == BinaryOperator::Rem) {
Steve Naroff1926c832007-04-24 00:23:05 +0000600 if (!resType->isIntegerType())
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000601 return Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff1926c832007-04-24 00:23:05 +0000602 } else { // *, /
603 if (!resType->isArithmeticType())
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000604 return Diag(loc, diag::err_typecheck_invalid_operands);
605 }
Steve Naroff1926c832007-04-24 00:23:05 +0000606 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, resType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000607}
608
Steve Naroff1926c832007-04-24 00:23:05 +0000609Action::ExprResult Sema::CheckAdditiveOperands( // C99 6.5.6
610 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
611{
Steve Naroffae4143e2007-04-26 20:39:23 +0000612 // FIXME: add type checking and fix result type
Steve Naroff1926c832007-04-24 00:23:05 +0000613 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000614}
615
Steve Naroff1926c832007-04-24 00:23:05 +0000616Action::ExprResult Sema::CheckShiftOperands( // C99 6.5.7
617 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
618{
619 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
620
621 if (!resType->isIntegerType())
622 return Diag(loc, diag::err_typecheck_invalid_operands);
623
624 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, resType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000625}
626
Steve Naroff1926c832007-04-24 00:23:05 +0000627Action::ExprResult Sema::CheckRelationalOperands( // C99 6.5.8
628 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
629{
630 QualType lType = lex->getType(), rType = rex->getType();
631
632 if (lType->isRealType() && rType->isRealType())
633 ;
634 else if (lType->isPointerType() && rType->isPointerType())
635 ;
636 else {
637 // The following test is for GCC compatibility.
638 if (lType->isIntegerType() || rType->isIntegerType())
639 return Diag(loc, diag::err_typecheck_comparison_of_pointer_integer);
640 return Diag(loc, diag::err_typecheck_invalid_operands);
641 }
642 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code,
643 Context.IntTy);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000644}
645
Steve Naroff1926c832007-04-24 00:23:05 +0000646Action::ExprResult Sema::CheckEqualityOperands( // C99 6.5.9
647 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
648{
649 QualType lType = lex->getType(), rType = rex->getType();
650
651 if (lType->isArithmeticType() && rType->isArithmeticType())
652 ;
653 else if (lType->isPointerType() && rType->isPointerType())
654 ;
655 else {
656 // The following test is for GCC compatibility.
657 if (lType->isIntegerType() || rType->isIntegerType())
658 return Diag(loc, diag::err_typecheck_comparison_of_pointer_integer);
659 return Diag(loc, diag::err_typecheck_invalid_operands);
660 }
661 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code,
662 Context.IntTy);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000663}
664
Steve Naroff1926c832007-04-24 00:23:05 +0000665Action::ExprResult Sema::CheckBitwiseOperands(
666 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
667{
668 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
669
670 if (!resType->isIntegerType())
671 return Diag(loc, diag::err_typecheck_invalid_operands);
672
673 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, resType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000674}
675
Steve Naroff1926c832007-04-24 00:23:05 +0000676Action::ExprResult Sema::CheckLogicalOperands( // C99 6.5.[13,14]
677 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
678{
Steve Naroffae4143e2007-04-26 20:39:23 +0000679 // FIXME: add type checking and fix result type
680 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
681}
682
683Action::ExprResult Sema::CheckAssignmentOperands( // C99 6.5.16
684 Expr *lex, Expr *rex, SourceLocation loc, unsigned code)
685{
686 // FIXME: add type checking and fix result type
687 return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy);
688}
689
690Action::ExprResult Sema::CheckCommaOperands( // C99 6.5.17
691 Expr *lex, Expr *rex, SourceLocation loc)
692{
693 // FIXME: add type checking and fix result type
694 return new BinaryOperator(lex, rex, BinaryOperator::Comma, Context.IntTy);
Steve Naroff95af0132007-03-30 23:47:58 +0000695}
696
697Action::ExprResult
698Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc,
Steve Naroff78403362007-04-02 22:55:05 +0000699 unsigned OpCode) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000700 QualType qType = op->getType();
Steve Naroff95af0132007-03-30 23:47:58 +0000701
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000702 assert(!qType.isNull() && "no type for increment/decrement expression");
703
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000704 QualType canonType = qType.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000705
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000706 // C99 6.5.2.4p1
707 if (const PointerType *pt = dyn_cast<PointerType>(canonType)) {
708 if (!pt->getPointeeType()->isObjectType()) // C99 6.5.2.4p2, 6.5.6p2
709 return Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, qType);
710 } else if (!canonType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000711 // FIXME: Allow Complex as a GCC extension.
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000712 return Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, qType);
713 }
Steve Naroff95af0132007-03-30 23:47:58 +0000714 // At this point, we know we have a real or pointer type. As a result, the
715 // following predicate is overkill (i.e. it will check for types we know we
716 // don't have in this context). Nevertheless, we model the C99 spec closely.
Steve Naroffd50c88e2007-04-05 21:15:20 +0000717 if (!canonType.isModifiableLvalue())
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000718 return Diag(OpLoc, diag::err_typecheck_not_modifiable, qType);
Steve Naroff95af0132007-03-30 23:47:58 +0000719
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000720 return new UnaryOperator(op, (UnaryOperator::Opcode)OpCode, qType);
Steve Naroff26c8ea52007-03-21 21:08:52 +0000721}
722
Steve Naroff1926c832007-04-24 00:23:05 +0000723/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000724/// This routine allows us to typecheck complex/recursive expressions
725/// where the declaration is needed for type checking. Here are some
726/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000727static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000728 switch (e->getStmtClass()) {
729 case Stmt::DeclRefExprClass:
730 return cast<DeclRefExpr>(e)->getDecl();
731 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000732 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000733 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000734 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000735 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000736 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000737 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000738 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000739 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000740 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000741 default:
742 return 0;
743 }
744}
745
746/// CheckAddressOfOperand - The operand of & must be either a function
747/// designator or an lvalue designating an object. If it is an lvalue, the
748/// object cannot be declared with storage class register or be a bit field.
749/// Note: The usual conversions are *not* applied to the operand of the &
750/// operator, and its result is never an lvalue.
751Action::ExprResult
Steve Narofff633d092007-04-25 19:01:39 +0000752Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000753 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000754
755 if (!op->isLvalue()) {
756 if (dcl && isa<FunctionDecl>(dcl))
757 ; // C99 6.5.3.2p1: Allow function designators.
758 else
759 return Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
760 } else if (dcl) {
761 // We have an lvalue with a decl. Make sure the decl is not declared
762 // with the register storage-class specifier.
763 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
764 if (vd->getStorageClass() == VarDecl::Register)
765 return Diag(OpLoc, diag::err_typecheck_address_of_register);
Steve Narofff633d092007-04-25 19:01:39 +0000766 } else
767 assert(0 && "Unknown/unexpected decl type");
768
Steve Naroff47500512007-04-19 23:00:49 +0000769 // FIXME: add check for bitfields!
770 }
771 // If the operand has type "type", the result has type "pointer to type".
Steve Narofff633d092007-04-25 19:01:39 +0000772 return new UnaryOperator(op, UnaryOperator::AddrOf,
Steve Naroff47500512007-04-19 23:00:49 +0000773 Context.getPointerType(op->getType()));
774}
775
776Action::ExprResult
Steve Narofff633d092007-04-25 19:01:39 +0000777Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff47500512007-04-19 23:00:49 +0000778 QualType qType = op->getType();
779
780 assert(!qType.isNull() && "no type for * expression");
781
782 QualType canonType = qType.getCanonicalType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000783
784 // FIXME: add type checking and fix result type
Steve Naroff47500512007-04-19 23:00:49 +0000785
Steve Naroffae4143e2007-04-26 20:39:23 +0000786 return new UnaryOperator(op, UnaryOperator::Deref, Context.IntTy);
Steve Naroff47500512007-04-19 23:00:49 +0000787}
Steve Naroff1926c832007-04-24 00:23:05 +0000788
789/// CheckArithmeticOperand - Check the arithmetic unary operators (C99 6.5.3.3).
790Action::ExprResult
791Sema::CheckArithmeticOperand(Expr *op, SourceLocation OpLoc, unsigned Opc) {
792 QualType resultType = UsualUnaryConversion(op->getType());
793
794 switch (Opc) {
795 case UnaryOperator::Plus:
796 case UnaryOperator::Minus:
797 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
798 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
799 break;
800 case UnaryOperator::Not: // bitwise complement
801 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
802 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
803 break;
804 case UnaryOperator::LNot: // logical negation
805 if (!resultType->isScalarType()) // C99 6.5.3.3p1
806 return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType);
807 break;
808 }
809 return new UnaryOperator(op, (UnaryOperator::Opcode)Opc, resultType);
810}