Skip to content

Commit cff34ba

Browse files
Improve CobolFieldAttribute variable names (#775)
* wip: wip * add: append information of digits and scales * fix: when scales are negative * Update cobj/codegen.c Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 6171195 commit cff34ba

1 file changed

Lines changed: 123 additions & 12 deletions

File tree

cobj/codegen.c

Lines changed: 123 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static struct attr_list {
103103
int scale;
104104
int flags;
105105
int lenstr;
106+
char *suffix;
106107
} *attr_cache = NULL;
107108

108109
static struct literal_list {
@@ -150,6 +151,10 @@ enum joutput_stmt_type {
150151
JOUTPUT_STMT_TRIM,
151152
};
152153

154+
/* Forward declarations */
155+
static char *build_attr_suffix(int type, int digits, int scale, int flags);
156+
static const char *lookup_attr_suffix(int id);
157+
153158
static cb_tree call_parameters = NULL;
154159

155160
static const struct system_table system_tab[] = {
@@ -1057,6 +1062,7 @@ static int lookup_attr(int type, int digits, int scale, int flags,
10571062
l->flags = flags;
10581063
l->pic = pic;
10591064
l->lenstr = lenstr;
1065+
l->suffix = build_attr_suffix(type, digits, scale, flags);
10601066
l->next = attr_cache;
10611067
attr_cache = l;
10621068

@@ -1159,7 +1165,7 @@ static void joutput_attr(cb_tree x) {
11591165
fprintf(stderr, "Unexpected tree tag %d\n", CB_TREE_TAG(x));
11601166
ABORT();
11611167
}
1162-
joutput("%s%d", CB_PREFIX_ATTR, id);
1168+
joutput("%s%d%s", CB_PREFIX_ATTR, id, lookup_attr_suffix(id));
11631169
}
11641170

11651171
/**
@@ -5067,6 +5073,108 @@ static const char *get_type_constant_name(int type) {
50675073
}
50685074
}
50695075

5076+
/**
5077+
* typeの値を変数名用の短縮名に変換する
5078+
*/
5079+
static const char *get_type_short_name(int type) {
5080+
switch (type) {
5081+
case COB_TYPE_UNKNOWN:
5082+
return "Unknown";
5083+
case COB_TYPE_GROUP:
5084+
return "Group";
5085+
case COB_TYPE_BOOLEAN:
5086+
return "Boolean";
5087+
case COB_TYPE_NUMERIC_DISPLAY:
5088+
return "NumericDisplay";
5089+
case COB_TYPE_NUMERIC_BINARY:
5090+
return "NumericBinary";
5091+
case COB_TYPE_NUMERIC_PACKED:
5092+
return "NumericPacked";
5093+
case COB_TYPE_NUMERIC_FLOAT:
5094+
return "NumericFloat";
5095+
case COB_TYPE_NUMERIC_DOUBLE:
5096+
return "NumericDouble";
5097+
case COB_TYPE_NUMERIC_EDITED:
5098+
return "NumericEdited";
5099+
case COB_TYPE_ALPHANUMERIC:
5100+
return "Alphanumeric";
5101+
case COB_TYPE_ALPHANUMERIC_ALL:
5102+
return "AlphanumericAll";
5103+
case COB_TYPE_ALPHANUMERIC_EDITED:
5104+
return "AlphanumericEdited";
5105+
case COB_TYPE_NATIONAL:
5106+
return "National";
5107+
case COB_TYPE_NATIONAL_EDITED:
5108+
return "NationalEdited";
5109+
case COB_TYPE_NATIONAL_ALL:
5110+
return "NationalAll";
5111+
default:
5112+
return "Unknown";
5113+
}
5114+
}
5115+
5116+
/**
5117+
* type, digits, scale, flagsから変数名のサフィックスを生成する
5118+
*/
5119+
static char *build_attr_suffix(int type, int digits, int scale, int flags) {
5120+
char buf[256];
5121+
char *p = buf;
5122+
5123+
p += sprintf(p, "_%s", get_type_short_name(type));
5124+
5125+
/* NUMERIC型の場合はdigitsとscaleを追加 */
5126+
if (type == COB_TYPE_NUMERIC_DISPLAY || type == COB_TYPE_NUMERIC_BINARY ||
5127+
type == COB_TYPE_NUMERIC_PACKED || type == COB_TYPE_NUMERIC_FLOAT ||
5128+
type == COB_TYPE_NUMERIC_DOUBLE || type == COB_TYPE_NUMERIC_EDITED) {
5129+
p += sprintf(p, "_Digits%d", digits);
5130+
if (scale < 0) {
5131+
p += sprintf(p, "_ScaleNeg%d", -scale);
5132+
} else {
5133+
p += sprintf(p, "_Scale%d", scale);
5134+
}
5135+
}
5136+
5137+
if (flags & COB_FLAG_HAVE_SIGN) {
5138+
p += sprintf(p, "_HaveSign");
5139+
}
5140+
if (flags & COB_FLAG_SIGN_SEPARATE) {
5141+
p += sprintf(p, "_SignSeparate");
5142+
}
5143+
if (flags & COB_FLAG_SIGN_LEADING) {
5144+
p += sprintf(p, "_SignLeading");
5145+
}
5146+
if (flags & COB_FLAG_BLANK_ZERO) {
5147+
p += sprintf(p, "_BlankZero");
5148+
}
5149+
if (flags & COB_FLAG_JUSTIFIED) {
5150+
p += sprintf(p, "_Justified");
5151+
}
5152+
if (flags & COB_FLAG_BINARY_SWAP) {
5153+
p += sprintf(p, "_BinarySwap");
5154+
}
5155+
if (flags & COB_FLAG_REAL_BINARY) {
5156+
p += sprintf(p, "_RealBinary");
5157+
}
5158+
if (flags & COB_FLAG_IS_POINTER) {
5159+
p += sprintf(p, "_IsPointer");
5160+
}
5161+
5162+
return strdup(buf);
5163+
}
5164+
5165+
/**
5166+
* idからattr_listのサフィックスを検索する
5167+
*/
5168+
static const char *lookup_attr_suffix(int id) {
5169+
struct attr_list *l;
5170+
for (l = attr_cache; l; l = l->next) {
5171+
if (l->id == id) {
5172+
return l->suffix ? l->suffix : "";
5173+
}
5174+
}
5175+
return "";
5176+
}
5177+
50705178
/**
50715179
* flagsの値をCobolFieldAttributeのフラグ定数名の組み合わせとして出力する
50725180
* additional_indent: 2番目以降のフラグの前に付ける追加インデントのスペース数
@@ -5340,8 +5448,9 @@ static void joutput_init_method(struct cb_program *prog) {
53405448
if (gen_native) {
53415449
int index = lookup_attr(COB_TYPE_ALPHANUMERIC, 0, 0, 0, NULL, 0);
53425450
joutput_line("%snative = CobolFieldFactory.makeCobolField(256, new "
5343-
"CobolDataStorage(cob_native), %s%d);\n",
5344-
CB_PREFIX_FIELD, CB_PREFIX_ATTR, index);
5451+
"CobolDataStorage(cob_native), %s%d%s);\n",
5452+
CB_PREFIX_FIELD, CB_PREFIX_ATTR, index,
5453+
lookup_attr_suffix(index));
53455454
}
53465455

53475456
joutput_indent_level -= 2;
@@ -5356,7 +5465,8 @@ static void joutput_init_method(struct cb_program *prog) {
53565465
attr_cache = attr_list_reverse(attr_cache);
53575466
for (j = attr_cache; j; j = j->next) {
53585467
joutput_prefix();
5359-
joutput("%s%d = new CobolFieldAttribute(", CB_PREFIX_ATTR, j->id);
5468+
joutput("%s%d%s = new CobolFieldAttribute(", CB_PREFIX_ATTR, j->id,
5469+
j->suffix);
53605470
joutput_newline();
53615471
joutput_indent_level += 2;
53625472
// type
@@ -5471,9 +5581,9 @@ static void joutput_alphabet_name_initialization(struct cb_alphabet_name *p) {
54715581
joutput("%s%s = new CobolDataStorage(%s_byte_array_%s);", CB_PREFIX_SEQUENCE,
54725582
p->cname, CB_PREFIX_SEQUENCE, p->cname);
54735583
i = lookup_attr(COB_TYPE_ALPHANUMERIC, 0, 0, 0, NULL, 0);
5474-
joutput("%s%s = CobolFieldFactory.makeCobolField(256, %s%s, %s%d);\n",
5584+
joutput("%s%s = CobolFieldFactory.makeCobolField(256, %s%s, %s%d%s);\n",
54755585
CB_PREFIX_FIELD, p->cname, CB_PREFIX_SEQUENCE, p->cname,
5476-
CB_PREFIX_ATTR, i);
5586+
CB_PREFIX_ATTR, i, lookup_attr_suffix(i));
54775587
joutput("\n");
54785588
}
54795589

@@ -5728,7 +5838,8 @@ static void joutput_declare_member_variables(struct cb_program *prog,
57285838
joutput_line("/* Attributes */\n");
57295839
attr_cache = attr_list_reverse(attr_cache);
57305840
for (j = attr_cache; j; j = j->next) {
5731-
joutput_line("private CobolFieldAttribute %s%d;", CB_PREFIX_ATTR, j->id);
5841+
joutput_line("private CobolFieldAttribute %s%d%s;", CB_PREFIX_ATTR, j->id,
5842+
j->suffix);
57325843
}
57335844
joutput("\n");
57345845
}
@@ -6630,9 +6741,9 @@ void codegen(struct cb_program *prog, const int nested, char **program_id_list,
66306741
i = lookup_attr(COB_TYPE_ALPHANUMERIC, 0, 0, 0, NULL, 0);
66316742
joutput(" ");
66326743
joutput("private static AbstractCobolField %sebcdic = "
6633-
"CobolFieldFactori.makeField(256, new "
6634-
"CobolDataStorage(cob_ebcdic), %s%d);\n",
6635-
CB_PREFIX_FIELD, CB_PREFIX_ATTR, i);
6744+
"CobolFieldFactory.makeField(256, new "
6745+
"CobolDataStorage(cob_ebcdic), %s%d%s);\n",
6746+
CB_PREFIX_FIELD, CB_PREFIX_ATTR, i, lookup_attr_suffix(i));
66366747
joutput("\n");
66376748
}
66386749
if (gen_ebcdic_ascii) {
@@ -6711,8 +6822,8 @@ void codegen(struct cb_program *prog, const int nested, char **program_id_list,
67116822
joutput(" ");
67126823
joutput("private static AbstractCobolField %sebcdic_ascii = "
67136824
"CobolFieldFactory.makeField(256, new "
6714-
"CobolDataStorage(cob_ebcdic_ascii), %s%d);\n",
6715-
CB_PREFIX_FIELD, CB_PREFIX_ATTR, i);
6825+
"CobolDataStorage(cob_ebcdic_ascii), %s%d%s);\n",
6826+
CB_PREFIX_FIELD, CB_PREFIX_ATTR, i, lookup_attr_suffix(i));
67166827
joutput("\n");
67176828
}
67186829
if (gen_native) {

0 commit comments

Comments
 (0)