| author | |
| committer | |
| log | 04472f57be5e91e82adf9346e71c1421725716d5 |
| tree | 7c9cddd7234a4c79fb6aa548ddf4984ccbb175da |
| parent | 1b0e90f70b4dc26c2ba96b7b5709a3ff269bb48a |
5 files changed, 70 insertions(+), 9 deletions(-)
src/c_tokenizer.cpp+4-3| ... | @@ -216,9 +216,8 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) { | ... | @@ -216,9 +216,8 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) { |
| 216 | buf_append_char(&ctok->buf, '0'); | 216 | buf_append_char(&ctok->buf, '0'); |
| 217 | break; | 217 | break; |
| 218 | case '.': | 218 | case '.': |
| 219 | begin_token(ctok, CTokIdNumLitFloat); | 219 | begin_token(ctok, CTokIdDot); |
| 220 | ctok->state = CTokStateFloat; | 220 | end_token(ctok); |
| 221 | buf_init_from_str(&ctok->buf, "0."); | ||
| 222 | break; | 221 | break; |
| 223 | case '(': | 222 | case '(': |
| 224 | begin_token(ctok, CTokIdLParen); | 223 | begin_token(ctok, CTokIdLParen); |
| ... | @@ -238,6 +237,8 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) { | ... | @@ -238,6 +237,8 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) { |
| 238 | break; | 237 | break; |
| 239 | case CTokStateFloat: | 238 | case CTokStateFloat: |
| 240 | switch (*c) { | 239 | switch (*c) { |
| 240 | case '.': | ||
| 241 | break; | ||
| 241 | case 'e': | 242 | case 'e': |
| 242 | case 'E': | 243 | case 'E': |
| 243 | buf_append_char(&ctok->buf, 'e'); | 244 | buf_append_char(&ctok->buf, 'e'); |
src/c_tokenizer.hpp+1| ... | @@ -21,6 +21,7 @@ enum CTokId { | ... | @@ -21,6 +21,7 @@ enum CTokId { |
| 21 | CTokIdLParen, | 21 | CTokIdLParen, |
| 22 | CTokIdRParen, | 22 | CTokIdRParen, |
| 23 | CTokIdEOF, | 23 | CTokIdEOF, |
| 24 | CTokIdDot, | ||
| 24 | }; | 25 | }; |
| 25 | 26 | ||
| 26 | enum CNumLitSuffix { | 27 | enum CNumLitSuffix { |
src/ir.cpp+2-1| ... | @@ -6302,8 +6302,9 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char | ... | @@ -6302,8 +6302,9 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char |
| 6302 | buf_appendf(name, ")"); | 6302 | buf_appendf(name, ")"); |
| 6303 | return name; | 6303 | return name; |
| 6304 | } else { | 6304 | } else { |
| 6305 | //Note: C-imports do not have valid location information | ||
| 6305 | return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name, | 6306 | return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name, |
| 6306 | buf_ptr(source_node->owner->path), source_node->line + 1, source_node->column + 1); | 6307 | (source_node->owner->path != nullptr) ? buf_ptr(source_node->owner->path) : "(null)", source_node->line + 1, source_node->column + 1); |
| 6307 | } | 6308 | } |
| 6308 | } | 6309 | } |
| 6309 | } | 6310 | } |
src/translate_c.cpp+27-3| ... | @@ -3442,7 +3442,6 @@ static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl) { | ... | @@ -3442,7 +3442,6 @@ static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl) { |
| 3442 | } | 3442 | } |
| 3443 | 3443 | ||
| 3444 | const char *raw_name = decl_name(record_decl); | 3444 | const char *raw_name = decl_name(record_decl); |
| 3445 | |||
| 3446 | const char *container_kind_name; | 3445 | const char *container_kind_name; |
| 3447 | ContainerKind container_kind; | 3446 | ContainerKind container_kind; |
| 3448 | if (record_decl->isUnion()) { | 3447 | if (record_decl->isUnion()) { |
| ... | @@ -3794,9 +3793,33 @@ static AstNode *parse_ctok(Context *c, CTokenize *ctok, size_t *tok_i) { | ... | @@ -3794,9 +3793,33 @@ static AstNode *parse_ctok(Context *c, CTokenize *ctok, size_t *tok_i) { |
| 3794 | return parse_ctok_num_lit(c, ctok, tok_i, false); | 3793 | return parse_ctok_num_lit(c, ctok, tok_i, false); |
| 3795 | case CTokIdSymbol: | 3794 | case CTokIdSymbol: |
| 3796 | { | 3795 | { |
| 3797 | *tok_i += 1; | 3796 | bool need_symbol = false; |
| 3797 | CTokId curr_id = CTokIdSymbol; | ||
| 3798 | Buf *symbol_name = buf_create_from_buf(&tok->data.symbol); | 3798 | Buf *symbol_name = buf_create_from_buf(&tok->data.symbol); |
| 3799 | return trans_create_node_symbol(c, symbol_name); | 3799 | AstNode *curr_node = trans_create_node_symbol(c, symbol_name); |
| 3800 | AstNode *parent_node = curr_node; | ||
| 3801 | do { | ||
| 3802 | *tok_i += 1; | ||
| 3803 | CTok* curr_tok = &ctok->tokens.at(*tok_i); | ||
| 3804 | if (need_symbol) { | ||
| 3805 | if (curr_tok->id == CTokIdSymbol) { | ||
| 3806 | symbol_name = buf_create_from_buf(&curr_tok->data.symbol); | ||
| 3807 | curr_node = trans_create_node_field_access(c, parent_node, buf_create_from_buf(symbol_name)); | ||
| 3808 | parent_node = curr_node; | ||
| 3809 | need_symbol = false; | ||
| 3810 | } else { | ||
| 3811 | return nullptr; | ||
| 3812 | } | ||
| 3813 | } else { | ||
| 3814 | if (curr_tok->id == CTokIdDot) { | ||
| 3815 | need_symbol = true; | ||
| 3816 | continue; | ||
| 3817 | } else { | ||
| 3818 | break; | ||
| 3819 | } | ||
| 3820 | } | ||
| 3821 | } while (curr_id != CTokIdEOF); | ||
| 3822 | return curr_node; | ||
| 3800 | } | 3823 | } |
| 3801 | case CTokIdLParen: | 3824 | case CTokIdLParen: |
| 3802 | { | 3825 | { |
| ... | @@ -3810,6 +3833,7 @@ static AstNode *parse_ctok(Context *c, CTokenize *ctok, size_t *tok_i) { | ... | @@ -3810,6 +3833,7 @@ static AstNode *parse_ctok(Context *c, CTokenize *ctok, size_t *tok_i) { |
| 3810 | *tok_i += 1; | 3833 | *tok_i += 1; |
| 3811 | return inner_node; | 3834 | return inner_node; |
| 3812 | } | 3835 | } |
| 3836 | case CTokIdDot: | ||
| 3813 | case CTokIdEOF: | 3837 | case CTokIdEOF: |
| 3814 | case CTokIdRParen: | 3838 | case CTokIdRParen: |
| 3815 | // not able to make sense of this | 3839 | // not able to make sense of this |
test/translate_c.zig+36-2| ... | @@ -1041,9 +1041,43 @@ pub fn addCases(cases: &tests.TranslateCContext) { | ... | @@ -1041,9 +1041,43 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 1041 | \\ return x + 13; | 1041 | \\ return x + 13; |
| 1042 | \\} | 1042 | \\} |
| 1043 | ); | 1043 | ); |
| 1044 | } | ||
| 1045 | |||
| 1046 | 1044 | ||
| 1045 | cases.add("macros with field targets", | ||
| 1046 | \\typedef unsigned int GLbitfield; | ||
| 1047 | \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask); | ||
| 1048 | \\typedef void(*OpenGLProc)(void); | ||
| 1049 | \\union OpenGLProcs { | ||
| 1050 | \\ OpenGLProc ptr[1]; | ||
| 1051 | \\ struct { | ||
| 1052 | \\ PFNGLCLEARPROC Clear; | ||
| 1053 | \\ } gl; | ||
| 1054 | \\}; | ||
| 1055 | \\extern union OpenGLProcs glProcs; | ||
| 1056 | \\#define glClearUnion glProcs.gl.Clear | ||
| 1057 | \\#define glClearPFN PFNGLCLEARPROC | ||
| 1058 | , | ||
| 1059 | \\pub const GLbitfield = c_uint; | ||
| 1060 | , | ||
| 1061 | \\pub const PFNGLCLEARPROC = ?extern fn(GLbitfield); | ||
| 1062 | , | ||
| 1063 | \\pub const OpenGLProc = ?extern fn(); | ||
| 1064 | , | ||
| 1065 | \\pub const union_OpenGLProcs = extern union { | ||
| 1066 | \\ ptr: [1]OpenGLProc, | ||
| 1067 | \\ gl: extern struct { | ||
| 1068 | \\ Clear: PFNGLCLEARPROC, | ||
| 1069 | \\ }, | ||
| 1070 | \\}; | ||
| 1071 | , | ||
| 1072 | \\pub extern var glProcs: union_OpenGLProcs; | ||
| 1073 | , | ||
| 1074 | \\pub const glClearPFN = PFNGLCLEARPROC; | ||
| 1075 | , | ||
| 1076 | \\pub const glClearUnion = glProcs.gl.Clear; | ||
| 1077 | , | ||
| 1078 | \\pub const OpenGLProcs = union_OpenGLProcs; | ||
| 1079 | ); | ||
| 1080 | } | ||
| 1047 | 1081 | ||
| 1048 | // TODO | 1082 | // TODO |
| 1049 | //float *ptrcast(int *a) { | 1083 | //float *ptrcast(int *a) { |