LCOV - code coverage report
Current view: top level - src/compiler - codegen_c.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 61 80.3 %
Date: 2016-11-30 13:12:14 Functions: 4 4 100.0 %
Branches: 50 74 67.6 %

           Branch data     Line data    Source code
       1                 :            : #ifndef CODEGEN_C_H
       2                 :            : #define CODEGEN_C_H
       3                 :            : 
       4                 :            : #include <assert.h>
       5                 :            : #include <stdarg.h>
       6                 :            : 
       7                 :            : #include "symbols.h"
       8                 :            : #include "parser.h"
       9                 :            : #include "codegen.h"
      10                 :            : 
      11                 :            : #define __FLATCC_ERROR_TYPE "INTERNAL_ERROR_UNEXPECTED_TYPE"
      12                 :            : 
      13                 :            : #ifndef gen_panic
      14                 :            : #define gen_panic(context, msg) fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, msg), assert(0), exit(-1)
      15                 :            : #endif
      16                 :            : 
      17                 :            : static inline void token_name(fb_token_t *t, int *n, const char **s) {
      18                 :       1882 :     *n = t->len;
      19                 :            :     *s = t->text;
      20                 :            : }
      21                 :            : 
      22                 :            : typedef char fb_symbol_text_t[FLATCC_NAME_BUFSIZ];
      23                 :            : typedef struct fb_scoped_name fb_scoped_name_t;
      24                 :            : 
      25                 :            : /* Should be zeroed because scope is cached across updates. */
      26                 :            : struct fb_scoped_name {
      27                 :            :     fb_symbol_text_t text;
      28                 :            :     fb_scope_t *scope;
      29                 :            :     int scope_len, len, total_len;
      30                 :            : };
      31                 :            : 
      32                 :            : #define fb_clear(x) (memset(&(x), 0, sizeof(x)))
      33                 :            : 
      34                 :            : /* Returns length or -1 if length exceeds namespace max. */
      35                 :            : int __flatcc_fb_copy_scope(fb_scope_t *scope, char *buf);
      36                 :            : #define fb_copy_scope __flatcc_fb_copy_scope
      37                 :            : 
      38                 :            : void __flatcc_fb_scoped_symbol_name(fb_scope_t *scope, fb_symbol_t *sym, fb_scoped_name_t *sn);
      39                 :            : #define fb_scoped_symbol_name __flatcc_fb_scoped_symbol_name
      40                 :            : 
      41                 :            : static inline void fb_compound_name(fb_compound_type_t *ct, fb_scoped_name_t *sn)
      42                 :            : {
      43                 :       1776 :     fb_scoped_symbol_name(ct->scope, &ct->symbol, sn);
      44                 :            : }
      45                 :            : 
      46                 :            : static inline void symbol_name(fb_symbol_t *sym, int *n, const char **s) {
      47                 :            :     token_name(sym->ident, n, s);
      48                 :            : }
      49                 :            : 
      50                 :            : static inline const char *scalar_type_ns(fb_scalar_type_t scalar_type, const char *ns)
      51                 :            : {
      52 [ +  - ][ +  + ]:        891 :     return scalar_type == fb_bool ? ns : "";
         [ +  - ][ +  + ]
         [ +  + ][ +  + ]
         [ +  - ][ +  - ]
      53                 :            : }
      54                 :            : 
      55                 :        795 : static inline const char *scalar_type_prefix(fb_scalar_type_t scalar_type)
      56                 :            : {
      57                 :            :     const char *tname;
      58   [ +  +  +  +  :        795 :     switch (scalar_type) {
          +  +  +  +  +  
                +  -  + ]
      59                 :            :     case fb_ulong:
      60                 :            :         tname = "uint64";
      61                 :            :         break;
      62                 :            :     case fb_uint:
      63                 :            :         tname = "uint32";
      64                 :         55 :         break;
      65                 :            :     case fb_ushort:
      66                 :            :         tname = "uint16";
      67                 :         16 :         break;
      68                 :            :     case fb_ubyte:
      69                 :            :         tname = "uint8";
      70                 :         85 :         break;
      71                 :            :     case fb_bool:
      72                 :            :         tname = "bool";
      73                 :         40 :         break;
      74                 :            :     case fb_long:
      75                 :            :         tname = "int64";
      76                 :         73 :         break;
      77                 :            :     case fb_int:
      78                 :            :         tname = "int32";
      79                 :         64 :         break;
      80                 :            :     case fb_short:
      81                 :            :         tname = "int16";
      82                 :        101 :         break;
      83                 :            :     case fb_byte:
      84                 :            :         tname = "int8";
      85                 :         92 :         break;
      86                 :            :     case fb_float:
      87                 :            :         tname = "float";
      88                 :        182 :         break;
      89                 :            :     case fb_double:
      90                 :            :         tname = "double";
      91                 :         55 :         break;
      92                 :            :     default:
      93                 :          0 :         gen_panic(out, "internal error: unexpected type during code generation");
      94                 :            :         tname = __FLATCC_ERROR_TYPE;
      95                 :            :         break;
      96                 :            :     }
      97                 :        795 :     return tname;
      98                 :            : }
      99                 :            : 
     100                 :        923 : static inline const char *scalar_type_name(fb_scalar_type_t scalar_type)
     101                 :            : {
     102                 :            :     const char *tname;
     103   [ +  +  +  +  :        923 :     switch (scalar_type) {
          +  +  +  +  +  
                +  -  + ]
     104                 :            :     case fb_ulong:
     105                 :            :         tname = "uint64_t";
     106                 :            :         break;
     107                 :            :     case fb_uint:
     108                 :            :         tname = "uint32_t";
     109                 :         73 :         break;
     110                 :            :     case fb_ushort:
     111                 :            :         tname = "uint16_t";
     112                 :         22 :         break;
     113                 :            :     case fb_ubyte:
     114                 :            :         tname = "uint8_t";
     115                 :         87 :         break;
     116                 :            :     case fb_bool:
     117                 :            :         tname = "bool_t";
     118                 :         38 :         break;
     119                 :            :     case fb_long:
     120                 :            :         tname = "int64_t";
     121                 :        102 :         break;
     122                 :            :     case fb_int:
     123                 :            :         tname = "int32_t";
     124                 :        105 :         break;
     125                 :            :     case fb_short:
     126                 :            :         tname = "int16_t";
     127                 :        138 :         break;
     128                 :            :     case fb_byte:
     129                 :            :         tname = "int8_t";
     130                 :        108 :         break;
     131                 :            :     case fb_float:
     132                 :            :         tname = "float";
     133                 :        157 :         break;
     134                 :            :     case fb_double:
     135                 :            :         tname = "double";
     136                 :         49 :         break;
     137                 :            :     default:
     138                 :          0 :         gen_panic(out, "internal error: unexpected type during code generation");
     139                 :            :         tname = __FLATCC_ERROR_TYPE;
     140                 :            :         break;
     141                 :            :     }
     142                 :        923 :     return tname;
     143                 :            : }
     144                 :            : 
     145                 :         25 : static inline const char *scalar_vector_type_name(fb_scalar_type_t scalar_type)
     146                 :            : {
     147                 :            :     const char *tname;
     148   [ -  -  +  +  :         25 :     switch (scalar_type) {
          -  -  -  -  +  
                -  -  - ]
     149                 :            :     case fb_ulong:
     150                 :            :         tname = "uint64_vec_t";
     151                 :            :         break;
     152                 :            :     case fb_uint:
     153                 :            :         tname = "uint32_vec_t";
     154                 :          0 :         break;
     155                 :            :     case fb_ushort:
     156                 :            :         tname = "uint16_vec_t";
     157                 :          0 :         break;
     158                 :            :     case fb_ubyte:
     159                 :            :         tname = "uint8_vec_t";
     160                 :         17 :         break;
     161                 :            :     case fb_bool:
     162                 :            :         tname = "uint8_vec_t";
     163                 :          7 :         break;
     164                 :            :     case fb_long:
     165                 :            :         tname = "int64_vec_t";
     166                 :          0 :         break;
     167                 :            :     case fb_int:
     168                 :            :         tname = "int32_vec_t";
     169                 :          0 :         break;
     170                 :            :     case fb_short:
     171                 :            :         tname = "int16_vec_t";
     172                 :          0 :         break;
     173                 :            :     case fb_byte:
     174                 :            :         tname = "bool_vec_t";
     175                 :          0 :         break;
     176                 :            :     case fb_float:
     177                 :            :         tname = "float_vec_t";
     178                 :          1 :         break;
     179                 :            :     case fb_double:
     180                 :            :         tname = "double_vec_t";
     181                 :          0 :         break;
     182                 :            :     default:
     183                 :          0 :         gen_panic(out, "internal error: unexpected type during code generation");
     184                 :            :         tname = __FLATCC_ERROR_TYPE;
     185                 :            :         break;
     186                 :            :     }
     187                 :         25 :     return tname;
     188                 :            : }
     189                 :            : 
     190                 :         36 : static inline const char *scalar_suffix(fb_scalar_type_t scalar_type)
     191                 :            : {
     192                 :            :     const char *suffix;
     193   [ +  -  +  +  :         36 :     switch (scalar_type) {
          +  +  +  +  -  
                      - ]
     194                 :            :     case fb_ulong:
     195                 :            :         suffix = "ULL";
     196                 :            :         break;
     197                 :            :     case fb_uint:
     198                 :            :         suffix = "UL";
     199                 :          1 :         break;
     200                 :            :     case fb_ushort:
     201                 :            :         suffix = "U";
     202                 :          0 :         break;
     203                 :            :     case fb_ubyte:
     204                 :            :         suffix = "U";
     205                 :          9 :         break;
     206                 :            :     case fb_bool:
     207                 :            :         suffix = "U";
     208                 :          1 :         break;
     209                 :            :     case fb_long:
     210                 :            :         suffix = "LL";
     211                 :          7 :         break;
     212                 :            :     case fb_int:
     213                 :            :         suffix = "L";
     214                 :          8 :         break;
     215                 :            :     case fb_short:
     216                 :            :         suffix = "";
     217                 :          2 :         break;
     218                 :            :     case fb_byte:
     219                 :            :         suffix = "";
     220                 :          8 :         break;
     221                 :            :     case fb_double:
     222                 :            :         suffix = "";
     223                 :            :     case fb_float:
     224                 :            :         suffix = "F";
     225                 :            :     default:
     226                 :          0 :         gen_panic(out, "internal error: unexpected type during code generation");
     227                 :            :         suffix = "";
     228                 :            :         break;
     229                 :            :     }
     230                 :         36 :     return suffix;
     231                 :            : }
     232                 :            : 
     233                 :            : /* See also: https://github.com/philsquared/Catch/issues/376 */
     234                 :            : static inline int gen_pragma_push(fb_output_t *out)
     235                 :            : {
     236   [ +  -  +  - ]:         97 :     if (out->opts->cgen_pragmas) {
         [ +  - ][ +  - ]
     237                 :         97 :         fprintf(out->fp,
     238                 :            :                 "#define PDIAGNOSTIC_IGNORE_UNUSED\n"
     239                 :            :                 "#include \"flatcc/portable/pdiagnostic_push.h\"\n");
     240                 :            :     }
     241                 :            :     return 0;
     242                 :            : }
     243                 :            : 
     244                 :            : static inline int gen_pragma_pop(fb_output_t *out)
     245                 :            : {
     246   [ +  -  +  - ]:         97 :     if (out->opts->cgen_pragmas) {
     247                 :         97 :         fprintf(out->fp,
     248                 :            :                 "#include \"flatcc/portable/pdiagnostic_pop.h\"\n");
     249                 :            :     }
     250                 :            :     return 0;
     251                 :            : }
     252                 :            : 
     253                 :            : /* This assumes the output context is named out which it is by convention. */
     254                 :            : #define indent() (out->indent++)
     255                 :            : #define unindent() { assert(out->indent); out->indent--; }
     256                 :            : #define margin() { out->tmp_indent = out->indent; out->indent = 0; }
     257                 :            : #define unmargin() { out->indent = out->tmp_indent; }
     258                 :            : 
     259                 :            : /* Redefine names to avoid polluting library namespace. */
     260                 :            : 
     261                 :            : int __flatcc_fb_init_output_c(fb_output_t *out, fb_options_t *opts);
     262                 :            : #define fb_init_output_c __flatcc_fb_init_output_c
     263                 :            : 
     264                 :            : int __flatcc_fb_open_output_file(fb_output_t *out, const char *name, size_t len, const char *ext);
     265                 :            : #define fb_open_output_file __flatcc_fb_open_output_file
     266                 :            : 
     267                 :            : void __flatcc_fb_close_output_file(fb_output_t *out);
     268                 :            : #define fb_close_output_file __flatcc_fb_close_output_file
     269                 :            : 
     270                 :            : void __flatcc_fb_gen_c_includes(fb_output_t *out, const char *ext, const char *extup);
     271                 :            : #define fb_gen_c_includes __flatcc_fb_gen_c_includes
     272                 :            : 
     273                 :            : int __flatcc_fb_gen_common_c_header(fb_output_t *out);
     274                 :            : #define fb_gen_common_c_header __flatcc_fb_gen_common_c_header
     275                 :            : 
     276                 :            : int __flatcc_fb_gen_common_c_builder_header(fb_output_t *out);
     277                 :            : #define fb_gen_common_c_builder_header __flatcc_fb_gen_common_c_builder_header
     278                 :            : 
     279                 :            : int __flatcc_fb_gen_c_reader(fb_output_t *out);
     280                 :            : #define fb_gen_c_reader __flatcc_fb_gen_c_reader
     281                 :            : 
     282                 :            : int __flatcc_fb_gen_c_builder(fb_output_t *out);
     283                 :            : #define fb_gen_c_builder __flatcc_fb_gen_c_builder
     284                 :            : 
     285                 :            : int __flatcc_fb_gen_c_verifier(fb_output_t *out);
     286                 :            : #define fb_gen_c_verifier __flatcc_fb_gen_c_verifier
     287                 :            : 
     288                 :            : int __flatcc_fb_gen_c_json_parser(fb_output_t *out);
     289                 :            : #define fb_gen_c_json_parser __flatcc_fb_gen_c_json_parser
     290                 :            : 
     291                 :            : int __flatcc_fb_gen_c_json_printer(fb_output_t *out);
     292                 :            : #define fb_gen_c_json_printer __flatcc_fb_gen_c_json_printer
     293                 :            : 
     294                 :            : #endif /* CODEGEN_C_H */

Generated by: LCOV version 1.12