Skip to content

Declare @ccall only when asked by the user #1094

Open
@czgdp1807

Description

@czgdp1807

So as of now any function declared with @ccall in LPython code is always declared in the generated C code. However if someone has already declared their function in some other .h file (and included this file by editing the generated code) then function decorated with @ccall shouldn't be re-declared in the generated code. So we have to make the declaration of @ccall functions optional. Some options,

  1. ccall(static=true).
  2. ccall(declare=false).

An example:

@ccall
def foo(a: i16):
    pass

def myfunc():
    a: i16 = 1
    foo(a)

Generated C code:

#include <inttypes.h>
#include <foo_header.h> # foo already declared in foo_header.h
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>

#define ASSERT(cond)                                                           \
    {                                                                          \
        if (!(cond)) {                                                         \
            printf("%s%s", "ASSERT failed: ", __FILE__);                       \
            printf("%s%s", "\nfunction ", __func__);                           \
            printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
            printf("%s%s", #cond, "\n");                                       \
            exit(1);                                                           \
        }                                                                      \
    }
#define ASSERT_MSG(cond, msg)                                                  \
    {                                                                          \
        if (!(cond)) {                                                         \
            printf("%s%s", "ASSERT failed: ", __FILE__);                       \
            printf("%s%s", "\nfunction ", __func__);                           \
            printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
            printf("%s%s", #cond, "\n");                                       \
            printf("%s", "ERROR MESSAGE:\n");                                  \
            printf("%s%s", msg, "\n");                                         \
            exit(1);                                                           \
        }                                                                      \
    }


struct dimension_descriptor
{
    int32_t lower_bound, length;
};
// Forward declarations
void _lpython_main_program();
#ifndef IGNORE_DECLARATION
void foo(int16_t a);
#endif
void myfunc();

// Implementations
void _lpython_main_program()
{
    myfunc();
}

void myfunc()
{
    int16_t a;
    a = 1;
    foo(a);
}

int main(int argc, char* argv[])
{
    _lpython_main_program();
    return 0;
}

As you can see foo is declared but I have added foo_header.h on my own in the generated C code. So I will receive a re-declaration from C compiler. That's why an option should be provided to make declaration of @ccall generated functions optional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cLabel for C language related changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions