aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/dmd/semantic2.c
blob: 7bcf6ce4f33c398e0db61b7335ce3f99e7d7e167 (plain)
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
63
64
65
66
67
68
69
70
71
72
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

/* Compiler implementation of the D programming language
 * Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
 * written by Walter Bright
 * http://www.digitalmars.com
 * Distributed under the Boost Software License, Version 1.0.
 * http://www.boost.org/LICENSE_1_0.txt
 */

#include "dsymbol.h"
#include "aggregate.h"
#include "attrib.h"
#include "declaration.h"
#include "errors.h"
#include "import.h"
#include "init.h"
#include "module.h"
#include "nspace.h"
#include "objc.h"
#include "scope.h"
#include "staticassert.h"
#include "template.h"
#include "visitor.h"

bool evalStaticCondition(Scope *sc, Expression *exp, Expression *e, bool &errors);
void udaExpressionEval(Scope *sc, Expressions *exps);
Objc *objc();

class Semantic2Visitor : public Visitor
{
public:
    Scope *sc;

    Semantic2Visitor(Scope *sc)
    {
        this->sc = sc;
    }

    void visit(Dsymbol *)
    {
        // Most Dsymbols have no further semantic analysis needed
    }

    void visit(StaticAssert *sa)
    {
        //printf("StaticAssert::semantic2() %s\n", toChars());
        ScopeDsymbol *sds = new ScopeDsymbol();
        sc = sc->push(sds);
        sc->tinst = NULL;
        sc->minst = NULL;

        bool errors = false;
        bool result = evalStaticCondition(sc, sa->exp, sa->exp, errors);
        sc = sc->pop();
        if (errors)
        {
            errorSupplemental(sa->loc, "while evaluating: static assert(%s)", sa->exp->toChars());
        }
        else if (!result)
        {
            if (sa->msg)
            {
                sc = sc->startCTFE();
                sa->msg = expressionSemantic(sa->msg, sc);
                sa->msg = resolveProperties(sc, sa->msg);
                sc = sc->endCTFE();
                sa->msg = sa->msg->ctfeInterpret();
                if (StringExp * se = sa->msg->toStringExp())
                {
                    // same with pragma(msg)
                    se = se->toUTF8(sc);
                    sa->error("\"%.*s\"", (int)se->len, (char *)se->string);
                }
                else
                    sa->error("%s", sa->msg->toChars());
            }
            else
                sa->error("(%s) is false", sa->exp->toChars());
            if (sc->tinst)
                sc->tinst->printInstantiationTrace();
            if (!global.gag)
                fatal();
        }
    }

    void visit(TemplateInstance *tempinst)
    {
        if (tempinst->semanticRun >= PASSsemantic2)
            return;
        tempinst->semanticRun = PASSsemantic2;
        if (!tempinst->errors && tempinst->members)
        {
            TemplateDeclaration *tempdecl = tempinst->tempdecl->isTemplateDeclaration();
            assert(tempdecl);

            sc = tempdecl->_scope;
            assert(sc);
            sc = sc->push(tempinst->argsym);
            sc = sc->push(tempinst);
            sc->tinst = tempinst;
            sc->minst = tempinst->minst;

            int needGagging = (tempinst->gagged && !global.gag);
            unsigned int olderrors = global.errors;
            int oldGaggedErrors = -1;       // dead-store to prevent spurious warning
            if (needGagging)
                oldGaggedErrors = global.startGagging();

            for (size_t i = 0; i < tempinst->members->length; i++)
            {
                Dsymbol *s = (*tempinst->members)[i];
                semantic2(s, sc);
                if (tempinst->gagged && global.errors != olderrors)
                    break;
            }

            if (global.errors != olderrors)
            {
                if (!tempinst->errors)
                {
                    if (!tempdecl->literal)
                        tempinst->error(tempinst->loc, "error instantiating");
                    if (tempinst->tinst)
                        tempinst->tinst->printInstantiationTrace();
                }
                tempinst->errors = true;
            }
            if (needGagging)
                global.endGagging(oldGaggedErrors);

            sc = sc->pop();
            sc->pop();
        }
    }

