aboutsummaryrefslogtreecommitdiffstats
path: root/AST.cpp
blob: bda366aed404330c08fd0805fee21b97c0adab1b (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
/*
    This file is part of cpp-ethereum.

    cpp-ethereum 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.

    cpp-ethereum 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 cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * Solidity abstract syntax tree.
 */

#include <algorithm>
#include <libsolidity/Utils.h>
#include <libsolidity/AST.h>
#include <libsolidity/ASTVisitor.h>
#include <libsolidity/Exceptions.h>
#include <libsolidity/AST_accept.h>

#include <libdevcrypto/SHA3.h>

using namespace std;

namespace dev
{
namespace solidity
{

TypeError ASTNode::createTypeError(string const& _description) const
{
    return TypeError() << errinfo_sourceLocation(getLocation()) << errinfo_comment(_description);
}

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

void ContractDefinition::checkTypeRequirements()
{
    for (ASTPointer<InheritanceSpecifier> const& baseSpecifier: getBaseContracts())
        baseSpecifier->checkTypeRequirements();

    checkIllegalOverrides();

    FunctionDefinition const* constructor = getConstructor();
    if (constructor && !constructor->getReturnParameters().empty())
        BOOST_THROW_EXCEPTION(constructor->getReturnParameterList()->createTypeError(
                                  "Non-empty \"returns\" directive for constructor."));

    for (ASTPointer<ModifierDefinition> const& modifier: getFunctionModifiers())
        modifier->checkTypeRequirements();

    for (ASTPointer<FunctionDefinition> const& function: getDefinedFunctions())
        function->checkTypeRequirements();

    // check for hash collisions in function signatures
    set<FixedHash<4>> hashes;
    for (auto const& hashAndFunction: getInterfaceFunctionList())
    {
        FixedHash<4> const& hash = std::get<0>(hashAndFunction);
        if (hashes.count(hash))
            BOOST_THROW_EXCEPTION(createTypeError(
                                      std::string("Function signature hash collision for ") +
                                      std::get<1>(hashAndFunction)->getCanonicalSignature(std::get<2>(hashAndFunction)->getName())));
        hashes.insert(hash);
    }
}

map<FixedHash<4>, FunctionDescription> ContractDefinition::getInterfaceFunctions() const
{
    auto exportedFunctionList = getInterfaceFunctionList();

    map<FixedHash<4>, FunctionDescription> exportedFunctions;
    for (auto const& it: exportedFunctionList)
        exportedFunctions.insert(make_pair(std::get<0>(it), FunctionDescription(std::get<1>(it), std::get<2>(it))));

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

    return exportedFunctions;
}

FunctionDefinition const* ContractDefinition::getConstructor() const
{
    for (ASTPointer<FunctionDefinition> const& f: m_definedFunctions)
        if (f->isConstructor())
            return f.get();
    return nullptr;
}

void ContractDefinition::checkIllegalOverrides() const
{
    // TODO unify this at a later point. for this we need to put the constness and the access specifier
    // into the types
    map<string, FunctionDefinition const*> functions;
    map<string, ModifierDefinition const*> modifiers;

    // We search from derived to base, so the stored item causes the error.
    for (ContractDefinition const* contract: getLinearizedBaseContracts())
    {
        for (ASTPointer<FunctionDefinition> const& function: contract->getDefinedFunctions())
        {
            if (function->isConstructor())
                continue; // constructors can neither be overridden nor override anything
            string const& name = function->getName();
            if (modifiers.count(name))
                BOOST_THROW_EXCEPTION(modifiers[name]->createTypeError("Override changes function to modifier."));
            FunctionDefinition const*& override = functions[name];
            if (!override)
                override = function.get();
            else if (override->isPublic() != function->isPublic() ||
                     override->isDeclaredConst() != function->isDeclaredConst() ||
                     FunctionType(*override) != FunctionType(*function))
                BOOST_THROW_EXCEPTION(override->createTypeError("Override changes extended function signature."));
        }
        for (ASTPointer<ModifierDefinition> const& modifier: contract->getFunctionModifiers())
        {
            string const& name = modifier->getName();
            if (functions.count(name))
                BOOST_THROW_EXCEPTION(functions[name]->createTypeError("Override changes modifier to function."));
            ModifierDefinition const*& override = modifiers[name];
            if (!override)
                override = modifier.get();
            else if (ModifierType(*override) != ModifierType(*modifier))
                BOOST_THROW_EXCEPTION(override->createTypeError("Override changes modifier signature."));
        }
    }
}

vector<tuple<FixedHash<4>, std::shared_ptr<FunctionType const>, Declaration const*>> const& ContractDefinition::getInterfaceFunctionList() const
{
    if (!m_interfaceFunctionList)
    {
        set<string> functionsSeen;
        m_interfaceFunctionList.reset(new vector<tuple<FixedHash<4>, std::shared_ptr<FunctionType const>, Declaration const*>>());
        for (ContractDefinition const* contract: getLinearizedBaseContracts())
        {
            for (ASTPointer<FunctionDefinition> const& f: contract->getDefinedFunctions())
                if (f->isPublic() && !f->isConstructor() && functionsSeen.count(f->getName()) == 0)
                {
                    functionsSeen.insert(f->getName());
                    FixedHash<4> hash(dev::sha3(f->getCanonicalSignature()));
                    m_interfaceFunctionList->push_back(make_tuple(hash, make_shared<FunctionType>(*f, false), f.get()));
                }

            for (ASTPointer<VariableDeclaration> const& v: contract->getStateVariables())
                if (v->isPublic() && functionsSeen.count(v->getName()) == 0)
                {
                    FunctionType ftype(*v);
                    functionsSeen.insert(v->getName());
                    FixedHash<4> hash(dev::sha3(ftype.getCanonicalSignature(v->getName())));
                    m_interfaceFunctionList->push_back(make_tuple(hash, make_shared<FunctionType>(*v), v.get()));
                }
        }
    }
    return *m_interfaceFunctionList;
}

void InheritanceSpecifier::checkTypeRequirements()
{
    m_baseName->checkTypeRequirements();
    for (ASTPointer<Expression> const& argument: m_arguments)
        argument->checkTypeRequirements();

    ContractDefinition const* base = dynamic_cast<ContractDefinition const*>(m_baseName->getReferencedDeclaration());
    solAssert(base, "Base contract not available.");
    TypePointers parameterTypes = ContractType(*base).getConstructorType()->getParameterTypes();
    if (parameterTypes.size() != m_arguments.size())
        BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for constructor call."));
    for (size_t i = 0; i < m_arguments.size(); ++i)
        if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
            BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in constructer call."));
}

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

void StructDefinition::checkMemberTypes() const
{
    for (ASTPointer<VariableDeclaration> const& member: getMembers())
        if (!member->getType()->canBeStored())
            BOOST_THROW_EXCEPTION(member->createTypeError("Type cannot be used in struct."));
}

void StructDefinition::checkRecursion() const
{
    set<StructDefinition const*> definitionsSeen;
    vector<StructDefinition const*> queue = {this};
    while (!queue.empty())
    {
        StructDefinition const* def = queue.back();
        queue.pop_back();
        if (definitionsSeen.count(def))
            BOOST_THROW_EXCEPTION(ParserError() << errinfo_sourceLocation(def->getLocation())
                                                << errinfo_comment("Recursive struct definition."));
        definitionsSeen.insert(def);
        for (ASTPointer<VariableDeclaration> const& member: def->getMembers())
            if (member->getType()->getCategory() == Type::Category::STRUCT)
            {
                UserDefinedTypeName const& typeName = dynamic_cast<UserDefinedTypeName const&>(*member->getTypeName());
                queue.push_back(&dynamic_cast<StructDefinition const&>(*typeName.getReferencedDeclaration()));
            }
    }
}

TypePointer FunctionDefinition::getType(ContractDefinition const*) const
{
    return make_shared<FunctionType>(*this);
}

void FunctionDefinition::checkTypeRequirements()
{
    for (ASTPointer<VariableDeclaration> const& var: getParameters() + getReturnParameters())
        if (!var->getType()->canLiveOutsideStorage())
            BOOST_THROW_EXCEPTION(var->createTypeError("Type is required to live outside storage."));
    for (ASTPointer<ModifierInvocation> const& modifier: m_functionModifiers)
        modifier->checkTypeRequirements();

    m_body->checkTypeRequirements();
}

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

Declaration::LValueType VariableDeclaration::getLValueType() const
{
    if (dynamic_cast<FunctionDefinition const*>(getScope()) || dynamic_cast<ModifierDefinition const*>(getScope()))
        return Declaration::LValueType::LOCAL;
    else
        return Declaration::LValueType::STORAGE;
}

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

void ModifierDefinition::checkTypeRequirements()
{
    m_body->checkTypeRequirements();
}

void ModifierInvocation::checkTypeRequirements()
{
    m_modifierName->checkTypeRequirements();
    for (ASTPointer<Expression> const& argument: m_arguments)
        argument->checkTypeRequirements();

    ModifierDefinition const* modifier = dynamic_cast<ModifierDefinition const*>(m_modifierName->getReferencedDeclaration());
    solAssert(modifier, "Function modifier not found.");
    vector<ASTPointer<VariableDeclaration>> const& parameters = modifier->getParameters();
    if (parameters.size() != m_arguments.size())
        BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for modifier invocation."));
    for (size_t i = 0; i < m_arguments.size(); ++i)
        if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameters[i]->getType()))
            BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in modifier invocation."));
}

void Block::checkTypeRequirements()
{
    for (shared_ptr<Statement> const& statement: m_statements)
        statement->checkTypeRequirements();
}

void IfStatement::checkTypeRequirements()
{
    m_condition->expectType(BoolType());
    m_trueBody->checkTypeRequirements();
    if (m_falseBody)
        m_falseBody->checkTypeRequirements();
}

void WhileStatement::checkTypeRequirements()
{
    m_condition->expectType(BoolType());
    m_body->checkTypeRequirements();
}

void ForStatement::checkTypeRequirements()
{
    if (m_initExpression)
        m_initExpression->checkTypeRequirements();
    if (m_condExpression)
        m_condExpression->expectType(BoolType());
    if (m_loopExpression)
        m_loopExpression->checkTypeRequirements();
    m_body->checkTypeRequirements();
}

void Return::checkTypeRequirements()
{
    if (!m_expression)
        return;
    if (!m_returnParameters)
        BOOST_THROW_EXCEPTION(createTypeError("Return arguments not allowed."));
    if (m_returnParameters->getParameters().size() != 1)
        BOOST_THROW_EXCEPTION(createTypeError("Different number of arguments in return statement "
                                              "than in returns declaration."));
    // this could later be changed such that the paramaters type is an anonymous struct type,
    // but for now, we only allow one return parameter
    m_expression->expectType(*m_returnParameters->getParameters().front()->getType());
}

void VariableDefinition::checkTypeRequirements()
{
    // Variables can be declared without type (with "var"), in which case the first assignment
    // sets the type.
    // Note that assignments before the first declaration are legal because of the special scoping
    // rules inherited from JavaScript.
    if (m_value)
    {
        if (m_variable->getType())
            m_value->expectType(*m_variable->getType());
        else
        {
            // no type declared and no previous assignment, infer the type
            m_value->checkTypeRequirements();
            TypePointer type = m_value->getType();
            if (type->getCategory() == Type::Category::INTEGER_CONSTANT)
            {
                auto intType = dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType();
                if (!intType)
                    BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString()));
                type = intType;
            }
            m_variable->setType(type);
        }
    }
}

