From f7032824c806cb33aa3eb5dbd810fa5095e12e63 Mon Sep 17 00:00:00 2001
From: BenceJanosSzabo <bence.janos.szabo@ericsson.com>
Date: Tue, 14 Nov 2017 12:16:34 +0100
Subject: [PATCH] Fixed asn1 opentype codegen issue (Bug 527244)

Change-Id: I112fc357fb16bba1575dfe2db1dec589a44b22f6
Signed-off-by: BenceJanosSzabo <bence.janos.szabo@ericsson.com>
---
 compiler2/asn1/TableConstraint.cc             |  3 +-
 compiler2/union.c                             | 36 +++++++------
 regression_test/compileonly/Makefile          |  2 +-
 .../compileonly/asn1_opentype/.gitignore      |  3 ++
 .../compileonly/asn1_opentype/Definitions.asn | 48 +++++++++++++++++
 .../asn1_opentype/Imported_Definitions.asn    | 29 +++++++++++
 .../compileonly/asn1_opentype/Makefile        | 51 +++++++++++++++++++
 .../compileonly/asn1_opentype/Test.ttcn       | 27 ++++++++++
 8 files changed, 182 insertions(+), 17 deletions(-)
 create mode 100644 regression_test/compileonly/asn1_opentype/.gitignore
 create mode 100644 regression_test/compileonly/asn1_opentype/Definitions.asn
 create mode 100644 regression_test/compileonly/asn1_opentype/Imported_Definitions.asn
 create mode 100644 regression_test/compileonly/asn1_opentype/Makefile
 create mode 100644 regression_test/compileonly/asn1_opentype/Test.ttcn

diff --git a/compiler2/asn1/TableConstraint.cc b/compiler2/asn1/TableConstraint.cc
index 39dfe5091..50b938a2e 100644
--- a/compiler2/asn1/TableConstraint.cc
+++ b/compiler2/asn1/TableConstraint.cc
@@ -9,6 +9,7 @@
  *   Balasko, Jeno
  *   Delic, Adam
  *   Raduly, Csaba
+ *   Szabo, Bence Janos
  *
  ******************************************************************************/
 #include "TableConstraint.hh"
@@ -258,6 +259,7 @@ namespace Asn {
 	  const Common::Identifier& altname = t_type->get_otaltname(is_strange);
           if(!t_ot->has_comp_withName(altname)) {
             Type * otype = new Type(Type::T_REFDSPEC, t_type);
+            otype->set_my_scope(t_type->get_my_scope());
             otype->set_genname(t_type->get_genname_own());
             t_ot->add_comp(new CompField(altname.clone(),
 					 otype));
@@ -268,7 +270,6 @@ namespace Asn {
 		"open type `%s'", dispname_str, t_ot->get_fullname().c_str());
           } // t_ot ! has the type
         } // for i (objs)
-        t_ot->set_my_scope(t_ot->get_my_scope());
         t_ot->set_fullname(t_ot->get_fullname());
         t_ot->chk();
       } // if ans
diff --git a/compiler2/union.c b/compiler2/union.c
index 4b07424f8..4ed9b55a1 100644
--- a/compiler2/union.c
+++ b/compiler2/union.c
@@ -2371,8 +2371,8 @@ void defUnionClass(struct_def const *sdef, output_struct *output)
   if (oer_needed) {
     // OER encode
     src = mputprintf(src,
-      "int %s::OER_encode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf) const\n"
-      "{\n", name);
+      "int %s::OER_encode(const TTCN_Typedescriptor_t&%s, TTCN_Buffer& p_buf) const\n"
+      "{\n", name, use_runtime_2 ? " p_td" : "");
     if (use_runtime_2) {
       src = mputstr(src, "  if (err_descr) return OER_encode_negtest(err_descr, p_td, p_buf);\n");
     }