    void visit(TemplateMixin *tmix)
    {
        if (tmix->semanticRun >= PASSsemantic2)
            return;
        tmix->semanticRun = PASSsemantic2;
        if (tmix->members)
        {
            assert(sc);
            sc = sc->push(tmix->argsym);
            sc = sc->push(tmix);
            for (size_t i = 0; i < tmix->members->length; i++)
            {
                Dsymbol *s = (*tmix->members)[i];
                semantic2(s, sc);
            }
            sc = sc->pop();
            sc->pop();
        }
    }

    void visit(VarDeclaration *vd)
    {
        if (vd->semanticRun < PASSsemanticdone && vd->inuse)
            return;

        //printf("VarDeclaration::semantic2('%s')\n", toChars());

        if (vd->_init && !vd->toParent()->isFuncDeclaration())
        {
            vd->inuse++;
            // Bugzilla 14166: Don't run CTFE for the temporary variables inside typeof
            vd->_init = initializerSemantic(vd->_init, sc, vd->type, sc->intypeof == 1 ? INITnointerpret : INITinterpret);
            vd->inuse--;
        }
        if (vd->_init && (vd->storage_class & STCmanifest))
        {
            /* Cannot initializer enums with CTFE classreferences and addresses of struct literals.
             * Scan initializer looking for them. Issue error if found.
             */
            if (ExpInitializer *ei = vd->_init->isExpInitializer())
            {
                struct EnumInitializer
                {
                    static bool arrayHasInvalidEnumInitializer(Expressions *elems)
                    {
                        for (size_t i = 0; i < elems->length; i++)
                        {
                            Expression *e = (*elems)[i];
                            if (e && hasInvalidEnumInitializer(e))
                                return true;
                        }
                        return false;
                    }

                    static bool hasInvalidEnumInitializer(Expression *e)
                    {
                        if (e->op == TOKclassreference)
                            return true;
                        if (e->op == TOKaddress && ((AddrExp *)e)->e1->op == TOKstructliteral)
                            return true;
                        if (e->op == TOKarrayliteral)
                            return arrayHasInvalidEnumInitializer(((ArrayLiteralExp *)e)->elements);
                        if (e->op == TOKstructliteral)
                            return arrayHasInvalidEnumInitializer(((StructLiteralExp *)e)->elements);
                        if (e->op == TOKassocarrayliteral)
                        {
                            AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e;
                            return arrayHasInvalidEnumInitializer(ae->values) ||
                                arrayHasInvalidEnumInitializer(ae->keys);
                        }
                        return false;
                    }
                };
                if (EnumInitializer::hasInvalidEnumInitializer(ei->exp))
                    vd->error(": Unable to initialize enum with class or pointer to struct. Use static const variable instead.");
            }
        }
        else if (vd->_init && vd->isThreadlocal())
        {
            if ((vd->type->ty == Tclass) && vd->type->isMutable() && !vd->type->isShared())
            {
                ExpInitializer *ei = vd->_init->isExpInitializer();
                if (ei && ei->exp->op == TOKclassreference)
                    vd->error("is mutable. Only const or immutable class thread local variable are allowed, not %s", vd->type->toChars());
            }
            else if (vd->type->ty == Tpointer && vd->type->nextOf()->ty == Tstruct && vd->type->nextOf()->isMutable() && !vd->type->nextOf()->isShared())
            {
                ExpInitializer *ei = vd->_init->isExpInitializer();
                if (ei && ei->exp->op == TOKaddress && ((AddrExp *)ei->exp)->e1->op == TOKstructliteral)
                {
                    vd->error("is a pointer to mutable struct. Only pointers to const, immutable or shared struct thread local variable are allowed, not %s", vd->type->toChars());
                }
            }
        }
        vd->semanticRun = PASSsemantic2done;
    }

    void visit(Module *mod)
    {
        //printf("Module::semantic2('%s'): parent = %p\n", toChars(), mod->parent);
        if (mod->semanticRun != PASSsemanticdone)       // semantic() not completed yet - could be recursive call
            return;
        mod->semanticRun = PASSsemantic2;

        // Note that modules get their own scope, from scratch.
        // This is so regardless of where in the syntax a module
        // gets imported, it is unaffected by context.
        Scope *sc = Scope::createGlobal(mod);      // create root scope
        //printf("Module = %p\n", sc.scopesym);

        // Pass 2 semantic routines: do initializers and function bodies
        for (size_t i = 0; i < mod->members->length; i++)
        {
            Dsymbol *s = (*mod->members)[i];
            semantic2(s, sc);
        }

        if (mod->userAttribDecl)
        {
            semantic2(mod->userAttribDecl, sc);
        }

        sc = sc->pop();
        sc->pop();
        mod->semanticRun = PASSsemantic2done;
        //printf("-Module::semantic2('%s'): parent = %p\n", toChars(), mod->parent);
    }

