authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-28 00:44:16-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-28 00:44:16-05:00
logf537c51f2560564042da690e8fe3b6e85d9592c8
tree8c1570ea09a4978047bb45bd889f0f4dd989bb9c
parent1ab84a27d374e666463c606dc1cd1c4972b52a74
parent57049b95b3fe183ea995f212b18e70212eaac23e

Merge branch 'c-field-expr' of https://github.com/dimenus/zig into dimenus-c-field-expr


5 files changed, 70 insertions(+), 7 deletions(-)

src/c_tokenizer.cpp+4-3
......@@ -216,9 +216,8 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
216216 buf_append_char(&ctok->buf, '0');
217217 break;
218218 case '.':
219 begin_token(ctok, CTokIdNumLitFloat);
220 ctok->state = CTokStateFloat;
221 buf_init_from_str(&ctok->buf, "0.");
219 begin_token(ctok, CTokIdDot);
220 end_token(ctok);
222221 break;
223222 case '(':
224223 begin_token(ctok, CTokIdLParen);
......@@ -238,6 +237,8 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
238237 break;
239238 case CTokStateFloat:
240239 switch (*c) {
240 case '.':
241 break;
241242 case 'e':
242243 case 'E':
243244 buf_append_char(&ctok->buf, 'e');
src/c_tokenizer.hpp+1
......@@ -21,6 +21,7 @@ enum CTokId {
2121 CTokIdLParen,
2222 CTokIdRParen,
2323 CTokIdEOF,
24 CTokIdDot,
2425};
2526
2627enum CNumLitSuffix {
src/ir.cpp+2-1
......@@ -6302,8 +6302,9 @@ static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char
63026302 buf_appendf(name, ")");
63036303 return name;
63046304 } else {
6305 //Note: C-imports do not have valid location information
63056306 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);
63076308 }
63086309 }
63096310}
src/translate_c.cpp+27-3
......@@ -3517,7 +3517,6 @@ static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl) {
35173517 }
35183518
35193519 const char *raw_name = decl_name(record_decl);
3520
35213520 const char *container_kind_name;
35223521 ContainerKind container_kind;
35233522 if (record_decl->isUnion()) {
......@@ -3869,9 +3868,33 @@ static AstNode *parse_ctok(Context *c, CTokenize *ctok, size_t *tok_i) {
38693868 return parse_ctok_num_lit(c, ctok, tok_i, false);
38703869 case CTokIdSymbol:
38713870 {
3872 *tok_i += 1;
3871 bool need_symbol = false;
3872 CTokId curr_id = CTokIdSymbol;
38733873 Buf *symbol_name = buf_create_from_buf(&tok->data.symbol);
3874 return trans_create_node_symbol(c, symbol_name);
3874 AstNode *curr_node = trans_create_node_symbol(c, symbol_name);
3875 AstNode *parent_node = curr_node;
3876 do {
3877 *tok_i += 1;
3878 CTok* curr_tok = &ctok->tokens.at(*tok_i);
3879 if (need_symbol) {
3880 if (curr_tok->id == CTokIdSymbol) {
3881 symbol_name = buf_create_from_buf(&curr_tok->data.symbol);
3882 curr_node = trans_create_node_field_access(c, parent_node, buf_create_from_buf(symbol_name));
3883 parent_node = curr_node;
3884 need_symbol = false;
3885 } else {
3886 return nullptr;
3887 }
3888 } else {
3889 if (curr_tok->id == CTokIdDot) {
3890 need_symbol = true;
3891 continue;
3892 } else {
3893 break;
3894 }
3895 }
3896 } while (curr_id != CTokIdEOF);
3897 return curr_node;
38753898 }
38763899 case CTokIdLParen:
38773900 {
......@@ -3885,6 +3908,7 @@ static AstNode *parse_ctok(Context *c, CTokenize *ctok, size_t *tok_i) {
38853908 *tok_i += 1;
38863909 return inner_node;
38873910 }
3911 case CTokIdDot:
38883912 case CTokIdEOF:
38893913 case CTokIdRParen:
38903914 // not able to make sense of this
test/translate_c.zig+36
......@@ -1046,6 +1046,42 @@ pub fn addCases(cases: &tests.TranslateCContext) {
10461046 \\ return x + 13;
10471047 \\}
10481048 );
1049
1050 cases.add("macros with field targets",
1051 \\typedef unsigned int GLbitfield;
1052 \\typedef void (*PFNGLCLEARPROC) (GLbitfield mask);
1053 \\typedef void(*OpenGLProc)(void);
1054 \\union OpenGLProcs {
1055 \\ OpenGLProc ptr[1];
1056 \\ struct {
1057 \\ PFNGLCLEARPROC Clear;
1058 \\ } gl;
1059 \\};
1060 \\extern union OpenGLProcs glProcs;
1061 \\#define glClearUnion glProcs.gl.Clear
1062 \\#define glClearPFN PFNGLCLEARPROC
1063 ,
1064 \\pub const GLbitfield = c_uint;
1065 ,
1066 \\pub const PFNGLCLEARPROC = ?extern fn(GLbitfield);
1067 ,
1068 \\pub const OpenGLProc = ?extern fn();
1069 ,
1070 \\pub const union_OpenGLProcs = extern union {
1071 \\ ptr: [1]OpenGLProc,
1072 \\ gl: extern struct {
1073 \\ Clear: PFNGLCLEARPROC,
1074 \\ },
1075 \\};
1076 ,
1077 \\pub extern var glProcs: union_OpenGLProcs;
1078 ,
1079 \\pub const glClearPFN = PFNGLCLEARPROC;
1080 ,
1081 \\pub const glClearUnion = glProcs.gl.Clear;
1082 ,
1083 \\pub const OpenGLProcs = union_OpenGLProcs;
1084 );
10491085
10501086 cases.add("switch statement with no default",
10511087 \\int foo(int x) {