aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis/NameAndTypeResolver.cpp
blob: c5ed079d72c98af34a24c9b2b8bf063f83dac8c0 (plain) (blame)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * Parser part that determines the declarations corresponding to names and the types of expressions.
 */

#include <libsolidity/analysis/NameAndTypeResolver.h>

#include <libsolidity/ast/AST.h>
#include <libsolidity/analysis/TypeChecker.h>
#include <libsolidity/interface/ErrorReporter.h>
#include <libdevcore/StringUtils.h>

#include <boost/algorithm/string.hpp>

using namespace std;

namespace dev
{
namespace solidity
{

NameAndTypeResolver::NameAndTypeResolver(
    vector<Declaration const*> const& _globals,
    map<ASTNode const*, shared_ptr<DeclarationContainer>>& _scopes,
    ErrorReporter& _errorReporter
) :
    m_scopes(_scopes),
    m_errorReporter(_errorReporter)
{
    if (!m_scopes[nullptr])
        m_scopes[nullptr].reset(new DeclarationContainer());
    for (Declaration const* declaration: _globals)
    {
        solAssert(m_scopes[nullptr]->registerDeclaration(*declaration), "Unable to register global declaration.");
    }
}

bool NameAndTypeResolver::registerDeclarations(SourceUnit& _sourceUnit, ASTNode const* _currentScope)
{
    // The helper registers all declarations in m_scopes as a side-effect of its construction.
    try
    {
        DeclarationRegistrationHelper registrar(m_scopes, _sourceUnit, m_errorReporter, _currentScope);
    }
    catch (FatalError const&)
    {
        if (m_errorReporter.errors().empty())
            throw; // Something is weird here, rather throw again.
        return false;
    }
    return true;
}

bool NameAndTypeResolver::performImports(SourceUnit& _sourceUnit, map<string, SourceUnit const*> const& _sourceUnits)
{
    DeclarationContainer& target = *m_scopes.at(&_sourceUnit);
    bool error = false;
    for (auto const& node: _sourceUnit.nodes())
        if (auto imp = dynamic_cast<ImportDirective const*>(node.get()))
        {
            string const& path = imp->annotation().absolutePath;
            if (!_sourceUnits.count(path))
            {
                m_errorReporter.declarationError(
                    imp->location(),
                    "Import \"" + path + "\" (referenced as \"" + imp->path() + "\") not found."
                );
                error = true;
                continue;
            }
            auto scope = m_scopes.find(_sourceUnits.at(path));
            solAssert(scope != end(m_scopes), "");
            if (!imp->symbolAliases().empty())
                for (auto const& alias: imp->symbolAliases())
                {
                    auto declarations = scope->second->resolveName(alias.first->name(), false);
                    if (declarations.empty())
                    {
                        m_errorReporter.declarationError(
                            imp->location(),
                            "Declaration \"" +
                            alias.first->name() +
                            "\" not found in \"" +
                            path +
                            "\" (referenced as \"" +
                            imp->path() +
                            "\")."
                        );
                        error = true;
                    }
                    else
                        for (Declaration const* declaration: declarations)
                            if (!DeclarationRegistrationHelper::registerDeclaration(
                                target, *declaration, alias.second.get(), &imp->location(), true, false, m_errorReporter
                            ))
                                error = true;
                }
            else if (imp->name().empty())
                for (auto const& nameAndDeclaration: scope->second->declarations())
                    for (auto const& declaration: nameAndDeclaration.second)
                        if (!DeclarationRegistrationHelper::registerDeclaration(
                            target, *declaration, &nameAndDeclaration.first, &imp->location(), true, false, m_errorReporter
                        ))
                            error =  true;
        }
    return !error;
}

bool NameAndTypeResolver::resolveNamesAndTypes(ASTNode& _node, bool _resolveInsideCode)
{
    try
    {
        return resolveNamesAndTypesInternal(_node, _resolveInsideCode);
    }
    catch (FatalError const&)
    {
        if (m_errorReporter.errors().empty())
            throw; // Something is weird here, rather throw again.
        return false;
    }
}

bool NameAndTypeResolver::updateDeclaration(Declaration const& _declaration)
{
    try
    {
        m_scopes[nullptr]->registerDeclaration(_declaration, nullptr, false, true);
        solAssert(_declaration.scope() == nullptr, "Updated declaration outside global scope.");
    }
    catch (FatalError const&)
    {
        if (m_errorReporter.errors().empty())
            throw; // Something is weird here, rather throw again.
        return false;
    }
    return true;
}

void NameAndTypeResolver::activateVariable(string const& _name)
{
    solAssert(m_currentScope, "");
    // Scoped local variables are invisible before activation.
    // When a local variable is activated, its name is removed
    // from a scope's invisible variables.
    // This is used to avoid activation of variables of same name
    // in the same scope (an error is returned).
    if (m_currentScope->isInvisible(_name))
        m_currentScope->activateVariable(_name);
}

vector<Declaration const*> NameAndTypeResolver::resolveName(ASTString const& _name, ASTNode const* _scope) const
{
    auto iterator = m_scopes.find(_scope);
    if (iterator == end(m_scopes))
        return vector<Declaration const*>({});
    return iterator->second->resolveName(_name, false);
}

vector<Declaration const*> NameAndTypeResolver::nameFromCurrentScope(ASTString const& _name, bool _includeInvisibles) const
{
    return m_currentScope->resolveName(_name, true, _includeInvisibles);
}

Declaration const* NameAndTypeResolver::pathFromCurrentScope(vector<ASTString> const& _path) const
{
    solAssert(!_path.empty(), "");
    vector<Declaration const*> candidates = m_currentScope->resolveName(_path.front(), true);
    for (size_t i = 1; i < _path.size() && candidates.size() == 1; i++)
    {
        if (!m_scopes.count(candidates.front()))
            return nullptr;
        candidates = m_scopes.at(candidates.front())->resolveName(_path[i], false);
    }
    if (candidates.size() == 1)
        return candidates.front();
    else
        return nullptr;
}

vector<Declaration const*> NameAndTypeResolver::cleanedDeclarations(
        Identifier const& _identifier,
        vector<Declaration const*> const& _declarations
)
{
    solAssert(_declarations.size() > 1, "");
    vector<Declaration const*> uniqueFunctions;

    for (Declaration const* declaration: _declarations)
    {
        solAssert(declaration, "");
        // the declaration is functionDefinition, eventDefinition or a VariableDeclaration while declarations > 1
        solAssert(
            dynamic_cast<FunctionDefinition const*>(declaration) ||
            dynamic_cast<EventDefinition const*>(declaration) ||
            dynamic_cast<VariableDeclaration const*>(declaration) ||
            dynamic_cast<MagicVariableDeclaration const*>(declaration),
            "Found overloading involving something not a function, event or a (magic) variable."
        );

        FunctionTypePointer functionType { declaration->functionType(false) };
        if (!functionType)
            functionType = declaration->functionType(true);
        solAssert(functionType, "Failed to determine the function type of the overloaded.");

        for (auto parameter: functionType->parameterTypes() + functionType->returnParameterTypes())
            if (!parameter)
                m_errorReporter.fatalDeclarationError(_identifier.location(), "Function type can not be used in this context.");

        if (uniqueFunctions.end() == find_if(
            uniqueFunctions.begin(),
            uniqueFunctions.end(),
            [&](Declaration const* d)
            {
                shared_ptr<FunctionType const> newFunctionType { d->functionType(false) };
                if (!newFunctionType)
                    newFunctionType = d->functionType(true);
                return newFunctionType && functionType->hasEqualParameterTypes(*newFunctionType);
            }
        ))
            uniqueFunctions.push_back(declaration);
    }
    return uniqueFunctions;
}

void NameAndTypeResolver::warnVariablesNamedLikeInstructions()
{
    for (auto const& instruction: c_instructions)
    {
        string const instructionName{boost::algorithm::to_lower_copy(instruction.first)};
        auto declarations = nameFromCurrentScope(instructionName, true);
        for (Declaration const* const declaration: declarations)
        {
            solAssert(!!declaration, "");
            if (dynamic_cast<MagicVariableDeclaration const* const>(declaration))
                // Don't warn the user for what the user did not.
                continue;
            m_errorReporter.warning(
                declaration->location(),
                "Variable is shadowed in inline assembly by an instruction of the same name"
            );
        }
    }
}

void NameAndTypeResolver::setScope(ASTNode const* _node)
{
    m_currentScope = m_scopes[_node].get();
}

bool NameAndTypeResolver::resolveNamesAndTypesInternal(ASTNode& _node, bool _resolveInsideCode)
{
    if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(&_node))
    {
        bool success = true;
        setScope(contract->scope());
        solAssert(!!m_currentScope, "");

        for (ASTPointer<InheritanceSpecifier> const& baseContract: contract->baseContracts())
            if (!resolveNamesAndTypes(*baseContract, true))
                success = false;

        setScope(contract);

        if (success)
        {
            linearizeBaseContracts(*contract);
            vector<ContractDefinition const*> properBases(
                ++contract->annotation().linearizedBaseContracts.begin(),
                contract->annotation().linearizedBaseContracts.end()
            );

            for (ContractDefinition const* base: properBases)
                importInheritedScope(*base);
        }

        // these can contain code, only resolve parameters for now
        for (ASTPointer<ASTNode> const& node: contract->subNodes())
        {
            setScope(contract);
            if (!resolveNamesAndTypes(*node, false))
            {
                success = false;
                break;
            }
        }

        if (!success)
            return false;

        if (!_resolveInsideCode)
            return success;

        setScope(contract);

        // now resolve references inside the code
        for (ASTPointer<ASTNode> const& node: contract->subNodes())
        {
            setScope(contract);
            if (!resolveNamesAndTypes(*node, true))
                success = false;
        }
        return success;
    }
    else
    {
        if (m_scopes.count(&_node))
            setScope(&_node);
        return ReferencesResolver(m_errorReporter, *this, _resolveInsideCode).resolve(_node);
    }
}

void NameAndTypeResolver::importInheritedScope(ContractDefinition const& _base)
{
    auto iterator = m_scopes.find(&_base);
    solAssert(iterator != end(m_scopes), "");
    for (auto const& nameAndDeclaration: iterator->second->declarations())
        for (auto const& declaration: nameAndDeclaration.second)
            // Import if it was declared in the base, is not the constructor and is visible in derived classes
            if (declaration->scope() == &_base && declaration->isVisibleInDerivedContracts())
                if (!m_currentScope->registerDeclaration(*declaration))
                {
                    SourceLocation firstDeclarationLocation;
                    SourceLocation secondDeclarationLocation;
                    Declaration const* conflictingDeclaration = m_currentScope->conflictingDeclaration(*declaration);
                    solAssert(conflictingDeclaration, "");

                    // Usual shadowing is not an error
                    if (dynamic_cast<VariableDeclaration const*>(declaration) && dynamic_cast<VariableDeclaration const*>(conflictingDeclaration))
                        continue;

                    // Usual shadowing is not an error
                    if (dynamic_cast<ModifierDefinition const*>(declaration) && dynamic_cast<ModifierDefinition const*>(conflictingDeclaration))
                        continue;

                    if (declaration->location().start < conflictingDeclaration->location().start)
                    {
                        firstDeclarationLocation = declaration->location();
                        secondDeclarationLocation = conflictingDeclaration->location();
                    }
                    else
                    {
                        firstDeclarationLocation = conflictingDeclaration->location();
                        secondDeclarationLocation = declaration->location();
                    }

                    m_errorReporter.declarationError(
                        secondDeclarationLocation,
                        SecondarySourceLocation().append("The previous declaration is here:", firstDeclarationLocation),
                        "Identifier already declared."
                    );
                }
}

void NameAndTypeResolver::linearizeBaseContracts(ContractDefinition& _contract)
{
    // order in the lists is from derived to base
    // list of lists to linearize, the last element is the list of direct bases
    list<list<ContractDefinition const*>> input(1, list<ContractDefinition const*>{});
    for (ASTPointer<InheritanceSpecifier> const& baseSpecifier: _contract.baseContracts())
    {
        UserDefinedTypeName const& baseName = baseSpecifier->name();
        auto base = dynamic_cast<ContractDefinition const*>(baseName.annotation().referencedDeclaration);
        if (!base)
            m_errorReporter.fatalTypeError(baseName.location(), "Contract expected.");
        // "push_front" has the effect that bases mentioned later can overwrite members of bases
        // mentioned earlier
        input.back().push_front(base);
        vector<ContractDefinition const*> const& basesBases = base->annotation().linearizedBaseContracts;
        if (basesBases.empty())
            m_errorReporter.fatalTypeError(baseName.location(), "Definition of base has to precede definition of derived contract");
        input.push_front(list<ContractDefinition const*>(basesBases.begin(), basesBases.end()));
    }
    input.back().push_front(&_contract);
    vector<ContractDefinition const*> result = cThreeMerge(input);
    if (result.empty())
        m_errorReporter.fatalTypeError(_contract.location(), "Linearization of inheritance graph impossible");
    _contract.annotation().linearizedBaseContracts = result;
    _contract.annotation().contractDependencies.insert(result.begin() + 1, result.end());
}

template <class _T>
vector<_T const*> NameAndTypeResolver::cThreeMerge(list<list<_T const*>>& _toMerge)
{
    // returns true iff _candidate appears only as last element of the lists
    auto appearsOnlyAtHead = [&](_T const* _candidate) -> bool
    {
        for (list<_T const*> const& bases: _toMerge)
        {
            solAssert(!bases.empty(), "");
            if (find(++bases.begin(), bases.end(), _candidate) != bases.end())
                return false;
        }
        return true;
    };
    // returns the next candidate to append to the linearized list or nullptr on failure
    auto nextCandidate = [&]() -> _T const*
    {
        for (list<_T const*> const& bases: _toMerge)
        {
            solAssert(!bases.empty(), "");
            if (appearsOnlyAtHead(bases.front()))
                return bases.front();
        }
        return nullptr;
    };
    // removes the given contract from all lists
    auto removeCandidate = [&](_T const* _candidate)
    {
        for (auto it = _toMerge.begin(); it != _toMerge.end();)
        {
            it->remove(_candidate);
            if (it->empty())
                it = _toMerge.erase(it);
            else
                ++it;
        }
    };

    _toMerge.remove_if([](list<_T const*> const& _bases) { return _bases.empty(); });
    vector<_T const*> result;
    while (!_toMerge.empty())
    {
        _T const* candidate = nextCandidate();
        if (!candidate)
            return vector<_T const*>();
        result.push_back(candidate);
        removeCandidate(candidate);
    }
    return result;
}

string NameAndTypeResolver::similarNameSuggestions(ASTString const& _name) const
{
    return quotedAlternativesList(m_currentScope->similarNames(_name));
}

DeclarationRegistrationHelper::DeclarationRegistrationHelper(
    map<ASTNode const*, shared_ptr<DeclarationContainer>>& _scopes,
    ASTNode& _astRoot,
    ErrorReporter& _errorReporter,
    ASTNode const* _currentScope
):
    m_scopes(_scopes),
    m_currentScope(_currentScope),
    m_errorReporter(_errorReporter)
{
    _astRoot.accept(*this);
    solAssert(m_currentScope == _currentScope, "Scopes not correctly closed.");
}

bool DeclarationRegistrationHelper::registerDeclaration(
    DeclarationContainer& _container,
    Declaration const& _declaration,
    string const* _name,
    SourceLocation const* _errorLocation,
    bool _warnOnShadow,
    bool _inactive,
    ErrorReporter& _errorReporter
)
{
    if (!_errorLocation)
        _errorLocation = &_declaration.location();

    string name = _name ? *_name : _declaration.name();
    Declaration const* shadowedDeclaration = nullptr;
    if (_warnOnShadow && !name.empty() && _container.enclosingContainer())
        for (auto const* decl: _container.enclosingContainer()->resolveName(name, true, true))
            shadowedDeclaration = decl;

    // We use "invisible" for both inactive variables in blocks and for members invisible in contracts.
    // They cannot both be true at the same time.
    solAssert(!(_inactive && !_declaration.isVisibleInContract()), "");
    if (!_container.registerDeclaration(_declaration, _name, !_declaration.isVisibleInContract() || _inactive))
    {
        SourceLocation firstDeclarationLocation;
        SourceLocation secondDeclarationLocation;
        Declaration const* conflictingDeclaration = _container.conflictingDeclaration(_declaration, _name);
        solAssert(conflictingDeclaration, "");
        bool const comparable =
            _errorLocation->sourceName &&
            conflictingDeclaration->location().sourceName &&
            *_errorLocation->sourceName == *conflictingDeclaration->location().sourceName;
        if (comparable && _errorLocation->start < conflictingDeclaration->location().start)
        {
            firstDeclarationLocation = *_errorLocation;
            secondDeclarationLocation = conflictingDeclaration->location();
        }
        else
        {
            firstDeclarationLocation = conflictingDeclaration->location();
            secondDeclarationLocation = *_errorLocation;
        }

        _errorReporter.declarationError(
            secondDeclarationLocation,
            SecondarySourceLocation().append("The previous declaration is here:", firstDeclarationLocation),
            "Identifier already declared."
        );
        return false;
    }
    else if (shadowedDeclaration)
    {
        if (dynamic_cast<MagicVariableDeclaration const*>(shadowedDeclaration))
            _errorReporter.warning(
                _declaration.location(),
                "This declaration shadows a builtin symbol."
            );
        else
        {
            auto shadowedLocation = shadowedDeclaration->location();
            _errorReporter.warning(
                _declaration.location(),
                "This declaration shadows an existing declaration.",
                SecondarySourceLocation().append("The shadowed declaration is here:", shadowedLocation)
            );
        }
    }
    return true;
}

bool DeclarationRegistrationHelper::visit(SourceUnit& _sourceUnit)
{
    if (!m_scopes[&_sourceUnit])
        // By importing, it is possible that the container already exists.
        m_scopes[&_sourceUnit].reset(new DeclarationContainer(m_currentScope, m_scopes[m_currentScope].get()));
    m_currentScope = &_sourceUnit;
    return true;
}

void DeclarationRegistrationHelper::endVisit(SourceUnit& _sourceUnit)
{
    _sourceUnit.annotation().exportedSymbols = m_scopes[&_sourceUnit]->declarations();
    closeCurrentScope();
}

bool DeclarationRegistrationHelper::visit(ImportDirective& _import)
{
    SourceUnit const* importee = _import.annotation().sourceUnit;
    solAssert(!!importee, "");
    if (!m_scopes[importee])
        m_scopes[importee].reset(new DeclarationContainer(nullptr, m_scopes[nullptr].get()));
    m_scopes[&_import] = m_scopes[importee];
    registerDeclaration(_import, false);
    return true;
}

bool DeclarationRegistrationHelper::visit(ContractDefinition& _contract)
{
    registerDeclaration(_contract, true);
    _contract.annotation().canonicalName = currentCanonicalName();
    return true;
}

void DeclarationRegistrationHelper::endVisit(ContractDefinition&)
{
    closeCurrentScope();
}

bool DeclarationRegistrationHelper::visit(StructDefinition& _struct)
{
    registerDeclaration(_struct, true);
    _struct.annotation().canonicalName = currentCanonicalName();
    return true;
}

void DeclarationRegistrationHelper::endVisit(StructDefinition&)
{
    closeCurrentScope();
}

bool DeclarationRegistrationHelper::visit(EnumDefinition& _enum)
{
    registerDeclaration(_enum, true);
    _enum.annotation().canonicalName = currentCanonicalName();
    return true;
}

void DeclarationRegistrationHelper::endVisit(EnumDefinition&)
{
    closeCurrentScope();
}

bool DeclarationRegistrationHelper::visit(EnumValue& _value)
{
    registerDeclaration(_value, false);
    return true;
}

bool DeclarationRegistrationHelper::visit(FunctionDefinition& _function)
{
    registerDeclaration(_function, true);
    m_currentFunction = &_function;
    return true;
}

void DeclarationRegistrationHelper::endVisit(FunctionDefinition&)
{
    m_currentFunction = nullptr;
    closeCurrentScope();
}

bool DeclarationRegistrationHelper::visit(ModifierDefinition& _modifier)
{
    registerDeclaration(_modifier, true);
    m_currentFunction = &_modifier;
    return true;
}

void DeclarationRegistrationHelper::endVisit(ModifierDefinition&)
{
    m_currentFunction = nullptr;
    closeCurrentScope();
}

bool DeclarationRegistrationHelper::visit(Block& _block)
{
    _block.setScope(m_currentScope);
    enterNewSubScope(_block);
    return true;
}

void DeclarationRegistrationHelper::endVisit(Block&)
{
    closeCurrentScope();
}

bool DeclarationRegistrationHelper::visit(ForStatement& _for)
{
    _for.setScope(m_currentScope);
    enterNewSubScope(_for);
    return true;
}

void DeclarationRegistrationHelper::endVisit(ForStatement&)
{
    closeCurrentScope();
}

void DeclarationRegistrationHelper::endVisit(VariableDeclarationStatement& _variableDeclarationStatement)
{
    // Register the local variables with the function
    // This does not fit here perfectly, but it saves us another AST visit.
    solAssert(m_currentFunction, "Variable declaration without function.");
    for (ASTPointer<VariableDeclaration> const& var: _variableDeclarationStatement.declarations())
        if (var)
            m_currentFunction->addLocalVariable(*var);
}

bool DeclarationRegistrationHelper::visit(VariableDeclaration& _declaration)
{
    registerDeclaration(_declaration, false);
    return true;
}

bool DeclarationRegistrationHelper::visit(EventDefinition& _event)
{
    registerDeclaration(_event, true);
    return true;
}

void DeclarationRegistrationHelper::endVisit(EventDefinition&)
{
    closeCurrentScope();
}

void DeclarationRegistrationHelper::enterNewSubScope(ASTNode& _subScope)
{
    map<ASTNode const*, shared_ptr<DeclarationContainer>>::iterator iter;
    bool newlyAdded;
    shared_ptr<DeclarationContainer> container(new DeclarationContainer(m_currentScope, m_scopes[m_currentScope].get()));
    tie(iter, newlyAdded) = m_scopes.emplace(&_subScope, move(container));
    solAssert(newlyAdded, "Unable to add new scope.");
    m_currentScope = &_subScope;
}

void DeclarationRegistrationHelper::closeCurrentScope()
{
    solAssert(m_currentScope && m_scopes.count(m_currentScope), "Closed non-existing scope.");
    m_currentScope = m_scopes[m_currentScope]->enclosingNode();
}

void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration, bool _opensScope)
{
    solAssert(m_currentScope && m_scopes.count(m_currentScope), "No current scope.");

    bool warnAboutShadowing = true;
    // Do not warn about shadowing for structs and enums because their members are
    // not accessible without prefixes. Also do not warn about event parameters
    // because they don't participate in any proper scope.
    if (
        dynamic_cast<StructDefinition const*>(m_currentScope) ||
        dynamic_cast<EnumDefinition const*>(m_currentScope) ||
        dynamic_cast<EventDefinition const*>(m_currentScope)
    )
        warnAboutShadowing = false;

    // Register declaration as inactive if we are in block scope.
    bool inactive =
        (dynamic_cast<Block const*>(m_currentScope) || dynamic_cast<ForStatement const*>(m_currentScope));

    registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, inactive, m_errorReporter);

    _declaration.setScope(m_currentScope);
    if (_opensScope)
        enterNewSubScope(_declaration);
}

string DeclarationRegistrationHelper::currentCanonicalName() const
{
    string ret;
    for (
        ASTNode const* scope = m_currentScope;
        scope != nullptr;
        scope = m_scopes[scope]->enclosingNode()
    )
    {
        if (auto decl = dynamic_cast<Declaration const*>(scope))
        {
            if (!ret.empty())
                ret = "." + ret;
            ret = decl->name() + ret;
        }
    }
    return ret;
}

}
}