void Assignment::checkTypeRequirements()
{
    m_leftHandSide->checkTypeRequirements();
    m_leftHandSide->requireLValue();
    //@todo later, assignments to structs might be possible, but not to mappings
    if (!m_leftHandSide->getType()->isValueType() && !m_leftHandSide->isLocalLValue())
        BOOST_THROW_EXCEPTION(createTypeError("Assignment to non-local non-value lvalue."));
    m_type = m_leftHandSide->getType();
    if (m_assigmentOperator == Token::ASSIGN)
        m_rightHandSide->expectType(*m_type);
    else
    {
        // compound assignment
        m_rightHandSide->checkTypeRequirements();
        TypePointer resultType = m_type->binaryOperatorResult(Token::AssignmentToBinaryOp(m_assigmentOperator),
                                                              m_rightHandSide->getType());
        if (!resultType || *resultType != *m_type)
            BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_assigmentOperator)) +
                                                  " not compatible with types " +
                                                  m_type->toString() + " and " +
                                                  m_rightHandSide->getType()->toString()));
    }
}

void ExpressionStatement::checkTypeRequirements()
{
    m_expression->checkTypeRequirements();
    if (m_expression->getType()->getCategory() == Type::Category::INTEGER_CONSTANT)
        if (!dynamic_pointer_cast<IntegerConstantType const>(m_expression->getType())->getIntegerType())
            BOOST_THROW_EXCEPTION(m_expression->createTypeError("Invalid integer constant."));
}

