aboutsummaryrefslogtreecommitdiff
path: root/src/parseh.cpp
blob: 2c07f88a4ead3f45a138f37dfef1d886337c0c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * Copyright (c) 2015 Andrew Kelley
 *
 * This file is part of zig, which is MIT licensed.
 * See http://opensource.org/licenses/MIT
 */

#include "parseh.hpp"
#include "config.h"
#include "os.hpp"
#include "error.hpp"

#include <clang/Frontend/ASTUnit.h>
#include <clang/Frontend/CompilerInstance.h>

#include <string.h>

using namespace clang;

struct Context {
    ParseH *parse_h;
    bool warnings_on;
    VisibMod visib_mod;
    AstNode *c_void_decl_node;
};

static AstNode *type_node_from_qual_type(Context *c, QualType qt);

static AstNode *create_node(Context *c, NodeType type) {
    AstNode *node = allocate<AstNode>(1);
    node->type = type;
    return node;
}

static AstNode *simple_type_node(Context *c, const char *type_name) {
    AstNode *node = create_node(c, NodeTypeSymbol);
    buf_init_from_str(&node->data.symbol_expr.symbol, type_name);
    return node;
}

static const char *decl_name(const Decl *decl) {
    const NamedDecl *named_decl = static_cast<const NamedDecl *>(decl);
    return (const char *)named_decl->getName().bytes_begin();
}

static AstNode *create_typedef_node(Context *c, const char *new_name, const char *target_name) {
    AstNode *node = create_node(c, NodeTypeVariableDeclaration);
    buf_init_from_str(&node->data.variable_declaration.symbol, new_name);
    node->data.variable_declaration.is_const = true;
    node->data.variable_declaration.visib_mod = c->visib_mod;
    node->data.variable_declaration.expr = simple_type_node(c, target_name);
    return node;
}

static AstNode *convert_to_c_void(Context *c, AstNode *type_node) {
    if (type_node->type == NodeTypeSymbol &&
        buf_eql_str(&type_node->data.symbol_expr.symbol, "void"))
    {
        if (!c->c_void_decl_node) {
            c->c_void_decl_node = create_typedef_node(c, "c_void", "u8");
            c->parse_h->var_list.append(c->c_void_decl_node);
        }
        return simple_type_node(c, "c_void");
    } else {
        return type_node;
    }
}

static AstNode *pointer_to_type(Context *c, AstNode *type_node, bool is_const) {
    AstNode *node = create_node(c, NodeTypePrefixOpExpr);
    node->data.prefix_op_expr.prefix_op = is_const ? PrefixOpConstAddressOf : PrefixOpAddressOf;
    node->data.prefix_op_expr.primary_expr = convert_to_c_void(c, type_node);
    return node;
}