    void visit(FuncDeclaration *fd)
    {
        if (fd->semanticRun >= PASSsemantic2done)
            return;
        assert(fd->semanticRun <= PASSsemantic2);
        fd->semanticRun = PASSsemantic2;

        objc()->setSelector(fd, sc);
        objc()->validateSelector(fd);

        if (fd->parent->isClassDeclaration())
        {
            objc()->checkLinkage(fd);
        }
        if (!fd->type || fd->type->ty != Tfunction)
            return;
        TypeFunction *f = fd->type->toTypeFunction();
        const size_t nparams = f->parameterList.length();
        // semantic for parameters' UDAs
        for (size_t i = 0; i < nparams; i++)
        {
            Parameter *param = f->parameterList[i];
            if (param && param->userAttribDecl)
                semantic2(param->userAttribDecl, sc);
        }
    }

    void visit(Import *i)
    {
        //printf("Import::semantic2('%s')\n", toChars());
        if (i->mod)
        {
            semantic2(i->mod, NULL);
            if (i->mod->needmoduleinfo)
            {
                //printf("module5 %s because of %s\n", sc->_module->toChars(), i->mod->toChars());
                if (sc)
                    sc->_module->needmoduleinfo = 1;
            }
        }
    }

    void visit(Nspace *ns)
    {
        if (ns->semanticRun >= PASSsemantic2)
            return;
        ns->semanticRun = PASSsemantic2;
        if (ns->members)
        {
            assert(sc);
            sc = sc->push(ns);
            sc->linkage = LINKcpp;
            for (size_t i = 0; i < ns->members->length; i++)
            {
                Dsymbol *s = (*ns->members)[i];
                semantic2(s, sc);
            }
            sc->pop();
        }
    }

    void visit(AttribDeclaration *ad)
    {
        Dsymbols *d = ad->include(sc);

        if (d)
        {
            Scope *sc2 = ad->newScope(sc);

            for (size_t i = 0; i < d->length; i++)
            {
                Dsymbol *s = (*d)[i];
                semantic2(s, sc2);
            }

            if (sc2 != sc)
                sc2->pop();
        }
    }

    /**
     * Run the DeprecatedDeclaration's semantic2 phase then its members.
     *
     * The message set via a `DeprecatedDeclaration` can be either of:
     * - a string literal
     * - an enum
     * - a static immutable
     * So we need to call ctfe to resolve it.
     * Afterward forwards to the members' semantic2.
     */
    void visit(DeprecatedDeclaration *dd)
    {
        dd->getMessage();
        visit((AttribDeclaration *)dd);
    }

    void visit(AlignDeclaration *ad)
    {
        ad->getAlignment(sc);
        visit((AttribDeclaration *)ad);
    }

    void visit(UserAttributeDeclaration *uad)
    {
        if (uad->decl && uad->atts && uad->atts->length && uad->_scope)
        {
            uad->_scope = NULL;
            udaExpressionEval(sc, uad->atts);
        }
        visit((AttribDeclaration *)uad);
    }

    void visit(AggregateDeclaration *ad)
    {
        //printf("AggregateDeclaration::semantic2(%s) type = %s, errors = %d\n", toChars(), ad->type->toChars(), ad->errors);
        if (!ad->members)
            return;

        if (ad->_scope)
        {
            ad->error("has forward references");
            return;
        }

        Scope *sc2 = ad->newScope(sc);

        ad->determineSize(ad->loc);

        for (size_t i = 0; i < ad->members->length; i++)
        {
            Dsymbol *s = (*ad->members)[i];
            //printf("\t[%d] %s\n", i, s->toChars());
            semantic2(s, sc2);
        }

        sc2->pop();
    }
};

/*************************************
 * Does semantic analysis on initializers and members of aggregates.
 */
void semantic2(Dsymbol *dsym, Scope *sc)
{
    Semantic2Visitor v(sc);
    dsym->accept(&v);
}