From: Hwankyu Jhun Date: Mon, 20 Nov 2017 10:48:06 +0000 (+0900) Subject: Add structure for custom type X-Git-Tag: accepted/tizen/unified/20180302.061550~51 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=407968ec610e6a8edda53d922abed616e8184b5f;p=platform%2Fcore%2Fappfw%2Ftidl.git Add structure for custom type Change-Id: I2848a9e44d9a5a1151b44b5d8c30c73f0aa3d4c1 Signed-off-by: Hwankyu Jhun --- diff --git a/idlc/CMakeLists.txt b/idlc/CMakeLists.txt index 7e93fd8..5341487 100644 --- a/idlc/CMakeLists.txt +++ b/idlc/CMakeLists.txt @@ -19,6 +19,9 @@ SET(SOURCES src/interface.cc src/type.cc src/parameter.cc + src/attribute.cc + src/structure.cc + src/block.cc src/generator.cc src/proxy_gen.cc src/stub_gen.cc diff --git a/idlc/inc/attribute.h b/idlc/inc/attribute.h new file mode 100644 index 0000000..f66ef28 --- /dev/null +++ b/idlc/inc/attribute.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_INC_ATTRIBUTE_H_ +#define IDLC_INC_ATTRIBUTE_H_ + +#include +#include +#include + +#include "type.h" +#include "parameter.h" + +namespace tidl { + +class Attribute { + public: + Attribute(const std::string& id, BaseType* type); + + const std::string& GetID() const; + const BaseType& GetType() const; + + private: + std::string id_; + std::unique_ptr type_; +}; + +class Attributes { + public: + void Add(Attribute* decl); + const std::list>& GetAttrs() const; + + private: + std::list> attrs_; +}; + +} // namespace tidl + +#endif // IDLC_INC_ATTRIBUTE_H_ diff --git a/idlc/inc/Interface.h b/idlc/inc/block.h similarity index 72% rename from idlc/inc/Interface.h rename to idlc/inc/block.h index 17b9f8a..48ce0d9 100644 --- a/idlc/inc/Interface.h +++ b/idlc/inc/block.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef IDLC_INC_INTERFACE_H_ -#define IDLC_INC_INTERFACE_H_ +#ifndef IDLC_INC_BLOCK_H_ +#define IDLC_INC_BLOCK_H_ #include #include @@ -24,19 +24,25 @@ namespace tidl { -class Interface { +class Block { public: - Interface(const std::string& id, Declarations* decls); - + enum Type { + TYPE_INTERFACE = 0, + TYPE_STRUCTURE, + }; + + Block(const std::string& id, Block::Type type); + virtual ~Block() = default; + const std::string& GetID() const; - const Declarations& GetDeclarations() const; + const Block::Type& GetType() const; private: std::string id_; - std::unique_ptr decls_; + Block::Type type_; }; } // namespace tidl -#endif // IDLC_INC_INTERFACE_H_ +#endif // IDLC_INC_BLOCK_H_ diff --git a/idlc/inc/document.h b/idlc/inc/document.h index d7750dd..56893db 100644 --- a/idlc/inc/document.h +++ b/idlc/inc/document.h @@ -26,11 +26,13 @@ namespace tidl { class Document { public: - void AddInterface(Interface* interface); - const std::list>& GetInterfaces() const; + Document(); + + void AddBlock(Block* block); + const std::list>& GetBlocks() const; private: - std::list> interfaces_; + std::list> blocks_; }; } // namespace tidl diff --git a/idlc/inc/generator.h b/idlc/inc/generator.h index 05e4722..7d6c81f 100644 --- a/idlc/inc/generator.h +++ b/idlc/inc/generator.h @@ -37,7 +37,12 @@ class Generator { virtual void OnInterfaceBegin(const std::string& id) = 0; virtual void OnInterfaceEnd(const std::string& id) = 0; virtual void OnDeclartionGen(std::ofstream& stream, const std::string& id, - const Parameters& args, const BaseType& ret) = 0; + const Parameters& args, const BaseType& ret) = 0; + virtual void OnStructureBegin(std::ofstream& stream, + const std::string& id) = 0; + virtual void OnStructureEnd(std::ofstream& stream, const std::string& id) = 0; + virtual void OnAttributeGen(std::ofstream& stream, const std::string& id, + const BaseType& type) = 0; virtual void OnInitGen(std::ofstream& stream) = 0; virtual void OnFiniGen(std::ofstream& stream) = 0; diff --git a/idlc/inc/interface.h b/idlc/inc/interface.h index 17b9f8a..510f1fe 100644 --- a/idlc/inc/interface.h +++ b/idlc/inc/interface.h @@ -20,19 +20,18 @@ #include #include +#include "block.h" #include "declaration.h" namespace tidl { -class Interface { +class Interface : public Block { public: Interface(const std::string& id, Declarations* decls); - - const std::string& GetID() const; + const Declarations& GetDeclarations() const; private: - std::string id_; std::unique_ptr decls_; }; diff --git a/idlc/inc/parser.h b/idlc/inc/parser.h index f9117a7..e1ef422 100644 --- a/idlc/inc/parser.h +++ b/idlc/inc/parser.h @@ -32,8 +32,8 @@ class Parser { void* Scanner() const { return scanner_; } bool Parse(const std::string& input); bool ParseFromFile(const std::string& path); - void AttachDoc(Document* doc); - std::shared_ptr DetachDoc(); + void SetDoc(Document* doc); + std::shared_ptr GetDoc(); private: void* scanner_; diff --git a/idlc/inc/proxy_gen.h b/idlc/inc/proxy_gen.h index 7a1d315..ac59f1c 100644 --- a/idlc/inc/proxy_gen.h +++ b/idlc/inc/proxy_gen.h @@ -30,11 +30,15 @@ class ProxyGen : public Generator { virtual ~ProxyGen() = default; void OnDeclartionGen(std::ofstream& stream, const std::string& id, - const Parameters& args, const BaseType& ret) override; + const Parameters& args, const BaseType& ret) override; void OnInitGen(std::ofstream& stream) override; void OnFiniGen(std::ofstream& stream) override; void OnInterfaceBegin(const std::string& id) override; void OnInterfaceEnd(const std::string& id) override; + void OnAttributeGen(std::ofstream& stream, const std::string& id, + const BaseType& type) override; + void OnStructureBegin(std::ofstream& stream, const std::string& id) override; + void OnStructureEnd(std::ofstream& stream, const std::string& id) override; private: std::string ConvertTypeToCType(const BaseType& type); diff --git a/idlc/inc/structure.h b/idlc/inc/structure.h new file mode 100644 index 0000000..4022bb9 --- /dev/null +++ b/idlc/inc/structure.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_INC_STRUCTURE_H_ +#define IDLC_INC_STRUCTURE_H_ + +#include +#include + +#include "block.h" +#include "attribute.h" + +namespace tidl { + +class Structure : public Block { + public: + Structure(const std::string& id, Attributes* attrs); + + const Attributes& GetAttributes() const; + + private: + std::unique_ptr attrs_; +}; + +} // namespace tidl + +#endif // IDLC_INC_STRUCTURE_H_ + diff --git a/idlc/inc/stub_gen.h b/idlc/inc/stub_gen.h index 331467e..bf54bc1 100644 --- a/idlc/inc/stub_gen.h +++ b/idlc/inc/stub_gen.h @@ -35,6 +35,10 @@ class StubGen : public Generator { void OnFiniGen(std::ofstream& stream) override; void OnInterfaceBegin(const std::string& id) override; void OnInterfaceEnd(const std::string& id) override; + void OnAttributeGen(std::ofstream& stream, const std::string& id, + const BaseType& type) override; + void OnStructureBegin(std::ofstream& stream, const std::string& id) override; + void OnStructureEnd(std::ofstream& stream, const std::string& id) override; }; } // namespace tidl diff --git a/idlc/src/attribute.cc b/idlc/src/attribute.cc new file mode 100644 index 0000000..9083e15 --- /dev/null +++ b/idlc/src/attribute.cc @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "attribute.h" +#include "type.h" + +namespace tidl { + +Attribute::Attribute(const std::string& id, BaseType* type) + : id_(id), type_(type) {} + +const std::string& Attribute::GetID() const { + return id_; +} + +const BaseType& Attribute::GetType() const { + return *type_; +} + +void Attributes::Add(Attribute* attr) { + attrs_.emplace_back(attr); +} + +const std::list>& Attributes::GetAttrs() const { + return attrs_; +} + +} // namespace tidl diff --git a/idlc/src/block.cc b/idlc/src/block.cc new file mode 100644 index 0000000..4557795 --- /dev/null +++ b/idlc/src/block.cc @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "type.h" +#include "block.h" + +namespace tidl { + +Block::Block(const std::string& id, Block::Type type) + : id_(id), type_(type) { +} + +const std::string& Block::GetID() const { + return id_; +} + +const Block::Type& Block::GetType() const { + return type_; +} + +} // namespace tidl diff --git a/idlc/src/document.cc b/idlc/src/document.cc index c8cf5ff..2fd0372 100644 --- a/idlc/src/document.cc +++ b/idlc/src/document.cc @@ -19,12 +19,14 @@ namespace tidl { -void Document::AddInterface(Interface* interface) { - interfaces_.emplace_back(interface); +Document::Document() {} + +void Document::AddBlock(Block* block) { + blocks_.emplace_back(block); } -const std::list>& Document::GetInterfaces() const { - return interfaces_; +const std::list>& Document::GetBlocks() const { + return blocks_; } } // namespace tidl diff --git a/idlc/src/generator.cc b/idlc/src/generator.cc index 549fc09..6274910 100644 --- a/idlc/src/generator.cc +++ b/idlc/src/generator.cc @@ -15,7 +15,9 @@ */ #include "generator.h" +#include "block.h" #include "interface.h" +#include "structure.h" #include "declaration.h" namespace tidl { @@ -28,13 +30,23 @@ void Generator::Run(const std::string& file_name) { out_file_.open(FileName); OnInitGen(out_file_); - for (auto& interface : doc_->GetInterfaces()) { - OnInterfaceBegin(interface->GetID()); - for (auto& decl : interface->GetDeclarations().GetDecls()) { - OnDeclartionGen(out_file_, decl->GetID(), decl->GetParameters(), - decl->GetType()); + for (auto& block : doc_->GetBlocks()) { + if (block->GetType() == Block::TYPE_INTERFACE) { + Interface* interface = static_cast(block.get()); + OnInterfaceBegin(interface->GetID()); + for (auto& decl : interface->GetDeclarations().GetDecls()) { + OnDeclartionGen(out_file_, decl->GetID(), decl->GetParameters(), + decl->GetType()); + } + OnInterfaceEnd(interface->GetID()); + } else if (block->GetType() == Block::TYPE_STRUCTURE) { + Structure* structure = static_cast(block.get()); + OnStructureBegin(out_file_, structure->GetID()); + for (auto& attr : structure->GetAttributes().GetAttrs()) { + OnAttributeGen(out_file_, attr->GetID(), attr->GetType()); + } + OnStructureEnd(out_file_, structure->GetID()); } - OnInterfaceEnd(interface->GetID()); } OnFiniGen(out_file_); diff --git a/idlc/src/interface.cc b/idlc/src/interface.cc index 85fb37d..77069f9 100644 --- a/idlc/src/interface.cc +++ b/idlc/src/interface.cc @@ -16,15 +16,12 @@ #include "declaration.h" #include "interface.h" +#include "block.h" namespace tidl { Interface::Interface(const std::string& id, Declarations* decls) - : id_(id), decls_(decls) { -} - -const std::string& Interface::GetID() const { - return id_; + : Block::Block(id, Block::TYPE_INTERFACE), decls_(decls) { } const Declarations& Interface::GetDeclarations() const { diff --git a/idlc/src/main.cc b/idlc/src/main.cc index 0a62a0b..96ac258 100644 --- a/idlc/src/main.cc +++ b/idlc/src/main.cc @@ -63,10 +63,10 @@ int main(int argc, char** argv) { ps.ParseFromFile(path); if (opt == ::Option::PROXY) { - tidl::ProxyGen proxy(ps.DetachDoc()); + tidl::ProxyGen proxy(ps.GetDoc()); proxy.Run(argv[3]); } else { - tidl::StubGen stub(ps.DetachDoc()); + tidl::StubGen stub(ps.GetDoc()); stub.Run(argv[3]); } diff --git a/idlc/src/parser.cc b/idlc/src/parser.cc index 3432c39..934b1da 100644 --- a/idlc/src/parser.cc +++ b/idlc/src/parser.cc @@ -22,6 +22,8 @@ #include "declaration.h" #include "parameter.h" #include "type.h" +#include "attribute.h" +#include "structure.h" #include "../tidlc_y.hpp" struct yy_buffer_state; @@ -39,11 +41,11 @@ Parser::~Parser() { yylex_destroy(scanner_); } -void Parser::AttachDoc(Document* doc) { +void Parser::SetDoc(Document* doc) { doc_ = std::shared_ptr(doc); } -std::shared_ptr Parser::DetachDoc() { +std::shared_ptr Parser::GetDoc() { return doc_; } diff --git a/idlc/src/proxy_gen.cc b/idlc/src/proxy_gen.cc index c1fd16f..2c0919f 100644 --- a/idlc/src/proxy_gen.cc +++ b/idlc/src/proxy_gen.cc @@ -63,4 +63,18 @@ void ProxyGen::OnInterfaceEnd(const std::string& id) { interface_name_ = ""; } +void ProxyGen::OnAttributeGen(std::ofstream& stream, const std::string& id, + const BaseType& type) { + stream << "\t" << type.GetFullName() << " " << id << ";" << std::endl; +} + +void ProxyGen::OnStructureBegin(std::ofstream& stream, const std::string& id) { + stream << "typedef struct" << " " << id + "_t" << " " << "{" << std::endl; +} + +void ProxyGen::OnStructureEnd(std::ofstream& stream, const std::string& id) { + stream << "}" << " " << id << ";" << std::endl; + stream << std::endl; +} + } // namespace tidl diff --git a/idlc/src/structure.cc b/idlc/src/structure.cc new file mode 100644 index 0000000..5ebc5ee --- /dev/null +++ b/idlc/src/structure.cc @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "attribute.h" +#include "structure.h" +#include "block.h" + +namespace tidl { + +Structure::Structure(const std::string& id, Attributes* attrs) + : Block::Block(id, Block::TYPE_STRUCTURE), attrs_(attrs) { +} + +const Attributes& Structure::GetAttributes() const { + return *attrs_; +} + +} // namespace tidl diff --git a/idlc/src/stub_gen.cc b/idlc/src/stub_gen.cc index 03287c2..4a40015 100644 --- a/idlc/src/stub_gen.cc +++ b/idlc/src/stub_gen.cc @@ -32,4 +32,11 @@ void StubGen::OnInterfaceBegin(const std::string& id) {} void StubGen::OnInterfaceEnd(const std::string& id) {} +void StubGen::OnAttributeGen(std::ofstream& stream, const std::string& id, + const BaseType& type) {} + +void StubGen::OnStructureBegin(std::ofstream& stream, const std::string& id) {} + +void StubGen::OnStructureEnd(std::ofstream& stream, const std::string& id) {} + } // namespace tidl diff --git a/idlc/tidlc.ll b/idlc/tidlc.ll index 0f4e157..9b85a6e 100644 --- a/idlc/tidlc.ll +++ b/idlc/tidlc.ll @@ -7,6 +7,9 @@ #include "type.h" #include "parameter.h" #include "interface.h" +#include "attribute.h" +#include "structure.h" +#include "block.h" #include "tidlc_y.hpp" #define YY_USER_ACTION yylloc->columns(yyleng); @@ -45,6 +48,7 @@ "<" {return yy::parser::token::T_META_OPEN;} ">" {return yy::parser::token::T_META_CLOSE;} "list" {return yy::parser::token::T_LIST;} +"struct" {return yy::parser::token::T_STRUCTURE;} [A-Za-z_][A-Za-z0-9_]* { yylval->token = new tidl::Token(yytext); diff --git a/idlc/tidlc.yy b/idlc/tidlc.yy index 8f1fa70..15ad238 100644 --- a/idlc/tidlc.yy +++ b/idlc/tidlc.yy @@ -9,6 +9,9 @@ #include "type.h" #include "parameter.h" #include "interface.h" +#include "attribute.h" +#include "structure.h" +#include "block.h" #include "tidlc_y.hpp" int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); @@ -22,7 +25,7 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); %token T_LEFT T_RIGHT T_ID T_COMMA T_SEMICOLON T_BRACE_OPEN T_BRACE_CLOSE %token T_CHAR T_SHORT T_INT T_LONG T_FLOAT T_DOUBLE T_VOID T_BUNDLE T_STRING T_BOOL -%token T_IN T_OUT T_REF T_ASYNC T_INTERFACE +%token T_IN T_OUT T_REF T_ASYNC T_INTERFACE T_STRUCTURE %token T_META_OPEN T_META_CLOSE T_LIST @@ -42,11 +45,17 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); tidl::Parameter* param; tidl::Parameters* params; tidl::Token* token; + tidl::Structure* structure; + tidl::Attribute* attr; + tidl::Attributes* attrs; + tidl::Block* blk; } %token T_ID -%type interface_blocks +%type blocks +%type block +%type structure_block %type interface_block %type declarations %type declaration @@ -57,21 +66,53 @@ int yylex(yy::parser::semantic_type *, yy::parser::location_type *, void *); %type parameter_list %type direction_specifier %type container_type_name +%type attribute +%type attributes %% -start: interface_blocks { - ps->AttachDoc($1); +start: blocks { + ps->SetDoc($1); } ; -interface_blocks: interface_block { +blocks: block { $$ = new tidl::Document(); - $$->AddInterface($1); + $$->AddBlock($1); } - | interface_blocks interface_block { + | blocks block { $$ = $1; - $$->AddInterface($2); + $$->AddBlock($2); + } +; + +block: interface_block { + $$ = $1; + } + | structure_block { + $$ = $1; + } +; + +structure_block: T_STRUCTURE T_ID T_BRACE_OPEN attributes T_BRACE_CLOSE { + $$ = new tidl::Structure($2->ToString(), $4); + delete $2; + } +; + +attributes: attribute { + $$ = new tidl::Attributes(); + $$->Add($1); + } + | attributes attribute { + $$ = $1; + $$->Add($2); + } +; + +attribute: base_type T_ID T_SEMICOLON { + $$ = new tidl::Attribute($2->ToString(), $1); + delete $2; } ;