void Expression::expectType(Type const& _expectedType)
{
    checkTypeRequirements();
    Type const& type = *getType();
    if (!type.isImplicitlyConvertibleTo(_expectedType))
        BOOST_THROW_EXCEPTION(createTypeError("Type " + type.toString() +
                                              " not implicitly convertible to expected type "
                                              + _expectedType.toString() + "."));
}

void Expression::requireLValue()
{
    if (!isLValue())
        BOOST_THROW_EXCEPTION(createTypeError("Expression has to be an lvalue."));
    m_lvalueRequested = true;
}

void UnaryOperation::checkTypeRequirements()
{
    // INC, DEC, ADD, SUB, NOT, BIT_NOT, DELETE
    m_subExpression->checkTypeRequirements();
    if (m_operator == Token::Value::INC || m_operator == Token::Value::DEC || m_operator == Token::Value::DELETE)
        m_subExpression->requireLValue();
    m_type = m_subExpression->getType()->unaryOperatorResult(m_operator);
    if (!m_type)
        BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
}

void BinaryOperation::checkTypeRequirements()
{
    m_left->checkTypeRequirements();
    m_right->checkTypeRequirements();
    m_commonType = m_left->getType()->binaryOperatorResult(m_operator, m_right->getType());
    if (!m_commonType)
        BOOST_THROW_EXCEPTION(createTypeError("Operator " + string(Token::toString(m_operator)) +
                                              " not compatible with types " +
                                              m_left->getType()->toString() + " and " +
                                              m_right->getType()->toString()));
    m_type = Token::isCompareOp(m_operator) ? make_shared<BoolType>() : m_commonType;
}

