diff --git a/pkg/service/class.go b/pkg/service/class.go index c8d7a1a..6086eb4 100644 --- a/pkg/service/class.go +++ b/pkg/service/class.go @@ -62,13 +62,27 @@ func (c *Class) methodImplementationsToString() (code string) { code += "\n" return } + +var ( + isInMethod = false + currentReceiverName = "" +) + func (c *Class) methodImplementationToString(m *ast.FuncDecl) (code string) { code += "void " code += c.Name + "::" code += m.Name.String() + "(" code += ") {" + if len(m.Recv.List) > 0 { + n := m.Recv.List[0].Names + if len(n) > 0 { + isInMethod = true + currentReceiverName = n[0].Name + } + } code += handleBlockStmt(m.Body) + isInMethod = false code += "}" code += "\n" diff --git a/pkg/service/expr.go b/pkg/service/expr.go index 53c9442..0371eb5 100644 --- a/pkg/service/expr.go +++ b/pkg/service/expr.go @@ -87,9 +87,13 @@ func handleSelectorExpr(expr ast.Expr) string { code := "" switch x := s.X.(type) { case *ast.Ident: - code += handleIdentExpr(x) + if isInMethod && x.Name == currentReceiverName { + code += "this->" + } else { + code += handleIdentExpr(x) + code += "." + } } - code += "." code += handleIdentExpr(s.Sel) if val, ok := mapping[code]; ok { code = val diff --git a/pkg/service/features/app.feature b/pkg/service/features/app.feature index 6fd0dec..0ef34e7 100644 --- a/pkg/service/features/app.feature +++ b/pkg/service/features/app.feature @@ -79,7 +79,7 @@ public: std::string c,d,e; }; ``` -@f + Сценарий: Структура с методом * Исходник: ``` @@ -101,4 +101,34 @@ public: }; void device::doSomething() { } +``` + + Сценарий: Структура с вызовом метода + * Исходник: +``` +package test + +type device struct { + a int +} + +func (d *device) doSomething() { + d.doSomethingElse() +} +func (d *device) doSomethingElse() { +} +``` + * Результат: +``` +class device { +public: + int a; + void doSomething(); + void doSomethingElse(); +}; +void device::doSomething() { +this->doSomethingElse(); +} +void device::doSomethingElse() { +} ```