1 /* 2 * Copyright (c) 2015 Andrew Kelley 3 * 4 * This file is part of zig, which is MIT licensed. 5 * See http://opensource.org/licenses/MIT 6 */ 7 8 #ifndef ZIG_ANALYZE_HPP 9 #define ZIG_ANALYZE_HPP 10 11 #include "all_types.hpp" 12 13 void semantic_analyze(CodeGen *g); 14 ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg); 15 ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, TokenIndex token, Buf *msg); 16 ErrorMsg *add_token_error_offset(CodeGen *g, ZigType *owner, TokenIndex token, Buf *msg, 17 uint32_t bad_index); 18 ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node, Buf *msg); 19 ZigType *new_type_table_entry(ZigTypeId id); 20 ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn); 21 ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const); 22 ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, 23 bool is_const, bool is_volatile, PtrLen ptr_len, 24 uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count, 25 bool allow_zero); 26 ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, 27 bool is_const, bool is_volatile, PtrLen ptr_len, 28 uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count, 29 bool allow_zero, uint32_t vector_index, InferredStructField *inferred_struct_field, 30 ZigValue *sentinel); 31 uint64_t type_size(CodeGen *g, ZigType *type_entry); 32 uint64_t type_size_bits(CodeGen *g, ZigType *type_entry); 33 ZigType *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits); 34 ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type); 35 ZigType **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type); 36 ZigType *get_c_int_type(CodeGen *g, CIntType c_int_type); 37 ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id); 38 ZigType *get_optional_type(CodeGen *g, ZigType *child_type); 39 ZigType *get_optional_type2(CodeGen *g, ZigType *child_type); 40 ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, ZigValue *sentinel); 41 ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type); 42 ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, 43 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout); 44 ZigType *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x); 45 ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type); 46 ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry); 47 ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *full_name, Buf *bare_name); 48 ZigType *get_test_fn_type(CodeGen *g); 49 ZigType *get_any_frame_type(CodeGen *g, ZigType *result_type); 50 bool handle_is_ptr(CodeGen *g, ZigType *type_entry); 51 Error emit_error_unless_callconv_allowed_for_target(CodeGen *g, AstNode *source_node, CallingConvention cc); 52 uint32_t get_async_frame_align_bytes(CodeGen *g); 53 54 bool type_has_bits(CodeGen *g, ZigType *type_entry); 55 Error type_has_bits2(CodeGen *g, ZigType *type_entry, bool *result); 56 57 bool fn_returns_c_abi_small_struct(FnTypeId *fn_type_id); 58 59 enum ExternPosition { 60 ExternPositionFunctionParameter, 61 ExternPositionFunctionReturn, 62 ExternPositionOther, // array element, struct field, optional element, etc 63 }; 64 65 Error type_allowed_in_extern(CodeGen *g, ZigType *type_entry, ExternPosition position, bool *result); 66 bool ptr_allows_addr_zero(ZigType *ptr_type); 67 68 // Deprecated, use `type_is_nonnull_ptr2` 69 bool type_is_nonnull_ptr(CodeGen *g, ZigType *type); 70 Error type_is_nonnull_ptr2(CodeGen *g, ZigType *type, bool *result); 71 72 ZigType *get_codegen_ptr_type_bail(CodeGen *g, ZigType *type); 73 Error get_codegen_ptr_type(CodeGen *g, ZigType *type, ZigType **result); 74 75 enum SourceKind { 76 SourceKindRoot, 77 SourceKindPkgMain, 78 SourceKindNonRoot, 79 SourceKindCImport, 80 }; 81 ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Buf *source_code, 82 SourceKind source_kind); 83 84 ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope); 85 Tld *find_decl(CodeGen *g, Scope *scope, Buf *name); 86 Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name); 87 void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy); 88 void resolve_container_usingnamespace_decls(CodeGen *g, ScopeDecls *decls_scope); 89 90 ZigType *get_src_ptr_type(ZigType *type); 91 uint32_t get_ptr_align(CodeGen *g, ZigType *type); 92 bool get_ptr_const(CodeGen *g, ZigType *type); 93 ZigType *validate_var_type(CodeGen *g, AstNodeVariableDeclaration *source_node, ZigType *type_entry); 94 ZigType *container_ref_type(ZigType *type_entry); 95 bool type_is_complete(ZigType *type_entry); 96 bool type_is_resolved(ZigType *type_entry, ResolveStatus status); 97 bool type_is_invalid(ZigType *type_entry); 98 bool type_is_global_error_set(ZigType *err_set_type); 99 ScopeDecls *get_container_scope(ZigType *type_entry); 100 TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name); 101 TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name); 102 TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name); 103 TypeEnumField *find_enum_field_by_tag(ZigType *enum_type, const BigInt *tag); 104 TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag); 105 106 bool is_ref(ZigType *type_entry); 107 bool is_array_ref(ZigType *type_entry); 108 bool is_container_ref(ZigType *type_entry); 109 Error is_valid_vector_elem_type(CodeGen *g, ZigType *elem_type, bool *result); 110 void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node); 111 ZigFn *scope_fn_entry(Scope *scope); 112 ZigPackage *scope_package(Scope *scope); 113 ZigType *get_scope_import(Scope *scope); 114 ScopeTypeOf *get_scope_typeof(Scope *scope); 115 void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source_node, Scope *parent_scope); 116 ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf *name, 117 bool is_const, ZigValue *init_value, Tld *src_tld, ZigType *var_type); 118 ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node); 119 void append_namespace_qualification(CodeGen *g, Buf *buf, ZigType *container_type); 120 ZigFn *create_fn(CodeGen *g, AstNode *proto_node); 121 void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, CallingConvention cc, size_t param_count_alloc); 122 AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index); 123 Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status); 124 void complete_enum(CodeGen *g, ZigType *enum_type); 125 bool ir_get_var_is_comptime(ZigVar *var); 126 bool const_values_equal(CodeGen *g, ZigValue *a, ZigValue *b); 127 void eval_min_max_value(CodeGen *g, ZigType *type_entry, ZigValue *const_val, bool is_max); 128 void eval_min_max_value_int(CodeGen *g, ZigType *int_type, BigInt *bigint, bool is_max); 129 130 void render_const_value(CodeGen *g, Buf *buf, ZigValue *const_val); 131 132 ScopeDecls *create_decls_scope(CodeGen *g, AstNode *node, Scope *parent, ZigType *container_type, ZigType *import, Buf *bare_name); 133 ScopeBlock *create_block_scope(CodeGen *g, AstNode *node, Scope *parent); 134 ScopeDefer *create_defer_scope(CodeGen *g, AstNode *node, Scope *parent); 135 ScopeDeferExpr *create_defer_expr_scope(CodeGen *g, AstNode *node, Scope *parent); 136 Scope *create_var_scope(CodeGen *g, AstNode *node, Scope *parent, ZigVar *var); 137 ScopeCImport *create_cimport_scope(CodeGen *g, AstNode *node, Scope *parent); 138 ScopeLoop *create_loop_scope(CodeGen *g, AstNode *node, Scope *parent); 139 ScopeSuspend *create_suspend_scope(CodeGen *g, AstNode *node, Scope *parent); 140 ScopeFnDef *create_fndef_scope(CodeGen *g, AstNode *node, Scope *parent, ZigFn *fn_entry); 141 Scope *create_comptime_scope(CodeGen *g, AstNode *node, Scope *parent); 142 Scope *create_nosuspend_scope(CodeGen *g, AstNode *node, Scope *parent); 143 Scope *create_runtime_scope(CodeGen *g, AstNode *node, Scope *parent, Stage1ZirInst *is_comptime); 144 Scope *create_typeof_scope(CodeGen *g, AstNode *node, Scope *parent); 145 ScopeExpr *create_expr_scope(CodeGen *g, AstNode *node, Scope *parent); 146 147 void init_const_str_lit(CodeGen *g, ZigValue *const_val, Buf *str, bool move_str); 148 ZigValue *create_const_str_lit(CodeGen *g, Buf *str); 149 ZigValue *create_sentineled_str_lit(CodeGen *g, Buf *str, ZigValue *sentinel); 150 151 void init_const_bigint(ZigValue *const_val, ZigType *type, const BigInt *bigint); 152 ZigValue *create_const_bigint(CodeGen *g, ZigType *type, const BigInt *bigint); 153 154 void init_const_unsigned_negative(ZigValue *const_val, ZigType *type, uint64_t x, bool negative); 155 ZigValue *create_const_unsigned_negative(CodeGen *g, ZigType *type, uint64_t x, bool negative); 156 157 void init_const_signed(ZigValue *const_val, ZigType *type, int64_t x); 158 ZigValue *create_const_signed(CodeGen *g, ZigType *type, int64_t x); 159 160 void init_const_usize(CodeGen *g, ZigValue *const_val, uint64_t x); 161 ZigValue *create_const_usize(CodeGen *g, uint64_t x); 162 163 void init_const_float(ZigValue *const_val, ZigType *type, double value); 164 ZigValue *create_const_float(CodeGen *g, ZigType *type, double value); 165 166 void init_const_enum(ZigValue *const_val, ZigType *type, const BigInt *tag); 167 ZigValue *create_const_enum(CodeGen *g, ZigType *type, const BigInt *tag); 168 169 void init_const_bool(CodeGen *g, ZigValue *const_val, bool value); 170 ZigValue *create_const_bool(CodeGen *g, bool value); 171 172 void init_const_type(CodeGen *g, ZigValue *const_val, ZigType *type_value); 173 ZigValue *create_const_type(CodeGen *g, ZigType *type_value); 174 175 void init_const_runtime(ZigValue *const_val, ZigType *type); 176 ZigValue *create_const_runtime(CodeGen *g, ZigType *type); 177 178 void init_const_ptr_ref(CodeGen *g, ZigValue *const_val, ZigValue *pointee_val, bool is_const); 179 ZigValue *create_const_ptr_ref(CodeGen *g, ZigValue *pointee_val, bool is_const); 180 181 void init_const_ptr_hard_coded_addr(CodeGen *g, ZigValue *const_val, ZigType *pointee_type, 182 size_t addr, bool is_const); 183 ZigValue *create_const_ptr_hard_coded_addr(CodeGen *g, ZigType *pointee_type, 184 size_t addr, bool is_const); 185 186 void init_const_ptr_array(CodeGen *g, ZigValue *const_val, ZigValue *array_val, 187 size_t elem_index, bool is_const, PtrLen ptr_len); 188 ZigValue *create_const_ptr_array(CodeGen *g, ZigValue *array_val, size_t elem_index, 189 bool is_const, PtrLen ptr_len); 190 191 void init_const_slice(CodeGen *g, ZigValue *const_val, ZigValue *array_val, 192 size_t start, size_t len, bool is_const, ZigValue *sentinel); 193 ZigValue *create_const_slice(CodeGen *g, ZigValue *array_val, size_t start, size_t len, bool is_const, ZigValue *sentinel); 194 195 void init_const_null(ZigValue *const_val, ZigType *type); 196 ZigValue *create_const_null(CodeGen *g, ZigType *type); 197 198 void init_const_fn(ZigValue *const_val, ZigFn *fn); 199 ZigValue *create_const_fn(CodeGen *g, ZigFn *fn); 200 201 ZigValue **alloc_const_vals_ptrs(CodeGen *g, size_t count); 202 ZigValue **realloc_const_vals_ptrs(CodeGen *g, ZigValue **ptr, size_t old_count, size_t new_count); 203 204 TypeStructField **alloc_type_struct_fields(size_t count); 205 TypeStructField **realloc_type_struct_fields(TypeStructField **ptr, size_t old_count, size_t new_count); 206 207 ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits); 208 void expand_undef_array(CodeGen *g, ZigValue *const_val); 209 void expand_undef_struct(CodeGen *g, ZigValue *const_val); 210 void update_compile_var(CodeGen *g, Buf *name, ZigValue *value); 211 212 const char *type_id_name(ZigTypeId id); 213 ZigTypeId type_id_at_index(size_t index); 214 size_t type_id_len(); 215 size_t type_id_index(ZigType *entry); 216 ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id); 217 bool optional_value_is_null(ZigValue *val); 218 219 uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry); 220 ZigType *get_align_amt_type(CodeGen *g); 221 ZigPackage *new_anonymous_package(void); 222 223 Buf *const_value_to_buffer(ZigValue *const_val); 224 void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, CallingConvention cc); 225 void add_var_export(CodeGen *g, ZigVar *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage); 226 227 228 ZigValue *get_builtin_value(CodeGen *codegen, const char *name); 229 ZigType *get_builtin_type(CodeGen *codegen, const char *name); 230 ZigType *get_stack_trace_type(CodeGen *g); 231 bool resolve_inferred_error_set(CodeGen *g, ZigType *err_set_type, AstNode *source_node); 232 233 ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry); 234 235 bool fn_type_can_fail(FnTypeId *fn_type_id); 236 bool type_can_fail(ZigType *type_entry); 237 bool fn_eval_cacheable(Scope *scope, ZigType *return_type); 238 AstNode *type_decl_node(ZigType *type_entry); 239 240 Error get_primitive_type(CodeGen *g, Buf *name, ZigType **result); 241 242 bool calling_convention_allows_zig_types(CallingConvention cc); 243 const char *calling_convention_name(CallingConvention cc); 244 245 const char *address_space_name(AddressSpace as); 246 247 Error ATTRIBUTE_MUST_USE file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents); 248 249 void walk_function_params(CodeGen *g, ZigType *fn_type, FnWalk *fn_walk); 250 X64CABIClass type_c_abi_x86_64_class(CodeGen *g, ZigType *ty); 251 bool type_is_c_abi_int_bail(CodeGen *g, ZigType *ty); 252 Error type_is_c_abi_int(CodeGen *g, ZigType *ty, bool *result); 253 bool want_first_arg_sret(CodeGen *g, FnTypeId *fn_type_id); 254 const char *container_string(ContainerKind kind); 255 256 uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field); 257 258 enum ReqCompTime { 259 ReqCompTimeInvalid, 260 ReqCompTimeNo, 261 ReqCompTimeYes, 262 }; 263 ReqCompTime type_requires_comptime(CodeGen *g, ZigType *type_entry); 264 265 OnePossibleValue type_has_one_possible_value(CodeGen *g, ZigType *type_entry); 266 267 Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, 268 ZigValue *const_val, ZigType *wanted_type); 269 270 void typecheck_panic_fn(CodeGen *g, TldFn *tld_fn, ZigFn *panic_fn); 271 Buf *type_bare_name(ZigType *t); 272 Buf *type_h_name(ZigType *t); 273 274 LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type); 275 LLVMTypeRef get_llvm_c_abi_type(CodeGen *g, ZigType *type); 276 ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type); 277 278 void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c, 279 FileExt source_kind); 280 281 void src_assert_impl(bool ok, AstNode *source_node, const char *file, unsigned int line); 282 bool is_container(ZigType *type_entry); 283 ZigValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry, 284 Buf *type_name, UndefAllowed undef); 285 286 void resolve_llvm_types_fn(CodeGen *g, ZigFn *fn); 287 bool fn_is_async(ZigFn *fn); 288 CallingConvention cc_from_fn_proto(AstNodeFnProto *fn_proto); 289 bool is_valid_return_type(ZigType* type); 290 bool is_valid_param_type(ZigType* type); 291 292 Error type_val_resolve_abi_align(CodeGen *g, AstNode *source_node, ZigValue *type_val, uint32_t *abi_align); 293 Error type_val_resolve_abi_size(CodeGen *g, AstNode *source_node, ZigValue *type_val, 294 size_t *abi_size, size_t *size_in_bits); 295 Error type_val_resolve_zero_bits(CodeGen *g, ZigValue *type_val, ZigType *parent_type, 296 ZigValue *parent_type_val, bool *is_zero_bits); 297 ZigType *resolve_union_field_type(CodeGen *g, TypeUnionField *union_field); 298 ZigType *resolve_struct_field_type(CodeGen *g, TypeStructField *struct_field); 299 300 void add_async_error_notes(CodeGen *g, ErrorMsg *msg, ZigFn *fn); 301 302 Error analyze_import(CodeGen *codegen, ZigType *source_import, Buf *import_target_str, 303 ZigType **out_import, Buf **out_import_target_path, Buf *out_full_path); 304 ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry); 305 bool is_anon_container(ZigType *ty); 306 void copy_const_val(CodeGen *g, ZigValue *dest, ZigValue *src); 307 bool type_has_optional_repr(ZigType *ty); 308 bool is_opt_err_set(ZigType *ty); 309 bool type_is_numeric(ZigType *ty); 310 const char *float_op_to_name(BuiltinFnId op); 311 312 #define src_assert(OK, SOURCE_NODE) src_assert_impl((OK), (SOURCE_NODE), __FILE__, __LINE__) 313 314 #endif 315