void FunctionCall::checkTypeRequirements()
{
    m_expression->checkTypeRequirements();
    for (ASTPointer<Expression> const& argument: m_arguments)
        argument->checkTypeRequirements();

    Type const* expressionType = m_expression->getType().get();
    if (isTypeConversion())
    {
        TypeType const& type = dynamic_cast<TypeType const&>(*expressionType);
        //@todo for structs, we have to check the number of arguments to be equal to the
        // number of non-mapping members
        if (m_arguments.size() != 1)
            BOOST_THROW_EXCEPTION(createTypeError("More than one argument for explicit type conversion."));
        if (!m_names.empty())
            BOOST_THROW_EXCEPTION(createTypeError("Type conversion can't allow named arguments."));
        if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type.getActualType()))
            BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
        m_type = type.getActualType();
    }
    else if (FunctionType const* functionType = dynamic_cast<FunctionType const*>(expressionType))
    {
        //@todo would be nice to create a struct type from the arguments
        // and then ask if that is implicitly convertible to the struct represented by the
        // function parameters
        TypePointers const& parameterTypes = functionType->getParameterTypes();
        if (parameterTypes.size() != m_arguments.size())
            BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));

        if (m_names.empty())
        {
            for (size_t i = 0; i < m_arguments.size(); ++i)
                if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[i]))
                    BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
        }
        else
        {
            auto const& parameterNames = functionType->getParameterNames();
            if (parameterNames.size() != m_names.size())
                BOOST_THROW_EXCEPTION(createTypeError("Some argument names are missing."));

            // check duplicate names
            for (size_t i = 0; i < m_names.size(); i++) {
                for (size_t j = i + 1; j < m_names.size(); j++) {
                    if (m_names[i] == m_names[j])
                        BOOST_THROW_EXCEPTION(createTypeError("Duplicate named argument."));
                }
            }

            for (size_t i = 0; i < m_names.size(); i++) {
                bool found = false;
                for (size_t j = 0; j < parameterNames.size(); j++) {
                    if (parameterNames[j] == m_names[i]) {
                        // check type convertible
                        if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameterTypes[j]))
                            BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));

                        found = true;
                        break;
                    }
                }
                if (!found)
                    BOOST_THROW_EXCEPTION(createTypeError("Named argument doesn't match function declaration."));
            }
        }

        // @todo actually the return type should be an anonymous struct,
        // but we change it to the type of the first return value until we have structs
        if (functionType->getReturnParameterTypes().empty())
            m_type = make_shared<VoidType>();
        else
            m_type = functionType->getReturnParameterTypes().front();
    }
    else
        BOOST_THROW_EXCEPTION(createTypeError("Type is not callable."));
}

bool FunctionCall::isTypeConversion() const
{
    return m_expression->getType()->getCategory() == Type::Category::TYPE;
}

void NewExpression::checkTypeRequirements()
{
    m_contractName->checkTypeRequirements();
    m_contract = dynamic_cast<ContractDefinition const*>(m_contractName->getReferencedDeclaration());
    if (!m_contract)
        BOOST_THROW_EXCEPTION(createTypeError("Identifier is not a contract."));
    shared_ptr<ContractType const> contractType = make_shared<ContractType>(*m_contract);
    TypePointers const& parameterTypes = contractType->getConstructorType()->getParameterTypes();
    m_type = make_shared<FunctionType>(parameterTypes, TypePointers{contractType},
                                       FunctionType::Location::CREATION);
}

