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