build_vdm_skeletons.c 5.41 KB
Newer Older
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
/* Buildsupport is (c) 2008-2016 European Space Agency
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/* build_vdm_skeletons.c

   Generate code skeletons for VDM functions
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <assert.h>

#include "my_types.h"
#include "practical_functions.h"

static FILE *user_code = NULL, *interface = NULL;


/* Adds header to user_code.h and (if new) user_code.c */
void vdm_gw_preamble(FV * fv)
{
    assert (NULL != interface);

    fprintf(interface,
          "-- This file was generated automatically: DO NOT MODIFY IT ! \n\n");

    fprintf(interface,
            "class %s_Interface\n"
            "operations\n"
            "    public Startup: () ==> ()\n"
            "    Startup (-) is subclass responsibility\n\n",
            fv->name);


    if (NULL != user_code) {
        fprintf(user_code,
              "-- User code: This file will not be overwritten by TASTE.\n\n");

        fprintf(user_code,
            "class %s\n"
            "is subclass of %s_Interface\n"
            "operations\n"
            "    public Startup: () ==> ()\n"
            "    -- user: fill your code\n\n",
            fv->name,
            fv->name);
    }
}

/* Creates interface and if necessary user code template (if it did not exist) */
void Init_VDM_GW_Backend(FV *fv)
{
    char *path = NULL;
    char *filename = NULL;

    path     = make_string("%s/%s", OUTPUT_PATH, fv->name);
    filename = make_string("%s_interface.vdmpp", fv->name);
    create_file(path, filename, &interface);
    free(filename);
Maxime Perrotin's avatar
Maxime Perrotin committed
63
    filename = make_string("%s.vdmpp", fv->name);
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    if (!file_exists(path, filename)) {
        create_file(path, filename, &user_code);
        free(filename);
    }

    free(path);
    vdm_gw_preamble(fv);
}


/* Add a Provided interface. Can contain in and out parameters
 * Write in user_code.h the declaration of the user functions
 * and if user_code.c is new, copy these declarations there too.
 */
void add_pi_to_vdm_gw(Interface * i)
{
    if (NULL == interface)
        return;

Maxime Perrotin's avatar
Maxime Perrotin committed
84
    char *signature = make_string("    public PI_%s: ",
85
86
87
88
89
90
91
92
93
94
95
                                  i->name);


    fprintf(interface, "%s", signature);

    if (NULL != user_code) {
        fprintf(user_code, "%s", signature);
    }

    char *sep = " * ";
    bool comma = false;
Maxime Perrotin's avatar
Maxime Perrotin committed
96
97
    char *params = " (";
    char *sep2 = ", ";
98
99
100
101
102
103

    FOREACH (p, Parameter, i->in, {
        char *sort = make_string("%s%s`%s",
                                 comma? sep: "",
                                 p->asn1_module,
                                 p->type);
Maxime Perrotin's avatar
Maxime Perrotin committed
104
105
106
107
        params = make_string("%s%s%s",
                             params,
                             comma? sep2: "",
                             p->name);
108
109
110
111
112
113
114
115
        fprintf(interface, "%s", sort);
        if(NULL != user_code) {
            fprintf(user_code, "%s",sort);
        }
        free(sort);
        comma = true;
    });

Maxime Perrotin's avatar
Maxime Perrotin committed
116
117
    params = make_string("%s)", params);

118
119
120
121
122
123
124
125
126
127
    if (NULL != i->out) {
        char *out = make_string(" ==> %s`%s",
                                i->out->value->asn1_module,
                                i->out->value->type);
        fprintf(interface, "%s", out);
        if(NULL != user_code) {
            fprintf(user_code, "%s", out);
        }
        free(out);
    }
Maxime Perrotin's avatar
Maxime Perrotin committed
128
    else {
Maxime Perrotin's avatar
Maxime Perrotin committed
129
130
131
132
        fprintf(interface, " ==> ()");
        if(NULL != user_code) {
            fprintf(user_code, " ==> ()");
        }
Maxime Perrotin's avatar
Maxime Perrotin committed
133
    }
134
135
136

    fprintf(interface,
            "\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
137
            "    %s%s == is subclass responsibility\n\n",
138
139
140
141
            i->name,
            NULL != i->in? " (-)" : "");

    if (NULL != user_code)
Maxime Perrotin's avatar
Maxime Perrotin committed
142

143
144
        fprintf(user_code,
                "\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
145
                "    %s%s == -- Write your code here\n\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
146
147
                i->name,
                NULL != i->in? params: "");
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
}

/* Declaration of the RI */
void add_ri_to_vdm_gw(Interface * i)
{
    if (NULL == interface || NULL == i)
        return;
    // TODO
}


void End_VDM_GW_Backend(FV *fv)
{
    fprintf(interface, "end %s_Interface\n", fv->name);
    close_file(&interface);
Maxime Perrotin's avatar
Maxime Perrotin committed
163
164
165
166
    if (NULL != user_code) {
        fprintf(user_code, "end %s\n", fv->name);
        close_file(&user_code);
    }
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
}

/* Function to process one interface of the FV */
void GW_VDM_Interface(Interface * i)
{
    switch (i->direction) {
        case PI:
            add_pi_to_vdm_gw(i);
            break;

        case RI:
            /*
             * There can be duplicate RI name but one sync, the other async
             * In that case, discard the async one (generated by VT to allow
             * a mix of sync and async PI in one block
             */
            if (asynch == i->synchronism) {
                FOREACH(ri, Interface, i->parent_fv->interfaces, {
                    if (RI == ri->direction &&
                        !strcmp(ri->name, i->name) &&
                        synch == ri->synchronism) {
                        return;
                    }

                });
            }
            // RI not supported yet
            //add_ri_to_vdm_gw(i);
            break;
        default:
            break;
    }
}

/* External interface (the one and unique) */
void GW_VDM_Backend(FV * fv)
{
    if (fv->system_ast->context->onlycv)
        return;
    if (vdm == fv->language) {
        Init_VDM_GW_Backend(fv);
        FOREACH(i, Interface, fv->interfaces, {
            GW_VDM_Interface(i);
        });
        End_VDM_GW_Backend(fv);
    }
}