static AstNode *type_node(Context *c, const Type *ty) {
    switch (ty->getTypeClass()) {
        case Type::Builtin:
            {
                const BuiltinType *builtin_ty = static_cast<const BuiltinType*>(ty);
                switch (builtin_ty->getKind()) {
                    case BuiltinType::Void:
                        return simple_type_node(c, "void");
                    case BuiltinType::Bool:
                        return simple_type_node(c, "bool");
                    case BuiltinType::Char_U:
                    case BuiltinType::UChar:
                        return simple_type_node(c, "u8");
                    case BuiltinType::Char_S:
                    case BuiltinType::SChar:
                        return simple_type_node(c, "i8");
                    case BuiltinType::UShort:
                        return simple_type_node(c, "c_ushort");
                    case BuiltinType::UInt:
                        return simple_type_node(c, "c_uint");
                    case BuiltinType::ULong:
                        return simple_type_node(c, "c_ulong");
                    case BuiltinType::ULongLong:
                        return simple_type_node(c, "c_ulonglong");
                    case BuiltinType::Short:
                        return simple_type_node(c, "c_short");
                    case BuiltinType::Int:
                        return simple_type_node(c, "c_int");
                    case BuiltinType::Long:
                        return simple_type_node(c, "c_long");
                    case BuiltinType::LongLong:
                        return simple_type_node(c, "c_longlong");
                    case BuiltinType::Float:
                        return simple_type_node(c, "f32");
                    case BuiltinType::Double:
                        return simple_type_node(c, "f64");
                    case BuiltinType::LongDouble:
                        return simple_type_node(c, "f128");
                    case BuiltinType::WChar_U:
                    case BuiltinType::Char16:
                    case BuiltinType::Char32:
                    case BuiltinType::UInt128:
                    case BuiltinType::WChar_S:
                    case BuiltinType::Int128:
                    case BuiltinType::Half:
                    case BuiltinType::NullPtr:
                    case BuiltinType::ObjCId:
                    case BuiltinType::ObjCClass:
                    case BuiltinType::ObjCSel:
                    case BuiltinType::OCLImage1d:
                    case BuiltinType::OCLImage1dArray:
                    case BuiltinType::OCLImage1dBuffer:
                    case BuiltinType::OCLImage2d:
                    case BuiltinType::OCLImage2dArray:
                    case BuiltinType::OCLImage3d:
                    case BuiltinType::OCLSampler:
                    case BuiltinType::OCLEvent:
                    case BuiltinType::Dependent:
                    case BuiltinType::Overload:
                    case BuiltinType::BoundMember:
                    case BuiltinType::PseudoObject:
                    case BuiltinType::UnknownAny:
                    case BuiltinType::BuiltinFn:
                    case BuiltinType::ARCUnbridgedCast:
                        zig_panic("TODO - make error for these types");
                }
                break;
            }
        case Type::Pointer:
            {
                const PointerType *pointer_ty = static_cast<const PointerType*>(ty);
                QualType child_qt = pointer_ty->getPointeeType();
                AstNode *type_node = type_node_from_qual_type(c, child_qt);
                return pointer_to_type(c, type_node, child_qt.isConstQualified());
            }
        case Type::Typedef:
            {
                const TypedefType *typedef_ty = static_cast<const TypedefType*>(ty);
                const TypedefNameDecl *typedef_decl = typedef_ty->getDecl();
                const char *type_name = buf_ptr(buf_create_from_str(decl_name(typedef_decl)));
                return simple_type_node(c, type_name);
            }
        case Type::FunctionProto:
        case Type::Record:
        case Type::Enum:
        case Type::BlockPointer:
        case Type::LValueReference:
        case Type::RValueReference:
        case Type::MemberPointer:
        case Type::ConstantArray:
        case Type::IncompleteArray:
        case Type::VariableArray:
        case Type::DependentSizedArray:
        case Type::DependentSizedExtVector:
        case Type::Vector:
        case Type::ExtVector:
        case Type::FunctionNoProto:
        case Type::UnresolvedUsing:
        case Type::Paren:
        case Type::Adjusted:
        case Type::Decayed:
        case Type::TypeOfExpr:
        case Type::TypeOf:
        case Type::Decltype:
        case Type::UnaryTransform:
        case Type::Elaborated:
        case Type::Attributed:
        case Type::TemplateTypeParm:
        case Type::SubstTemplateTypeParm:
        case Type::SubstTemplateTypeParmPack:
        case Type::TemplateSpecialization:
        case Type::Auto:
        case Type::InjectedClassName:
        case Type::DependentName:
        case Type::DependentTemplateSpecialization:
        case Type::PackExpansion:
        case Type::ObjCObject:
        case Type::ObjCInterface:
        case Type::Complex:
        case Type::ObjCObjectPointer:
        case Type::Atomic:
            zig_panic("TODO - make error for type: %s", ty->getTypeClassName());
    }
}

static AstNode *type_node_from_qual_type(Context *c, QualType qt) {
    return type_node(c, qt.getTypePtr());
}

static bool decl_visitor(void *context, const Decl *decl) {
    Context *c = (Context*)context;

    switch (decl->getKind()) {
        case Decl::Function:
            {
                const FunctionDecl *fn_decl = static_cast<const FunctionDecl*>(decl);
                AstNode *node = create_node(c, NodeTypeFnProto);
                node->data.fn_proto.is_extern = true;
                node->data.fn_proto.visib_mod = c->visib_mod;
                node->data.fn_proto.is_var_args = fn_decl->isVariadic();
                buf_init_from_str(&node->data.fn_proto.name, decl_name(decl));

                int arg_count = fn_decl->getNumParams();
                for (int i = 0; i < arg_count; i += 1) {
                    const ParmVarDecl *param = fn_decl->getParamDecl(i);
                    AstNode *param_decl_node = create_node(c, NodeTypeParamDecl);
                    const char *name = decl_name(param);
                    if (strlen(name) == 0) {
                        name = buf_ptr(buf_sprintf("arg%d", i));
                    }
                    buf_init_from_str(&param_decl_node->data.param_decl.name, name);
                    QualType qt = param->getOriginalType();
                    param_decl_node->data.param_decl.is_noalias = qt.isRestrictQualified();
                    param_decl_node->data.param_decl.type = type_node_from_qual_type(c, qt);
                    node->data.fn_proto.params.append(param_decl_node);
                }

                if (fn_decl->isNoReturn()) {
                    node->data.fn_proto.return_type = simple_type_node(c, "unreachable");
                } else {
                    node->data.fn_proto.return_type = type_node_from_qual_type(c, fn_decl->getReturnType());
                }

                c->parse_h->fn_list.append(node);

                break;
            }
            /*
        case Decl::Typedef:
            {
                AstNode *node = create_node(c, NodeTypeVariableDeclaration);
                node->data.variable_declaration.is_const = true;
                buf_init_from_str(&node->data.variable_declaration.symbol, decl_name(decl));

                break;
            }
            */
        default:
            if (c->warnings_on) {
                fprintf(stderr, "ignoring %s\n", decl->getDeclKindName());
            }
    }

    return true;
}

