Chris Lattner | 7cee11f | 2006-11-03 06:42:29 +0000 | [diff] [blame] | 1 | //===--- ASTBuilder.cpp - AST Builder Implementation ----------------------===// |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 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 the actions class which builds an AST out of a parse |
| 11 | // stream. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 7cee11f | 2006-11-03 06:42:29 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTBuilder.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame^] | 18 | #include "clang/Parse/Action.h" |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Scope.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 20 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame^] | 22 | #include "clang/Basic/TargetInfo.h" |
| 23 | #include "llvm/ADT/SmallString.h" |
| 24 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | using namespace clang; |
| 27 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Symbol table tracking callbacks. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | 2abeb12 | 2006-10-28 19:51:26 +0000 | [diff] [blame] | 32 | bool ASTBuilder::isTypeName(const IdentifierInfo &II, Scope *S) const { |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 33 | Decl *D = II.getFETokenInfo<Decl>(); |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 34 | return D != 0 && D->getDeclSpec().StorageClassSpec == DeclSpec::SCS_typedef; |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 37 | Action::DeclTy * |
| 38 | ASTBuilder::ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init, |
| 39 | DeclTy *LastInGroup) { |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 40 | IdentifierInfo *II = D.getIdentifier(); |
| 41 | Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0; |
| 42 | |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 43 | Decl *New; |
| 44 | if (D.isFunctionDeclarator()) |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 45 | New = new FunctionDecl(II, D, PrevDecl); |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 46 | else |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 47 | New = new VarDecl(II, D, PrevDecl); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 48 | |
| 49 | // If this has an identifier, add it to the scope stack. |
| 50 | if (II) { |
| 51 | // If PrevDecl includes conflicting name here, emit a diagnostic. |
| 52 | II->setFETokenInfo(New); |
| 53 | S->AddDecl(II); |
| 54 | } |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 0535ebb | 2006-10-25 05:28:22 +0000 | [diff] [blame] | 56 | // If this is a top-level decl that is chained to some other (e.g. int A,B,C;) |
| 57 | // remember this in the LastInGroupList list. |
| 58 | if (LastInGroup && S->getParent() == 0) |
| 59 | LastInGroupList.push_back((Decl*)LastInGroup); |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 60 | |
| 61 | return New; |
| 62 | } |
| 63 | |
| 64 | Action::DeclTy * |
| 65 | ASTBuilder::ParseFunctionDefinition(Scope *S, Declarator &D, StmtTy *Body) { |
| 66 | FunctionDecl *FD = (FunctionDecl *)ParseDeclarator(S, D, 0, 0); |
Chris Lattner | 30f910e | 2006-10-16 05:52:41 +0000 | [diff] [blame] | 67 | |
| 68 | FD->setBody((Stmt*)Body); |
| 69 | |
Chris Lattner | 2dacc3f | 2006-10-16 00:33:54 +0000 | [diff] [blame] | 70 | return FD; |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) { |
| 74 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 75 | I != E; ++I) { |
| 76 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 77 | Decl *D = II.getFETokenInfo<Decl>(); |
| 78 | assert(D && "This decl didn't get pushed??"); |
| 79 | |
| 80 | Decl *Next = D->getNext(); |
| 81 | |
| 82 | // FIXME: Push the decl on the parent function list if in a function. |
| 83 | delete D; |
| 84 | |
| 85 | II.setFETokenInfo(Next); |
| 86 | } |
| 87 | } |
| 88 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 89 | //===--------------------------------------------------------------------===// |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 90 | // Statement Parsing Callbacks. |
| 91 | //===--------------------------------------------------------------------===// |
| 92 | |
| 93 | Action::StmtResult |
| 94 | ASTBuilder::ParseCompoundStmt(SourceLocation L, SourceLocation R, |
| 95 | StmtTy **Elts, unsigned NumElts) { |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 96 | if (NumElts > 1) |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 97 | return new CompoundStmt((Stmt**)Elts, NumElts); |
| 98 | else if (NumElts == 1) |
| 99 | return Elts[0]; // {stmt} -> stmt |
| 100 | else |
| 101 | return 0; // {} -> ; |
| 102 | } |
| 103 | |
Chris Lattner | 6c0ff13 | 2006-11-05 00:19:50 +0000 | [diff] [blame] | 104 | Action::StmtResult |
| 105 | ASTBuilder::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal, |
| 106 | SourceLocation DotDotDotLoc, ExprTy *RHSVal, |
| 107 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 108 | return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt); |
| 109 | } |
| 110 | |
| 111 | Action::StmtResult |
| 112 | ASTBuilder::ParseDefaultStmt(SourceLocation DefaultLoc, |
| 113 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 114 | return new DefaultStmt((Stmt*)SubStmt); |
| 115 | } |
| 116 | |
| 117 | Action::StmtResult |
| 118 | ASTBuilder::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
| 119 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 120 | return new LabelStmt(II, (Stmt*)SubStmt); |
| 121 | } |
| 122 | |
Chris Lattner | 5f84a06 | 2006-10-25 05:55:20 +0000 | [diff] [blame] | 123 | Action::StmtResult |
| 124 | ASTBuilder::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 125 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 126 | StmtTy *ElseVal) { |
| 127 | return new IfStmt((Expr*)CondVal, (Stmt*)ThenVal, (Stmt*)ElseVal); |
| 128 | } |
Chris Lattner | f2174b6 | 2006-11-04 20:59:27 +0000 | [diff] [blame] | 129 | Action::StmtResult |
| 130 | ASTBuilder::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, |
| 131 | StmtTy *Body) { |
| 132 | return new SwitchStmt((Expr*)Cond, (Stmt*)Body); |
| 133 | } |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 134 | |
| 135 | Action::StmtResult |
Chris Lattner | 85ed873 | 2006-11-04 20:40:44 +0000 | [diff] [blame] | 136 | ASTBuilder::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body){ |
| 137 | return new WhileStmt((Expr*)Cond, (Stmt*)Body); |
| 138 | } |
| 139 | |
| 140 | Action::StmtResult |
| 141 | ASTBuilder::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 142 | SourceLocation WhileLoc, ExprTy *Cond) { |
| 143 | return new DoStmt((Stmt*)Body, (Expr*)Cond); |
| 144 | } |
| 145 | |
Chris Lattner | 16976d3 | 2006-11-05 01:46:01 +0000 | [diff] [blame] | 146 | Action::StmtResult |
| 147 | ASTBuilder::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 148 | StmtTy *First, ExprTy *Second, ExprTy *Third, |
| 149 | SourceLocation RParenLoc, StmtTy *Body) { |
| 150 | return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | Action::StmtResult |
| 155 | ASTBuilder::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 156 | IdentifierInfo *LabelII) { |
| 157 | return new GotoStmt(LabelII); |
| 158 | } |
| 159 | Action::StmtResult |
| 160 | ASTBuilder::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 161 | ExprTy *DestExp) { |
| 162 | return new IndirectGotoStmt((Expr*)DestExp); |
| 163 | } |
| 164 | |
| 165 | Action::StmtResult |
| 166 | ASTBuilder::ParseContinueStmt(SourceLocation ContinueLoc) { |
| 167 | return new ContinueStmt(); |
| 168 | } |
| 169 | |
| 170 | Action::StmtResult |
| 171 | ASTBuilder::ParseBreakStmt(SourceLocation GotoLoc) { |
| 172 | return new BreakStmt(); |
| 173 | } |
| 174 | |
| 175 | |
Chris Lattner | 85ed873 | 2006-11-04 20:40:44 +0000 | [diff] [blame] | 176 | Action::StmtResult |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 177 | ASTBuilder::ParseReturnStmt(SourceLocation ReturnLoc, |
| 178 | ExprTy *RetValExp) { |
Chris Lattner | 6d9a685 | 2006-10-25 05:11:20 +0000 | [diff] [blame] | 179 | return new ReturnStmt((Expr*)RetValExp); |
Chris Lattner | e5cca06 | 2006-10-25 04:29:46 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | //===--------------------------------------------------------------------===// |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 183 | // Expression Parsing Callbacks. |
| 184 | //===--------------------------------------------------------------------===// |
| 185 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 186 | Action::ExprResult ASTBuilder::ParseSimplePrimaryExpr(SourceLocation Loc, |
| 187 | tok::TokenKind Kind) { |
| 188 | switch (Kind) { |
Chris Lattner | 879b9ad | 2006-08-24 04:53:44 +0000 | [diff] [blame] | 189 | default: |
| 190 | assert(0 && "Unknown simple primary expr!"); |
| 191 | case tok::identifier: { |
| 192 | // Could be enum-constant or decl. |
| 193 | //Tok.getIdentifierInfo() |
Chris Lattner | f42cce7 | 2006-10-25 04:09:21 +0000 | [diff] [blame] | 194 | return new DeclRefExpr(*(Decl*)0); |
Chris Lattner | 879b9ad | 2006-08-24 04:53:44 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | case tok::char_constant: // constant: character-constant |
| 198 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 199 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 200 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | 94b4ce3 | 2006-10-06 05:51:35 +0000 | [diff] [blame] | 201 | //assert(0 && "FIXME: Unimp so far!"); |
Chris Lattner | f42cce7 | 2006-10-25 04:09:21 +0000 | [diff] [blame] | 202 | return new DeclRefExpr(*(Decl*)0); |
Chris Lattner | 879b9ad | 2006-08-24 04:53:44 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 206 | Action::ExprResult ASTBuilder::ParseIntegerConstant(SourceLocation Loc) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 207 | return new IntegerConstant(); |
| 208 | } |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 209 | Action::ExprResult ASTBuilder::ParseFloatingConstant(SourceLocation Loc) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 210 | return new FloatingConstant(); |
| 211 | } |
| 212 | |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 213 | Action::ExprResult ASTBuilder::ParseParenExpr(SourceLocation L, |
| 214 | SourceLocation R, |
| 215 | ExprTy *Val) { |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 216 | return Val; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame^] | 219 | |
| 220 | |
| 221 | |
| 222 | /// HexDigitValue - Return the value of the specified hex digit, or -1 if it's |
| 223 | /// not valid. |
| 224 | static int HexDigitValue(char C) { |
| 225 | if (C >= '0' && C <= '9') return C-'0'; |
| 226 | if (C >= 'a' && C <= 'f') return C-'a'+10; |
| 227 | if (C >= 'A' && C <= 'F') return C-'A'+10; |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | /// ParseStringExpr - The specified tokens were lexed as pasted string |
| 232 | /// fragments (e.g. "foo" "bar" L"baz"). |
| 233 | |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 234 | /// ParseStringExpr - This accepts a string after semantic analysis. This string |
| 235 | /// may be the result of string concatenation ([C99 5.1.1.2, translation phase |
| 236 | /// #6]), so it may come from multiple tokens. |
| 237 | /// |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame^] | 238 | Action::ExprResult |
| 239 | ASTBuilder::ParseStringExpr(const LexerToken *StringToks, |
| 240 | unsigned NumStringToks) { |
| 241 | assert(NumStringToks && "Must have at least one string!"); |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 697e5d6 | 2006-11-09 06:32:27 +0000 | [diff] [blame^] | 243 | // Scan all of the string portions, remember the max individual token length, |
| 244 | // computing a bound on the concatenated string length, and see whether any |
| 245 | // piece is a wide-string. If any of the string portions is a wide-string |
| 246 | // literal, the result is a wide-string literal [C99 6.4.5p4]. |
| 247 | unsigned MaxTokenLength = StringToks[0].getLength(); |
| 248 | unsigned SizeBound = StringToks[0].getLength()-2; // -2 for "". |
| 249 | bool AnyWide = StringToks[0].getKind() == tok::wide_string_literal; |
| 250 | |
| 251 | // The common case is that there is only one string fragment. |
| 252 | for (unsigned i = 1; i != NumStringToks; ++i) { |
| 253 | // The string could be shorter than this if it needs cleaning, but this is a |
| 254 | // reasonable bound, which is all we need. |
| 255 | SizeBound += StringToks[i].getLength()-2; // -2 for "". |
| 256 | |
| 257 | // Remember maximum string piece length. |
| 258 | if (StringToks[i].getLength() > MaxTokenLength) |
| 259 | MaxTokenLength = StringToks[i].getLength(); |
| 260 | |
| 261 | // Remember if we see any wide strings. |
| 262 | AnyWide |= StringToks[i].getKind() == tok::wide_string_literal; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | // Include space for the null terminator. |
| 267 | ++SizeBound; |
| 268 | |
| 269 | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
| 270 | |
| 271 | // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not |
| 272 | // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true. |
| 273 | unsigned wchar_tByteWidth = ~0U; |
| 274 | if (AnyWide) |
| 275 | wchar_tByteWidth = |
| 276 | PP.getTargetInfo().getWCharWidth(StringToks[0].getLocation()); |
| 277 | |
| 278 | // The output buffer size needs to be large enough to hold wide characters. |
| 279 | // This is a worst-case assumption which basically corresponds to L"" "long". |
| 280 | if (AnyWide) |
| 281 | SizeBound *= wchar_tByteWidth; |
| 282 | |
| 283 | // Create a temporary buffer to hold the result string data. |
| 284 | SmallString<512> ResultBuf; |
| 285 | ResultBuf.resize(SizeBound); |
| 286 | |
| 287 | // Likewise, but for each string piece. |
| 288 | SmallString<512> TokenBuf; |
| 289 | TokenBuf.resize(MaxTokenLength); |
| 290 | |
| 291 | // Loop over all the strings, getting their spelling, and expanding them to |
| 292 | // wide strings as appropriate. |
| 293 | char *ResultPtr = &ResultBuf[0]; // Next byte to fill in. |
| 294 | |
| 295 | for (unsigned i = 0, e = NumStringToks; i != e; ++i) { |
| 296 | const char *ThisTokBuf = &TokenBuf[0]; |
| 297 | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
| 298 | // that ThisTokBuf points to a buffer that is big enough for the whole token |
| 299 | // and 'spelled' tokens can only shrink. |
| 300 | unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf); |
| 301 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote. |
| 302 | |
| 303 | // TODO: Input character set mapping support. |
| 304 | |
| 305 | // Skip L marker for wide strings. |
| 306 | if (ThisTokBuf[0] == 'L') ++ThisTokBuf; |
| 307 | |
| 308 | assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?"); |
| 309 | ++ThisTokBuf; |
| 310 | |
| 311 | while (ThisTokBuf != ThisTokEnd) { |
| 312 | // Is this a span of non-escape characters? |
| 313 | if (ThisTokBuf[0] != '\\') { |
| 314 | const char *InStart = ThisTokBuf; |
| 315 | do { |
| 316 | ++ThisTokBuf; |
| 317 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 318 | |
| 319 | // Copy the character span over. |
| 320 | unsigned Len = ThisTokBuf-InStart; |
| 321 | if (!AnyWide) { |
| 322 | memcpy(ResultPtr, InStart, Len); |
| 323 | ResultPtr += Len; |
| 324 | } else { |
| 325 | // Note: our internal rep of wide char tokens is always little-endian. |
| 326 | for (; Len; --Len, ++InStart) { |
| 327 | *ResultPtr++ = InStart[0]; |
| 328 | // Add zeros at the end. |
| 329 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 330 | *ResultPtr++ = 0; |
| 331 | } |
| 332 | } |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | // Otherwise, this is an escape character. Skip the '\' char. |
| 337 | ++ThisTokBuf; |
| 338 | |
| 339 | // We know that this character can't be off the end of the buffer, because |
| 340 | // that would have been \", which would not have been the end of string. |
| 341 | unsigned ResultChar = *ThisTokBuf++; |
| 342 | switch (ResultChar) { |
| 343 | // These map to themselves. |
| 344 | case '\\': case '\'': case '"': case '?': break; |
| 345 | |
| 346 | // These have fixed mappings. |
| 347 | case 'a': |
| 348 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 349 | ResultChar = 7; |
| 350 | break; |
| 351 | case 'b': |
| 352 | ResultChar = 8; |
| 353 | break; |
| 354 | case 'e': |
| 355 | PP.Diag(StringToks[i], diag::ext_nonstandard_escape, "e"); |
| 356 | ResultChar = 27; |
| 357 | break; |
| 358 | case 'f': |
| 359 | ResultChar = 12; |
| 360 | break; |
| 361 | case 'n': |
| 362 | ResultChar = 10; |
| 363 | break; |
| 364 | case 'r': |
| 365 | ResultChar = 13; |
| 366 | break; |
| 367 | case 't': |
| 368 | ResultChar = 9; |
| 369 | break; |
| 370 | case 'v': |
| 371 | ResultChar = 11; |
| 372 | break; |
| 373 | |
| 374 | //case 'u': case 'U': // FIXME: UCNs. |
| 375 | case 'x': // Hex escape. |
| 376 | if (ThisTokBuf == ThisTokEnd || |
| 377 | (ResultChar = HexDigitValue(*ThisTokBuf)) == ~0U) { |
| 378 | PP.Diag(StringToks[i], diag::err_hex_escape_no_digits); |
| 379 | ResultChar = 0; |
| 380 | break; |
| 381 | } |
| 382 | ++ThisTokBuf; // Consumed one hex digit. |
| 383 | |
| 384 | assert(0 && "hex escape: unimp!"); |
| 385 | break; |
| 386 | case '0': case '1': case '2': case '3': |
| 387 | case '4': case '5': case '6': case '7': |
| 388 | // Octal escapes. |
| 389 | assert(0 && "octal escape: unimp!"); |
| 390 | break; |
| 391 | |
| 392 | // Otherwise, these are not valid escapes. |
| 393 | case '(': case '{': case '[': case '%': |
| 394 | // GCC accepts these as extensions. We warn about them as such though. |
| 395 | if (!PP.getLangOptions().NoExtensions) { |
| 396 | PP.Diag(StringToks[i], diag::ext_nonstandard_escape, |
| 397 | std::string()+(char)ResultChar); |
| 398 | break; |
| 399 | } |
| 400 | // FALL THROUGH. |
| 401 | default: |
| 402 | if (isgraph(ThisTokBuf[0])) { |
| 403 | PP.Diag(StringToks[i], diag::ext_unknown_escape, |
| 404 | std::string()+(char)ResultChar); |
| 405 | } else { |
| 406 | PP.Diag(StringToks[i], diag::ext_unknown_escape, |
| 407 | "x"+utohexstr(ResultChar)); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Note: our internal rep of wide char tokens is always little-endian. |
| 412 | *ResultPtr++ = ResultChar & 0xFF; |
| 413 | |
| 414 | if (AnyWide) { |
| 415 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 416 | *ResultPtr++ = ResultChar >> i*8; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Add zero terminator. |
| 422 | *ResultPtr = 0; |
| 423 | if (AnyWide) { |
| 424 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 425 | *ResultPtr++ = 0; |
| 426 | } |
| 427 | |
| 428 | SmallVector<SourceLocation, 4> StringTokLocs; |
| 429 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 430 | StringTokLocs.push_back(StringToks[i].getLocation()); |
| 431 | |
| 432 | // FIXME: use factory. |
| 433 | |
| 434 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
| 435 | return new StringExpr(&ResultBuf[0], ResultPtr-&ResultBuf[0], AnyWide); |
| 436 | } |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 438 | // Unary Operators. 'Tok' is the token for the operator. |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 439 | Action::ExprResult ASTBuilder::ParseUnaryOp(SourceLocation OpLoc, |
| 440 | tok::TokenKind Op, |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 441 | ExprTy *Input) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 442 | UnaryOperator::Opcode Opc; |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 443 | switch (Op) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 444 | default: assert(0 && "Unknown unary op!"); |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 445 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 446 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 447 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 448 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 449 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 450 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 451 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 452 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 453 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 454 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 455 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 456 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 457 | case tok::ampamp: Opc = UnaryOperator::AddrLabel; break; |
Chris Lattner | c52b118 | 2006-10-25 05:45:55 +0000 | [diff] [blame] | 458 | case tok::kw___extension__: |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 459 | return Input; |
| 460 | //Opc = UnaryOperator::Extension; |
| 461 | //break; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 464 | return new UnaryOperator((Expr*)Input, Opc); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Chris Lattner | 26da730 | 2006-08-24 06:49:19 +0000 | [diff] [blame] | 467 | Action::ExprResult ASTBuilder:: |
| 468 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
| 469 | SourceLocation LParenLoc, TypeTy *Ty, |
| 470 | SourceLocation RParenLoc) { |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 471 | return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty); |
Chris Lattner | 26da730 | 2006-08-24 06:49:19 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 475 | Action::ExprResult ASTBuilder::ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 476 | tok::TokenKind Kind, |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 477 | ExprTy *Input) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 478 | UnaryOperator::Opcode Opc; |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 479 | switch (Kind) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 480 | default: assert(0 && "Unknown unary op!"); |
| 481 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 482 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 483 | } |
| 484 | |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 485 | return new UnaryOperator((Expr*)Input, Opc); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 488 | Action::ExprResult ASTBuilder:: |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 489 | ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 490 | ExprTy *Idx, SourceLocation RLoc) { |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 491 | return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 494 | Action::ExprResult ASTBuilder:: |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 495 | ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
| 496 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 497 | IdentifierInfo &Member) { |
Chris Lattner | 6f3a117 | 2006-08-24 05:19:28 +0000 | [diff] [blame] | 498 | Decl *MemberDecl = 0; |
| 499 | // TODO: Look up MemberDecl. |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 500 | return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 504 | /// This provides the location of the left/right parens and a list of comma |
| 505 | /// locations. |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 506 | Action::ExprResult ASTBuilder:: |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 507 | ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc, |
| 508 | ExprTy **Args, unsigned NumArgs, |
| 509 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 510 | return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs); |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 513 | Action::ExprResult ASTBuilder:: |
| 514 | ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 515 | SourceLocation RParenLoc, ExprTy *Op) { |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 516 | return new CastExpr((Type*)Ty, (Expr*)Op); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 521 | // Binary Operators. 'Tok' is the token for the operator. |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 522 | Action::ExprResult ASTBuilder::ParseBinOp(SourceLocation TokLoc, |
| 523 | tok::TokenKind Kind, ExprTy *LHS, |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 524 | ExprTy *RHS) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 525 | BinaryOperator::Opcode Opc; |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 526 | switch (Kind) { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 527 | default: assert(0 && "Unknown binop!"); |
| 528 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 529 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 530 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 531 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 532 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 533 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 534 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 535 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 536 | case tok::less: Opc = BinaryOperator::LT; break; |
| 537 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 538 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 539 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 540 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 541 | case tok::amp: Opc = BinaryOperator::And; break; |
| 542 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 543 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 544 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 545 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 546 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 547 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 548 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 549 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 550 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 551 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 552 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 553 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 554 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 555 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 556 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 557 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 558 | } |
| 559 | |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 560 | return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 564 | /// in the case of a the GNU conditional expr extension. |
Chris Lattner | 98286a4 | 2006-08-24 05:02:11 +0000 | [diff] [blame] | 565 | Action::ExprResult ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc, |
| 566 | SourceLocation ColonLoc, |
| 567 | ExprTy *Cond, ExprTy *LHS, |
| 568 | ExprTy *RHS) { |
Chris Lattner | 72b7d39 | 2006-11-04 06:37:16 +0000 | [diff] [blame] | 569 | return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 570 | } |
| 571 | |