| 1 |
/* |
| 2 |
* Copyright (c) 2021-2025 Symas Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of the Symas Corporation nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
/* This module exists in support of genapi.c |
| 31 |
|
| 32 |
It creates the declarations for structures that are implemented in the |
| 33 |
the libgcobol run-time library. These are type_decls; the analog in the |
| 34 |
C world would be that these are typedefs: |
| 35 |
|
| 36 |
typedef struct XXX_ |
| 37 |
{ |
| 38 |
.... |
| 39 |
} XXX; |
| 40 |
|
| 41 |
These functions don't, on their own, allocate any storage. That gets done |
| 42 |
when the type_decl is handed to the build_decl routine, which creates |
| 43 |
a var_decl. And that gets added to the GENERIC tree when the var_decl |
| 44 |
is turned into a decl_expr by build1() and then the decl_expr is added |
| 45 |
to the current statement list. |
| 46 |
|
| 47 |
Your best bet is to simply emulate the code here to create the type_decl |
| 48 |
for each structure, and then just use gg_declare_variable() to create the |
| 49 |
storage when you need it. |
| 50 |
|
| 51 |
Learning from the code in genapi.c is your best bet. |
| 52 |
|
| 53 |
*/ |
| 54 |
|
| 55 |
#include "cobol-system.h" |
| 56 |
#include "coretypes.h" |
| 57 |
#include "tree.h" |
| 58 |
#include "../../libgcobol/ec.h" |
| 59 |
#include "../../libgcobol/common-defs.h" |
| 60 |
#include "util.h" |
| 61 |
#include "cbldiag.h" |
| 62 |
#include "symbols.h" |
| 63 |
#include "gengen.h" |
| 64 |
|
| 65 |
tree |
| 66 |
var_decl_node_p_of( cbl_field_t *var ) |
| 67 |
{ |
| 68 |
if( var->var_decl_node ) |
| 69 |
{ |
| 70 |
return gg_get_address_of(var->var_decl_node); |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
return null_pointer_node; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// These routines return references, rather than values. So, in cases |
| 79 |
// like MOVE TABLE(a) TO TABLE (b), you need to gg_assign the returned |
| 80 |
// value elsewhere, rather than use them directly, because the second |
| 81 |
// refer_qualification calculation will overwrite the first. |
| 82 |
|
| 83 |
tree |
| 84 |
member(tree var, const char *member_name) |
| 85 |
{ |
| 86 |
return gg_struct_field_ref(var, member_name); |
| 87 |
} |
| 88 |
|
| 89 |
tree |
| 90 |
member(cbl_field_t *var, const char *member_name) |
| 91 |
{ |
| 92 |
return gg_struct_field_ref(var->var_decl_node, member_name); |
| 93 |
} |
| 94 |
|
| 95 |
tree |
| 96 |
member(cbl_file_t *var, const char *member_name) |
| 97 |
{ |
| 98 |
return gg_struct_field_ref(var->var_decl_node, member_name); |
| 99 |
} |
| 100 |
|
| 101 |
void |
| 102 |
member(tree var, const char *member_name, int value) |
| 103 |
{ |
| 104 |
gg_assign( member(var, member_name), |
| 105 |
build_int_cst_type(INT, value) ); |
| 106 |
} |
| 107 |
|
| 108 |
void |
| 109 |
member(tree var, const char *member_name, tree value) |
| 110 |
{ |
| 111 |
gg_assign( member(var, member_name), |
| 112 |
value ); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
member(cbl_field_t *var, const char *member_name, tree value) |
| 117 |
{ |
| 118 |
gg_assign( member(var->var_decl_node, member_name), |
| 119 |
value ); |
| 120 |
} |
| 121 |
|
| 122 |
tree |
| 123 |
member2(tree var, const char *member_name, const char *submember) |
| 124 |
{ |
| 125 |
tree level1 = member(var, member_name); |
| 126 |
return member(level1, submember ); |
| 127 |
} |
| 128 |
|
| 129 |
void |
| 130 |
member2(tree var, const char *member_name, const char *submember, int value) |
| 131 |
{ |
| 132 |
tree level1 = member(var, member_name); |
| 133 |
tree level2 = member(level1, submember ); |
| 134 |
gg_assign(level2, build_int_cst_type(INT, value) ); |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
member2(tree var, const char *member_name, const char *submember, tree value) |
| 139 |
{ |
| 140 |
tree level1 = member(var, member_name); |
| 141 |
tree level2 = member(level1, submember ); |
| 142 |
gg_assign(level2, value); |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
member3(tree var, const char *mem, const char *sub2, const char *sub3, tree value) |
| 147 |
{ |
| 148 |
tree level1 = member(var, mem); |
| 149 |
tree level2 = member(level1, sub2 ); |
| 150 |
tree level3 = member(level2, sub3 ); |
| 151 |
gg_assign(level3, value); |
| 152 |
} |
| 153 |
|
| 154 |
tree cblc_field_type_node; |
| 155 |
tree cblc_field_p_type_node; |
| 156 |
tree cblc_field_pp_type_node; |
| 157 |
tree cblc_file_type_node; |
| 158 |
tree cblc_file_p_type_node; |
| 159 |
tree cbl_enabled_exception_type_node; |
| 160 |
tree cblc_goto_type_node; |
| 161 |
|
| 162 |
// The following functions return type_decl nodes for the various structures |
| 163 |
|
| 164 |
static tree |
| 165 |
create_cblc_field_t() |
| 166 |
{ |
| 167 |
/* |
| 168 |
typedef struct cblc_field_t |
| 169 |
{ |
| 170 |
unsigned char *data; // The runtime data. There is no null terminator |
| 171 |
size_t capacity; // The size of "data" |
| 172 |
size_t allocated; // The number of bytes available for capacity |
| 173 |
size_t offset; // Offset from our ancestor |
| 174 |
char *name; // The null-terminated name of this variable |
| 175 |
char *picture; // The null-terminated picture string. |
| 176 |
char *initial; // The null_terminated initial value |
| 177 |
struct cblc_field_t *parent;// This field's immediate parent field |
| 178 |
size_t occurs_lower; // non-zero for a table |
| 179 |
size_t occurs_upper; // non-zero for a table |
| 180 |
uint64_t attr; // See cbl_field_attr_t |
| 181 |
signed char type; // A one-byte copy of cbl_field_type_t |
| 182 |
signed char level; // This variable's level in the naming heirarchy |
| 183 |
signed char digits; // Digits specified in PIC string; e.g. 5 for 99v999 |
| 184 |
signed char rdigits; // Digits to the right of the decimal point. 3 for 99v999 |
| 185 |
} cblc_field_t; |
| 186 |
*/ |
| 187 |
tree retval = NULL_TREE; |
| 188 |
retval = gg_get_filelevel_struct_type_decl( "cblc_field_t", |
| 189 |
16, |
| 190 |
UCHAR_P, "data", |
| 191 |
SIZE_T, "capacity", |
| 192 |
SIZE_T, "allocated", |
| 193 |
SIZE_T, "offset", |
| 194 |
CHAR_P, "name", |
| 195 |
CHAR_P, "picture", |
| 196 |
CHAR_P, "initial", |
| 197 |
CHAR_P, "parent", |
| 198 |
SIZE_T, "occurs_lower", |
| 199 |
SIZE_T, "occurs_upper", |
| 200 |
ULONGLONG, "attr", |
| 201 |
SCHAR, "type", |
| 202 |
SCHAR, "level", |
| 203 |
SCHAR, "digits", |
| 204 |
SCHAR, "rdigits", |
| 205 |
INT, "dummy"); // Needed to make it an even number of 32-bit ints |
| 206 |
retval = TREE_TYPE(retval); |
| 207 |
|
| 208 |
return retval; |
| 209 |
} |
| 210 |
|
| 211 |
static tree |
| 212 |
create_cblc_file_t() |
| 213 |
{ |
| 214 |
// When doing FILE I/O, you need the cblc_file_t structure |
| 215 |
|
| 216 |
/* |
| 217 |
typedef struct cblc_file_t |
| 218 |
{ |
| 219 |
char *name; // This is the name of the structure; might be the name of an environment variable |
| 220 |
size_t symbol_index; // The symbol table index of the related cbl_file_t structure |
| 221 |
char *filename; // The name of the file to be opened |
| 222 |
FILE *file_pointer; // The FILE *pointer |
| 223 |
cblc_field_t *default_record; // The record_area |
| 224 |
size_t record_area_min; // The size of the smallest 01 record in the FD |
| 225 |
size_t record_area_max; // The size of the largest 01 record in the FD |
| 226 |
cblc_field_t **keys; // For relative and indexed files. The first is the primary key. Null-terminated. |
| 227 |
int *key_numbers; // One per key -- each key has a number. This table is key_number + 1 |
| 228 |
int *uniques; // One per key |
| 229 |
cblc_field_t *password; // |
| 230 |
cblc_field_t *status; // This must exist, and is the cbl_field_t version of io_status |
| 231 |
cblc_field_t *user_status; // This might exist, and is another copy See 2014 standard, section 9.1.12 |
| 232 |
cblc_field_t *vsam_status; // |
| 233 |
cblc_field_t *record_length; // |
| 234 |
supplemental_t *supplemental; // |
| 235 |
void *implementation; // reserved for any implementation |
| 236 |
size_t reserve; // From I-O section RESERVE clause |
| 237 |
long prior_read_location; // Location of immediately preceding successful read |
| 238 |
cbl_file_org_t org; // from ORGANIZATION clause |
| 239 |
cbl_file_access_t access; // from ACCESS MODE clause |
| 240 |
int mode_char; // 'r', 'w', '+', or 'a' from FILE OPEN statement |
| 241 |
int errnum; // most recent errno; can't reuse "errno" as the name |
| 242 |
file_status_t io_status; // See 2014 standard, section 9.1.12 |
| 243 |
int padding; // Actually a char |
| 244 |
int delimiter; // ends a record; defaults to '\n'. |
| 245 |
int flags; // cblc_file_flags_t |
| 246 |
int recent_char; // This is the most recent char sent to the file |
| 247 |
int recent_key; |
| 248 |
cblc_file_prior_op_t prior_op; |
| 249 |
int dummy // We need an even number of INT |
| 250 |
} cblc_file_t; |
| 251 |
*/ |
| 252 |
|
| 253 |
tree retval = NULL_TREE; |
| 254 |
retval = gg_get_filelevel_struct_type_decl( "cblc_file_t", |
| 255 |
31, |
| 256 |
CHAR_P, "name", |
| 257 |
SIZE_T, "symbol_table_index", |
| 258 |
CHAR_P, "filename", |
| 259 |
FILE_P, "file_pointer", |
| 260 |
cblc_field_p_type_node, "default_record", |
| 261 |
SIZE_T, "record_area_min", |
| 262 |
SIZE_T, "record_area_max", |
| 263 |
build_pointer_type(cblc_field_p_type_node), "keys", |
| 264 |
build_pointer_type(INT),"key_numbers", |
| 265 |
build_pointer_type(INT),"uniques", |
| 266 |
cblc_field_p_type_node, "password", |
| 267 |
cblc_field_p_type_node, "status", |
| 268 |
cblc_field_p_type_node, "user_status", |
| 269 |
cblc_field_p_type_node, "vsam_status", |
| 270 |
cblc_field_p_type_node, "record_length", |
| 271 |
VOID_P, "supplemental", |
| 272 |
VOID_P, "implementation", |
| 273 |
SIZE_T, "reserve", |
| 274 |
LONG, "prior_read_location", |
| 275 |
INT, "org", |
| 276 |
INT, "access", |
| 277 |
INT, "mode_char", |
| 278 |
INT, "errnum", |
| 279 |
INT, "io_status", |
| 280 |
INT, "padding", |
| 281 |
INT, "delimiter", |
| 282 |
INT, "flags", |
| 283 |
INT, "recent_char", |
| 284 |
INT, "recent_key", |
| 285 |
INT, "prior_op", |
| 286 |
INT, "dummy"); |
| 287 |
retval = TREE_TYPE(retval); |
| 288 |
return retval; |
| 289 |
} |
| 290 |
|
| 291 |
static tree |
| 292 |
create_cbl_enabled_exception_t() |
| 293 |
{ |
| 294 |
/* |
| 295 |
struct cbl_enabled_exception_t |
| 296 |
{ |
| 297 |
bool enabled, location; |
| 298 |
ec_type_t ec; |
| 299 |
size_t file; |
| 300 |
}; |
| 301 |
*/ |
| 302 |
tree retval = NULL_TREE; |
| 303 |
retval = gg_get_filelevel_struct_type_decl( "cbl_enabled_exception_t", |
| 304 |
4, |
| 305 |
BOOL, "enabled", |
| 306 |
BOOL, "location", |
| 307 |
UINT, "ec", |
| 308 |
SIZE_T, "file"); |
| 309 |
retval = TREE_TYPE(retval); |
| 310 |
|
| 311 |
return retval; |
| 312 |
} |
| 313 |
|
| 314 |
void |
| 315 |
create_our_type_nodes() |
| 316 |
{ |
| 317 |
static bool just_once = true; |
| 318 |
if( just_once ) |
| 319 |
{ |
| 320 |
just_once = false; |
| 321 |
cblc_field_type_node = create_cblc_field_t(); |
| 322 |
cblc_field_p_type_node = build_pointer_type(cblc_field_type_node); |
| 323 |
cblc_field_pp_type_node = build_pointer_type(cblc_field_p_type_node); |
| 324 |
cblc_file_type_node = create_cblc_file_t(); |
| 325 |
cblc_file_p_type_node = build_pointer_type(cblc_file_type_node); |
| 326 |
cbl_enabled_exception_type_node = create_cbl_enabled_exception_t(); |
| 327 |
} |
| 328 |
} |
| 329 |
|