void MemberAccess::checkTypeRequirements()
{
    m_expression->checkTypeRequirements();
    Type const& type = *m_expression->getType();
    m_type = type.getMemberType(*m_memberName);
    if (!m_type)
        BOOST_THROW_EXCEPTION(createTypeError("Member \"" + *m_memberName + "\" not found or not "
                                              "visible in " + type.toString()));
    //@todo later, this will not always be STORAGE
    m_lvalue = type.getCategory() == Type::Category::STRUCT ? Declaration::LValueType::STORAGE : Declaration::LValueType::NONE;
}

void IndexAccess::checkTypeRequirements()
{
    m_base->checkTypeRequirements();
    if (m_base->getType()->getCategory() != Type::Category::MAPPING)
        BOOST_THROW_EXCEPTION(m_base->createTypeError("Indexed expression has to be a mapping (is " +
                                                      m_base->getType()->toString() + ")"));
    MappingType const& type = dynamic_cast<MappingType const&>(*m_base->getType());
    m_index->expectType(*type.getKeyType());
    m_type = type.getValueType();
    m_lvalue = Declaration::LValueType::STORAGE;
}

void Identifier::checkTypeRequirements()
{
    solAssert(m_referencedDeclaration, "Identifier not resolved.");

    m_lvalue = m_referencedDeclaration->getLValueType();
    m_type = m_referencedDeclaration->getType(m_currentContract);
    if (!m_type)
        BOOST_THROW_EXCEPTION(createTypeError("Declaration referenced before type could be determined."));
}

void ElementaryTypeNameExpression::checkTypeRequirements()
{
    m_type = make_shared<TypeType>(Type::fromElementaryTypeName(m_typeToken));
}

void Literal::checkTypeRequirements()
{
    m_type = Type::forLiteral(*this);
    if (!m_type)
        BOOST_THROW_EXCEPTION(createTypeError("Invalid literal value."));
}

std::string const& ParamDescription::getName() const
{
    return m_description.first;
}

std::string const& ParamDescription::getType() const
{
    return m_description.second;
}

ASTPointer<ASTString> FunctionDescription::getDocumentation() const
{
    auto function = dynamic_cast<FunctionDefinition const*>(m_description.second);
    if (function)
        return function->getDocumentation();

    return ASTPointer<ASTString>();
}

string FunctionDescription::getSignature() const
{
    return m_description.first->getCanonicalSignature(m_description.second->getName());
}

string FunctionDescription::getName() const
{
    return m_description.second->getName();
}

bool FunctionDescription::isConstant() const
{
    auto function = dynamic_cast<FunctionDefinition const*>(m_description.second);
    if (function)
        return function->isDeclaredConst();

    return true;
}

vector<ParamDescription> const FunctionDescription::getParameters() const
{
    auto function = dynamic_cast<FunctionDefinition const*>(m_description.second);
    if (function)
    {
        vector<ParamDescription> paramsDescription;
        for (auto const& param: function->getParameters())
            paramsDescription.push_back(ParamDescription(param->getName(), param->getType()->toString()));

        return paramsDescription;
    }

    // else for now let's assume no parameters to accessors
    // LTODO: fix this for mapping types
    return {};
}

vector<ParamDescription> const FunctionDescription::getReturnParameters() const
{
    auto function = dynamic_cast<FunctionDefinition const*>(m_description.second);
    if (function)
    {
        vector<ParamDescription> paramsDescription;
        for (auto const& param: function->getReturnParameters())
            paramsDescription.push_back(ParamDescription(param->getName(), param->getType()->toString()));

        return paramsDescription;
    }

    auto vardecl = dynamic_cast<VariableDeclaration const*>(m_description.second);
    return {ParamDescription(vardecl->getName(), vardecl->getType()->toString())};
}

Declaration const* FunctionDescription::getDeclaration() const
{
    return m_description.second;
}

VariableDeclaration const* FunctionDescription::getVariableDeclaration() const
{
    return dynamic_cast<VariableDeclaration const*>(m_description.second);
}

FunctionDefinition const* FunctionDescription::getFunctionDefinition() const
{
    return dynamic_cast<FunctionDefinition const*>(m_description.second);
}

shared_ptr<FunctionType const> FunctionDescription::getFunctionTypeShared() const
{
    return m_description.first;
}


FunctionType const* FunctionDescription::getFunctionType() const
{
    return m_description.first.get();
}


}
}