aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/ast/AST.cpp
blob: 7719d0803dc422eb93e499b67dba1a8fa69e2fd8 (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
/*
    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
 * Solidity abstract syntax tree.
 */

#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/ASTVisitor.h>
#include <libsolidity/ast/AST_accept.h>

#include <libdevcore/SHA3.h>

#include <boost/algorithm/string.hpp>

#include <algorithm>
#include <functional>

using namespace std;
using namespace dev;
using namespace dev::solidity;

class IDDispenser
{
public:
    static size_t next() { return ++instance(); }
    static void reset() { instance() = 0; }
private:
    static size_t& instance()
    {
        static IDDispenser dispenser;
        return dispenser.id;
    }
    size_t id = 0;
};

ASTNode::ASTNode(SourceLocation const& _location):
    m_id(IDDispenser::next()),
    m_location(_location)
{
}

ASTNode::~ASTNode()
{
    delete m_annotation;
}

void ASTNode::resetID()
{
    IDDispenser::reset();
}

ASTAnnotation& ASTNode::annotation() const
{
    if (!m_annotation)
        m_annotation = new ASTAnnotation();
    return *m_annotation;
}

SourceUnitAnnotation& SourceUnit::annotation() const
{
    if (!m_annotation)
        m_annotation = new SourceUnitAnnotation();
    return dynamic_cast<SourceUnitAnnotation&>(*m_annotation);
}

set<SourceUnit const*> SourceUnit::referencedSourceUnits(bool _recurse, set<SourceUnit const*> _skipList) const
{
    set<SourceUnit const*> sourceUnits;
    for (ImportDirective const* importDirective: filteredNodes<ImportDirective>(nodes()))
    {
        auto const& sourceUnit = importDirective->annotation().sourceUnit;
        if (!_skipList.count(sourceUnit))
        {
            _skipList.insert(sourceUnit);
            sourceUnits.insert(sourceUnit);
            if (_recurse)
                sourceUnits += sourceUnit->referencedSourceUnits(true, _skipList);
        }
    }
    return sourceUnits;
}

ImportAnnotation& ImportDirective::annotation() const
{
    if (!m_annotation)
        m_annotation = new ImportAnnotation();
    return dynamic_cast<ImportAnnotation&>(*m_annotation);
}

TypePointer ImportDirective::type() const
{
    solAssert(!!annotation().sourceUnit, "");
    return make_shared<ModuleType>(*annotation().sourceUnit);
}

map<FixedHash<4>, FunctionTypePointer> ContractDefinition::interfaceFunctions() const
{
    auto exportedFunctionList = interfaceFunctionList();

    map<FixedHash<4>, FunctionTypePointer> exportedFunctions;
    for (auto const& it: exportedFunctionList)
        exportedFunctions.insert(it);

    solAssert(
        exportedFunctionList.size() == exportedFunctions.size(),
        "Hash collision at Function Definition Hash calculation"
    );

    return exportedFunctions;
}

FunctionDefinition const* ContractDefinition::constructor() const
{
    for (FunctionDefinition const* f: definedFunctions())
        if (f->isConstructor())
            return f;
    return nullptr;
}

bool ContractDefinition::constructorIsPublic() const
{
    FunctionDefinition const* f = constructor();
    return !f || f->isPublic();
}

FunctionDefinition const* ContractDefinition::fallbackFunction() const
{
    for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
        for (FunctionDefinition const* f: contract->definedFunctions())
            if (f->isFallback())
                return f;
    return nullptr;
}

vector<EventDefinition const*> const& ContractDefinition::interfaceEvents() const
{
    if (!m_interfaceEvents)
    {
        set<string> eventsSeen;
        m_interfaceEvents.reset(new vector<EventDefinition const*>());
        for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
            for (EventDefinition const* e: contract->events())
            {
                /// NOTE: this requires the "internal" version of an Event,
                ///       though here internal strictly refers to visibility,
                ///       and not to function encoding (jump vs. call)
                auto const& function = e->functionType(true);
                solAssert(function, "");
                string eventSignature = function->externalSignature();
                if (eventsSeen.count(eventSignature) == 0)
                {
                    eventsSeen.insert(eventSignature);
                    m_interfaceEvents->push_back(e);
                }
            }
    }
    return *m_interfaceEvents;
}

vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::interfaceFunctionList() const
{
    if (!m_interfaceFunctionList)
    {
        set<string> signaturesSeen;
        m_interfaceFunctionList.reset(new vector<pair<FixedHash<4>, FunctionTypePointer>>());
        for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
        {
            vector<FunctionTypePointer> functions;
            for (FunctionDefinition const* f: contract->definedFunctions())
                if (f->isPartOfExternalInterface())
                    functions.push_back(make_shared<FunctionType>(*f, false));
            for (VariableDeclaration const* v: contract->stateVariables())
                if (v->isPartOfExternalInterface())
                    functions.push_back(make_shared<FunctionType>(*v));
            for (FunctionTypePointer const& fun: functions)
            {
                if (!fun->interfaceFunctionType())
                    // Fails hopefully because we already registered the error
                    continue;
                string functionSignature = fun->externalSignature();
                if (signaturesSeen.count(functionSignature) == 0)
                {
                    signaturesSeen.insert(functionSignature);
                    FixedHash<4> hash(dev::keccak256(functionSignature));
                    m_interfaceFunctionList->push_back(make_pair(hash, fun));
                }
            }
        }
    }
    return *m_interfaceFunctionList;
}

vector<Declaration const*> const& ContractDefinition::inheritableMembers() const
{
    if (!m_inheritableMembers)
    {
        set<string> memberSeen;
        m_inheritableMembers.reset(new vector<Declaration const*>());
        auto addInheritableMember = [&](Declaration const* _decl)
        {
            solAssert(_decl, "addInheritableMember got a nullpointer.");
            if (memberSeen.count(_decl->name()) == 0 && _decl->isVisibleInDerivedContracts())
            {
                memberSeen.insert(_decl->name());
                m_inheritableMembers->push_back(_decl);
            }
        };

        for (FunctionDefinition const* f: definedFunctions())
            addInheritableMember(f);

        for (VariableDeclaration const* v: stateVariables())
            addInheritableMember(v);

        for (StructDefinition const* s: definedStructs())
            addInheritableMember(s);

        for (EnumDefinition const* e: definedEnums())
            addInheritableMember(e);

        for (EventDefinition const* e: events())
            addInheritableMember(e);
    }
    return *m_inheritableMembers;
}

TypePointer ContractDefinition::type() const
{
    return make_shared<TypeType>(make_shared<ContractType>(*this));
}

ContractDefinitionAnnotation& ContractDefinition::annotation() const
{
    if (!m_annotation)
        m_annotation = new ContractDefinitionAnnotation();
    return dynamic_cast<ContractDefinitionAnnotation&>(*m_annotation);
}

TypeNameAnnotation& TypeName::annotation() const
{
    if (!m_annotation)
        m_annotation = new TypeNameAnnotation();
    return dynamic_cast<TypeNameAnnotation&>(*m_annotation);
}

TypePointer StructDefinition::type() const
{
    return make_shared<TypeType>(make_shared<StructType>(*this));
}

TypeDeclarationAnnotation& StructDefinition::annotation() const
{
    if (!m_annotation)
        m_annotation = new TypeDeclarationAnnotation();
    return dynamic_cast<TypeDeclarationAnnotation&>(*m_annotation);
}

TypePointer EnumValue::type() const
{
    auto parentDef = dynamic_cast<EnumDefinition const*>(scope());
    solAssert(parentDef, "Enclosing Scope of EnumValue was not set");
    return make_shared<EnumType>(*parentDef);
}

TypePointer EnumDefinition::type() const
{
    return make_shared<TypeType>(make_shared<EnumType>(*this));
}

TypeDeclarationAnnotation& EnumDefinition::annotation() const
{
    if (!m_annotation)
        m_annotation = new TypeDeclarationAnnotation();
    return dynamic_cast<TypeDeclarationAnnotation&>(*m_annotation);
}

ContractDefinition::ContractKind FunctionDefinition::inContractKind() const
{
    auto contractDef = dynamic_cast<ContractDefinition const*>(scope());
    solAssert(contractDef, "Enclosing Scope of FunctionDefinition was not set.");
    return contractDef->contractKind();
}

FunctionTypePointer FunctionDefinition::functionType(bool _internal) const
{
    if (_internal)
    {
        switch (visibility())
        {
        case Declaration::Visibility::Default:
            solAssert(false, "visibility() should not return Default");
        case Declaration::Visibility::Private:
        case Declaration::Visibility::Internal:
        case Declaration::Visibility::Public:
            return make_shared<FunctionType>(*this, _internal);
        case Declaration::Visibility::External:
            return {};
        default:
            solAssert(false, "visibility() should return a Visibility");
        }
    }
    else
    {
        switch (visibility())
        {
        case Declaration::Visibility::Default:
            solAssert(false, "visibility() should not return Default");
        case Declaration::Visibility::Private:
        case Declaration::Visibility::Internal:
            return {};
        case Declaration::Visibility::Public:
        case Declaration::Visibility::External:
            return make_shared<FunctionType>(*this, _internal);
        default:
            solAssert(false, "visibility() should return a Visibility");
        }
    }

    // To make the compiler happy
    return {};
}

TypePointer FunctionDefinition::type() const
{
    solAssert(visibility() != Declaration::Visibility::External, "");
    return make_shared<FunctionType>(*this);
}

string FunctionDefinition::externalSignature() const
{
    return FunctionType(*this).externalSignature();
}

string FunctionDefinition::fullyQualifiedName() const
{
    auto const* contract = dynamic_cast<ContractDefinition const*>(scope());
    solAssert(contract, "Enclosing scope of function definition was not set.");

    auto fname = name().empty() ? "<fallback>" : name();
    return sourceUnitName() + ":" + contract->name() + "." + fname;
}

FunctionDefinitionAnnotation& FunctionDefinition::annotation() const
{
    if (!m_annotation)
        m_annotation = new FunctionDefinitionAnnotation();
    return dynamic_cast<FunctionDefinitionAnnotation&>(*m_annotation);
}

TypePointer ModifierDefinition::type() const
{
    return make_shared<ModifierType>(*this);
}

ModifierDefinitionAnnotation& ModifierDefinition::annotation() const
{
    if (!m_annotation)
        m_annotation = new ModifierDefinitionAnnotation();
    return dynamic_cast<ModifierDefinitionAnnotation&>(*m_annotation);
}

TypePointer EventDefinition::type() const
{
    return make_shared<FunctionType>(*this);
}

FunctionTypePointer EventDefinition::functionType(bool _internal) const
{
    if (_internal)
        return make_shared<FunctionType>(*this);
    else
        return {};
}

EventDefinitionAnnotation& EventDefinition::annotation() const
{
    if (!m_annotation)
        m_annotation = new EventDefinitionAnnotation();
    return dynamic_cast<EventDefinitionAnnotation&>(*m_annotation);
}

UserDefinedTypeNameAnnotation& UserDefinedTypeName::annotation() const
{
    if (!m_annotation)
        m_annotation = new UserDefinedTypeNameAnnotation();
    return dynamic_cast<UserDefinedTypeNameAnnotation&>(*m_annotation);
}

SourceUnit const& Scopable::sourceUnit() const
{
    ASTNode const* s = scope();
    solAssert(s, "");
    // will not always be a declaratoion
    while (dynamic_cast<Scopable const*>(s) && dynamic_cast<Scopable const*>(s)->scope())
        s = dynamic_cast<Scopable const*>(s)->scope();
    return dynamic_cast<SourceUnit const&>(*s);
}

string Scopable::sourceUnitName() const
{
    return sourceUnit().annotation().path;
}

bool VariableDeclaration::isLValue() const
{
    // External function parameters and constant declared variables are Read-Only
    return !isExternalCallableParameter() && !m_isConstant;
}

bool VariableDeclaration::isLocalVariable() const
{
    auto s = scope();
    return
        dynamic_cast<CallableDeclaration const*>(s) ||
        dynamic_cast<Block const*>(s) ||
        dynamic_cast<ForStatement const*>(s);
}

bool VariableDeclaration::isCallableParameter() const
{
    auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
    if (!callable)
        return false;
    for (auto const& variable: callable->parameters())
        if (variable.get() == this)
            return true;
    if (callable->returnParameterList())
        for (auto const& variable: callable->returnParameterList()->parameters())
            if (variable.get() == this)
                return true;
    return false;
}

bool VariableDeclaration::isLocalOrReturn() const
{
    return isReturnParameter() || (isLocalVariable() && !isCallableParameter());
}

bool VariableDeclaration::isReturnParameter() const
{
    auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
    if (!callable)
        return false;
    if (callable->returnParameterList())
        for (auto const& variable: callable->returnParameterList()->parameters())
            if (variable.get() == this)
                return true;
    return false;
}

bool VariableDeclaration::isExternalCallableParameter() const
{
    auto const* callable = dynamic_cast<CallableDeclaration const*>(scope());
    if (!callable || callable->visibility() != Declaration::Visibility::External)
        return false;
    for (auto const& variable: callable->parameters())
        if (variable.get() == this)
            return true;
    return false;
}

bool VariableDeclaration::canHaveAutoType() const
{
    return isLocalVariable() && !isCallableParameter();
}

TypePointer VariableDeclaration::type() const
{
    return annotation().type;
}

FunctionTypePointer VariableDeclaration::functionType(bool _internal) const
{
    if (_internal)
        return {};
    switch (visibility())
    {
    case Declaration::Visibility::Default:
        solAssert(false, "visibility() should not return Default");
    case Declaration::Visibility::Private:
    case Declaration::Visibility::Internal:
        return {};
    case Declaration::Visibility::Public:
    case Declaration::Visibility::External:
        return make_shared<FunctionType>(*this);
    default:
        solAssert(false, "visibility() should not return a Visibility");
    }

    // To make the compiler happy
    return {};
}

VariableDeclarationAnnotation& VariableDeclaration::annotation() const
{
    if (!m_annotation)
        m_annotation = new VariableDeclarationAnnotation();
    return dynamic_cast<VariableDeclarationAnnotation&>(*m_annotation);
}

StatementAnnotation& Statement::annotation() const
{
    if (!m_annotation)
        m_annotation = new StatementAnnotation();
    return dynamic_cast<StatementAnnotation&>(*m_annotation);
}

InlineAssemblyAnnotation& InlineAssembly::annotation() const
{
    if (!m_annotation)
        m_annotation = new InlineAssemblyAnnotation();
    return dynamic_cast<InlineAssemblyAnnotation&>(*m_annotation);
}

ReturnAnnotation& Return::annotation() const
{
    if (!m_annotation)
        m_annotation = new ReturnAnnotation();
    return dynamic_cast<ReturnAnnotation&>(*m_annotation);
}

ExpressionAnnotation& Expression::annotation() const
{
    if (!m_annotation)
        m_annotation = new ExpressionAnnotation();
    return dynamic_cast<ExpressionAnnotation&>(*m_annotation);
}

MemberAccessAnnotation& MemberAccess::annotation() const
{
    if (!m_annotation)
        m_annotation = new MemberAccessAnnotation();
    return dynamic_cast<MemberAccessAnnotation&>(*m_annotation);
}

BinaryOperationAnnotation& BinaryOperation::annotation() const
{
    if (!m_annotation)
        m_annotation = new BinaryOperationAnnotation();
    return dynamic_cast<BinaryOperationAnnotation&>(*m_annotation);
}

FunctionCallAnnotation& FunctionCall::annotation() const
{
    if (!m_annotation)
        m_annotation = new FunctionCallAnnotation();
    return dynamic_cast<FunctionCallAnnotation&>(*m_annotation);
}

IdentifierAnnotation& Identifier::annotation() const
{
    if (!m_annotation)
        m_annotation = new IdentifierAnnotation();
    return dynamic_cast<IdentifierAnnotation&>(*m_annotation);
}

bool Literal::isHexNumber() const
{
    if (token() != Token::Number)
        return false;
    return boost::starts_with(value(), "0x");
}

bool Literal::looksLikeAddress() const
{
    if (subDenomination() != SubDenomination::None)
        return false;

    if (!isHexNumber())
        return false;

    return abs(int(value().length()) - 42) <= 1;
}

bool Literal::passesAddressChecksum() const
{
    solAssert(isHexNumber(), "Expected hex number");
    return dev::passesAddressChecksum(value(), true);
}

std::string Literal::getChecksummedAddress() const
{
    solAssert(isHexNumber(), "Expected hex number");
    /// Pad literal to be a proper hex address.
    string address = value().substr(2);
    if (address.length() > 40)
        return string();
    address.insert(address.begin(), 40 - address.size(), '0');
    return dev::getChecksummedAddress(address);
}