aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/InterfaceHandler.cpp
blob: fc79b493b32a1ec74c4a2a10c8379034f69454e0 (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

#include <libsolidity/InterfaceHandler.h>
#include <libsolidity/AST.h>
#include <libsolidity/CompilerStack.h>
using namespace std;

namespace dev
{
namespace solidity
{

/* -- public -- */

InterfaceHandler::InterfaceHandler()
{
    m_lastTag = DocTagType::None;
}

string InterfaceHandler::documentation(
    ContractDefinition const& _contractDef,
    DocumentationType _type
)
{
    switch(_type)
    {
    case DocumentationType::NatspecUser:
        return userDocumentation(_contractDef);
    case DocumentationType::NatspecDev:
        return devDocumentation(_contractDef);
    case DocumentationType::ABIInterface:
        return abiInterface(_contractDef);
    case DocumentationType::ABISolidityInterface:
        return ABISolidityInterface(_contractDef);
    }

    BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation type"));
    return "";
}

string InterfaceHandler::abiInterface(ContractDefinition const& _contractDef)
{
    Json::Value abi(Json::arrayValue);

    auto populateParameters = [](vector<string> const& _paramNames, vector<string> const& _paramTypes)
    {
        Json::Value params(Json::arrayValue);
        solAssert(_paramNames.size() == _paramTypes.size(), "Names and types vector size does not match");
        for (unsigned i = 0; i < _paramNames.size(); ++i)
        {
            Json::Value param;
            param["name"] = _paramNames[i];
            param["type"] = _paramTypes[i];
            params.append(param);
        }
        return params;
    };

    for (auto it: _contractDef.interfaceFunctions())
    {
        auto externalFunctionType = it.second->externalFunctionType();
        Json::Value method;
        method["type"] = "function";
        method["name"] = it.second->declaration().name();
        method["constant"] = it.second->isConstant();
        method["inputs"] = populateParameters(
            externalFunctionType->parameterNames(),
            externalFunctionType->parameterTypeNames()
        );
        method["outputs"] = populateParameters(
            externalFunctionType->returnParameterNames(),
            externalFunctionType->returnParameterTypeNames()
        );
        abi.append(method);
    }
    if (_contractDef.constructor())
    {
        Json::Value method;
        method["type"] = "constructor";
        auto externalFunction = FunctionType(*_contractDef.constructor()).externalFunctionType();
        solAssert(!!externalFunction, "");
        method["inputs"] = populateParameters(
            externalFunction->parameterNames(),
            externalFunction->parameterTypeNames()
        );
        abi.append(method);
    }

    for (auto const& it: _contractDef.interfaceEvents())
    {
        Json::Value event;
        event["type"] = "event";
        event["name"] = it->name();
        event["anonymous"] = it->isAnonymous();
        Json::Value params(Json::arrayValue);
        for (auto const& p: it->parameters())
        {
            Json::Value input;
            input["name"] = p->name();
            input["type"] = p->type()->toString(true);
            input["indexed"] = p->isIndexed();
            params.append(input);
        }
        event["inputs"] = params;
        abi.append(event);
    }
    return Json::FastWriter().write(abi);
}

string InterfaceHandler::ABISolidityInterface(ContractDefinition const& _contractDef)
{
    string ret = "contract " + _contractDef.name() + "{";

    auto populateParameters = [](vector<string> const& _paramNames, vector<string> const& _paramTypes)
    {
        string r = "";
        solAssert(_paramNames.size() == _paramTypes.size(), "Names and types vector size does not match");
        for (unsigned i = 0; i < _paramNames.size(); ++i)
            r += (r.size() ? "," : "(") + _paramTypes[i] + " " + _paramNames[i];
        return r.size() ? r + ")" : "()";
    };
    if (_contractDef.constructor())
    {
        auto externalFunction = FunctionType(*_contractDef.constructor()).externalFunctionType();
        solAssert(!!externalFunction, "");
        ret +=
            "function " +
            _contractDef.name() +
            populateParameters(externalFunction->parameterNames(), externalFunction->parameterTypeNames()) +
            ";";
    }
    for (auto const& it: _contractDef.interfaceFunctions())
    {
        ret += "function " + it.second->declaration().name() +
            populateParameters(it.second->parameterNames(), it.second->parameterTypeNames()) +
            (it.second->isConstant() ? "constant " : "");
        if (it.second->returnParameterTypes().size())
            ret += "returns" + populateParameters(it.second->returnParameterNames(), it.second->returnParameterTypeNames());
        else if (ret.back() == ' ')
            ret.pop_back();
        ret += ";";
    }

    return ret + "}";
}

string InterfaceHandler::userDocumentation(ContractDefinition const& _contractDef)
{
    Json::Value doc;
    Json::Value methods(Json::objectValue);

    for (auto const& it: _contractDef.interfaceFunctions())
    {
        Json::Value user;
        auto strPtr = it.second->documentation();
        if (strPtr)
        {
            resetUser();
            parseDocString(*strPtr, CommentOwner::Function);
            if (!m_notice.empty())
            {// since @notice is the only user tag if missing function should not appear
                user["notice"] = Json::Value(m_notice);
                methods[it.second->externalSignature()] = user;
            }
        }
    }
    doc["methods"] = methods;

    return Json::StyledWriter().write(doc);
}

string InterfaceHandler::devDocumentation(ContractDefinition const& _contractDef)
{
    // LTODO: Somewhere in this function warnings for mismatch of param names
    // should be thrown
    Json::Value doc;
    Json::Value methods(Json::objectValue);

    auto contractDoc = _contractDef.documentation();
    if (contractDoc)
    {
        m_contractAuthor.clear();
        m_title.clear();
        parseDocString(*contractDoc, CommentOwner::Contract);

        if (!m_contractAuthor.empty())
            doc["author"] = m_contractAuthor;

        if (!m_title.empty())
            doc["title"] = m_title;
    }

    for (auto const& it: _contractDef.interfaceFunctions())
    {
        Json::Value method;
        auto strPtr = it.second->documentation();
        if (strPtr)
        {
            resetDev();
            parseDocString(*strPtr, CommentOwner::Function);

            if (!m_dev.empty())
                method["details"] = Json::Value(m_dev);

            if (!m_author.empty())
                method["author"] = m_author;

            Json::Value params(Json::objectValue);
            vector<string> paramNames = it.second->parameterNames();
            for (auto const& pair: m_params)
            {
                if (find(paramNames.begin(), paramNames.end(), pair.first) == paramNames.end())
                    // LTODO: mismatching parameter name, throw some form of warning and not just an exception
                    BOOST_THROW_EXCEPTION(
                        DocstringParsingError() <<
                        errinfo_comment("documented parameter \"" + pair.first + "\" not found in the parameter list of the function.")
                    );                  
                params[pair.first] = pair.second;
            }

            if (!m_params.empty())
                method["params"] = params;

            if (!m_return.empty())
                method["return"] = m_return;

            if (!method.empty()) // add the function, only if we have any documentation to add
                methods[it.second->externalSignature()] = method;
        }
    }
    doc["methods"] = methods;

    return Json::StyledWriter().write(doc);
}

/* -- private -- */
void InterfaceHandler::resetUser()
{
    m_notice.clear();
}

void InterfaceHandler::resetDev()
{
    m_dev.clear();
    m_author.clear();
    m_return.clear();
    m_params.clear();
}

static inline string::const_iterator skipLineOrEOS(
    string::const_iterator _nlPos,
    string::const_iterator _end
)
{
    return (_nlPos == _end) ? _end : ++_nlPos;
}

string::const_iterator InterfaceHandler::parseDocTagLine(
    string::const_iterator _pos,
    string::const_iterator _end,
    string& _tagString,
    DocTagType _tagType,
    bool _appending
)
{
    auto nlPos = find(_pos, _end, '\n');
    if (_appending && _pos < _end && *_pos != ' ')
        _tagString += " ";
    copy(_pos, nlPos, back_inserter(_tagString));
    m_lastTag = _tagType;
    return skipLineOrEOS(nlPos, _end);
}

string::const_iterator InterfaceHandler::parseDocTagParam(
    string::const_iterator _pos,
    string::const_iterator _end
)
{
    // find param name
    auto currPos = find(_pos, _end, ' ');
    if (currPos == _end)
        BOOST_THROW_EXCEPTION(DocstringParsingError() << errinfo_comment("End of param name not found" + string(_pos, _end)));


    auto paramName = string(_pos, currPos);

    currPos += 1;
    auto nlPos = find(currPos, _end, '\n');
    auto paramDesc = string(currPos, nlPos);
    m_params.push_back(make_pair(paramName, paramDesc));

    m_lastTag = DocTagType::Param;
    return skipLineOrEOS(nlPos, _end);
}

string::const_iterator InterfaceHandler::appendDocTagParam(
    string::const_iterator _pos,
    string::const_iterator _end
)
{
    // Should never be called with an empty vector
    solAssert(!m_params.empty(), "Internal: Tried to append to empty parameter");

    auto pair = m_params.back();
    if (_pos < _end && *_pos != ' ')
        pair.second += " ";
    auto nlPos = find(_pos, _end, '\n');
    copy(_pos, nlPos, back_inserter(pair.second));

    m_params.at(m_params.size() - 1) = pair;

    return skipLineOrEOS(nlPos, _end);
}

string::const_iterator InterfaceHandler::parseDocTag(
    string::const_iterator _pos,
    string::const_iterator _end,
    string const& _tag,
    CommentOwner _owner
)
{
    // LTODO: need to check for @(start of a tag) between here and the end of line
    // for all cases. Also somehow automate list of acceptable tags for each
    // language construct since current way does not scale well.
    if (m_lastTag == DocTagType::None || _tag != "")
    {
        if (_tag == "dev")
            return parseDocTagLine(_pos, _end, m_dev, DocTagType::Dev, false);
        else if (_tag == "notice")
            return parseDocTagLine(_pos, _end, m_notice, DocTagType::Notice, false);
        else if (_tag == "return")
            return parseDocTagLine(_pos, _end, m_return, DocTagType::Return, false);
        else if (_tag == "author")
        {
            if (_owner == CommentOwner::Contract)
                return parseDocTagLine(_pos, _end, m_contractAuthor, DocTagType::Author, false);
            else if (_owner == CommentOwner::Function)
                return parseDocTagLine(_pos, _end, m_author, DocTagType::Author, false);
            else
                // LTODO: for now this else makes no sense but later comments will go to more language constructs
                BOOST_THROW_EXCEPTION(DocstringParsingError() << errinfo_comment("@author tag is legal only for contracts"));
        }
        else if (_tag == "title")
        {
            if (_owner == CommentOwner::Contract)
                return parseDocTagLine(_pos, _end, m_title, DocTagType::Title, false);
            else
                // LTODO: Unknown tag, throw some form of warning and not just an exception
                BOOST_THROW_EXCEPTION(DocstringParsingError() << errinfo_comment("@title tag is legal only for contracts"));
        }
        else if (_tag == "param")
            return parseDocTagParam(_pos, _end);
        else
            // LTODO: Unknown tag, throw some form of warning and not just an exception
            BOOST_THROW_EXCEPTION(DocstringParsingError() << errinfo_comment("Unknown tag " + _tag + " encountered"));
    }
    else
        return appendDocTag(_pos, _end, _owner);
}

string::const_iterator InterfaceHandler::appendDocTag(
    string::const_iterator _pos,
    string::const_iterator _end,
    CommentOwner _owner
)
{
    switch (m_lastTag)
    {
    case DocTagType::Dev:
        return parseDocTagLine(_pos, _end, m_dev, DocTagType::Dev, true);
    case DocTagType::Notice:
        return parseDocTagLine(_pos, _end, m_notice, DocTagType::Notice, true);
    case DocTagType::Return:
        return parseDocTagLine(_pos, _end, m_return, DocTagType::Return, true);
    case DocTagType::Author:
        if (_owner == CommentOwner::Contract)
            return parseDocTagLine(_pos, _end, m_contractAuthor, DocTagType::Author, true);
        else if (_owner == CommentOwner::Function)
            return parseDocTagLine(_pos, _end, m_author, DocTagType::Author, true);
        else
            // LTODO: Unknown tag, throw some form of warning and not just an exception
            BOOST_THROW_EXCEPTION(DocstringParsingError() << errinfo_comment("@author tag in illegal comment"));
    case DocTagType::Title:
        if (_owner == CommentOwner::Contract)
            return parseDocTagLine(_pos, _end, m_title, DocTagType::Title, true);
        else
            // LTODO: Unknown tag, throw some form of warning and not just an exception
            BOOST_THROW_EXCEPTION(DocstringParsingError() << errinfo_comment("@title tag in illegal comment"));
    case DocTagType::Param:
        return appendDocTagParam(_pos, _end);
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Internal: Illegal documentation tag type"));
        break;
    }
}

static inline string::const_iterator firstSpaceOrNl(
    string::const_iterator _pos,
    string::const_iterator _end
)
{
    auto spacePos = find(_pos, _end, ' ');
    auto nlPos = find(_pos, _end, '\n');
    return (spacePos < nlPos) ? spacePos : nlPos;
}

void InterfaceHandler::parseDocString(string const& _string, CommentOwner _owner)
{
    auto currPos = _string.begin();
    auto end = _string.end();

    while (currPos != end)
    {
        auto tagPos = find(currPos, end, '@');
        auto nlPos = find(currPos, end, '\n');

        if (tagPos != end && tagPos < nlPos)
        {
            // we found a tag
            auto tagNameEndPos = firstSpaceOrNl(tagPos, end);
            if (tagNameEndPos == end)
                BOOST_THROW_EXCEPTION(
                    DocstringParsingError() <<
                    errinfo_comment("End of tag " + string(tagPos, tagNameEndPos) + "not found"));

            currPos = parseDocTag(tagNameEndPos + 1, end, string(tagPos + 1, tagNameEndPos), _owner);
        }
        else if (m_lastTag != DocTagType::None) // continuation of the previous tag
            currPos = appendDocTag(currPos, end, _owner);
        else if (currPos != end)
        {
            // if it begins without a tag then consider it as @notice
            if (currPos == _string.begin())
            {
                currPos = parseDocTag(currPos, end, "notice", CommentOwner::Function);
                continue;
            }
            else if (nlPos == end) //end of text
                return;
            // else skip the line if a newline was found and we get here
            currPos = nlPos + 1;
        }
    }
}

} //solidity NS
} // dev NS