semantic_checks.c 12.6 KB
Newer Older
Maxime Perrotin's avatar
Maxime Perrotin committed
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
/* Buildsupport is (c) 2008-2015 European Space Agency
 * contact: maxime.perrotin@esa.int
 * License is LGPL, check LICENSE file */
/* semantic_checks.c

  this backend verifies some semantic properties on the interface and deployment views
 
*/
#define ID "$Id: semantic_checks.c 400 2009-11-24 13:28:24Z maxime1008 $"

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

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

/* Various semantic checks on an interface */
void Interface_Semantic_Check(Interface * i)
{
    int count_param = 0;

    /* Check that Cyclic PIs are not connected. */
    if (PI == i->direction && cyclic == i->rcm && NULL != i->distant_fv) {

        ERROR
28
            ("[ERROR] In Function \"%s\", interface \"%s\" is cyclic and should not be connected. \n",
Maxime Perrotin's avatar
Maxime Perrotin committed
29
30
31
32
33
34
35
36
37
38
             i->parent_fv->name, i->name);
        add_error();

    }

    /* Check if Cyclic PIs have parameters, which is forbidden */
    if (PI == i->direction && cyclic == i->rcm
        && (NULL != i->in || NULL != i->out)) {

        ERROR
39
            ("[ERROR] In Function \"%s\", interface \"%s\" is cyclic and should not have parameters. \n",
Maxime Perrotin's avatar
Maxime Perrotin committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
             i->parent_fv->name, i->name);
        add_error();

    }

    if (PI == i->direction && (sporadic == i->rcm || variator == i->rcm)) {


        /* Check that SPO interfaces only have ONE input parameter */
        FOREACH(p, Parameter, i->in, {
                (void) p;
                count_param++;
                });

        if (count_param > 1) {
            ERROR
56
                ("[ERROR] In Function \"%s\", sporadic interface \"%s\" cannot have more than 1 IN param.\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
57
58
59
60
61
62
63
                 i->parent_fv->name, i->name);
            add_error();
        }

        /* Check that SPO interfaces don't have any OUT parameters */
        if (NULL != i->out) {
            ERROR
64
                ("[ERROR]  In Function \"%s\", sporadic interface \"%s\" cannot have OUT parameters.\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
65
66
67
68
69
70
71
72
                 i->parent_fv->name, i->name);
            add_error();
        }

    }

}

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
bool Equal_Params(Parameter * p1, Parameter * p2)
{
    bool result=true;

    if (!Compare_String (p1->name, p2->name)) {
        result=false;
    }
    if (!Compare_String (p1->type, p2->type)) {
        result=false;
    }
    if (!Compare_ASN1_Filename (p1->asn1_filename, p2->asn1_filename)) {
        result=false;
    }
    if (!Compare_ASN1_Module (p1->asn1_module, p2->asn1_module)) {
        result=false;
    }
    if (p1->basic_type != p2->basic_type) {
        result=false;
    }
    if (p1->encoding != p2->encoding) {
        result=false;
    }
    if (p1->param_direction != p2->param_direction) {
        result=false;
    }

    return result;
}

bool MultiInstance_SDL_Interface_Check(Interface * i, Interface * j)
{
    bool result=true;
    int in_param_cnt_i = 0;
    int in_param_cnt_j = 0;
    int out_param_cnt_i = 0;
    int out_param_cnt_j = 0;

    if (!Compare_Interface (i, j)) {
        result=false;
    }

    if (i->direction != j->direction) {
        result=false;
    }

    FOREACH(p, Parameter, i->in, {
        Parameter *inp = FindInParameter (i, p->name);
        if (!Equal_Params (p, inp)){
            result=false;
        }
        in_param_cnt_i++;
    });
    FOREACH(p, Parameter, j->in, {
        UNUSED (p);
        in_param_cnt_j++;
    });

    FOREACH(p, Parameter, i->out, {
        Parameter *outp = FindOutParameter (i, p->name);
        if (!Equal_Params (p, outp)){
            result=false;
        }
        out_param_cnt_i++;
    });
    FOREACH(p, Parameter, j->out, {
        UNUSED (p);
        out_param_cnt_j++;
    });

    if (in_param_cnt_i != in_param_cnt_j) {
        result=false;
    }
    if (out_param_cnt_i != out_param_cnt_j) {
        result=false;
    }

    return result;
}
Maxime Perrotin's avatar
Maxime Perrotin committed
151
152
153
154
155
156

