Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 1 | //===--- Builder.cpp - AST Builder Implementation -------------------------===// |
| 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 | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Action.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" |
| 18 | #include "clang/Parse/Scope.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 19 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexerToken.h" |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Visibility.h" |
| 22 | using namespace llvm; |
| 23 | using namespace clang; |
| 24 | |
| 25 | /// ASTBuilder |
| 26 | namespace { |
| 27 | class VISIBILITY_HIDDEN ASTBuilder : public Action { |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 28 | /// FullLocInfo - If this is true, the ASTBuilder constructs AST Nodes that |
| 29 | /// capture maximal location information for each source-language construct. |
| 30 | bool FullLocInfo; |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 31 | public: |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 32 | ASTBuilder(bool fullLocInfo) : FullLocInfo(fullLocInfo) {} |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 33 | //===--------------------------------------------------------------------===// |
| 34 | // Symbol table tracking callbacks. |
| 35 | // |
| 36 | virtual bool isTypedefName(const IdentifierInfo &II, Scope *S) const; |
| 37 | virtual void ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D, |
| 38 | ExprTy *Init); |
| 39 | virtual void PopScope(SourceLocation Loc, Scope *S); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 40 | |
| 41 | //===--------------------------------------------------------------------===// |
| 42 | // Expression Parsing Callbacks. |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame^] | 43 | |
| 44 | // Primary Expressions. |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 45 | virtual ExprTy *ParseIntegerConstant(const LexerToken &Tok); |
| 46 | virtual ExprTy *ParseFloatingConstant(const LexerToken &Tok); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame^] | 47 | virtual ExprTy *ParseParenExpr(SourceLocation L, SourceLocation R, |
| 48 | ExprTy *Val); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame^] | 50 | // Binary/Unary Operators. 'Tok' is the token for the operator. |
| 51 | virtual ExprTy *ParseUnaryOp(const LexerToken &Tok, ExprTy *Input); |
| 52 | virtual ExprTy *ParsePostfixUnaryOp(const LexerToken &Tok, ExprTy *Input); |
| 53 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 54 | virtual ExprTy *ParseBinOp(const LexerToken &Tok, ExprTy *LHS, ExprTy *RHS); |
| 55 | |
| 56 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 57 | /// in the case of a the GNU conditional expr extension. |
| 58 | virtual ExprTy *ParseConditionalOp(SourceLocation QuestionLoc, |
| 59 | SourceLocation ColonLoc, |
| 60 | ExprTy *Cond, ExprTy *LHS, ExprTy *RHS); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 61 | }; |
| 62 | } // end anonymous namespace |
| 63 | |
| 64 | |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | // Symbol table tracking callbacks. |
| 67 | //===----------------------------------------------------------------------===// |
| 68 | |
| 69 | bool ASTBuilder::isTypedefName(const IdentifierInfo &II, Scope *S) const { |
| 70 | Decl *D = II.getFETokenInfo<Decl>(); |
| 71 | return D != 0 && D->getDeclSpecs().StorageClassSpec == DeclSpec::SCS_typedef; |
| 72 | } |
| 73 | |
| 74 | void ASTBuilder::ParseDeclarator(SourceLocation Loc, Scope *S, Declarator &D, |
| 75 | ExprTy *Init) { |
| 76 | IdentifierInfo *II = D.getIdentifier(); |
| 77 | Decl *PrevDecl = II ? II->getFETokenInfo<Decl>() : 0; |
| 78 | |
| 79 | Decl *New = new Decl(II, D.getDeclSpec(), Loc, PrevDecl); |
| 80 | |
| 81 | // If this has an identifier, add it to the scope stack. |
| 82 | if (II) { |
| 83 | // If PrevDecl includes conflicting name here, emit a diagnostic. |
| 84 | II->setFETokenInfo(New); |
| 85 | S->AddDecl(II); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void ASTBuilder::PopScope(SourceLocation Loc, Scope *S) { |
| 90 | for (Scope::decl_iterator I = S->decl_begin(), E = S->decl_end(); |
| 91 | I != E; ++I) { |
| 92 | IdentifierInfo &II = *static_cast<IdentifierInfo*>(*I); |
| 93 | Decl *D = II.getFETokenInfo<Decl>(); |
| 94 | assert(D && "This decl didn't get pushed??"); |
| 95 | |
| 96 | Decl *Next = D->getNext(); |
| 97 | |
| 98 | // FIXME: Push the decl on the parent function list if in a function. |
| 99 | delete D; |
| 100 | |
| 101 | II.setFETokenInfo(Next); |
| 102 | } |
| 103 | } |
| 104 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 105 | //===--------------------------------------------------------------------===// |
| 106 | // Expression Parsing Callbacks. |
| 107 | //===--------------------------------------------------------------------===// |
| 108 | |
| 109 | ASTBuilder::ExprTy *ASTBuilder::ParseIntegerConstant(const LexerToken &Tok) { |
| 110 | return new IntegerConstant(); |
| 111 | } |
| 112 | ASTBuilder::ExprTy *ASTBuilder::ParseFloatingConstant(const LexerToken &Tok) { |
| 113 | return new FloatingConstant(); |
| 114 | } |
| 115 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame^] | 116 | ASTBuilder::ExprTy *ASTBuilder::ParseParenExpr(SourceLocation L, |
| 117 | SourceLocation R, |
| 118 | ExprTy *Val) { |
| 119 | // FIXME: This is obviously just for testing. |
| 120 | ((Expr*)Val)->dump(); |
| 121 | if (!FullLocInfo) return Val; |
| 122 | |
| 123 | return new ParenExpr(L, R, (Expr*)Val); |
| 124 | } |
| 125 | |
| 126 | // Unary Operators. 'Tok' is the token for the operator. |
| 127 | ASTBuilder::ExprTy *ASTBuilder::ParseUnaryOp(const LexerToken &Tok, |
| 128 | ExprTy *Input) { |
| 129 | UnaryOperator::Opcode Opc; |
| 130 | switch (Tok.getKind()) { |
| 131 | default: assert(0 && "Unknown unary op!"); |
| 132 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 133 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 134 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 135 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 136 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 137 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 138 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 139 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 140 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 141 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 142 | } |
| 143 | |
| 144 | if (!FullLocInfo) |
| 145 | return new UnaryOperator((Expr*)Input, Opc); |
| 146 | else |
| 147 | return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc); |
| 148 | } |
| 149 | |
| 150 | ASTBuilder::ExprTy *ASTBuilder::ParsePostfixUnaryOp(const LexerToken &Tok, |
| 151 | ExprTy *Input) { |
| 152 | UnaryOperator::Opcode Opc; |
| 153 | switch (Tok.getKind()) { |
| 154 | default: assert(0 && "Unknown unary op!"); |
| 155 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 156 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 157 | } |
| 158 | |
| 159 | if (!FullLocInfo) |
| 160 | return new UnaryOperator((Expr*)Input, Opc); |
| 161 | else |
| 162 | return new UnaryOperatorLOC(Tok.getLocation(), (Expr*)Input, Opc); |
| 163 | } |
| 164 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 165 | // Binary Operators. 'Tok' is the token for the operator. |
| 166 | ASTBuilder::ExprTy *ASTBuilder::ParseBinOp(const LexerToken &Tok, ExprTy *LHS, |
| 167 | ExprTy *RHS) { |
| 168 | BinaryOperator::Opcode Opc; |
| 169 | switch (Tok.getKind()) { |
| 170 | default: assert(0 && "Unknown binop!"); |
| 171 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 172 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 173 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 174 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 175 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 176 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 177 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 178 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 179 | case tok::less: Opc = BinaryOperator::LT; break; |
| 180 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 181 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 182 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 183 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 184 | case tok::amp: Opc = BinaryOperator::And; break; |
| 185 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 186 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 187 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 188 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 189 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 190 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 191 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 192 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 193 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 194 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 195 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 196 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 197 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 198 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 199 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 200 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 201 | } |
| 202 | |
| 203 | if (!FullLocInfo) |
| 204 | return new BinaryOperator((Expr*)LHS, (Expr*)RHS, Opc); |
| 205 | else |
| 206 | return new BinaryOperatorLOC((Expr*)LHS, Tok.getLocation(), (Expr*)RHS,Opc); |
| 207 | } |
| 208 | |
| 209 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 210 | /// in the case of a the GNU conditional expr extension. |
| 211 | ASTBuilder::ExprTy *ASTBuilder::ParseConditionalOp(SourceLocation QuestionLoc, |
| 212 | SourceLocation ColonLoc, |
| 213 | ExprTy *Cond, ExprTy *LHS, |
| 214 | ExprTy *RHS) { |
| 215 | if (!FullLocInfo) |
| 216 | return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS); |
| 217 | else |
| 218 | return new ConditionalOperatorLOC((Expr*)Cond, QuestionLoc, (Expr*)LHS, |
| 219 | ColonLoc, (Expr*)RHS); |
| 220 | } |
| 221 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 222 | |
| 223 | /// Interface to the Builder.cpp file. |
| 224 | /// |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 225 | Action *CreateASTBuilderActions(bool FullLocInfo) { |
| 226 | return new ASTBuilder(FullLocInfo); |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | |
| 230 | |