int parse_h_buf(ParseH *parse_h, Buf *source, const char *libc_include_path) {
    int err;
    Buf tmp_file_path = BUF_INIT;
    if ((err = os_buf_to_tmp_file(source, buf_create_from_str(".h"), &tmp_file_path))) {
        return err;
    }
    ZigList<const char *> clang_argv = {0};
    clang_argv.append(buf_ptr(&tmp_file_path));

    clang_argv.append("-isystem");
    clang_argv.append(libc_include_path);

    err = parse_h_file(parse_h, &clang_argv);

    os_delete_file(&tmp_file_path);

    return err;
}

int parse_h_file(ParseH *parse_h, ZigList<const char *> *clang_argv) {
    Context context = {0};
    Context *c = &context;
    c->parse_h = parse_h;

    char *ZIG_PARSEH_CFLAGS = getenv("ZIG_PARSEH_CFLAGS");
    if (ZIG_PARSEH_CFLAGS) {
        Buf tmp_buf = BUF_INIT;
        char *start = ZIG_PARSEH_CFLAGS;
        char *space = strstr(start, " ");
        while (space) {
            if (space - start > 0) {
                buf_init_from_mem(&tmp_buf, start, space - start);
                clang_argv->append(buf_ptr(buf_create_from_buf(&tmp_buf)));
            }
            start = space + 1;
            space = strstr(start, " ");
        }
        buf_init_from_str(&tmp_buf, start);
        clang_argv->append(buf_ptr(buf_create_from_buf(&tmp_buf)));
    }

    clang_argv->append("-isystem");
    clang_argv->append(ZIG_HEADERS_DIR);

    // we don't need spell checking and it slows things down
    clang_argv->append("-fno-spell-checking");
    // to make the end argument work
    clang_argv->append(nullptr);

    IntrusiveRefCntPtr<DiagnosticsEngine> diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));

    std::shared_ptr<PCHContainerOperations> pch_container_ops = std::make_shared<PCHContainerOperations>();

    bool skip_function_bodies = true;
    bool only_local_decls = true;
    bool capture_diagnostics = true;
    bool user_files_are_volatile = true;
    bool allow_pch_with_compiler_errors = false;
    const char *resources_path = ZIG_HEADERS_DIR;
    std::unique_ptr<ASTUnit> err_unit;
    std::unique_ptr<ASTUnit> ast_unit(ASTUnit::LoadFromCommandLine(
            &clang_argv->at(0), &clang_argv->last(),
            pch_container_ops, diags, resources_path,
            only_local_decls, capture_diagnostics, None, true, false, TU_Complete,
            false, false, allow_pch_with_compiler_errors, skip_function_bodies,
            user_files_are_volatile, false, &err_unit));


    // Early failures in LoadFromCommandLine may return with ErrUnit unset.
    if (!ast_unit && !err_unit) {
        return ErrorFileSystem;
    }

    if (diags->getClient()->getNumErrors() > 0) {
        if (ast_unit) {
            err_unit = std::move(ast_unit);
        }

        for (ASTUnit::stored_diag_iterator it = err_unit->stored_diag_begin(), 
                it_end = err_unit->stored_diag_end();
                it != it_end; ++it)
        {
            switch (it->getLevel()) {
                case DiagnosticsEngine::Ignored:
                case DiagnosticsEngine::Note:
                case DiagnosticsEngine::Remark:
                case DiagnosticsEngine::Warning:
                    continue;
                case DiagnosticsEngine::Error:
                case DiagnosticsEngine::Fatal:
                    break;
            }
            StringRef msg_str_ref = it->getMessage();
            FullSourceLoc fsl = it->getLocation();
            FileID file_id = fsl.getFileID();
            StringRef filename = fsl.getManager().getFilename(fsl);
            unsigned line = fsl.getSpellingLineNumber() - 1;
            unsigned column = fsl.getSpellingColumnNumber() - 1;
            unsigned offset = fsl.getManager().getFileOffset(fsl);
            const char *source = (const char *)fsl.getManager().getBufferData(file_id).bytes_begin();
            Buf *msg = buf_create_from_str((const char *)msg_str_ref.bytes_begin());
            Buf *path = buf_create_from_str((const char *)filename.bytes_begin());

            ErrorMsg *err_msg = err_msg_create_with_offset(path, line, column, offset, source, msg);

            parse_h->errors.append(err_msg);
        }

        return 0;
    }

    ast_unit->visitLocalTopLevelDecls(c, decl_visitor);

    return 0;
}