| 1 | /****************************************************************************** |
| 2 | * Copyright (c) 2000-2016 Ericsson Telecom AB |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Balasko, Jeno |
| 10 | * Baranyi, Botond |
| 11 | * Raduly, Csaba |
| 12 | * Szabo, Bence Janos |
| 13 | * Zalanyi, Balazs Andor |
| 14 | * |
| 15 | ******************************************************************************/ |
| 16 | #include "CodeGenHelper.hh" |
| 17 | #include "Code.hh" |
| 18 | #include "error.h" |
| 19 | #include "main.hh" |
| 20 | #include <cstdio> |
| 21 | #include <cstring> |
| 22 | |
| 23 | namespace Common { |
| 24 | |
| 25 | CodeGenHelper* CodeGenHelper::instance = 0; |
| 26 | |
| 27 | CodeGenHelper::generated_output_t::generated_output_t() : |
| 28 | is_module(false), |
| 29 | is_ttcn(true), |
| 30 | has_circular_import(false) |
| 31 | { |
| 32 | Code::init_output(&os); |
| 33 | } |
| 34 | |
| 35 | CodeGenHelper::generated_output_t::~generated_output_t() { |
| 36 | Code::free_output(&os); |
| 37 | } |
| 38 | |
| 39 | // from Type.cc |
| 40 | const char* const CodeGenHelper::typetypemap[] = { |
| 41 | "", /**< undefined */ |
| 42 | "", /**< erroneous (e.g. nonexistent reference) */ |
| 43 | "", /**< null (ASN.1) */ |
| 44 | "", /**< boolean */ |
| 45 | "", /**< integer */ |
| 46 | "", /**< integer / ASN */ |
| 47 | "", /**< real/float */ |
| 48 | "", /**< enumerated / ASN */ |
| 49 | "", /**< enumerated / TTCN */ |
| 50 | "", /**< bitstring */ |
| 51 | "", /**< bitstring */ |
| 52 | "", /**< hexstring (TTCN-3) */ |
| 53 | "", /**< octetstring */ |
| 54 | "", /**< charstring (TTCN-3) */ |
| 55 | "", /**< universal charstring (TTCN-3) */ |
| 56 | "", /**< UTF8String (ASN.1) */ |
| 57 | "", /**< NumericString (ASN.1) */ |
| 58 | "", /**< PrintableString (ASN.1) */ |
| 59 | "", /**< TeletexString (ASN.1) */ |
| 60 | "", /**< VideotexString (ASN.1) */ |
| 61 | "", /**< IA5String (ASN.1) */ |
| 62 | "", /**< GraphicString (ASN.1) */ |
| 63 | "", /**< VisibleString (ASN.1) */ |
| 64 | "", /**< GeneralString (ASN.1) */ |
| 65 | "", /**< UniversalString (ASN.1) */ |
| 66 | "", /**< BMPString (ASN.1) */ |
| 67 | "", /**< UnrestrictedCharacterString (ASN.1) */ |
| 68 | "", /**< UTCTime (ASN.1) */ |
| 69 | "", /**< GeneralizedTime (ASN.1) */ |
| 70 | "", /** Object descriptor, a kind of string (ASN.1) */ |
| 71 | "", /**< object identifier */ |
| 72 | "", /**< relative OID (ASN.1) */ |
| 73 | "_union", /**< choice /ASN, uses u.secho */ |
| 74 | "_union", /**< union /TTCN, uses u.secho */ |
| 75 | "_seqof", /**< sequence (record) of */ |
| 76 | "_setof", /**< set of */ |
| 77 | "_seq", /**< sequence /ASN, uses u.secho */ |
| 78 | "_seq", /**< record /TTCN, uses u.secho */ |
| 79 | "_set", /**< set /ASN, uses u.secho */ |
| 80 | "_set", /**< set /TTCN, uses u.secho */ |
| 81 | "", /**< ObjectClassFieldType (ASN.1) */ |
| 82 | "", /**< open type (ASN.1) */ |
| 83 | "", /**< ANY (deprecated ASN.1) */ |
| 84 | "", /**< %EXTERNAL (ASN.1) */ |
| 85 | "", /**< EMBEDDED PDV (ASN.1) */ |
| 86 | "", /**< referenced */ |
| 87 | "", /**< special referenced (by pointer, not by name) */ |
| 88 | "", /**< selection type (ASN.1) */ |
| 89 | "", /**< verdict type (TTCN-3) */ |
| 90 | "", /**< port type (TTCN-3) */ |
| 91 | "", /**< component type (TTCN-3) */ |
| 92 | "", /**< address type (TTCN-3) */ |
| 93 | "", /**< default type (TTCN-3) */ |
| 94 | "", /**< array (TTCN-3), uses u.array */ |
| 95 | "", /**< signature (TTCN-3) */ |
| 96 | "", /**< function reference (TTCN-3) */ |
| 97 | "", /**< altstep reference (TTCN-3) */ |
| 98 | "", /**< testcase reference (TTCN-3) */ |
| 99 | "", /**< anytype (TTCN-3) */ |
| 100 | 0 |
| 101 | }; |
| 102 | |
| 103 | CodeGenHelper::CodeGenHelper() : |
| 104 | split_mode(SPLIT_NONE), |
| 105 | slice_num(1) |
| 106 | { |
| 107 | if (instance != 0) |
| 108 | FATAL_ERROR("Attempted to create a second code generator."); |
| 109 | instance = this; |
| 110 | } |
| 111 | |
| 112 | CodeGenHelper& CodeGenHelper::GetInstance() { |
| 113 | if (instance == 0) |
| 114 | FATAL_ERROR("Trying to access to the already destroyed code generator."); |
| 115 | return *instance; |
| 116 | } |
| 117 | |
| 118 | void CodeGenHelper::set_split_mode(split_type st) { |
| 119 | split_mode = st; |
| 120 | |
| 121 | if (split_mode == SPLIT_TO_SLICES) { |
| 122 | split_to_slices = true; |
| 123 | } else { |
| 124 | split_to_slices = false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | bool CodeGenHelper::set_split_mode(const char* type) { |
| 129 | int n; |
| 130 | if (strcmp(type, "none") == 0) { |
| 131 | split_mode = SPLIT_NONE; |
| 132 | split_to_slices = false; |
| 133 | } else if (strcmp(type, "type") == 0) { |
| 134 | split_mode = SPLIT_BY_KIND; |
| 135 | split_to_slices = false; |
| 136 | } else if ((n = atoi(type))) { |
| 137 | size_t length = strlen(type); |
| 138 | for (int i=0;i<length; i++) { |
| 139 | if (!isdigit(type[i])) { |
| 140 | ERROR("The number argument of -U must be a valid number."); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | split_mode = SPLIT_TO_SLICES; |
| 145 | if (n < 1 || n > 999999) { |
| 146 | ERROR("The number argument of -U must be between 1 and 999999."); |
| 147 | return false; |
| 148 | } |
| 149 | slice_num = n; |
| 150 | split_to_slices = slice_num > 1; // slice_num == 1 has no effect |
| 151 | } else |
| 152 | return false; |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | CodeGenHelper::split_type CodeGenHelper::get_split_mode() const { |
| 157 | return split_mode; |
| 158 | } |
| 159 | |
| 160 | void CodeGenHelper::add_module(const string& name, const string& dispname, |
| 161 | bool is_ttcn, bool has_circular_import) { |
| 162 | generated_output_t* go = new generated_output_t; |
| 163 | go->filename.clear(); |
| 164 | go->modulename = name; |
| 165 | go->module_dispname = dispname; |
| 166 | go->is_module = true; |
| 167 | go->is_ttcn = is_ttcn; |
| 168 | go->has_circular_import = has_circular_import; |
| 169 | generated_code.add(dispname, go); |
| 170 | module_names_t* mod_names = new module_names_t; |
| 171 | mod_names->name = name; |
| 172 | mod_names->dispname = dispname; |
| 173 | modules.add(mod_names); |
| 174 | } |
| 175 | |
| 176 | output_struct* CodeGenHelper::get_outputstruct(const string& name) { |
| 177 | return &generated_code[name]->os; |
| 178 | } |
| 179 | |
| 180 | void CodeGenHelper::set_current_module(const string& name) { |
| 181 | current_module = name; |
| 182 | } |
| 183 | |
| 184 | void CodeGenHelper::update_intervals(output_struct* const output) { |
| 185 | if(instance->split_mode != SPLIT_TO_SLICES) return; |
| 186 | |
| 187 | size_t tmp; |
| 188 | |
| 189 | // 1. check if some characters are added to the charstring |
| 190 | // 2. increment size variable |
| 191 | // 3. if size is bigger than the array's size, then double the array size |
| 192 | // 4. store new end position |
| 193 | |
| 194 | // class_defs are not counted as they will be in the header |
| 195 | tmp = mstrlen(output->source.function_bodies); |
| 196 | if (output->intervals.function_bodies[output->intervals.function_bodies_size] < tmp) { |
| 197 | output->intervals.function_bodies_size++; |
| 198 | if (output->intervals.function_bodies_size > output->intervals.function_bodies_max_size) { |
| 199 | output->intervals.function_bodies_max_size *= 2; |
| 200 | output->intervals.function_bodies = (size_t*)Realloc(output->intervals.function_bodies, output->intervals.function_bodies_max_size * sizeof(size_t)); |
| 201 | } |
| 202 | output->intervals.function_bodies[output->intervals.function_bodies_size] = tmp; |
| 203 | } |
| 204 | tmp = mstrlen(output->source.methods); |
| 205 | if (output->intervals.methods[output->intervals.methods_size] < tmp) { |
| 206 | output->intervals.methods_size++; |
| 207 | if (output->intervals.methods_size > output->intervals.methods_max_size) { |
| 208 | output->intervals.methods_max_size *= 2; |
| 209 | output->intervals.methods = (size_t*)Realloc(output->intervals.methods, output->intervals.methods_max_size * sizeof(size_t)); |
| 210 | } |
| 211 | output->intervals.methods[output->intervals.methods_size] = tmp; |
| 212 | } |
| 213 | tmp = mstrlen(output->source.static_conversion_function_bodies); |
| 214 | if (output->intervals.static_conversion_function_bodies[output->intervals.static_conversion_function_bodies_size] < tmp) { |
| 215 | output->intervals.static_conversion_function_bodies_size++; |
| 216 | if (output->intervals.static_conversion_function_bodies_size > output->intervals.static_conversion_function_bodies_max_size) { |
| 217 | output->intervals.static_conversion_function_bodies_max_size *= 2; |
| 218 | output->intervals.static_conversion_function_bodies = (size_t*)Realloc(output->intervals.static_conversion_function_bodies, output->intervals.static_conversion_function_bodies_max_size * sizeof(size_t)); |
| 219 | } |
| 220 | output->intervals.static_conversion_function_bodies[output->intervals.static_conversion_function_bodies_size] = tmp; |
| 221 | } |
| 222 | tmp = mstrlen(output->source.static_function_bodies); |
| 223 | if (output->intervals.static_function_bodies[output->intervals.static_function_bodies_size] < tmp) { |
| 224 | output->intervals.static_function_bodies_size++; |
| 225 | if (output->intervals.static_function_bodies_size > output->intervals.static_function_bodies_max_size) { |
| 226 | output->intervals.static_function_bodies_max_size *= 2; |
| 227 | output->intervals.static_function_bodies = (size_t*)Realloc(output->intervals.static_function_bodies, output->intervals.static_function_bodies_max_size * sizeof(size_t)); |
| 228 | } |
| 229 | output->intervals.static_function_bodies[output->intervals.static_function_bodies_size] = tmp; |
| 230 | } |
| 231 | } |
| 232 | //Advised to call update_intervals before this |
| 233 | size_t CodeGenHelper::size_of_sources(output_struct * const output) { |
| 234 | size_t size = 0; |
| 235 | // Calculate global var and string literals size |
| 236 | output->intervals.pre_things_size = mstrlen(output->source.global_vars) + mstrlen(output->source.string_literals); |
| 237 | |
| 238 | // Class_defs, static_conversion_function_prototypes, static_function_prototypes are in the header, |
| 239 | // and includes are not counted |
| 240 | size = output->intervals.pre_things_size + |
| 241 | output->intervals.function_bodies[output->intervals.function_bodies_size] + |
| 242 | output->intervals.methods[output->intervals.methods_size] + |
| 243 | output->intervals.static_conversion_function_bodies[output->intervals.static_conversion_function_bodies_size] + |
| 244 | output->intervals.static_function_bodies[output->intervals.static_function_bodies_size]; |
| 245 | return size; |
| 246 | } |
| 247 | |
| 248 | size_t CodeGenHelper::get_next_chunk_pos(const output_struct * const from, output_struct * const to, const size_t base_pos, const size_t chunk_size) { |
| 249 | size_t pos = 0; // Holds the position from the beginning |
| 250 | |
| 251 | pos += from->intervals.pre_things_size; |
| 252 | |
| 253 | if (pos > base_pos) { |
| 254 | to->source.global_vars = mputstr(to->source.global_vars, from->source.global_vars); |
| 255 | to->source.string_literals = mputstr(to->source.string_literals, from->source.string_literals); |
| 256 | } |
| 257 | |
| 258 | get_chunk_from_poslist(from->source.methods, to->source.methods, from->intervals.methods, from->intervals.methods_size, base_pos, chunk_size, pos); |
| 259 | get_chunk_from_poslist(from->source.function_bodies, to->source.function_bodies, from->intervals.function_bodies, from->intervals.function_bodies_size, base_pos, chunk_size, pos); |
| 260 | get_chunk_from_poslist(from->source.static_function_bodies, to->source.static_function_bodies, from->intervals.static_function_bodies, from->intervals.static_function_bodies_size, base_pos, chunk_size, pos); |
| 261 | get_chunk_from_poslist(from->source.static_conversion_function_bodies, to->source.static_conversion_function_bodies, from->intervals.static_conversion_function_bodies, from->intervals.static_conversion_function_bodies_size, base_pos, chunk_size, pos); |
| 262 | |
| 263 | return pos; |
| 264 | } |
| 265 | //if from null return. |
| 266 | void CodeGenHelper::get_chunk_from_poslist(const char* from, char *& to, const size_t interval_array[], const size_t interval_array_size, const size_t base_pos, const size_t chunk_size, size_t& pos) { |
| 267 | if (from == NULL) return; |
| 268 | // If we have enough to form a chunk |
| 269 | |
| 270 | // pos is unsigned so it can't be negative |
| 271 | if (pos > base_pos && pos - base_pos >= chunk_size) return; |
| 272 | |
| 273 | size_t tmp = pos; |
| 274 | |
| 275 | pos += interval_array[interval_array_size]; |
| 276 | |
| 277 | if (pos > base_pos) { // If we haven't finished with this interval_array |
| 278 | if (pos - base_pos >= chunk_size) { // It is a good chunk, but make it more precise because it may be too big |
| 279 | int ind = 0; |
| 280 | for (int i = 0; i <= interval_array_size; i++) { |
| 281 | if (tmp + interval_array[i] <= base_pos) { // Find the pos where we left off |
| 282 | ind = i; |
| 283 | } else if (tmp + interval_array[i] - base_pos >= chunk_size) { |
| 284 | // Found the first optimal position that is a good chunk |
| 285 | to = mputstrn(to, from + interval_array[ind], interval_array[i] - interval_array[ind]); |
| 286 | pos = tmp + interval_array[i]; |
| 287 | return; |
| 288 | } |
| 289 | } |
| 290 | } else { // We can't form a new chunk from the remaining characters |
| 291 | int ind = 0; |
| 292 | for (int i = 0; i <= interval_array_size; i++) { |
| 293 | if (tmp + interval_array[i] <= base_pos) { |
| 294 | ind = i; |
| 295 | } else { |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | // Put the remaining characters |
| 300 | to = mputstrn(to, from + interval_array[ind], interval_array[interval_array_size] - interval_array[ind]); |
| 301 | pos = tmp + interval_array[interval_array_size]; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | output_struct* CodeGenHelper::get_outputstruct(Ttcn::Definition* def) { |
| 307 | string key = get_key(*def); |
| 308 | const string& new_name = current_module + key; |
| 309 | if (!generated_code.has_key(new_name)) { |
| 310 | generated_output_t* go = new generated_output_t; |
| 311 | go->filename = key; |
| 312 | go->modulename = generated_code[current_module]->modulename; |
| 313 | go->module_dispname = generated_code[current_module]->module_dispname; |
| 314 | generated_code.add(new_name, go); |
| 315 | go->os.source.includes = mprintf("\n#include \"%s.hh\"\n" |
| 316 | , current_module.c_str()); |
| 317 | } |
| 318 | return &generated_code[new_name]->os; |
| 319 | } |
| 320 | |
| 321 | output_struct* CodeGenHelper::get_outputstruct(Type* type) { |
| 322 | string key = get_key(*type); |
| 323 | const string& new_name = current_module + key; |
| 324 | if (!generated_code.has_key(new_name)) { |
| 325 | generated_output_t* go = new generated_output_t; |
| 326 | go->filename = key; |
| 327 | go->modulename = generated_code[current_module]->modulename; |
| 328 | go->module_dispname = generated_code[current_module]->module_dispname; |
| 329 | generated_code.add(new_name, go); |
| 330 | go->os.source.includes = mprintf("\n#include \"%s.hh\"\n" |
| 331 | , current_module.c_str()); |
| 332 | } |
| 333 | return &generated_code[new_name]->os; |
| 334 | } |
| 335 | |
| 336 | output_struct* CodeGenHelper::get_current_outputstruct() { |
| 337 | return &generated_code[current_module]->os; |
| 338 | } |
| 339 | |
| 340 | void CodeGenHelper::transfer_value(char* &dst, char* &src) { |
| 341 | dst = mputstr(dst, src); |
| 342 | Free(src); |
| 343 | src = 0; |
| 344 | } |
| 345 | |
| 346 | void CodeGenHelper::finalize_generation(Type* type) { |
| 347 | string key = get_key(*type); |
| 348 | if (key.empty()) return; |
| 349 | |
| 350 | output_struct& dst = *get_current_outputstruct(); |
| 351 | output_struct& src = *get_outputstruct(current_module + key); |
| 352 | // key is not empty so these can never be the same |
| 353 | |
| 354 | transfer_value(dst.header.includes, src.header.includes); |
| 355 | transfer_value(dst.header.class_decls, src.header.class_decls); |
| 356 | transfer_value(dst.header.typedefs, src.header.typedefs); |
| 357 | transfer_value(dst.header.class_defs, src.header.class_defs); |
| 358 | transfer_value(dst.header.function_prototypes, src.header.function_prototypes); |
| 359 | transfer_value(dst.header.global_vars, src.header.global_vars); |
| 360 | transfer_value(dst.header.testport_includes, src.header.testport_includes); |
| 361 | |
| 362 | transfer_value(dst.source.global_vars, src.source.global_vars); |
| 363 | |
| 364 | transfer_value(dst.functions.pre_init, src.functions.pre_init); |
| 365 | transfer_value(dst.functions.post_init, src.functions.post_init); |
| 366 | |
| 367 | transfer_value(dst.functions.set_param, src.functions.set_param); |
| 368 | transfer_value(dst.functions.get_param, src.functions.get_param); |
| 369 | transfer_value(dst.functions.log_param, src.functions.log_param); |
| 370 | transfer_value(dst.functions.init_comp, src.functions.init_comp); |
| 371 | transfer_value(dst.functions.start, src.functions.start); |
| 372 | transfer_value(dst.functions.control, src.functions.control); |
| 373 | } |
| 374 | |
| 375 | string CodeGenHelper::get_key(Ttcn::Definition& def) const { |
| 376 | string retval; |
| 377 | switch (split_mode) { |
| 378 | case SPLIT_NONE: |
| 379 | // returns the current module |
| 380 | break; |
| 381 | case SPLIT_BY_KIND: |
| 382 | break; |
| 383 | case SPLIT_BY_NAME: |
| 384 | retval += "_" + def.get_id().get_name(); |
| 385 | break; |
| 386 | case SPLIT_BY_HEURISTICS: |
| 387 | break; |
| 388 | case SPLIT_TO_SLICES: |
| 389 | break; |
| 390 | } |
| 391 | return retval; |
| 392 | } |
| 393 | |
| 394 | string CodeGenHelper::get_key(Type& type) const { |
| 395 | string retval; |
| 396 | switch (split_mode) { |
| 397 | case SPLIT_NONE: |
| 398 | break; |
| 399 | case SPLIT_BY_KIND: { |
| 400 | Type::typetype_t tt = type.get_typetype(); |
| 401 | switch(tt) { |
| 402 | case Type::T_CHOICE_A: |
| 403 | case Type::T_CHOICE_T: |
| 404 | case Type::T_SEQOF: |
| 405 | case Type::T_SETOF: |
| 406 | case Type::T_SEQ_A: |
| 407 | case Type::T_SEQ_T: |
| 408 | case Type::T_SET_A: |
| 409 | case Type::T_SET_T: |
| 410 | retval += typetypemap[(int)tt]; |
| 411 | break; |
| 412 | default: |
| 413 | // put it into the module (no suffix) |
| 414 | break; |
| 415 | } |
| 416 | break; } |
| 417 | case SPLIT_BY_NAME: |
| 418 | break; |
| 419 | case SPLIT_BY_HEURISTICS: |
| 420 | break; |
| 421 | case SPLIT_TO_SLICES: |
| 422 | break; |
| 423 | } |
| 424 | return retval; |
| 425 | } |
| 426 | |
| 427 | void CodeGenHelper::write_output() { |
| 428 | size_t i, j; |
| 429 | if (split_mode == SPLIT_BY_KIND) { |
| 430 | // Create empty files to have a fix set of files to compile |
| 431 | string fname; |
| 432 | for (j = 0; j < modules.size(); j++) { |
| 433 | for (i = 0; typetypemap[i]; i++) { |
| 434 | fname = modules[j]->dispname + typetypemap[i]; |
| 435 | if (!generated_code.has_key(fname)) { |
| 436 | generated_output_t* go = new generated_output_t; |
| 437 | go->filename = typetypemap[i]; |
| 438 | go->modulename = modules[j]->name; |
| 439 | go->module_dispname = modules[j]->dispname; |
| 440 | go->os.source.includes = mcopystr( |
| 441 | "\n//This file is intentionally empty." |
| 442 | "\n#include <version.h>\n"); |
| 443 | generated_code.add(fname, go); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } else if (split_mode == SPLIT_TO_SLICES && slice_num > 0) { |
| 448 | // The strategy is the following: |
| 449 | // Goal: Get files with equal size |
| 450 | // Get the longest file's length and divide it by slice_num (chunk_size) |
| 451 | // We split every file to chunk_size sized chunks |
| 452 | size_t max = 0; |
| 453 | // Calculate maximum character length |
| 454 | for (j = 0; j < generated_code.size(); j++) { |
| 455 | update_intervals(&generated_code.get_nth_elem(j)->os); |
| 456 | size_t num_of_chars = size_of_sources(&generated_code.get_nth_elem(j)->os); |
| 457 | if (max < num_of_chars) { |
| 458 | max = num_of_chars; |
| 459 | } |
| 460 | } |
| 461 | // Calculate ideal chunk size |
| 462 | size_t chunk_size = max / slice_num; |
| 463 | string fname; |
| 464 | for (j = 0; j < modules.size(); j++) { |
| 465 | generated_output_t *output = generated_code[modules[j]->dispname]; |
| 466 | |
| 467 | // Just to be sure that everything is in the right place |
| 468 | update_intervals(&output->os); |
| 469 | |
| 470 | // Move static function prototypes to header (no longer static) |
| 471 | output->os.header.function_prototypes = mputstr(output->os.header.function_prototypes, output->os.source.static_function_prototypes); |
| 472 | Free(output->os.source.static_function_prototypes); |
| 473 | output->os.source.static_function_prototypes = NULL; |
| 474 | |
| 475 | output->os.header.function_prototypes = mputstr(output->os.header.function_prototypes, output->os.source.static_conversion_function_prototypes); |
| 476 | Free(output->os.source.static_conversion_function_prototypes); |
| 477 | output->os.source.static_conversion_function_prototypes = NULL; |
| 478 | |
| 479 | // Move internal class definitions to the header |
| 480 | output->os.header.class_defs = mputstr(output->os.header.class_defs, output->os.source.class_defs); |
| 481 | Free(output->os.source.class_defs); |
| 482 | output->os.source.class_defs = NULL; |
| 483 | |
| 484 | update_intervals(&output->os); |
| 485 | size_t num_of_chars = size_of_sources(&output->os); |
| 486 | char buffer[13]= ""; // Max is 999999 should be enough (checked in main.cc) | 6 digit + 2 underscore + part |
| 487 | // If we need to split |
| 488 | if (num_of_chars >= chunk_size) { |
| 489 | size_t base_pos = 0; |
| 490 | for (unsigned int i = 0; i < slice_num; i++) { |
| 491 | if (i == 0) { // The first slice has the module's name |
| 492 | fname = output->module_dispname; |
| 493 | } else { |
| 494 | sprintf(buffer, "_part_%d", (int)i); |
| 495 | fname = output->module_dispname + "_" + buffer; |
| 496 | } |
| 497 | if (i == 0 || !generated_code.has_key(fname)) { |
| 498 | generated_output_t* go = new generated_output_t; |
| 499 | go->filename = buffer; |
| 500 | go->modulename = output->modulename; |
| 501 | go->module_dispname = output->module_dispname; |
| 502 | size_t act_pos = get_next_chunk_pos(&output->os, &go->os, base_pos, chunk_size); |
| 503 | // True if the file is not empty |
| 504 | if (act_pos > base_pos) { |
| 505 | go->os.source.includes = mputstr(go->os.source.includes, output->os.source.includes); |
| 506 | } else { |
| 507 | go->os.source.includes = mcopystr( |
| 508 | "\n//This file is intentionally empty." |
| 509 | "\n#include <version.h>\n"); |
| 510 | } |
| 511 | // First slice: copy header and other properties and replace the original output struct |
| 512 | if (i == 0) { |
| 513 | go->has_circular_import = output->has_circular_import; |
| 514 | go->is_module = output->is_module; |
| 515 | go->is_ttcn = output->is_ttcn; |
| 516 | go->os.header.class_decls = mputstr(go->os.header.class_decls, output->os.header.class_decls); |
| 517 | go->os.header.class_defs = mputstr(go->os.header.class_defs, output->os.header.class_defs); |
| 518 | go->os.header.function_prototypes = mputstr(go->os.header.function_prototypes, output->os.header.function_prototypes); |
| 519 | go->os.header.global_vars = mputstr(go->os.header.global_vars, output->os.header.global_vars); |
| 520 | go->os.header.includes = mputstr(go->os.header.includes, output->os.header.includes); |
| 521 | go->os.header.testport_includes = mputstr(go->os.header.testport_includes, output->os.header.testport_includes); |
| 522 | go->os.header.typedefs = mputstr(go->os.header.typedefs, output->os.header.typedefs); |
| 523 | generated_code[modules[j]->dispname] = go; |
| 524 | } else { |
| 525 | generated_code.add(fname, go); |
| 526 | } |
| 527 | base_pos = act_pos; |
| 528 | } else { |
| 529 | // TODO: error handling: there is a module which has the same name as the |
| 530 | // numbered splitted file. splitting by type does not have this error |
| 531 | // handling so don't we |
| 532 | } |
| 533 | } |
| 534 | // Extra safety. If something is missing after the splitting, put the remaining |
| 535 | // things to the last file. (Should never happen) |
| 536 | if (base_pos < num_of_chars) { |
| 537 | get_next_chunk_pos(&output->os, &generated_code[fname]->os, base_pos, num_of_chars); |
| 538 | } |
| 539 | delete output; |
| 540 | } else { |
| 541 | // Create empty files. |
| 542 | for (i = 1; i < slice_num; i++) { |
| 543 | sprintf(buffer, "_part_%d", (int)i); |
| 544 | fname = output->module_dispname + "_" + buffer; |
| 545 | if (!generated_code.has_key(fname)) { |
| 546 | generated_output_t* go = new generated_output_t; |
| 547 | go->filename = buffer; |
| 548 | go->modulename = modules[j]->name; |
| 549 | go->module_dispname = modules[j]->dispname; |
| 550 | go->os.source.includes = mcopystr( |
| 551 | "\n//This file is intentionally empty." |
| 552 | "\n#include <version.h>\n"); |
| 553 | generated_code.add(fname, go); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | generated_output_t* go; |
| 560 | for (i = 0; i < generated_code.size(); i++) { |
| 561 | go = generated_code.get_nth_elem(i); |
| 562 | ::write_output(&go->os, go->modulename.c_str(), go->module_dispname.c_str(), |
| 563 | go->filename.c_str(), go->is_ttcn, go->has_circular_import, go->is_module); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | CodeGenHelper::~CodeGenHelper() { |
| 568 | size_t i; |
| 569 | for (i = 0; i < generated_code.size(); i++) |
| 570 | delete generated_code.get_nth_elem(i); |
| 571 | generated_code.clear(); |
| 572 | for (i = 0; i < modules.size(); i++) |
| 573 | delete modules[i]; |
| 574 | modules.clear(); |
| 575 | instance = 0; |
| 576 | } |
| 577 | |
| 578 | } |