cint_eval_ast_print.c (6092B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: cint_eval_ast_print.c 8 * Purpose: CINT print function 9 */ 10 11 #include "cint_eval_ast_print.h" 12 #include "cint_porting.h" 13 #include "cint_variables.h" 14 #include "cint_internal.h" 15 #include "cint_error.h" 16 #include "cint_debug.h" 17 #include "cint_eval_asts.h" 18 19 20 cint_variable_t* 21 cint_eval_ast_Print(cint_ast_t* ast) 22 { 23 cint_variable_t* v = NULL; 24 char _rstr[256] = {0}; 25 26 if(ast) { 27 28 switch(ast->utype.print.expression->ntype) 29 { 30 /* 31 * Make the output nicer for immediate strings and integers. 32 */ 33 case cintAstString: 34 { 35 /* print immediate string value */ 36 char *tmp, *nl; 37 38 tmp = cint_cstring_value 39 (ast->utype.print.expression->utype.string.s); 40 CINT_PRINTF("%s", tmp); 41 42 /* Check if the string terminates in a newline. If 43 not, then issue one. */ 44 nl=CINT_STRRCHR(tmp, '\n'); 45 if (nl == NULL || nl[1] != 0) { 46 /* 47 nl is NULL if there were no newlines in the 48 string at all. nl[0] is non-zero if the 49 last newline in the string is not before the 50 terminating null. 51 */ 52 CINT_PRINTF("\n"); 53 } 54 CINT_FREE(tmp); 55 break; 56 } 57 case cintAstInteger: 58 { 59 /* print immediate integer value */ 60 CINT_PRINTF("%ld\n", ast->utype.print.expression->utype.integer.i); 61 break; 62 } 63 #if CINT_CONFIG_INCLUDE_LONGLONGS == 1 64 case cintAstLongLong: 65 { 66 long long i = 67 ast->utype.print.expression->utype._longlong.i; 68 char buf1[50], *s1; 69 s1 = cint_lltoa(buf1, sizeof(buf1), i, 1, 10, 0); 70 /* print immediate integer value */ 71 CINT_PRINTF("%s\n", s1); 72 break; 73 } 74 #endif 75 case cintAstType: 76 { 77 /* Lookup the type and print information about it */ 78 cint_datatype_t dt; 79 CINT_MEMSET(&dt, 0, sizeof(dt)); 80 if(cint_datatype_find(ast->utype.print.expression->utype.type.s, &dt) == CINT_E_NONE) { 81 switch((dt.flags & CINT_DATATYPE_FLAGS_TYPE)) 82 { 83 case CINT_DATATYPE_F_ATOMIC: 84 { 85 CINT_PRINTF("%s: atomic datatype, size %d bytes\n", dt.basetype.ap->name, dt.basetype.ap->size); 86 break; 87 } 88 case CINT_DATATYPE_F_STRUCT: 89 { 90 const cint_parameter_desc_t* sm; 91 CINT_PRINTF("struct %s {\n", dt.basetype.sp->name); 92 for(sm = dt.basetype.sp->struct_members; sm->basetype; sm++) { 93 cint_datatype_t dt; 94 dt.desc = *sm; 95 CINT_PRINTF(" %s %s;\n", cint_datatype_format(&dt, _rstr, 0), sm->name); 96 } 97 CINT_PRINTF("}\n"); 98 CINT_PRINTF("size is %d bytes\n", dt.basetype.sp->size); 99 break; 100 } 101 case CINT_DATATYPE_F_ENUM: 102 { 103 const cint_enum_map_t* ep; 104 CINT_PRINTF("enum %s {\n", dt.basetype.ep->name); 105 for(ep = dt.basetype.ep->enum_map; ep->name; ep++) { 106 CINT_PRINTF(" %s = %d\n", ep->name, ep->value); 107 } 108 CINT_PRINTF("}\n"); 109 break; 110 } 111 case CINT_DATATYPE_F_FUNC_POINTER: 112 { 113 cint_parameter_desc_t* p = dt.basetype.fpp->params; 114 CINT_PRINTF("function pointer: '%s (*%s)", cint_datatype_format_pd(p, _rstr, 0), dt.basetype.fpp->name); 115 cint_fparams_print(p+1); 116 CINT_PRINTF("'\n"); 117 break; 118 } 119 case CINT_DATATYPE_F_TYPEDEF: 120 { 121 /* Nothing yet */ 122 break; 123 } 124 } 125 } 126 else { 127 /* Should never get here */ 128 } 129 break; 130 } 131 default: 132 { 133 v = cint_eval_ast(ast->utype.print.expression); 134 135 if(v) { 136 cint_variable_print(v, 0, v->name); 137 } 138 else { 139 /* No error necessary -- an error would have been printed during the evaluation */ 140 } 141 break; 142 } 143 } 144 } 145 146 return v; 147 }