/* External interface (the one and unique) */
void Function_Semantic_Check(FV * fv)
{
    if (fv->system_ast->context->glue && NULL == fv->process) {
        ERROR
157
            ("[ERROR] Function \"%s\" is not bound to any partition.\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
             fv->name);
        add_error();
    }

    if (fv->system_ast->context->onlycv) {
        return;
    }

    /*
     * A function must have at least ONE PI - otherwise it is dead code
     * (with the exception of blackbox devices which may be connected to
     * an interrupt in the future, and GUIs that may have only TCs)
     */
    if (blackbox_device != fv->language && gui != fv->language) {
        int count_pi = 0;
        FOREACH(i, Interface, fv->interfaces, {
                if (PI == i->direction) count_pi++;}
        );
        if (0 == count_pi) {
            ERROR
178
                ("[ERROR] Function \"%s\" shall contain at least one provided interface.\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
                 fv->name);
            add_error();
        }
    }

    /* 
     * SCADE/Simulink specific checks: exactly ONE PI and *NO* RI 
     */
    if (scade == fv->language || simulink == fv->language) {
        int count_pi = 0, count_ri = 0;

        FOREACH(i, Interface, fv->interfaces, {
                if (RI == i->direction) {
                count_ri++;}
                else {
                count_pi++;}
                }
        );

        if (1 != count_pi) {
            ERROR
200
                ("[ERROR] %s function \"%s\" must contain ONE provided interface\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
201
202
203
204
205
206
207
208
                 scade == fv->language ? "SCADE" : "Simulink", fv->name);
            ERROR
                ("** (but possibly several input and output parameters).\n");
            add_error();
        }

        if (0 != count_ri) {
            ERROR
209
                ("[ERROR] %s function \"%s\" must NOT contain any required interface\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                 scade == fv->language ? "SCADE" : "Simulink", fv->name);
            ERROR
                ("** (use OUT parameters in the interface specification).\n");
            add_error();
        }
    }

    /* 
     * QGenC/QGenAda specific checks: *NO* RI
     */
    if (qgenc == fv->language || qgenada == fv->language) {
        int count_ri = 0;

        FOREACH(i, Interface, fv->interfaces, {
            if (RI == i->direction) {
                count_ri++;}
            else {
                if (asynch == i->synchronism) {
                    ERROR
229
                    ("[ERROR] Required interface %s of function %s should be PROTECTED or UNPROTECTED\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
230
231
232
233
234
235
236
237
238
239
240
241
                     i->name, fv->name);
                    ERROR
                    ("   because it is connected to a function that is using QGen as a source language\n");
                    ERROR
                    ("   (remote calls cannot be made to QGen functions).\n");
                    add_error();
                }
            }
        });

        if (0 != count_ri) {
            ERROR
242
                ("[ERROR] %s function \"%s\" must NOT contain any required interface\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
                 qgenc == fv->language ? "QGenC" : "QGenAda", fv->name);
            ERROR
                ("** (use OUT parameters in the interface specification).\n");
            add_error();
        }
    }

    /* 
     * SCADE specific check: function name and PI name must match
     */
    if (scade == fv->language) {
        Interface *i = NULL;

        if (NULL != fv->interfaces) {
            i = fv->interfaces->value;
        }

        if (strcmp(i->name, fv->name)) {
            ERROR
262
            ("[ERROR] PI \"%s\" should have the same name as SCADE function \"%s\"\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
             i->name, fv->name);
            add_error();
        }
    }

    /* 
     * GUI-specific checks : 
     * (1) RIs must have a parameter
     * (2) All PI/RIs of GUIs must be sporadic
     */
    if (gui == fv->language) {
        FOREACH(i, Interface, fv->interfaces, {
            if (RI == i->direction) {
                if (NULL == i->in) {
277
                    ERROR("[ERROR] in GUI function \"%s\",\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
278
279
280
281
282
283
284
                           "**    interface \"%s\" must contain a parameter.\n",
                           fv->name,
                           i->name); 
                    add_error();}
                }
                /* Undefined RCM corresponds to "any type" (=inherited from PI) */ 
                if (undefined != i->rcm && sporadic != i->rcm && variator != i->rcm) {
285
                    ERROR("[ERROR] All interfaces of GUI \"%s\"\n"
Maxime Perrotin's avatar
Maxime Perrotin committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
                           "**       must be SPORADIC (e.g. %s is not).\n",
                           fv->name,
                           i->name);
                    add_error();
                }
            }
        );
    }

    /*
     * Pro/Unpro checks: Passive functions must be bound to the same process
     * as all their calling threads (to handle possible static data).
     */
    FOREACH(i, Interface, fv->interfaces, {
            if (RI == i->direction && synch == i->synchronism
                && NULL != i->distant_fv) {
            FOREACH(function, FV, get_system_ast()->functions, {
                    if (!strcmp(function->name, i->distant_fv)
                        && function->process != fv->process) {
                    ERROR
306
                    ("[ERROR] Required interface %s of function %s should be SPORADIC\n",
Maxime Perrotin's avatar
Maxime Perrotin committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
                     i->name, fv->name);
                    ERROR
                    ("   because it is connected to a function that is bound to a different\n");
                    ERROR
                    ("   partition in your deployment view (remote calls cannot be synchronous)\n");
                    add_error();}
                    }
            );}
            }
    );

    FOREACH(i, Interface, fv->interfaces, {
            Interface_Semantic_Check(i);
            }
    );
322
323

    /*
324
325
326
327
328
329
     * SDL specific checks: is_component_type can only be true for SDL functions.
     * instance_of can only point to functions with is_component_type set to true.
     */
    if (fv->is_component_type) {
        if (sdl != fv->language) {
            ERROR
330
            ("[ERROR] Is_Component_Type is True for \"%s\". This is allowed only for SDL functions.\n",
331
332
333
334
335
336
337
338
339
             fv->name);
            add_error();
        }
    }

    if (NULL != fv->instance_of) {
        FV *definition = FindFV (fv->instance_of);
        if (definition == NULL) {
            ERROR
340
            ("[ERROR] Component type \"%s\" (for instance \"%s\") not found.\n",
341
342
             fv->instance_of, fv->name);
            add_error();
343
            return;
344
        }
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
        else {
            if (false == definition->is_component_type) {
                ERROR
                ("[ERROR] Is_Component_Type is False for \"%s\". It should be True,\n",
                 definition->name);
                 ERROR
                ("   because function \"%s\" claims that it is an instance of \"%s\".\n",
                 fv->name, definition->name);
                add_error();
            }
            int fv_intf_cnt = 0;
            int definition_intf_cnt = 0;
            FOREACH(i, Interface, fv->interfaces, {
                Interface *def_i = FindInterface (definition, i->name);
                    if (def_i == NULL) {
360
                        ERROR
361
                        ("** Error: Interface \"%s\" of instance \"%s\" not found from definition \"%s\".\n",
362
363
                        i->name, fv->name, definition->name);
                        add_error();
364
365
366
367
368
369
370
                    } else {
                        if (!MultiInstance_SDL_Interface_Check (i, def_i)) {
                            ERROR
                            ("** Error: Interface \"%s\" of instance \"%s\" not matching with definition \"%s\".\n",
                            i->name, fv->name, definition->name);
                            add_error();
                        }
371
372
                    }

373
374
375
376
377
378
379
380
381
382
383
384
                fv_intf_cnt++;
            });
            FOREACH(i, Interface, definition->interfaces, {
                UNUSED (i);
                definition_intf_cnt++;
            });
            if (fv_intf_cnt != definition_intf_cnt) {
                ERROR
                ("** Error: Interface count mismatch between definition \"%s\" and instance \"%s\".\n",
                 definition->name, fv->name);
                add_error();
            }
385
386
        }
    }
Maxime Perrotin's avatar
Maxime Perrotin committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
}

/* External interface */
void Semantic_Checks()
{
    FOREACH(fv, FV, get_system_ast()->functions, {
            Function_Semantic_Check(fv);
    });

    /*
     * VHDL-specific check: there can be only ONE VHDL subsystem per node
     */
    FOREACH(process, Process, get_system_ast()->processes, {
            int count = 0;
            FOREACH(binding, Aplc_binding, process->bindings, {
                    if (vhdl == binding->fv->language) count++;}
            ); if (count > 1) {
            ERROR
405
            ("[ERROR] in a distributed system, a partition can contain only ONE\n");
Maxime Perrotin's avatar
Maxime Perrotin committed
406
407
408
409
410
            ERROR("   VHDL component. Partition \"%s\" has %d.\n",
                   process->name, count); add_error();}

    });
}