From 8797075b39e9d9ce7794fd260b5d97136a7525aa Mon Sep 17 00:00:00 2001
From: Botond Baranyi <botond.baranyi@ericsson.com>
Date: Fri, 8 Dec 2017 11:51:19 +0100
Subject: [PATCH] Fixed XER descriptor generation for in-line enumerated field
 of open type (bug 528313)

Change-Id: I5cdb9f709176c830a262120dc41f24f9800252bc
Signed-off-by: Botond Baranyi <botond.baranyi@ericsson.com>
---
 compiler2/Type_chk.cc                         |  1 +
 compiler2/Type_codegen.cc                     |  2 +-
 regression_test/RAW/Bug522656/Bug522656.ttcn  |  3 ++
 regression_test/compileonly/Makefile          |  2 +-
 .../compileonly/openTypeXER/.gitignore        |  4 ++
 .../compileonly/openTypeXER/Makefile          | 51 ++++++++++++++++++
 .../compileonly/openTypeXER/Types.asn         | 52 +++++++++++++++++++
 7 files changed, 113 insertions(+), 2 deletions(-)
 create mode 100644 regression_test/compileonly/openTypeXER/.gitignore
 create mode 100644 regression_test/compileonly/openTypeXER/Makefile
 create mode 100644 regression_test/compileonly/openTypeXER/Types.asn

diff --git a/compiler2/Type_chk.cc b/compiler2/Type_chk.cc
index 4a1b02784..9f27487b7 100644
--- a/compiler2/Type_chk.cc
+++ b/compiler2/Type_chk.cc
@@ -861,6 +861,7 @@ void Type::chk_encodings()
       case OT_RECORD_OF:
       case OT_COMP_FIELD:
       case OT_SELTYPE:
+      case OT_FIELDSETTING:
         // ASN.1 types automatically have BER, PER, XER, OER and JSON encoding
         add_coding(string("BER:2002"), Ttcn::MOD_NONE, true);
         add_coding(string(get_encoding_name(CT_PER)), Ttcn::MOD_NONE, true);
diff --git a/compiler2/Type_codegen.cc b/compiler2/Type_codegen.cc
index d4d2e4bd6..f6125fe50 100644
--- a/compiler2/Type_codegen.cc
+++ b/compiler2/Type_codegen.cc
@@ -518,7 +518,7 @@ void Type::generate_code_xerdescriptor(output_struct* target)
         continue;
       }
       else { // probably a built-in type, punt with the C++ class name
-        last_s = ot->get_genname_value(0);
+        last_s = ot->get_genname_value(ot->get_my_scope());
         break;
       }
     }
diff --git a/regression_test/RAW/Bug522656/Bug522656.ttcn b/regression_test/RAW/Bug522656/Bug522656.ttcn
index 9d3ff513a..40f225f35 100644
--- a/regression_test/RAW/Bug522656/Bug522656.ttcn
+++ b/regression_test/RAW/Bug522656/Bug522656.ttcn
@@ -32,6 +32,9 @@ type record Rec {
   Oct1 o,
   Char3 c
 }
+with {
+  variant "";
+}
 
 type component CT {}
 
diff --git a/regression_test/compileonly/Makefile b/regression_test/compileonly/Makefile
index a88a0e940..7ec82dcf2 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 asn1_opentype
+	typeDescrGenWIthNoXER languageSpec asn1_opentype openTypeXER
 
 ifdef DYN
 CODIRS +=  mfgen-xsd
diff --git a/regression_test/compileonly/openTypeXER/.gitignore b/regression_test/compileonly/openTypeXER/.gitignore
new file mode 100644
index 000000000..3781f975b
--- /dev/null
+++ b/regression_test/compileonly/openTypeXER/.gitignore
@@ -0,0 +1,4 @@
+openTypeXER
+openTypeXER.exe
+Types*.cc
+Types*.hh
diff --git a/regression_test/compileonly/openTypeXER/Makefile b/regression_test/compileonly/openTypeXER/Makefile
new file mode 100644
index 000000000..3c62789d8
--- /dev/null
+++ b/regression_test/compileonly/openTypeXER/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:
+#   Baranyi, Botond
+#
+##############################################################################
+TOPDIR := ../..
+include $(TOPDIR)/Makefile.regression
+
+.PHONY: all clean dep
+
+TTCN3_LIB = ttcn3$(RT2_SUFFIX)$(DYNAMIC_SUFFIX)
+
+TTCN3_MODULES =
+ASN1_MODULES = Types.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 = openTypeXER$(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) -a $(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/openTypeXER/Types.asn b/regression_test/compileonly/openTypeXER/Types.asn
new file mode 100644
index 000000000..7eb1ea8be
--- /dev/null
+++ b/regression_test/compileonly/openTypeXER/Types.asn
@@ -0,0 +1,52 @@
+--/////////////////////////////////////////////////////////////////////////////
+-- 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:
+--   Baranyi, Botond
+--
+--/////////////////////////////////////////////////////////////////////////////
+Types
+DEFINITIONS 
+
+AUTOMATIC TAGS
+EXTENSIBILITY IMPLIED
+::=
+ BEGIN
+
+IMPORTS ; -- nothing
+
+
+ERROR-CLASS ::= CLASS
+{
+  &category	PrintableString(SIZE(1)),
+  &code		INTEGER,
+  &Type	DEFAULT INTEGER
+}
+WITH SYNTAX {&category &code [&Type]}
+
+ErrorSet ERROR-CLASS ::=
+{
+  {"A" 1 [1] INTEGER} |
+  {"A" 2 [1] VisibleString} |
+  {"B" 1 [1] PrintableString} |
+  {"B" 2 [1] BOOLEAN} |
+  oneErrorObject
+}
+
+oneErrorObject ERROR-CLASS ::= { "C" 3 ENUMERATED{foobar} }
+
+ErrorReturn ::= SEQUENCE
+{
+  errorCategory	ERROR-CLASS.&category ({ErrorSet}) OPTIONAL,
+  errors	SEQUENCE OF SEQUENCE
+  {
+    errorCode	ERROR-CLASS.&code({ErrorSet}{@errorCategory}),
+    errorInfo	[99] ERROR-CLASS.&Type({ErrorSet}{@errorCategory,@.errorCode})
+  } OPTIONAL
+}
+
+END
-- 
GitLab