Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame^] | 1 | #include "slang_rs_context.hpp" |
| 2 | #include "slang_rs_pragma_handler.hpp" |
| 3 | |
| 4 | #include "clang/Lex/Preprocessor.h" /* for class Preprocessor */ |
| 5 | #include "clang/Lex/Token.h" /* for class Token */ |
| 6 | #include "clang/Basic/TokenKinds.h" /* for class Token */ |
| 7 | |
| 8 | #include "clang/Basic/IdentifierTable.h" /* for class IdentifierInfo */ |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | using namespace clang; |
| 13 | using namespace slang; |
| 14 | |
| 15 | class RSExportVarPragmaHandler : public RSPragmaHandler { |
| 16 | private: |
| 17 | void handleItem(const std::string& Item) { |
| 18 | mContext->addExportVar(Item); |
| 19 | } |
| 20 | |
| 21 | public: |
| 22 | RSExportVarPragmaHandler(IdentifierInfo* II, RSContext* Context) : RSPragmaHandler(II, Context) { return; } |
| 23 | |
| 24 | void HandlePragma(Preprocessor& PP, Token& FirstToken) { |
| 25 | this->handleItemListPragma(PP, FirstToken); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | class RSExportFuncPragmaHandler : public RSPragmaHandler { |
| 30 | private: |
| 31 | void handleItem(const std::string& Item) { |
| 32 | mContext->addExportFunc(Item); |
| 33 | } |
| 34 | |
| 35 | public: |
| 36 | RSExportFuncPragmaHandler(IdentifierInfo* II, RSContext* Context) : RSPragmaHandler(II, Context) { return; } |
| 37 | |
| 38 | void HandlePragma(Preprocessor& PP, Token& FirstToken) { |
| 39 | this->handleItemListPragma(PP, FirstToken); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | class RSExportTypePragmaHandler : public RSPragmaHandler { |
| 44 | private: |
| 45 | void handleItem(const std::string& Item) { |
| 46 | mContext->addExportType(Item); |
| 47 | } |
| 48 | |
| 49 | public: |
| 50 | RSExportTypePragmaHandler(IdentifierInfo* II, RSContext* Context) : RSPragmaHandler(II, Context) { return; } |
| 51 | |
| 52 | void HandlePragma(Preprocessor& PP, Token& FirstToken) { |
| 53 | this->handleItemListPragma(PP, FirstToken); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | } /* anonymous namespace */ |
| 58 | |
| 59 | namespace slang { |
| 60 | |
| 61 | RSPragmaHandler* RSPragmaHandler::CreatePragmaExportVarHandler(RSContext* Context) { |
| 62 | IdentifierInfo* II = Context->getPreprocessor()->getIdentifierInfo("export_var"); |
| 63 | if(II != NULL) |
| 64 | return new RSExportVarPragmaHandler(II, Context); |
| 65 | else |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | RSPragmaHandler* RSPragmaHandler::CreatePragmaExportFuncHandler(RSContext* Context) { |
| 70 | IdentifierInfo* II = Context->getPreprocessor()->getIdentifierInfo("export_func"); |
| 71 | if(II != NULL) |
| 72 | return new RSExportFuncPragmaHandler(II, Context); |
| 73 | else |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | RSPragmaHandler* RSPragmaHandler::CreatePragmaExportTypeHandler(RSContext* Context) { |
| 78 | IdentifierInfo* II = Context->getPreprocessor()->getIdentifierInfo("export_type"); |
| 79 | if(II != NULL) |
| 80 | return new RSExportTypePragmaHandler(II, Context); |
| 81 | else |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | void RSPragmaHandler::handleItemListPragma(Preprocessor& PP, Token& FirstToken) { |
| 86 | Token& PragmaToken = FirstToken; |
| 87 | |
| 88 | /* Skip first token, like "export_var" */ |
| 89 | PP.LexUnexpandedToken(PragmaToken); |
| 90 | |
| 91 | /* Now, the current token must be tok::lpara */ |
| 92 | if(PragmaToken.isNot(tok::l_paren)) |
| 93 | return; |
| 94 | |
| 95 | while(PragmaToken.isNot(tok::eom)) { |
| 96 | /* Lex variable name */ |
| 97 | PP.LexUnexpandedToken(PragmaToken); |
| 98 | if(PragmaToken.is(tok::identifier)) |
| 99 | this->handleItem( PP.getSpelling(PragmaToken) ); |
| 100 | else |
| 101 | break; |
| 102 | |
| 103 | assert(PragmaToken.isNot(tok::eom)); |
| 104 | |
| 105 | PP.LexUnexpandedToken(PragmaToken); |
| 106 | |
| 107 | if(PragmaToken.isNot(tok::comma)) |
| 108 | break; |
| 109 | } |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | } /* namespace slang */ |
| 114 | |