| 1 | // |
| 2 | // Parameter.cpp |
| 3 | // |
| 4 | // Library: CppParser |
| 5 | // Package: SymbolTable |
| 6 | // Module: Parameter |
| 7 | // |
| 8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/CppParser/Parameter.h" |
| 16 | #include "Poco/CppParser/NameSpace.h" |
| 17 | #include "Poco/CppParser/TypeDef.h" |
| 18 | #include "Poco/CppParser/Utility.h" |
| 19 | #include "Poco/String.h" |
| 20 | #include "Poco/NumberFormatter.h" |
| 21 | #include <cstddef> |
| 22 | |
| 23 | |
| 24 | namespace Poco { |
| 25 | namespace CppParser { |
| 26 | |
| 27 | |
| 28 | int Parameter::_count(0); |
| 29 | |
| 30 | |
| 31 | Parameter::Parameter(const std::string& decl, Function* /*pFunction*/): |
| 32 | Decl(handleDecl(decl), 0), // handle init values |
| 33 | _type(), |
| 34 | _isRef(false), |
| 35 | _isPointer(false), |
| 36 | _isConst(false) |
| 37 | { |
| 38 | std::size_t pos = declaration().rfind(name()); |
| 39 | std::string tmp; |
| 40 | if (pos == 0 && name().size() == declaration().size()) |
| 41 | tmp = declaration(); |
| 42 | else |
| 43 | tmp = declaration().substr(0, pos); |
| 44 | _type = Poco::trim(tmp); |
| 45 | std::size_t rightCut = _type.size(); |
| 46 | while (rightCut > 0 && (_type[rightCut-1] == '&' || _type[rightCut-1] == '*' || _type[rightCut-1] == '\t' || _type[rightCut-1] == ' ')) |
| 47 | { |
| 48 | if (_type[rightCut-1] == '&') |
| 49 | _isRef = true; |
| 50 | if (_type[rightCut-1] == '*') |
| 51 | _isPointer = true; |
| 52 | --rightCut; |
| 53 | } |
| 54 | _type = Poco::trim(_type.substr(0, rightCut)); |
| 55 | if (_type.find("const " ) == 0) |
| 56 | { |
| 57 | _isConst = true; |
| 58 | _type = _type.substr(6); |
| 59 | } |
| 60 | if (_type.find("const\t" ) == 0) |
| 61 | { |
| 62 | _type = _type.substr(6); |
| 63 | _isConst = true; |
| 64 | } |
| 65 | |
| 66 | Poco::trimInPlace(_type); |
| 67 | pos = decl.find("=" ); |
| 68 | _hasDefaultValue = (pos != std::string::npos); |
| 69 | if (_hasDefaultValue) |
| 70 | { |
| 71 | _defaultDecl = decl.substr(pos + 1); |
| 72 | Poco::trimInPlace(_defaultDecl); |
| 73 | std::size_t posStart = _defaultDecl.find("(" ); |
| 74 | std::size_t posEnd = _defaultDecl.rfind(")" ); |
| 75 | if (posStart != std::string::npos && posEnd != std::string::npos) |
| 76 | { |
| 77 | _defaultValue = _defaultDecl.substr(posStart + 1, posEnd-posStart - 1); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | poco_assert (posStart == std::string::npos && posEnd == std::string::npos); |
| 82 | _defaultValue = _defaultDecl; |
| 83 | } |
| 84 | Poco::trimInPlace(_defaultValue); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | Parameter::~Parameter() |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | |
| 94 | Symbol::Kind Parameter::kind() const |
| 95 | { |
| 96 | return Symbol::SYM_PARAMETER; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | bool Parameter::vectorType(const std::string& type, NameSpace* pNS) |
| 101 | { |
| 102 | bool ret = type.find("vector" ) != std::string::npos; |
| 103 | if (!ret) |
| 104 | { |
| 105 | Symbol* pSym = pNS->lookup(type); |
| 106 | if (pSym) |
| 107 | { |
| 108 | if (pSym->kind() == Symbol::SYM_TYPEDEF) |
| 109 | { |
| 110 | TypeDef* pType = static_cast<TypeDef*>(pSym); |
| 111 | ret = pType->declaration().find("vector" ) != std::string::npos; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | std::string Parameter::handleDecl(const std::string& decl) |
| 120 | { |
| 121 | std::size_t pos = decl.find('='); |
| 122 | std::string result(decl.substr(0, pos)); |
| 123 | // now check if we have to add a paramName |
| 124 | Poco::trimInPlace(result); |
| 125 | std::size_t posSpace = result.rfind(' '); |
| 126 | bool mustAdd = false; |
| 127 | if (posSpace>0) |
| 128 | { |
| 129 | std::string tmp(result.substr(posSpace+1)); |
| 130 | mustAdd = (tmp.find('<') != -1 || tmp.find('>') != -1 || tmp.find('*') != -1 || tmp.find('&') != -1); |
| 131 | } |
| 132 | else |
| 133 | mustAdd = true; |
| 134 | if (mustAdd) |
| 135 | { |
| 136 | result.append(" " ); |
| 137 | result.append("param" ); |
| 138 | result.append(Poco::NumberFormatter::format(++_count)); |
| 139 | //never add the default val it breaks the upper class parser |
| 140 | } |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | } } // namespace Poco::CppParser |
| 146 | |