* xvasprintf.c: New file.
[official-gcc.git] / gcc / jit / libgccjit.c
blob0f50c434d91663504ae5f97821261c35ed7d4277
1 /* Implementation of the C API; all wrappers into the internal C++ API
2 Copyright (C) 2013-2014 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "safe-ctype.h"
27 #include "libgccjit.h"
28 #include "jit-common.h"
29 #include "jit-recording.h"
30 #include "jit-result.h"
32 /* The opaque types used by the public API are actually subclasses
33 of the gcc::jit::recording classes. */
35 struct gcc_jit_context : public gcc::jit::recording::context
37 gcc_jit_context (gcc_jit_context *parent_ctxt) :
38 context (parent_ctxt)
42 struct gcc_jit_result : public gcc::jit::result
46 struct gcc_jit_object : public gcc::jit::recording::memento
50 struct gcc_jit_location : public gcc::jit::recording::location
54 struct gcc_jit_type : public gcc::jit::recording::type
58 struct gcc_jit_struct : public gcc::jit::recording::struct_
62 struct gcc_jit_field : public gcc::jit::recording::field
66 struct gcc_jit_function : public gcc::jit::recording::function
70 struct gcc_jit_block : public gcc::jit::recording::block
74 struct gcc_jit_rvalue : public gcc::jit::recording::rvalue
78 struct gcc_jit_lvalue : public gcc::jit::recording::lvalue
82 struct gcc_jit_param : public gcc::jit::recording::param
86 /**********************************************************************
87 Error-handling.
89 We try to gracefully handle API usage errors by being defensive
90 at the API boundary.
91 **********************************************************************/
93 #define JIT_BEGIN_STMT do {
94 #define JIT_END_STMT } while(0)
96 /* Each of these error-handling macros determines if TEST_EXPR holds.
98 If TEXT_EXPR fails to hold we return from the enclosing function and
99 print an error, either via adding an error on the given context CTXT
100 if CTXT is non-NULL, falling back to simply printing to stderr if CTXT
101 is NULL.
103 They have to be macros since they inject their "return" into the
104 function they are placed in.
106 The variant macros express:
108 (A) whether or not we need to return a value:
109 RETURN_VAL_IF_FAIL* vs
110 RETURN_IF_FAIL*,
111 with the former returning RETURN_EXPR, and
112 RETURN_NULL_IF_FAIL*
113 for the common case where a NULL value is to be returned on
114 error, and
116 (B) whether the error message is to be directly printed:
117 RETURN_*IF_FAIL
118 or is a format string with some number of arguments:
119 RETURN_*IF_FAIL_PRINTF*
121 They all use JIT_BEGIN_STMT/JIT_END_STMT so they can be written with
122 trailing semicolons.
125 #define RETURN_VAL_IF_FAIL(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_MSG) \
126 JIT_BEGIN_STMT \
127 if (!(TEST_EXPR)) \
129 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
130 return (RETURN_EXPR); \
132 JIT_END_STMT
134 #define RETURN_VAL_IF_FAIL_PRINTF1(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0) \
135 JIT_BEGIN_STMT \
136 if (!(TEST_EXPR)) \
138 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
139 __func__, (A0)); \
140 return (RETURN_EXPR); \
142 JIT_END_STMT
144 #define RETURN_VAL_IF_FAIL_PRINTF2(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
145 JIT_BEGIN_STMT \
146 if (!(TEST_EXPR)) \
148 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
149 __func__, (A0), (A1)); \
150 return (RETURN_EXPR); \
152 JIT_END_STMT
154 #define RETURN_VAL_IF_FAIL_PRINTF3(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
155 JIT_BEGIN_STMT \
156 if (!(TEST_EXPR)) \
158 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
159 __func__, (A0), (A1), (A2)); \
160 return (RETURN_EXPR); \
162 JIT_END_STMT
164 #define RETURN_VAL_IF_FAIL_PRINTF4(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
165 JIT_BEGIN_STMT \
166 if (!(TEST_EXPR)) \
168 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
169 __func__, (A0), (A1), (A2), (A3)); \
170 return (RETURN_EXPR); \
172 JIT_END_STMT
174 #define RETURN_VAL_IF_FAIL_PRINTF6(TEST_EXPR, RETURN_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
175 JIT_BEGIN_STMT \
176 if (!(TEST_EXPR)) \
178 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
179 __func__, (A0), (A1), (A2), (A3), (A4), (A5)); \
180 return (RETURN_EXPR); \
182 JIT_END_STMT
184 #define RETURN_NULL_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
185 RETURN_VAL_IF_FAIL ((TEST_EXPR), NULL, (CTXT), (LOC), (ERR_MSG))
187 #define RETURN_NULL_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
188 RETURN_VAL_IF_FAIL_PRINTF1 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0)
190 #define RETURN_NULL_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
191 RETURN_VAL_IF_FAIL_PRINTF2 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1)
193 #define RETURN_NULL_IF_FAIL_PRINTF3(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2) \
194 RETURN_VAL_IF_FAIL_PRINTF3 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2)
196 #define RETURN_NULL_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
197 RETURN_VAL_IF_FAIL_PRINTF4 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3)
199 #define RETURN_NULL_IF_FAIL_PRINTF6(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5) \
200 RETURN_VAL_IF_FAIL_PRINTF6 (TEST_EXPR, NULL, CTXT, LOC, ERR_FMT, A0, A1, A2, A3, A4, A5)
202 #define RETURN_IF_FAIL(TEST_EXPR, CTXT, LOC, ERR_MSG) \
203 JIT_BEGIN_STMT \
204 if (!(TEST_EXPR)) \
206 jit_error ((CTXT), (LOC), "%s: %s", __func__, (ERR_MSG)); \
207 return; \
209 JIT_END_STMT
211 #define RETURN_IF_FAIL_PRINTF1(TEST_EXPR, CTXT, LOC, ERR_FMT, A0) \
212 JIT_BEGIN_STMT \
213 if (!(TEST_EXPR)) \
215 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
216 __func__, (A0)); \
217 return; \
219 JIT_END_STMT
221 #define RETURN_IF_FAIL_PRINTF2(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1) \
222 JIT_BEGIN_STMT \
223 if (!(TEST_EXPR)) \
225 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
226 __func__, (A0), (A1)); \
227 return; \
229 JIT_END_STMT
231 #define RETURN_IF_FAIL_PRINTF4(TEST_EXPR, CTXT, LOC, ERR_FMT, A0, A1, A2, A3) \
232 JIT_BEGIN_STMT \
233 if (!(TEST_EXPR)) \
235 jit_error ((CTXT), (LOC), "%s: " ERR_FMT, \
236 __func__, (A0), (A1), (A2), (A3)); \
237 return; \
239 JIT_END_STMT
241 /* Check that BLOCK is non-NULL, and that it's OK to add statements to
242 it. This will fail if BLOCK has already been terminated by some
243 kind of jump or a return. */
244 #define RETURN_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
245 JIT_BEGIN_STMT \
246 RETURN_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
247 RETURN_IF_FAIL_PRINTF2 ( \
248 !(BLOCK)->has_been_terminated (), \
249 (BLOCK)->get_context (), \
250 (LOC), \
251 "adding to terminated block: %s (already terminated by: %s)", \
252 (BLOCK)->get_debug_string (), \
253 (BLOCK)->get_last_statement ()->get_debug_string ()); \
254 JIT_END_STMT
256 /* As RETURN_IF_NOT_VALID_BLOCK, but injecting a "return NULL;" if it
257 fails. */
258 #define RETURN_NULL_IF_NOT_VALID_BLOCK(BLOCK, LOC) \
259 JIT_BEGIN_STMT \
260 RETURN_NULL_IF_FAIL ((BLOCK), NULL, (LOC), "NULL block"); \
261 RETURN_NULL_IF_FAIL_PRINTF2 ( \
262 !(BLOCK)->has_been_terminated (), \
263 (BLOCK)->get_context (), \
264 (LOC), \
265 "adding to terminated block: %s (already terminated by: %s)", \
266 (BLOCK)->get_debug_string (), \
267 (BLOCK)->get_last_statement ()->get_debug_string ()); \
268 JIT_END_STMT
270 /* Format the given string, and report it as an error, either on CTXT
271 if non-NULL, or by printing to stderr if we have a NULL context.
272 LOC gives the source location where the error occcurred, and can be
273 NULL. */
275 static void
276 jit_error (gcc::jit::recording::context *ctxt,
277 gcc_jit_location *loc,
278 const char *fmt, ...)
279 GNU_PRINTF(3, 4);
281 static void
282 jit_error (gcc::jit::recording::context *ctxt,
283 gcc_jit_location *loc,
284 const char *fmt, ...)
286 va_list ap;
287 va_start (ap, fmt);
289 if (ctxt)
290 ctxt->add_error_va (loc, fmt, ap);
291 else
293 /* No context? Send to stderr. */
294 vfprintf (stderr, fmt, ap);
295 fprintf (stderr, "\n");
298 va_end (ap);
301 /* Determine whether or not we can write to lvalues of type LTYPE from
302 rvalues of type RTYPE, detecting type errors such as attempting to
303 write to an int with a string literal (without an explicit cast).
305 This is implemented by calling the
306 gcc::jit::recording::type::accepts_writes_from virtual function on
307 LTYPE. */
309 static bool
310 compatible_types (gcc::jit::recording::type *ltype,
311 gcc::jit::recording::type *rtype)
313 return ltype->accepts_writes_from (rtype);
316 /* Public entrypoint for acquiring a gcc_jit_context.
317 Note that this creates a new top-level context; contrast with
318 gcc_jit_context_new_child_context below.
320 The real work is done in the constructor for
321 gcc::jit::recording::context in jit-recording.c. */
323 gcc_jit_context *
324 gcc_jit_context_acquire (void)
326 return new gcc_jit_context (NULL);
329 /* Public entrypoint for releasing a gcc_jit_context.
330 The real work is done in the destructor for
331 gcc::jit::recording::context in jit-recording.c. */
333 void
334 gcc_jit_context_release (gcc_jit_context *ctxt)
336 delete ctxt;
339 /* Public entrypoint for creating a child context within
340 PARENT_CTXT. See description in libgccjit.h.
342 The real work is done in the constructor for
343 gcc::jit::recording::context in jit-recording.c. */
345 gcc_jit_context *
346 gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
348 return new gcc_jit_context (parent_ctxt);
351 /* Public entrypoint. See description in libgccjit.h.
353 After error-checking, the real work is done by the
354 gcc::jit::recording::context::new_location
355 method in jit-recording.c. */
357 gcc_jit_location *
358 gcc_jit_context_new_location (gcc_jit_context *ctxt,
359 const char *filename,
360 int line,
361 int column)
363 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
365 return (gcc_jit_location *)ctxt->new_location (filename, line, column);
368 /* Public entrypoint. See description in libgccjit.h.
370 After error-checking, this calls the trivial
371 gcc::jit::recording::memento::as_object method (a location is a
372 memento), in jit-recording.h. */
374 gcc_jit_object *
375 gcc_jit_location_as_object (gcc_jit_location *loc)
377 RETURN_NULL_IF_FAIL (loc, NULL, NULL, "NULL location");
379 return static_cast <gcc_jit_object *> (loc->as_object ());
382 /* Public entrypoint. See description in libgccjit.h.
384 After error-checking, this calls the trivial
385 gcc::jit::recording::memento::as_object method (a type is a
386 memento), in jit-recording.h. */
388 gcc_jit_object *
389 gcc_jit_type_as_object (gcc_jit_type *type)
391 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
393 return static_cast <gcc_jit_object *> (type->as_object ());
396 /* Public entrypoint for getting a specific type from a context.
398 After error-checking, the real work is done by the
399 gcc::jit::recording::context::get_type method, in
400 jit-recording.c */
402 gcc_jit_type *
403 gcc_jit_context_get_type (gcc_jit_context *ctxt,
404 enum gcc_jit_types type)
406 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
407 RETURN_NULL_IF_FAIL_PRINTF1 (
408 (type >= GCC_JIT_TYPE_VOID
409 && type <= GCC_JIT_TYPE_FILE_PTR),
410 ctxt, NULL,
411 "unrecognized value for enum gcc_jit_types: %i", type);
413 return (gcc_jit_type *)ctxt->get_type (type);
416 /* Public entrypoint for getting the integer type of the given size and
417 signedness.
419 After error-checking, the real work is done by the
420 gcc::jit::recording::context::get_int_type method,
421 in jit-recording.c. */
423 gcc_jit_type *
424 gcc_jit_context_get_int_type (gcc_jit_context *ctxt,
425 int num_bytes, int is_signed)
427 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
428 RETURN_NULL_IF_FAIL (num_bytes >= 0, ctxt, NULL, "negative size");
430 return (gcc_jit_type *)ctxt->get_int_type (num_bytes, is_signed);
433 /* Public entrypoint. See description in libgccjit.h.
435 After error-checking, the real work is done by the
436 gcc::jit::recording::type::get_pointer method, in
437 jit-recording.c */
439 gcc_jit_type *
440 gcc_jit_type_get_pointer (gcc_jit_type *type)
442 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
444 return (gcc_jit_type *)type->get_pointer ();
447 /* Public entrypoint. See description in libgccjit.h.
449 After error-checking, the real work is done by the
450 gcc::jit::recording::type::get_const method, in
451 jit-recording.c. */
453 gcc_jit_type *
454 gcc_jit_type_get_const (gcc_jit_type *type)
456 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
458 return (gcc_jit_type *)type->get_const ();
461 /* Public entrypoint. See description in libgccjit.h.
463 After error-checking, the real work is done by the
464 gcc::jit::recording::type::get_volatile method, in
465 jit-recording.c. */
467 gcc_jit_type *
468 gcc_jit_type_get_volatile (gcc_jit_type *type)
470 RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
472 return (gcc_jit_type *)type->get_volatile ();
475 /* Public entrypoint. See description in libgccjit.h.
477 After error-checking, the real work is done by the
478 gcc::jit::recording::context::new_array_type method, in
479 jit-recording.c. */
481 gcc_jit_type *
482 gcc_jit_context_new_array_type (gcc_jit_context *ctxt,
483 gcc_jit_location *loc,
484 gcc_jit_type *element_type,
485 int num_elements)
487 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
488 /* LOC can be NULL. */
489 RETURN_NULL_IF_FAIL (element_type, ctxt, loc, "NULL type");
490 RETURN_NULL_IF_FAIL (num_elements >= 0, ctxt, NULL, "negative size");
492 return (gcc_jit_type *)ctxt->new_array_type (loc,
493 element_type,
494 num_elements);
497 /* Public entrypoint. See description in libgccjit.h.
499 After error-checking, the real work is done by the
500 gcc::jit::recording::context::new_field method, in
501 jit-recording.c. */
503 gcc_jit_field *
504 gcc_jit_context_new_field (gcc_jit_context *ctxt,
505 gcc_jit_location *loc,
506 gcc_jit_type *type,
507 const char *name)
509 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
510 /* LOC can be NULL. */
511 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
512 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
514 return (gcc_jit_field *)ctxt->new_field (loc, type, name);
517 /* Public entrypoint. See description in libgccjit.h.
519 After error-checking, this calls the trivial
520 gcc::jit::recording::memento::as_object method (a field is a
521 memento), in jit-recording.h. */
523 gcc_jit_object *
524 gcc_jit_field_as_object (gcc_jit_field *field)
526 RETURN_NULL_IF_FAIL (field, NULL, NULL, "NULL field");
528 return static_cast <gcc_jit_object *> (field->as_object ());
531 /* Public entrypoint. See description in libgccjit.h.
533 After error-checking, the real work is done by the
534 gcc::jit::recording::context::new_struct_type method,
535 immediately followed by a "set_fields" call on the resulting
536 gcc::jit::recording::compound_type *, both in jit-recording.c */
538 gcc_jit_struct *
539 gcc_jit_context_new_struct_type (gcc_jit_context *ctxt,
540 gcc_jit_location *loc,
541 const char *name,
542 int num_fields,
543 gcc_jit_field **fields)
545 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
546 /* LOC can be NULL. */
547 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
548 if (num_fields)
549 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
550 for (int i = 0; i < num_fields; i++)
552 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
553 RETURN_NULL_IF_FAIL_PRINTF2 (
554 NULL == fields[i]->get_container (),
555 ctxt, loc,
556 "%s is already a field of %s",
557 fields[i]->get_debug_string (),
558 fields[i]->get_container ()->get_debug_string ());
561 gcc::jit::recording::struct_ *result =
562 ctxt->new_struct_type (loc, name);
563 result->set_fields (loc,
564 num_fields,
565 (gcc::jit::recording::field **)fields);
566 return static_cast<gcc_jit_struct *> (result);
569 /* Public entrypoint. See description in libgccjit.h.
571 After error-checking, the real work is done by the
572 gcc::jit::recording::context::new_struct_type method in
573 jit-recording.c. */
575 gcc_jit_struct *
576 gcc_jit_context_new_opaque_struct (gcc_jit_context *ctxt,
577 gcc_jit_location *loc,
578 const char *name)
580 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
581 /* LOC can be NULL. */
582 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
584 return (gcc_jit_struct *)ctxt->new_struct_type (loc, name);
587 /* Public entrypoint. See description in libgccjit.h.
589 After error-checking, this calls the trivial
590 gcc::jit::recording::struct_::as_object method in
591 jit-recording.h. */
593 gcc_jit_type *
594 gcc_jit_struct_as_type (gcc_jit_struct *struct_type)
596 RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct_type");
598 return static_cast <gcc_jit_type *> (struct_type->as_type ());
601 /* Public entrypoint. See description in libgccjit.h.
603 After error-checking, the real work is done by the
604 gcc::jit::recording::compound_type::set_fields method in
605 jit-recording.c. */
607 void
608 gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
609 gcc_jit_location *loc,
610 int num_fields,
611 gcc_jit_field **fields)
613 RETURN_IF_FAIL (struct_type, NULL, loc, "NULL struct_type");
614 /* LOC can be NULL. */
615 gcc::jit::recording::context *ctxt = struct_type->m_ctxt;
616 RETURN_IF_FAIL_PRINTF1 (
617 NULL == struct_type->get_fields (), ctxt, loc,
618 "%s already has had fields set",
619 struct_type->get_debug_string ());
620 if (num_fields)
621 RETURN_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
622 for (int i = 0; i < num_fields; i++)
624 RETURN_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
625 RETURN_IF_FAIL_PRINTF2 (
626 NULL == fields[i]->get_container (),
627 ctxt, loc,
628 "%s is already a field of %s",
629 fields[i]->get_debug_string (),
630 fields[i]->get_container ()->get_debug_string ());
633 struct_type->set_fields (loc, num_fields,
634 (gcc::jit::recording::field **)fields);
637 /* Public entrypoint. See description in libgccjit.h.
639 After error-checking, the real work is done by the
640 gcc::jit::recording::context::new_union_type method,
641 immediately followed by a "set_fields" call on the resulting
642 gcc::jit::recording::compound_type *, both in jit-recording.c */
644 gcc_jit_type *
645 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
646 gcc_jit_location *loc,
647 const char *name,
648 int num_fields,
649 gcc_jit_field **fields)
651 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
652 /* LOC can be NULL. */
653 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
654 if (num_fields)
655 RETURN_NULL_IF_FAIL (fields, ctxt, loc, "NULL fields ptr");
656 for (int i = 0; i < num_fields; i++)
658 RETURN_NULL_IF_FAIL (fields[i], ctxt, loc, "NULL field ptr");
659 RETURN_NULL_IF_FAIL_PRINTF2 (
660 NULL == fields[i]->get_container (),
661 ctxt, loc,
662 "%s is already a field of %s",
663 fields[i]->get_debug_string (),
664 fields[i]->get_container ()->get_debug_string ());
667 gcc::jit::recording::union_ *result =
668 ctxt->new_union_type (loc, name);
669 result->set_fields (loc,
670 num_fields,
671 (gcc::jit::recording::field **)fields);
672 return (gcc_jit_type *) (result);
675 /* Public entrypoint. See description in libgccjit.h.
677 After error-checking, the real work is done by the
678 gcc::jit::recording::context::new_function_ptr_type method,
679 in jit-recording.c */
681 gcc_jit_type *
682 gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,
683 gcc_jit_location *loc,
684 gcc_jit_type *return_type,
685 int num_params,
686 gcc_jit_type **param_types,
687 int is_variadic)
689 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
690 /* LOC can be NULL. */
691 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
692 RETURN_NULL_IF_FAIL (
693 (num_params == 0) || param_types,
694 ctxt, loc,
695 "NULL param_types creating function pointer type");
696 for (int i = 0; i < num_params; i++)
697 RETURN_NULL_IF_FAIL_PRINTF1 (
698 param_types[i],
699 ctxt, loc,
700 "NULL parameter type %i creating function pointer type", i);
702 return (gcc_jit_type*)
703 ctxt->new_function_ptr_type (loc, return_type,
704 num_params,
705 (gcc::jit::recording::type **)param_types,
706 is_variadic);
709 /* Constructing functions. */
711 /* Public entrypoint. See description in libgccjit.h.
713 After error-checking, the real work is done by the
714 gcc::jit::recording::context::new_param method, in jit-recording.c */
716 gcc_jit_param *
717 gcc_jit_context_new_param (gcc_jit_context *ctxt,
718 gcc_jit_location *loc,
719 gcc_jit_type *type,
720 const char *name)
722 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
723 /* LOC can be NULL. */
724 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
725 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
727 return (gcc_jit_param *)ctxt->new_param (loc, type, name);
730 /* Public entrypoint. See description in libgccjit.h.
732 After error-checking, this calls the trivial
733 gcc::jit::recording::memento::as_object method (a param is a memento),
734 in jit-recording.h. */
736 gcc_jit_object *
737 gcc_jit_param_as_object (gcc_jit_param *param)
739 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
741 return static_cast <gcc_jit_object *> (param->as_object ());
744 /* Public entrypoint. See description in libgccjit.h.
746 After error-checking, this calls the trivial
747 gcc::jit::recording::param::as_lvalue method in jit-recording.h. */
749 gcc_jit_lvalue *
750 gcc_jit_param_as_lvalue (gcc_jit_param *param)
752 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
754 return (gcc_jit_lvalue *)param->as_lvalue ();
757 /* Public entrypoint. See description in libgccjit.h.
759 After error-checking, this calls the trivial
760 gcc::jit::recording::lvalue::as_rvalue method (a param is an rvalue),
761 in jit-recording.h. */
763 gcc_jit_rvalue *
764 gcc_jit_param_as_rvalue (gcc_jit_param *param)
766 RETURN_NULL_IF_FAIL (param, NULL, NULL, "NULL param");
768 return (gcc_jit_rvalue *)param->as_rvalue ();
771 /* Public entrypoint. See description in libgccjit.h.
773 After error-checking, the real work is done by the
774 gcc::jit::recording::context::new_function method, in
775 jit-recording.c. */
777 gcc_jit_function *
778 gcc_jit_context_new_function (gcc_jit_context *ctxt,
779 gcc_jit_location *loc,
780 enum gcc_jit_function_kind kind,
781 gcc_jit_type *return_type,
782 const char *name,
783 int num_params,
784 gcc_jit_param **params,
785 int is_variadic)
787 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
788 /* LOC can be NULL. */
789 RETURN_NULL_IF_FAIL_PRINTF1 (
790 ((kind >= GCC_JIT_FUNCTION_EXPORTED)
791 && (kind <= GCC_JIT_FUNCTION_ALWAYS_INLINE)),
792 ctxt, loc,
793 "unrecognized value for enum gcc_jit_function_kind: %i",
794 kind);
795 RETURN_NULL_IF_FAIL (return_type, ctxt, loc, "NULL return_type");
796 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
797 /* The assembler can only handle certain names, so for now, enforce
798 C's rules for identiers upon the name, using ISALPHA and ISALNUM
799 from safe-ctype.h to ignore the current locale.
800 Eventually we'll need some way to interact with e.g. C++ name
801 mangling. */
803 /* Leading char: */
804 char ch = *name;
805 RETURN_NULL_IF_FAIL_PRINTF2 (
806 ISALPHA (ch) || ch == '_',
807 ctxt, loc,
808 "name \"%s\" contains invalid character: '%c'",
809 name, ch);
810 /* Subsequent chars: */
811 for (const char *ptr = name + 1; (ch = *ptr); ptr++)
813 RETURN_NULL_IF_FAIL_PRINTF2 (
814 ISALNUM (ch) || ch == '_',
815 ctxt, loc,
816 "name \"%s\" contains invalid character: '%c'",
817 name, ch);
820 RETURN_NULL_IF_FAIL_PRINTF1 (
821 (num_params == 0) || params,
822 ctxt, loc,
823 "NULL params creating function %s", name);
824 for (int i = 0; i < num_params; i++)
825 RETURN_NULL_IF_FAIL_PRINTF2 (
826 params[i],
827 ctxt, loc,
828 "NULL parameter %i creating function %s", i, name);
830 return (gcc_jit_function*)
831 ctxt->new_function (loc, kind, return_type, name,
832 num_params,
833 (gcc::jit::recording::param **)params,
834 is_variadic,
835 BUILT_IN_NONE);
838 /* Public entrypoint. See description in libgccjit.h.
840 After error-checking, the real work is done by the
841 gcc::jit::recording::context::get_builtin_function method, in
842 jit-recording.c. */
844 gcc_jit_function *
845 gcc_jit_context_get_builtin_function (gcc_jit_context *ctxt,
846 const char *name)
848 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
849 RETURN_NULL_IF_FAIL (name, ctxt, NULL, "NULL name");
851 return static_cast <gcc_jit_function *> (ctxt->get_builtin_function (name));
854 /* Public entrypoint. See description in libgccjit.h.
856 After error-checking, this calls the trivial
857 gcc::jit::recording::memento::as_object method (a function is a
858 memento), in jit-recording.h. */
860 gcc_jit_object *
861 gcc_jit_function_as_object (gcc_jit_function *func)
863 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
865 return static_cast <gcc_jit_object *> (func->as_object ());
868 /* Public entrypoint. See description in libgccjit.h.
870 After error-checking, the real work is done by the
871 gcc::jit::recording::function::get_param method, in
872 jit-recording.h. */
874 gcc_jit_param *
875 gcc_jit_function_get_param (gcc_jit_function *func, int index)
877 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
878 gcc::jit::recording::context *ctxt = func->m_ctxt;
879 RETURN_NULL_IF_FAIL (index >= 0, ctxt, NULL, "negative index");
880 int num_params = func->get_params ().length ();
881 RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
882 ctxt, NULL,
883 "index of %d is too large (%s has %d params)",
884 index,
885 func->get_debug_string (),
886 num_params);
888 return static_cast <gcc_jit_param *> (func->get_param (index));
891 /* Public entrypoint. See description in libgccjit.h.
893 After error-checking, the real work is done by the
894 gcc::jit::recording::function::dump_to_dot method, in
895 jit-recording.c. */
897 void
898 gcc_jit_function_dump_to_dot (gcc_jit_function *func,
899 const char *path)
901 RETURN_IF_FAIL (func, NULL, NULL, "NULL function");
902 gcc::jit::recording::context *ctxt = func->m_ctxt;
903 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
905 func->dump_to_dot (path);
908 /* Public entrypoint. See description in libgccjit.h.
910 After error-checking, the real work is done by the
911 gcc::jit::recording::function::new_block method, in
912 jit-recording.c. */
914 gcc_jit_block*
915 gcc_jit_function_new_block (gcc_jit_function *func,
916 const char *name)
918 RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function");
919 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
920 func->get_context (), NULL,
921 "cannot add block to an imported function");
922 /* name can be NULL. */
924 return (gcc_jit_block *)func->new_block (name);
927 /* Public entrypoint. See description in libgccjit.h.
929 After error-checking, this calls the trivial
930 gcc::jit::recording::memento::as_object method (a block is a
931 memento), in jit-recording.h. */
933 gcc_jit_object *
934 gcc_jit_block_as_object (gcc_jit_block *block)
936 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
938 return static_cast <gcc_jit_object *> (block->as_object ());
941 /* Public entrypoint. See description in libgccjit.h.
943 After error-checking, the real work is done by the
944 gcc::jit::recording::block::get_function method, in
945 jit-recording.h. */
947 gcc_jit_function *
948 gcc_jit_block_get_function (gcc_jit_block *block)
950 RETURN_NULL_IF_FAIL (block, NULL, NULL, "NULL block");
952 return static_cast <gcc_jit_function *> (block->get_function ());
955 /* Public entrypoint. See description in libgccjit.h.
957 After error-checking, the real work is done by the
958 gcc::jit::recording::context::new_global method, in
959 jit-recording.c. */
961 gcc_jit_lvalue *
962 gcc_jit_context_new_global (gcc_jit_context *ctxt,
963 gcc_jit_location *loc,
964 gcc_jit_type *type,
965 const char *name)
967 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
968 /* LOC can be NULL. */
969 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
970 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
972 return (gcc_jit_lvalue *)ctxt->new_global (loc, type, name);
975 /* Public entrypoint. See description in libgccjit.h.
977 After error-checking, this calls the trivial
978 gcc::jit::recording::memento::as_object method (an lvalue is a
979 memento), in jit-recording.h. */
981 gcc_jit_object *
982 gcc_jit_lvalue_as_object (gcc_jit_lvalue *lvalue)
984 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
986 return static_cast <gcc_jit_object *> (lvalue->as_object ());
989 /* Public entrypoint. See description in libgccjit.h.
991 After error-checking, this calls the trivial
992 gcc::jit::recording::lvalue::as_rvalue method in jit-recording.h. */
994 gcc_jit_rvalue *
995 gcc_jit_lvalue_as_rvalue (gcc_jit_lvalue *lvalue)
997 RETURN_NULL_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
999 return (gcc_jit_rvalue *)lvalue->as_rvalue ();
1002 /* Public entrypoint. See description in libgccjit.h.
1004 After error-checking, this calls the trivial
1005 gcc::jit::recording::memento::as_object method (an rvalue is a
1006 memento), in jit-recording.h. */
1008 gcc_jit_object *
1009 gcc_jit_rvalue_as_object (gcc_jit_rvalue *rvalue)
1011 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1013 return static_cast <gcc_jit_object *> (rvalue->as_object ());
1016 /* Public entrypoint. See description in libgccjit.h.
1018 After error-checking, the real work is done by the
1019 gcc::jit::recording::rvalue::get_type method, in
1020 jit-recording.h. */
1022 gcc_jit_type *
1023 gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue)
1025 RETURN_NULL_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");
1027 return static_cast <gcc_jit_type *> (rvalue->get_type ());
1030 /* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
1031 type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
1032 result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
1034 #define RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE(CTXT, NUMERIC_TYPE) \
1035 RETURN_NULL_IF_FAIL (NUMERIC_TYPE, CTXT, NULL, "NULL type"); \
1036 RETURN_NULL_IF_FAIL_PRINTF1 ( \
1037 NUMERIC_TYPE->is_numeric (), ctxt, NULL, \
1038 "not a numeric type: %s", \
1039 NUMERIC_TYPE->get_debug_string ());
1041 /* Public entrypoint. See description in libgccjit.h.
1043 After error-checking, the real work is done by the
1044 gcc::jit::recording::context::new_rvalue_from_int method in
1045 jit-recording.c. */
1047 gcc_jit_rvalue *
1048 gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
1049 gcc_jit_type *numeric_type,
1050 int value)
1052 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1053 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1055 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_int (numeric_type, value);
1058 /* Public entrypoint. See description in libgccjit.h.
1060 This is essentially equivalent to:
1061 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1062 albeit with slightly different error messages if an error occurs. */
1064 gcc_jit_rvalue *
1065 gcc_jit_context_zero (gcc_jit_context *ctxt,
1066 gcc_jit_type *numeric_type)
1068 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1069 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1071 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 0);
1074 /* Public entrypoint. See description in libgccjit.h.
1076 This is essentially equivalent to:
1077 gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1078 albeit with slightly different error messages if an error occurs. */
1080 gcc_jit_rvalue *
1081 gcc_jit_context_one (gcc_jit_context *ctxt,
1082 gcc_jit_type *numeric_type)
1084 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1085 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1087 return gcc_jit_context_new_rvalue_from_int (ctxt, numeric_type, 1);
1090 /* Public entrypoint. See description in libgccjit.h.
1092 After error-checking, the real work is done by the
1093 gcc::jit::recording::context::new_rvalue_from_double method in
1094 jit-recording.c. */
1096 gcc_jit_rvalue *
1097 gcc_jit_context_new_rvalue_from_double (gcc_jit_context *ctxt,
1098 gcc_jit_type *numeric_type,
1099 double value)
1101 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1102 RETURN_NULL_IF_FAIL_NONNULL_NUMERIC_TYPE (ctxt, numeric_type);
1104 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_double (numeric_type, value);
1107 /* Public entrypoint. See description in libgccjit.h.
1109 After error-checking, the real work is done by the
1110 gcc::jit::recording::context::new_rvalue_from_ptr method in
1111 jit-recording.c. */
1113 gcc_jit_rvalue *
1114 gcc_jit_context_new_rvalue_from_ptr (gcc_jit_context *ctxt,
1115 gcc_jit_type *pointer_type,
1116 void *value)
1118 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1119 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1120 RETURN_NULL_IF_FAIL_PRINTF1 (
1121 pointer_type->is_pointer (),
1122 ctxt, NULL,
1123 "not a pointer type (type: %s)",
1124 pointer_type->get_debug_string ());
1126 return (gcc_jit_rvalue *)ctxt->new_rvalue_from_ptr (pointer_type, value);
1129 /* Public entrypoint. See description in libgccjit.h.
1131 This is essentially equivalent to:
1132 gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1133 albeit with slightly different error messages if an error occurs. */
1135 gcc_jit_rvalue *
1136 gcc_jit_context_null (gcc_jit_context *ctxt,
1137 gcc_jit_type *pointer_type)
1139 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1140 RETURN_NULL_IF_FAIL (pointer_type, ctxt, NULL, "NULL type");
1141 RETURN_NULL_IF_FAIL_PRINTF1 (
1142 pointer_type->is_pointer (),
1143 ctxt, NULL,
1144 "not a pointer type (type: %s)",
1145 pointer_type->get_debug_string ());
1147 return gcc_jit_context_new_rvalue_from_ptr (ctxt, pointer_type, NULL);
1150 /* Public entrypoint. See description in libgccjit.h.
1152 After error-checking, the real work is done by the
1153 gcc::jit::recording::context::new_string_literal method in
1154 jit-recording.c. */
1156 gcc_jit_rvalue *
1157 gcc_jit_context_new_string_literal (gcc_jit_context *ctxt,
1158 const char *value)
1160 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1161 RETURN_NULL_IF_FAIL (value, ctxt, NULL, "NULL value");
1163 return (gcc_jit_rvalue *)ctxt->new_string_literal (value);
1166 /* Public entrypoint. See description in libgccjit.h.
1168 After error-checking, the real work is done by the
1169 gcc::jit::recording::context::new_unary_op method in
1170 jit-recording.c. */
1172 gcc_jit_rvalue *
1173 gcc_jit_context_new_unary_op (gcc_jit_context *ctxt,
1174 gcc_jit_location *loc,
1175 enum gcc_jit_unary_op op,
1176 gcc_jit_type *result_type,
1177 gcc_jit_rvalue *rvalue)
1179 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1180 /* LOC can be NULL. */
1181 RETURN_NULL_IF_FAIL_PRINTF1 (
1182 (op >= GCC_JIT_UNARY_OP_MINUS
1183 && op <= GCC_JIT_UNARY_OP_LOGICAL_NEGATE),
1184 ctxt, loc,
1185 "unrecognized value for enum gcc_jit_unary_op: %i",
1186 op);
1187 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1188 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1190 return (gcc_jit_rvalue *)ctxt->new_unary_op (loc, op, result_type, rvalue);
1193 /* Determine if OP is a valid value for enum gcc_jit_binary_op.
1194 For use by both gcc_jit_context_new_binary_op and
1195 gcc_jit_block_add_assignment_op. */
1197 static bool
1198 valid_binary_op_p (enum gcc_jit_binary_op op)
1200 return (op >= GCC_JIT_BINARY_OP_PLUS
1201 && op <= GCC_JIT_BINARY_OP_RSHIFT);
1204 /* Public entrypoint. See description in libgccjit.h.
1206 After error-checking, the real work is done by the
1207 gcc::jit::recording::context::new_binary_op method in
1208 jit-recording.c. */
1210 gcc_jit_rvalue *
1211 gcc_jit_context_new_binary_op (gcc_jit_context *ctxt,
1212 gcc_jit_location *loc,
1213 enum gcc_jit_binary_op op,
1214 gcc_jit_type *result_type,
1215 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1217 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1218 /* LOC can be NULL. */
1219 RETURN_NULL_IF_FAIL_PRINTF1 (
1220 valid_binary_op_p (op),
1221 ctxt, loc,
1222 "unrecognized value for enum gcc_jit_binary_op: %i",
1223 op);
1224 RETURN_NULL_IF_FAIL (result_type, ctxt, loc, "NULL result_type");
1225 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1226 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1227 RETURN_NULL_IF_FAIL_PRINTF4 (
1228 a->get_type () == b->get_type (),
1229 ctxt, loc,
1230 "mismatching types for binary op:"
1231 " a: %s (type: %s) b: %s (type: %s)",
1232 a->get_debug_string (),
1233 a->get_type ()->get_debug_string (),
1234 b->get_debug_string (),
1235 b->get_type ()->get_debug_string ());
1237 return (gcc_jit_rvalue *)ctxt->new_binary_op (loc, op, result_type, a, b);
1240 /* Public entrypoint. See description in libgccjit.h.
1242 After error-checking, the real work is done by the
1243 gcc::jit::recording::context::new_comparison method in
1244 jit-recording.c. */
1246 gcc_jit_rvalue *
1247 gcc_jit_context_new_comparison (gcc_jit_context *ctxt,
1248 gcc_jit_location *loc,
1249 enum gcc_jit_comparison op,
1250 gcc_jit_rvalue *a, gcc_jit_rvalue *b)
1252 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1253 /* LOC can be NULL. */
1254 RETURN_NULL_IF_FAIL_PRINTF1 (
1255 (op >= GCC_JIT_COMPARISON_EQ
1256 && op <= GCC_JIT_COMPARISON_GE),
1257 ctxt, loc,
1258 "unrecognized value for enum gcc_jit_comparison: %i",
1259 op);
1260 RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a");
1261 RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b");
1262 RETURN_NULL_IF_FAIL_PRINTF4 (
1263 a->get_type ()->unqualified () == b->get_type ()->unqualified (),
1264 ctxt, loc,
1265 "mismatching types for comparison:"
1266 " a: %s (type: %s) b: %s (type: %s)",
1267 a->get_debug_string (),
1268 a->get_type ()->get_debug_string (),
1269 b->get_debug_string (),
1270 b->get_type ()->get_debug_string ());
1272 return (gcc_jit_rvalue *)ctxt->new_comparison (loc, op, a, b);
1275 /* Public entrypoint. See description in libgccjit.h.
1277 After error-checking, the real work is done by the
1278 gcc::jit::recording::context::new_call method in
1279 jit-recording.c. */
1281 gcc_jit_rvalue *
1282 gcc_jit_context_new_call (gcc_jit_context *ctxt,
1283 gcc_jit_location *loc,
1284 gcc_jit_function *func,
1285 int numargs , gcc_jit_rvalue **args)
1287 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1288 /* LOC can be NULL. */
1289 RETURN_NULL_IF_FAIL (func, ctxt, loc, "NULL function");
1290 if (numargs)
1291 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1293 int min_num_params = func->get_params ().length ();
1294 bool is_variadic = func->is_variadic ();
1296 RETURN_NULL_IF_FAIL_PRINTF3 (
1297 numargs >= min_num_params,
1298 ctxt, loc,
1299 "not enough arguments to function \"%s\""
1300 " (got %i args, expected %i)",
1301 func->get_name ()->c_str (),
1302 numargs, min_num_params);
1304 RETURN_NULL_IF_FAIL_PRINTF3 (
1305 (numargs == min_num_params || is_variadic),
1306 ctxt, loc,
1307 "too many arguments to function \"%s\""
1308 " (got %i args, expected %i)",
1309 func->get_name ()->c_str (),
1310 numargs, min_num_params);
1312 for (int i = 0; i < min_num_params; i++)
1314 gcc::jit::recording::param *param = func->get_param (i);
1315 gcc_jit_rvalue *arg = args[i];
1317 RETURN_NULL_IF_FAIL_PRINTF4 (
1318 arg,
1319 ctxt, loc,
1320 "NULL argument %i to function \"%s\":"
1321 " param %s (type: %s)",
1322 i + 1,
1323 func->get_name ()->c_str (),
1324 param->get_debug_string (),
1325 param->get_type ()->get_debug_string ());
1327 RETURN_NULL_IF_FAIL_PRINTF6 (
1328 compatible_types (param->get_type (),
1329 arg->get_type ()),
1330 ctxt, loc,
1331 "mismatching types for argument %d of function \"%s\":"
1332 " assignment to param %s (type: %s) from %s (type: %s)",
1333 i + 1,
1334 func->get_name ()->c_str (),
1335 param->get_debug_string (),
1336 param->get_type ()->get_debug_string (),
1337 arg->get_debug_string (),
1338 arg->get_type ()->get_debug_string ());
1341 return (gcc_jit_rvalue *)ctxt->new_call (loc,
1342 func,
1343 numargs,
1344 (gcc::jit::recording::rvalue **)args);
1347 /* Public entrypoint. See description in libgccjit.h.
1349 After error-checking, the real work is done by the
1350 gcc::jit::recording::context::new_call_through_ptr method in
1351 jit-recording.c. */
1353 gcc_jit_rvalue *
1354 gcc_jit_context_new_call_through_ptr (gcc_jit_context *ctxt,
1355 gcc_jit_location *loc,
1356 gcc_jit_rvalue *fn_ptr,
1357 int numargs, gcc_jit_rvalue **args)
1359 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1360 /* LOC can be NULL. */
1361 RETURN_NULL_IF_FAIL (fn_ptr, ctxt, loc, "NULL fn_ptr");
1362 if (numargs)
1363 RETURN_NULL_IF_FAIL (args, ctxt, loc, "NULL args");
1365 gcc::jit::recording::type *ptr_type = fn_ptr->get_type ()->dereference ();
1366 RETURN_NULL_IF_FAIL_PRINTF2 (
1367 ptr_type, ctxt, loc,
1368 "fn_ptr is not a ptr: %s"
1369 " type: %s",
1370 fn_ptr->get_debug_string (),
1371 fn_ptr->get_type ()->get_debug_string ());
1373 gcc::jit::recording::function_type *fn_type =
1374 ptr_type->dyn_cast_function_type();
1375 RETURN_NULL_IF_FAIL_PRINTF2 (
1376 fn_type, ctxt, loc,
1377 "fn_ptr is not a function ptr: %s"
1378 " type: %s",
1379 fn_ptr->get_debug_string (),
1380 fn_ptr->get_type ()->get_debug_string ());
1382 int min_num_params = fn_type->get_param_types ().length ();
1383 bool is_variadic = fn_type->is_variadic ();
1385 RETURN_NULL_IF_FAIL_PRINTF3 (
1386 numargs >= min_num_params,
1387 ctxt, loc,
1388 "not enough arguments to fn_ptr: %s"
1389 " (got %i args, expected %i)",
1390 fn_ptr->get_debug_string (),
1391 numargs, min_num_params);
1393 RETURN_NULL_IF_FAIL_PRINTF3 (
1394 (numargs == min_num_params || is_variadic),
1395 ctxt, loc,
1396 "too many arguments to fn_ptr: %s"
1397 " (got %i args, expected %i)",
1398 fn_ptr->get_debug_string (),
1399 numargs, min_num_params);
1401 for (int i = 0; i < min_num_params; i++)
1403 gcc::jit::recording::type *param_type = fn_type->get_param_types ()[i];
1404 gcc_jit_rvalue *arg = args[i];
1406 RETURN_NULL_IF_FAIL_PRINTF3 (
1407 arg,
1408 ctxt, loc,
1409 "NULL argument %i to fn_ptr: %s"
1410 " (type: %s)",
1411 i + 1,
1412 fn_ptr->get_debug_string (),
1413 param_type->get_debug_string ());
1415 RETURN_NULL_IF_FAIL_PRINTF6 (
1416 compatible_types (param_type,
1417 arg->get_type ()),
1418 ctxt, loc,
1419 "mismatching types for argument %d of fn_ptr: %s:"
1420 " assignment to param %d (type: %s) from %s (type: %s)",
1421 i + 1,
1422 fn_ptr->get_debug_string (),
1423 i + 1,
1424 param_type->get_debug_string (),
1425 arg->get_debug_string (),
1426 arg->get_type ()->get_debug_string ());
1429 return (gcc_jit_rvalue *)(
1430 ctxt->new_call_through_ptr (loc,
1431 fn_ptr,
1432 numargs,
1433 (gcc::jit::recording::rvalue **)args));
1436 /* Helper function for determining if we can cast an rvalue from SRC_TYPE
1437 to DST_TYPE, for use by gcc_jit_context_new_cast.
1439 We only permit these kinds of cast:
1441 int <-> float
1442 int <-> bool
1443 P* <-> Q* for pointer types P and Q. */
1445 static bool
1446 is_valid_cast (gcc::jit::recording::type *src_type,
1447 gcc_jit_type *dst_type)
1449 bool src_is_int = src_type->is_int ();
1450 bool dst_is_int = dst_type->is_int ();
1451 bool src_is_float = src_type->is_float ();
1452 bool dst_is_float = dst_type->is_float ();
1453 bool src_is_bool = src_type->is_bool ();
1454 bool dst_is_bool = dst_type->is_bool ();
1456 if (src_is_int)
1457 if (dst_is_int || dst_is_float || dst_is_bool)
1458 return true;
1460 if (src_is_float)
1461 if (dst_is_int || dst_is_float)
1462 return true;
1464 if (src_is_bool)
1465 if (dst_is_int || dst_is_bool)
1466 return true;
1468 /* Permit casts between pointer types. */
1469 gcc::jit::recording::type *deref_src_type = src_type->is_pointer ();
1470 gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer ();
1471 if (deref_src_type && deref_dst_type)
1472 return true;
1474 return false;
1477 /* Public entrypoint. See description in libgccjit.h.
1479 After error-checking, the real work is done by the
1480 gcc::jit::recording::context::new_cast method in jit-recording.c. */
1482 gcc_jit_rvalue *
1483 gcc_jit_context_new_cast (gcc_jit_context *ctxt,
1484 gcc_jit_location *loc,
1485 gcc_jit_rvalue *rvalue,
1486 gcc_jit_type *type)
1488 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1489 /* LOC can be NULL. */
1490 RETURN_NULL_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1491 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1492 RETURN_NULL_IF_FAIL_PRINTF3 (
1493 is_valid_cast (rvalue->get_type (), type),
1494 ctxt, loc,
1495 "cannot cast %s from type: %s to type: %s",
1496 rvalue->get_debug_string (),
1497 rvalue->get_type ()->get_debug_string (),
1498 type->get_debug_string ());
1500 return static_cast <gcc_jit_rvalue *> (ctxt->new_cast (loc, rvalue, type));
1503 /* Public entrypoint. See description in libgccjit.h.
1505 After error-checking, the real work is done by the
1506 gcc::jit::recording::context::new_array_access method in
1507 jit-recording.c. */
1509 extern gcc_jit_lvalue *
1510 gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
1511 gcc_jit_location *loc,
1512 gcc_jit_rvalue *ptr,
1513 gcc_jit_rvalue *index)
1515 RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
1516 /* LOC can be NULL. */
1517 RETURN_NULL_IF_FAIL (ptr, ctxt, loc, "NULL ptr");
1518 RETURN_NULL_IF_FAIL (index, ctxt, loc, "NULL index");
1519 RETURN_NULL_IF_FAIL_PRINTF2 (
1520 ptr->get_type ()->dereference (),
1521 ctxt, loc,
1522 "ptr: %s (type: %s) is not a pointer or array",
1523 ptr->get_debug_string (),
1524 ptr->get_type ()->get_debug_string ());
1525 RETURN_NULL_IF_FAIL_PRINTF2 (
1526 index->get_type ()->is_numeric (),
1527 ctxt, loc,
1528 "index: %s (type: %s) is not of numeric type",
1529 index->get_debug_string (),
1530 index->get_type ()->get_debug_string ());
1532 return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
1535 /* Public entrypoint. See description in libgccjit.h.
1537 After error-checking, the real work is done by the
1538 gcc::jit::recording::memento::get_context method in
1539 jit-recording.h. */
1541 gcc_jit_context *
1542 gcc_jit_object_get_context (gcc_jit_object *obj)
1544 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1546 return static_cast <gcc_jit_context *> (obj->get_context ());
1549 /* Public entrypoint. See description in libgccjit.h.
1551 After error-checking, the real work is done by the
1552 gcc::jit::recording::memento::get_debug_string method in
1553 jit-recording.c. */
1555 const char *
1556 gcc_jit_object_get_debug_string (gcc_jit_object *obj)
1558 RETURN_NULL_IF_FAIL (obj, NULL, NULL, "NULL object");
1560 return obj->get_debug_string ();
1563 /* Public entrypoint. See description in libgccjit.h.
1565 After error-checking, the real work is done by the
1566 gcc::jit::recording::lvalue::access_field method in
1567 jit-recording.c. */
1569 gcc_jit_lvalue *
1570 gcc_jit_lvalue_access_field (gcc_jit_lvalue *struct_,
1571 gcc_jit_location *loc,
1572 gcc_jit_field *field)
1574 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1575 /* LOC can be NULL. */
1576 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1577 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1578 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1579 "field %s has not been placed in a struct",
1580 field->get_debug_string ());
1582 return (gcc_jit_lvalue *)struct_->access_field (loc, field);
1585 /* Public entrypoint. See description in libgccjit.h.
1587 After error-checking, the real work is done by the
1588 gcc::jit::recording::rvalue::access_field method in
1589 jit-recording.c. */
1591 gcc_jit_rvalue *
1592 gcc_jit_rvalue_access_field (gcc_jit_rvalue *struct_,
1593 gcc_jit_location *loc,
1594 gcc_jit_field *field)
1596 RETURN_NULL_IF_FAIL (struct_, NULL, loc, "NULL struct");
1597 /* LOC can be NULL. */
1598 gcc::jit::recording::context *ctxt = struct_->m_ctxt;
1599 RETURN_NULL_IF_FAIL (field, ctxt, loc, "NULL field");
1600 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1601 "field %s has not been placed in a struct",
1602 field->get_debug_string ());
1604 return (gcc_jit_rvalue *)struct_->access_field (loc, field);
1607 /* Public entrypoint. See description in libgccjit.h.
1609 After error-checking, the real work is done by the
1610 gcc::jit::recording::rvalue::deference_field method in
1611 jit-recording.c. */
1613 gcc_jit_lvalue *
1614 gcc_jit_rvalue_dereference_field (gcc_jit_rvalue *ptr,
1615 gcc_jit_location *loc,
1616 gcc_jit_field *field)
1618 RETURN_NULL_IF_FAIL (ptr, NULL, loc, "NULL ptr");
1619 /* LOC can be NULL. */
1620 RETURN_NULL_IF_FAIL (field, NULL, loc, "NULL field");
1621 gcc::jit::recording::type *underlying_type =
1622 ptr->get_type ()->is_pointer ();
1623 RETURN_NULL_IF_FAIL_PRINTF1 (field->get_container (), field->m_ctxt, loc,
1624 "field %s has not been placed in a struct",
1625 field->get_debug_string ());
1626 RETURN_NULL_IF_FAIL_PRINTF3 (
1627 underlying_type,
1628 ptr->m_ctxt, loc,
1629 "dereference of non-pointer %s (type: %s) when accessing ->%s",
1630 ptr->get_debug_string (),
1631 ptr->get_type ()->get_debug_string (),
1632 field->get_debug_string ());
1633 RETURN_NULL_IF_FAIL_PRINTF2 (
1634 (field->get_container ()->unqualified ()
1635 == underlying_type->unqualified ()),
1636 ptr->m_ctxt, loc,
1637 "%s is not a field of %s",
1638 field->get_debug_string (),
1639 underlying_type->get_debug_string ());
1641 return (gcc_jit_lvalue *)ptr->dereference_field (loc, field);
1644 /* Public entrypoint. See description in libgccjit.h.
1646 After error-checking, the real work is done by the
1647 gcc::jit::recording::rvalue::deference method in
1648 jit-recording.c. */
1650 gcc_jit_lvalue *
1651 gcc_jit_rvalue_dereference (gcc_jit_rvalue *rvalue,
1652 gcc_jit_location *loc)
1654 RETURN_NULL_IF_FAIL (rvalue, NULL, loc, "NULL rvalue");
1655 /* LOC can be NULL. */
1657 gcc::jit::recording::type *underlying_type =
1658 rvalue->get_type ()->is_pointer ();
1660 RETURN_NULL_IF_FAIL_PRINTF2 (
1661 underlying_type,
1662 rvalue->m_ctxt, loc,
1663 "dereference of non-pointer %s (type: %s)",
1664 rvalue->get_debug_string (),
1665 rvalue->get_type ()->get_debug_string ());
1667 return (gcc_jit_lvalue *)rvalue->dereference (loc);
1670 /* Public entrypoint. See description in libgccjit.h.
1672 After error-checking, the real work is done by the
1673 gcc::jit::recording::lvalue::get_address method in jit-recording.c. */
1675 gcc_jit_rvalue *
1676 gcc_jit_lvalue_get_address (gcc_jit_lvalue *lvalue,
1677 gcc_jit_location *loc)
1679 RETURN_NULL_IF_FAIL (lvalue, NULL, loc, "NULL lvalue");
1680 /* LOC can be NULL. */
1682 return (gcc_jit_rvalue *)lvalue->get_address (loc);
1685 /* Public entrypoint. See description in libgccjit.h.
1687 After error-checking, the real work is done by the
1688 gcc::jit::recording::function::new_local method in jit-recording.c. */
1690 gcc_jit_lvalue *
1691 gcc_jit_function_new_local (gcc_jit_function *func,
1692 gcc_jit_location *loc,
1693 gcc_jit_type *type,
1694 const char *name)
1696 RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
1697 /* LOC can be NULL. */
1698 gcc::jit::recording::context *ctxt = func->m_ctxt;
1699 RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
1700 ctxt, loc,
1701 "Cannot add locals to an imported function");
1702 RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
1703 RETURN_NULL_IF_FAIL (name, ctxt, loc, "NULL name");
1705 return (gcc_jit_lvalue *)func->new_local (loc, type, name);
1708 /* Public entrypoint. See description in libgccjit.h.
1710 After error-checking, the real work is done by the
1711 gcc::jit::recording::block::add_eval method in jit-recording.c. */
1713 void
1714 gcc_jit_block_add_eval (gcc_jit_block *block,
1715 gcc_jit_location *loc,
1716 gcc_jit_rvalue *rvalue)
1718 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1719 /* LOC can be NULL. */
1720 gcc::jit::recording::context *ctxt = block->get_context ();
1721 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1723 return block->add_eval (loc, rvalue);
1726 /* Public entrypoint. See description in libgccjit.h.
1728 After error-checking, the real work is done by the
1729 gcc::jit::recording::block::add_assignment method in
1730 jit-recording.c. */
1732 void
1733 gcc_jit_block_add_assignment (gcc_jit_block *block,
1734 gcc_jit_location *loc,
1735 gcc_jit_lvalue *lvalue,
1736 gcc_jit_rvalue *rvalue)
1738 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1739 /* LOC can be NULL. */
1740 gcc::jit::recording::context *ctxt = block->get_context ();
1741 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1742 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1743 RETURN_IF_FAIL_PRINTF4 (
1744 compatible_types (lvalue->get_type (),
1745 rvalue->get_type ()),
1746 ctxt, loc,
1747 "mismatching types:"
1748 " assignment to %s (type: %s) from %s (type: %s)",
1749 lvalue->get_debug_string (),
1750 lvalue->get_type ()->get_debug_string (),
1751 rvalue->get_debug_string (),
1752 rvalue->get_type ()->get_debug_string ());
1754 return block->add_assignment (loc, lvalue, rvalue);
1757 /* Public entrypoint. See description in libgccjit.h.
1759 After error-checking, the real work is done by the
1760 gcc::jit::recording::block::add_assignment_op method in
1761 jit-recording.c. */
1763 void
1764 gcc_jit_block_add_assignment_op (gcc_jit_block *block,
1765 gcc_jit_location *loc,
1766 gcc_jit_lvalue *lvalue,
1767 enum gcc_jit_binary_op op,
1768 gcc_jit_rvalue *rvalue)
1770 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1771 /* LOC can be NULL. */
1772 gcc::jit::recording::context *ctxt = block->get_context ();
1773 RETURN_IF_FAIL (lvalue, ctxt, loc, "NULL lvalue");
1774 RETURN_IF_FAIL_PRINTF1 (
1775 valid_binary_op_p (op),
1776 ctxt, loc,
1777 "unrecognized value for enum gcc_jit_binary_op: %i",
1778 op);
1779 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1781 return block->add_assignment_op (loc, lvalue, op, rvalue);
1784 /* Internal helper function for determining if rvalue BOOLVAL is of
1785 boolean type. For use by gcc_jit_block_end_with_conditional. */
1787 static bool
1788 is_bool (gcc_jit_rvalue *boolval)
1790 gcc::jit::recording::type *actual_type = boolval->get_type ();
1791 gcc::jit::recording::type *bool_type =
1792 boolval->m_ctxt->get_type (GCC_JIT_TYPE_BOOL);
1793 return actual_type == bool_type;
1796 /* Public entrypoint. See description in libgccjit.h.
1798 After error-checking, the real work is done by the
1799 gcc::jit::recording::block::end_with_conditional method in
1800 jit-recording.c. */
1802 void
1803 gcc_jit_block_end_with_conditional (gcc_jit_block *block,
1804 gcc_jit_location *loc,
1805 gcc_jit_rvalue *boolval,
1806 gcc_jit_block *on_true,
1807 gcc_jit_block *on_false)
1809 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1810 /* LOC can be NULL. */
1811 gcc::jit::recording::context *ctxt = block->get_context ();
1812 RETURN_IF_FAIL (boolval, ctxt, loc, "NULL boolval");
1813 RETURN_IF_FAIL_PRINTF2 (
1814 is_bool (boolval), ctxt, loc,
1815 "%s (type: %s) is not of boolean type ",
1816 boolval->get_debug_string (),
1817 boolval->get_type ()->get_debug_string ());
1818 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_true");
1819 RETURN_IF_FAIL (on_true, ctxt, loc, "NULL on_false");
1820 RETURN_IF_FAIL_PRINTF4 (
1821 block->get_function () == on_true->get_function (),
1822 ctxt, loc,
1823 "\"on_true\" block is not in same function:"
1824 " source block %s is in function %s"
1825 " whereas target block %s is in function %s",
1826 block->get_debug_string (),
1827 block->get_function ()->get_debug_string (),
1828 on_true->get_debug_string (),
1829 on_true->get_function ()->get_debug_string ());
1830 RETURN_IF_FAIL_PRINTF4 (
1831 block->get_function () == on_false->get_function (),
1832 ctxt, loc,
1833 "\"on_false\" block is not in same function:"
1834 " source block %s is in function %s"
1835 " whereas target block %s is in function %s",
1836 block->get_debug_string (),
1837 block->get_function ()->get_debug_string (),
1838 on_false->get_debug_string (),
1839 on_false->get_function ()->get_debug_string ());
1841 return block->end_with_conditional (loc, boolval, on_true, on_false);
1844 /* Public entrypoint. See description in libgccjit.h.
1846 After error-checking, the real work is done by the
1847 gcc::jit::recording::block::add_comment method in
1848 jit-recording.c. */
1850 void
1851 gcc_jit_block_add_comment (gcc_jit_block *block,
1852 gcc_jit_location *loc,
1853 const char *text)
1855 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1856 /* LOC can be NULL. */
1857 gcc::jit::recording::context *ctxt = block->get_context ();
1858 RETURN_IF_FAIL (text, ctxt, loc, "NULL text");
1860 block->add_comment (loc, text);
1863 /* Public entrypoint. See description in libgccjit.h.
1865 After error-checking, the real work is done by the
1866 gcc::jit::recording::block::end_with_jump method in
1867 jit-recording.c. */
1869 void
1870 gcc_jit_block_end_with_jump (gcc_jit_block *block,
1871 gcc_jit_location *loc,
1872 gcc_jit_block *target)
1874 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1875 /* LOC can be NULL. */
1876 gcc::jit::recording::context *ctxt = block->get_context ();
1877 RETURN_IF_FAIL (target, ctxt, loc, "NULL target");
1878 RETURN_IF_FAIL_PRINTF4 (
1879 block->get_function () == target->get_function (),
1880 ctxt, loc,
1881 "target block is not in same function:"
1882 " source block %s is in function %s"
1883 " whereas target block %s is in function %s",
1884 block->get_debug_string (),
1885 block->get_function ()->get_debug_string (),
1886 target->get_debug_string (),
1887 target->get_function ()->get_debug_string ());
1889 block->end_with_jump (loc, target);
1892 /* Public entrypoint. See description in libgccjit.h.
1894 After error-checking, the real work is done by the
1895 gcc::jit::recording::block::end_with_return method in
1896 jit-recording.c. */
1898 void
1899 gcc_jit_block_end_with_return (gcc_jit_block *block,
1900 gcc_jit_location *loc,
1901 gcc_jit_rvalue *rvalue)
1903 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1904 /* LOC can be NULL. */
1905 gcc::jit::recording::context *ctxt = block->get_context ();
1906 gcc::jit::recording::function *func = block->get_function ();
1907 RETURN_IF_FAIL (rvalue, ctxt, loc, "NULL rvalue");
1908 RETURN_IF_FAIL_PRINTF4 (
1909 compatible_types (
1910 func->get_return_type (),
1911 rvalue->get_type ()),
1912 ctxt, loc,
1913 "mismatching types:"
1914 " return of %s (type: %s) in function %s (return type: %s)",
1915 rvalue->get_debug_string (),
1916 rvalue->get_type ()->get_debug_string (),
1917 func->get_debug_string (),
1918 func->get_return_type ()->get_debug_string ());
1920 return block->end_with_return (loc, rvalue);
1923 /* Public entrypoint. See description in libgccjit.h.
1925 After error-checking, the real work is done by the
1926 gcc::jit::recording::block::end_with_return method in
1927 jit-recording.c. */
1929 void
1930 gcc_jit_block_end_with_void_return (gcc_jit_block *block,
1931 gcc_jit_location *loc)
1933 RETURN_IF_NOT_VALID_BLOCK (block, loc);
1934 /* LOC can be NULL. */
1935 gcc::jit::recording::context *ctxt = block->get_context ();
1936 gcc::jit::recording::function *func = block->get_function ();
1937 RETURN_IF_FAIL_PRINTF2 (
1938 func->get_return_type () == ctxt->get_type (GCC_JIT_TYPE_VOID),
1939 ctxt, loc,
1940 "mismatching types:"
1941 " void return in function %s (return type: %s)",
1942 func->get_debug_string (),
1943 func->get_return_type ()->get_debug_string ());
1945 return block->end_with_return (loc, NULL);
1948 /**********************************************************************
1949 Option-management
1950 **********************************************************************/
1952 /* Public entrypoint. See description in libgccjit.h.
1954 After error-checking, the real work is done by the
1955 gcc::jit::recording::context::set_str_option method in
1956 jit-recording.c. */
1958 void
1959 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
1960 enum gcc_jit_str_option opt,
1961 const char *value)
1963 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1964 /* opt is checked by the inner function.
1965 value can be NULL. */
1967 ctxt->set_str_option (opt, value);
1970 /* Public entrypoint. See description in libgccjit.h.
1972 After error-checking, the real work is done by the
1973 gcc::jit::recording::context::set_int_option method in
1974 jit-recording.c. */
1976 void
1977 gcc_jit_context_set_int_option (gcc_jit_context *ctxt,
1978 enum gcc_jit_int_option opt,
1979 int value)
1981 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1982 /* opt is checked by the inner function. */
1984 ctxt->set_int_option (opt, value);
1987 /* Public entrypoint. See description in libgccjit.h.
1989 After error-checking, the real work is done by the
1990 gcc::jit::recording::context::set_bool_option method in
1991 jit-recording.c. */
1993 void
1994 gcc_jit_context_set_bool_option (gcc_jit_context *ctxt,
1995 enum gcc_jit_bool_option opt,
1996 int value)
1998 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
1999 /* opt is checked by the inner function. */
2001 ctxt->set_bool_option (opt, value);
2004 /* Public entrypoint. See description in libgccjit.h.
2006 After error-checking, the real work is done by the
2007 gcc::jit::recording::context::enable_dump method in
2008 jit-recording.c. */
2010 void
2011 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,
2012 const char *dumpname,
2013 char **out_ptr)
2015 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2016 RETURN_IF_FAIL (dumpname, ctxt, NULL, "NULL dumpname");
2017 RETURN_IF_FAIL (out_ptr, ctxt, NULL, "NULL out_ptr");
2019 ctxt->enable_dump (dumpname, out_ptr);
2022 /* Public entrypoint. See description in libgccjit.h.
2024 After error-checking, the real work is done by the
2025 gcc::jit::recording::context::compile method in
2026 jit-recording.c. */
2028 gcc_jit_result *
2029 gcc_jit_context_compile (gcc_jit_context *ctxt)
2031 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2033 return (gcc_jit_result *)ctxt->compile ();
2036 /* Public entrypoint. See description in libgccjit.h.
2038 After error-checking, the real work is done by the
2039 gcc::jit::recording::context::dump_to_file method in
2040 jit-recording.c. */
2042 void
2043 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,
2044 const char *path,
2045 int update_locations)
2047 RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2048 RETURN_IF_FAIL (path, ctxt, NULL, "NULL path");
2049 ctxt->dump_to_file (path, update_locations);
2052 /* Public entrypoint. See description in libgccjit.h.
2054 After error-checking, the real work is done by the
2055 gcc::jit::recording::context::get_first_error method in
2056 jit-recording.c. */
2058 const char *
2059 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
2061 RETURN_NULL_IF_FAIL (ctxt, NULL, NULL, "NULL context");
2063 return ctxt->get_first_error ();
2066 /* Public entrypoint. See description in libgccjit.h.
2068 After error-checking, the real work is done by the
2069 gcc::jit::result::get_code method in jit-result.c. */
2071 void *
2072 gcc_jit_result_get_code (gcc_jit_result *result,
2073 const char *fnname)
2075 RETURN_NULL_IF_FAIL (result, NULL, NULL, "NULL result");
2076 RETURN_NULL_IF_FAIL (fnname, NULL, NULL, "NULL fnname");
2078 return result->get_code (fnname);
2081 /* Public entrypoint. See description in libgccjit.h.
2083 After error-checking, this is essentially a wrapper around the
2084 destructor for gcc::jit::result in jit-result.c. */
2086 void
2087 gcc_jit_result_release (gcc_jit_result *result)
2089 RETURN_IF_FAIL (result, NULL, NULL, "NULL result");
2091 delete result;