@@ -2474,21 +2474,27 @@ void defUnionClass(struct_def const *sdef, output_struct *output)
       "int %s::OER_decode(const TTCN_Typedescriptor_t&, TTCN_Buffer& p_buf, OER_struct& p_oer)\n"
       "{\n", name);
     if (sdef->ot == NULL) {
-      src = mputstr(src, 
-        "  const ASN_Tag_t& descr = decode_oer_tag(p_buf);\n");
-      for (i = 0; i < sdef->nElements; ++i) {
+      if (sdef->nElements > 0) {
+        src = mputstr(src, 
+          "  const ASN_Tag_t& descr = decode_oer_tag(p_buf);\n");
+        
+        for (i = 0; i < sdef->nElements; ++i) {
+          src = mputprintf(src,
+            "  if (%s_descr_.ber->tags[%s_descr_.ber->n_tags-1].tagclass == descr.tagclass &&\n"
+            "      %s_descr_.ber->tags[%s_descr_.ber->n_tags-1].tagnumber == descr.tagnumber) {\n"
+            "    %s%s().OER_decode(%s_descr_, p_buf, p_oer);\n"
+            "  } else \n"
+            , sdef->elements[i].typedescrname, sdef->elements[i].typedescrname, sdef->elements[i].typedescrname
+            , sdef->elements[i].typedescrname, at_field, sdef->elements[i].name, sdef->elements[i].typedescrname);
+        }
+        src = mputprintf(src,
+        "{\n"
+        "    TTCN_error(\"Cannot find matching tag for type %s\");\n"
+        "}\n", name);
+      } else {
         src = mputprintf(src,
-          "  if (%s_descr_.ber->tags[%s_descr_.ber->n_tags-1].tagclass == descr.tagclass &&\n"
-          "      %s_descr_.ber->tags[%s_descr_.ber->n_tags-1].tagnumber == descr.tagnumber) {\n"
-          "    %s%s().OER_decode(%s_descr_, p_buf, p_oer);\n"
-          "  } else \n"
-          , sdef->elements[i].typedescrname, sdef->elements[i].typedescrname, sdef->elements[i].typedescrname
-          , sdef->elements[i].typedescrname, at_field, sdef->elements[i].name, sdef->elements[i].typedescrname);
+        "  TTCN_error(\"Decoding empty union type %s\");\n", name);
       }
-      src = mputprintf(src,
-      "{\n"
-      "    TTCN_error(\"Cannot find matching tag for type %s\");\n"
-      "}\n", name);
     } else {
       src = mputstr(src,
         "  size_t pos = decode_oer_length(p_buf, FALSE);\n"
diff --git a/regression_test/compileonly/Makefile b/regression_test/compileonly/Makefile
index f87589080..a88a0e940 100644
--- a/regression_test/compileonly/Makefile
+++ b/regression_test/compileonly/Makefile
@@ -29,7 +29,7 @@ CODIRS := dynamicTemplate styleGuide topLevelPdu \
 	attribQualif HT48786 selectCase openTypeNames \
 	defaultParamUsageBeforeDecl deterministic readFromFile \
 	asn1_hyphen nameClash portTranslation UntaggedOnTopLevelUnionSwitch \
-	typeDescrGenWIthNoXER languageSpec
+	typeDescrGenWIthNoXER languageSpec asn1_opentype
 
 ifdef DYN
 CODIRS +=  mfgen-xsd
diff --git a/regression_test/compileonly/asn1_opentype/.gitignore b/regression_test/compileonly/asn1_opentype/.gitignore
new file mode 100644
index 000000000..efbdfb27e
--- /dev/null
+++ b/regression_test/compileonly/asn1_opentype/.gitignore
@@ -0,0 +1,3 @@
+*.cc
+*.hh
+opentype
diff --git a/regression_test/compileonly/asn1_opentype/Definitions.asn b/regression_test/compileonly/asn1_opentype/Definitions.asn
new file mode 100644
index 000000000..2f443d04a
--- /dev/null
+++ b/regression_test/compileonly/asn1_opentype/Definitions.asn
@@ -0,0 +1,48 @@
+--/////////////////////////////////////////////////////////////////////////////
+-- Copyright (c) 2000-2017 Ericsson Telecom AB
+-- All rights reserved. This program and the accompanying materials
+-- are made available under the terms of the Eclipse Public License v1.0
+-- which accompanies this distribution, and is available at
+-- http://www.eclipse.org/legal/epl-v10.html
+--
+-- Contributors:
+--   Szabo, Bence Janos
+--
+--/////////////////////////////////////////////////////////////////////////////
+
+Definitions 
+
+DEFINITIONS IMPLICIT TAGS ::=
+
+BEGIN
+
+IMPORTS
+
+  releaseCall-CAPv2
+    FROM Imported-Definitions ;
+
+OPERATION ::= CLASS {
+  &ArgumentType          ,
+  &operationCode         INTEGER UNIQUE
+}
+WITH SYNTAX {
+  [ARGUMENT &ArgumentType]
+  [CODE &operationCode]
+}
+
+Invoke{OPERATION:Operations} ::= SEQUENCE {
+  opcode
+    OPERATION.&operationCode
+      ({Operations}),
+  argument 
+    OPERATION.&ArgumentType
+      ({Operations}{@opcode})
+}
+
+CAPv2-Invoke ::= Invoke{{Supported-CAP-Operations-CAPv2}}
+
+Supported-CAP-Operations-CAPv2 OPERATION ::= {
+  releaseCall-CAPv2 
+}
+
+END
diff --git a/regression_test/compileonly/asn1_opentype/Imported_Definitions.asn b/regression_test/compileonly/asn1_opentype/Imported_Definitions.asn
new file mode 100644
index 000000000..6fa9fe2cc
--- /dev/null
+++ b/regression_test/compileonly/asn1_opentype/Imported_Definitions.asn
@@ -0,0 +1,29 @@
+--/////////////////////////////////////////////////////////////////////////////
+-- Copyright (c) 2000-2017 Ericsson Telecom AB
+-- All rights reserved. This program and the accompanying materials
+-- are made available under the terms of the Eclipse Public License v1.0
+-- which accompanies this distribution, and is available at
+-- http://www.eclipse.org/legal/epl-v10.html
+--
+-- Contributors:
+--   Szabo, Bence Janos
+--
+--/////////////////////////////////////////////////////////////////////////////
+
+Imported-Definitions
+DEFINITIONS IMPLICIT TAGS ::=
+BEGIN
+
+IMPORTS OPERATION FROM Definitions ;
+
+
+releaseCall-CAPv2           OPERATION ::= {
+  ARGUMENT
+    INTEGER
+        CODE 22
+}
+
+END
+
+
+
diff --git a/regression_test/compileonly/asn1_opentype/Makefile b/regression_test/compileonly/asn1_opentype/Makefile
new file mode 100644
index 000000000..be1dbc779
--- /dev/null
+++ b/regression_test/compileonly/asn1_opentype/Makefile
@@ -0,0 +1,51 @@
+##############################################################################
+# Copyright (c) 2000-2017 Ericsson Telecom AB
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+#   Szabo, Bence Janos
+#
+##############################################################################
+TOPDIR := ../..
+include $(TOPDIR)/Makefile.regression
+
+.PHONY: all clean dep
+
+TTCN3_LIB = ttcn3$(RT2_SUFFIX)$(DYNAMIC_SUFFIX)
+
+TTCN3_MODULES = Test.ttcn
+ASN1_MODULES = Definitions.asn Imported_Definitions.asn
+ 
+GENERATED_SOURCES = $(TTCN3_MODULES:.ttcn=.cc) $(ASN1_MODULES:.asn=.cc)
+GENERATED_HEADERS = $(GENERATED_SOURCES:.cc=.hh)
+ifdef CODE_SPLIT
+GENERATED_SOURCES := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), .cc _seq.cc _set.cc  _seqof.cc _setof.cc _union.cc))
+else ifdef SPLIT_TO_SLICES
+POSTFIXES := $(foreach file, $(SPLIT_TO_SLICES), $(addsuffix $(file), _part_))
+POSTFIXES := $(foreach file, $(POSTFIXES), $(addprefix $(file), .cc))
+GENERATED_SOURCES2 := $(foreach file, $(GENERATED_SOURCES:.cc=), $(addprefix $(file), $(POSTFIXES)))
+GENERATED_SOURCES += $(GENERATED_SOURCES2)
+endif
+
+OBJECTS = $(GENERATED_SOURCES:.cc=.o)
+
+TARGET = opentype$(EXESUFFIX)
+
+all: $(TARGET)
+
+$(TARGET): $(GENERATED_SOURCES) $(USER_SOURCES)
+	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ -L$(TTCN3_DIR)/lib -l$(TTCN3_LIB) \
+	-L$(OPENSSL_DIR)/lib -lcrypto $($(PLATFORM)_LIBS)
+
+$(GENERATED_SOURCES) $(GENERATED_HEADERS): $(TTCN3_MODULES) $(ASN1_MODULES)
+	$(TTCN3_COMPILER) $(COMPILER_FLAGS) $^
+
+clean distclean:
+	$(RM) $(TARGET) $(OBJECTS) $(GENERATED_HEADERS) \
+	$(GENERATED_SOURCES) compile *.log
+
+dep: $(GENERATED_SOURCES)
+	makedepend $(CPPFLAGS) $(GENERATED_SOURCES)
diff --git a/regression_test/compileonly/asn1_opentype/Test.ttcn b/regression_test/compileonly/asn1_opentype/Test.ttcn
new file mode 100644
index 000000000..ab6459c7c
--- /dev/null
+++ b/regression_test/compileonly/asn1_opentype/Test.ttcn
@@ -0,0 +1,27 @@
+/******************************************************************************
+ * Copyright (c) 2000-2017 Ericsson Telecom AB
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Szabo, Bence Janos
+ *
+ ******************************************************************************/
+
+module Test {
+
+import from Definitions all;
+
+
+	function f_EPTF_CAPv2_createRCFromParams() return CAPv2_Invoke
+	{
+	  var CAPv2_Invoke vl_CAPv2_RCMsg;
+
+	  vl_CAPv2_RCMsg.argument.iNTEGER := 4;
+
+	  return vl_CAPv2_RCMsg;
+	}
+
+}
\ No newline at end of file
-- 
GitLab