123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*##############################################################################
- Copyright (C) 2011 HPCC Systems.
- All rights reserved. This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ############################################################################## */
- #ifndef HQLWCPP_IPP
- #define HQLWCPP_IPP
- class HQLCPP_API CppWriterTemplate : public CInterface, public ITemplateExpander
- {
- public:
- CppWriterTemplate();
- ~CppWriterTemplate();
- IMPLEMENT_IINTERFACE
- virtual void generate(ISectionWriter & writer, unsigned pass, IProperties * properties = NULL);
- bool loadTemplate(const char * filename, const char *dir);
- void outputQuoted(ISectionWriter & writer, size32_t len, const char * str)
- {
- writer.noteLines(memcount(len, str, '\n'));
- outStream->write(len, str);
- }
- void setOutput(IFile * _output)
- {
- Owned<IFileIO> io = _output->open(IFOcreate);
- if (!io)
- throwError1(HQLERR_CouldNotCreateOutputX, _output->queryFilename());
- out.set(_output);
- outStream.setown(createIOStream(io));
- }
- private:
- enum TplSectionType { TplEmbed, TplExpand, TplCondition, TplEndCondition };
- struct CppTemplateSection : public CInterface
- {
- TplSectionType type;
- _ATOM id;
- const char * position;
- unsigned len;
- unsigned indent;
- };
- protected:
- char * text;
- unsigned len;
- CIArray sections;
- Owned<IFile> out;
- Owned<IIOStream> outStream;
- };
- class HQLCPP_API HqlCppWriter
- {
- public:
- HqlCppWriter(CompilerType _compiler);
- HqlCppWriter(StringBuffer & _out, CompilerType _compiler);
- StringBuffer & generateExprCpp(IHqlExpression * expr);
- bool generateFunctionPrototype(IHqlExpression * funcdef);
- void generateFunctionReturnType(StringBuffer & params, ITypeInfo * retType, IHqlExpression * attrs);
- void generateStatementsForPass(HqlStmts & stmts, unsigned indent, unsigned pass);
- void generateType(ITypeInfo * type, const char * name);
- void noteLines(size32_t count) { outputLineNum += count; }
- void setOutput(IFile * out, IIOStream * outStream);
- protected:
- void flush();
- void generate(HqlStmtArray & stmts);
- void generateChildren(IHqlStmt * stmt, bool addBraces);
- StringBuffer & generateChildExpr(StringBuffer & out, IHqlExpression * expr, unsigned childIndex);
- bool generateFunctionPrototype(IHqlExpression * funcdef, const char * name);
- void generateInitializer(IHqlExpression * expr);
- void generateParamCpp(IHqlExpression * expr);
- void generateSimpleAssign(IHqlExpression * target, IHqlExpression * source);
- void generateStmt(IHqlStmt * stmt);
- void generateStmtAssign(IHqlStmt * assign);
- void generateStmtAssignModify(IHqlStmt * assign);
- void generateStmtCase(IHqlStmt * stmt);
- void generateStmtDeclare(IHqlStmt * declare);
- void generateStmtFilter(IHqlStmt * stmt);
- void generateStmtFunction(IHqlStmt * stmt);
- void generateStmtLine(IHqlStmt * stmt);
- void generateStmtLoop(IHqlStmt * stmt);
- void generateStmtSwitch(IHqlStmt * stmt);
- void generateStmtForPass(IHqlStmt * stmt, unsigned pass);
- //Wrappers around recursive calls.
- StringBuffer & generateExprCpp(StringBuffer & out, IHqlExpression * expr);
- void generateType(StringBuffer & result, ITypeInfo * type, const char * name);
- StringBuffer & indent();
- void indent(int delta) { curIndent += delta; }
- StringBuffer & newline();
- StringBuffer & queryBreakLine();
- StringBuffer & queryIndent();
- void queryNewline();
- StringBuffer & generateChildExpr(IHqlExpression * expr, unsigned childIndex);
- StringBuffer & generateCommaChildren(IHqlExpression * expr);
- void generateOrderExpr(IHqlExpression * left, IHqlExpression * right);
- StringBuffer & generateExprAsChar(IHqlExpression * expr);
- protected:
- Linked<IFile> targetFile;
- Linked<IIOStream> target;
- StringBuffer & out;
- StringBuffer defaultOut;
- unsigned curIndent;
- unsigned startOffset;
- unsigned outputLineNum;
- CompilerType compiler;
- };
- class HQLCPP_API HqlCppSectionWriter : public CInterface, implements ISectionWriter
- {
- public:
- HqlCppSectionWriter(IHqlCppInstance & _instance, CompilerType _compiler) : instance(_instance), writer(_compiler)
- {
- }
- IMPLEMENT_IINTERFACE
- virtual void generateSection(unsigned indent, _ATOM section, unsigned pass);
- virtual void noteLines(size32_t count) { writer.noteLines(count); }
- virtual void setOutput(IFile * out, IIOStream * outStream) { writer.setOutput(out, outStream); }
- IHqlCppInstance & instance;
- HqlCppWriter writer;
- };
- #endif
|