3131
3232namespace
3333{
34- const QRegularExpression TYPE_RULE (" (\\ w+) \\ s+(?:(" + VerilogSyntax::RANGE + " )?\\ s*(" +
34+ const QRegularExpression TYPE_RULE (" (?:localparam|parameter)? \\ s*((?: \\ w|:)+ \\ s+)?((?: \\ w|:)+)? \\ s* (?:(" + VerilogSyntax::RANGE + " )?\\ s*(" +
3535 VerilogSyntax::RANGE + " ))?\\ s*" + VerilogSyntax::NAME_VALUE , QRegularExpression::CaseInsensitiveOption);
3636}
3737
@@ -63,12 +63,19 @@ void VerilogParameterParser::setHighlighter(Highlighter* highlighter)
6363void VerilogParameterParser::import (QString const & componentDeclaration, QSharedPointer<Component> targetComponent,
6464 QSharedPointer<ComponentInstantiation> targetComponentInstantiation)
6565{
66- QStringList declarations = findDeclarations (componentDeclaration);
66+ auto declarations = findDeclarations (componentDeclaration);
6767
68- QList<QSharedPointer<ModuleParameter> > parsedParameters;
69- for (QString const & declaration : declarations)
68+ QList<QPair< QSharedPointer<ModuleParameter>, QString> > parsedParameters; // For mapping parameter type (localparam/parameter) to parsed parameter
69+ for (auto const & declaration : declarations)
7070 {
71- parsedParameters.append (parseParameters (declaration));
71+ auto params = parseParameters (declaration.value_ );
72+
73+ // In most cases size of params should be 1
74+ std::for_each (params.begin (), params.end (),
75+ [&declaration, &parsedParameters](QSharedPointer<ModuleParameter> moduleParam)
76+ {
77+ parsedParameters.append (std::make_pair (moduleParam, declaration.type_ ));
78+ });
7279 }
7380
7481 if (targetComponentInstantiation.isNull () == false )
@@ -90,7 +97,11 @@ void VerilogParameterParser::import(QString const& componentDeclaration, QShared
9097 }
9198 }
9299
93- targetComponentInstantiation->getModuleParameters ()->append (parsedParameters);
100+ std::for_each (parsedParameters.cbegin (), parsedParameters.cend (),
101+ [&targetComponentInstantiation](auto const & pair)
102+ {
103+ targetComponentInstantiation->getModuleParameters ()->append (pair.first );
104+ });
94105 }
95106
96107 replaceNamesReferencesWithIds (parsedParameters, targetComponent, targetComponentInstantiation);
@@ -99,7 +110,8 @@ void VerilogParameterParser::import(QString const& componentDeclaration, QShared
99110// -----------------------------------------------------------------------------
100111// Function: VerilogParameterParser::findDeclarations()
101112// -----------------------------------------------------------------------------
102- QStringList VerilogParameterParser::findDeclarations (QString const & input)
113+ // QStringList VerilogParameterParser::findDeclarations(QString const& input)
114+ QList<VerilogParameterParser::ParameterDeclaration> VerilogParameterParser::findDeclarations (QString const & input)
103115{
104116 return findParameterDeclarations (input, findParameterSection (input));
105117}
@@ -146,6 +158,15 @@ QList<QSharedPointer<ModuleParameter> > VerilogParameterParser::parseParameters(
146158
147159 // Find the type and the declaration. Only one per declaration is supported.
148160 QString type = parseType (input);
161+ QString typeWithModifier = type;
162+ QString signingModifier = TYPE_RULE .match (input).captured (2 );
163+
164+ // Create complete type if there is a sign modifier
165+ if (QRegularExpression (" (signed|unsigned)" ).match (signingModifier.toLower ()).hasMatch ())
166+ {
167+ typeWithModifier.append (QStringLiteral (" " ) % signingModifier); // e.g. int unsigned
168+ }
169+
149170 QString bitWidthLeft = parseBitWidthLeft (input);
150171 QString bitWidthRight = parseBitWidthRight (input);
151172 QString arrayLeft = parseArrayLeft (input);
@@ -156,7 +177,7 @@ QList<QSharedPointer<ModuleParameter> > VerilogParameterParser::parseParameters(
156177 inputWithoutComments.remove (QRegularExpression (VerilogSyntax::COMMENT ));
157178
158179 QString parameterDefinition = " (" + VerilogSyntax::NAMES + " )\\ s*=((\\ s*(" +
159- VerilogSyntax::OPERATION_OR_ALPHANUMERIC + " )+\\ s*)+)" ;
180+ VerilogSyntax::PARAMETER_VALUE + " )+\\ s*)+)" ;
160181
161182 QRegularExpression parameterRule (parameterDefinition + " (\\ s*,\\ s*" + parameterDefinition + " )*" ,
162183 QRegularExpression::CaseInsensitiveOption);
@@ -177,7 +198,7 @@ QList<QSharedPointer<ModuleParameter> > VerilogParameterParser::parseParameters(
177198 // Each name value pair produces a new module parameter, but the type and the description is recycled.
178199 QSharedPointer<ModuleParameter> moduleParameter = QSharedPointer<ModuleParameter>(new ModuleParameter ());
179200 moduleParameter->setName (name);
180- moduleParameter->setDataType (type );
201+ moduleParameter->setDataType (typeWithModifier );
181202 moduleParameter->setType (createTypeFromDataType (type));
182203 moduleParameter->setValue (value);
183204 moduleParameter->setUsageType (" nontyped" );
@@ -211,39 +232,41 @@ QString VerilogParameterParser::createTypeFromDataType(QString const& dataType)
211232// -----------------------------------------------------------------------------
212233// Function: VerilogParameterParser::findDeclarations()
213234// -----------------------------------------------------------------------------
214- QStringList VerilogParameterParser::findParameterDeclarations (QString const & componentDeclaration,
235+ // QStringList VerilogParameterParser::findParameterDeclarations(QString const& componentDeclaration,
236+ QList<VerilogParameterParser::ParameterDeclaration > VerilogParameterParser::findParameterDeclarations (QString const & componentDeclaration,
215237 QString const & parameterArea)
216238{
217239 // List of detected parameter declarations.
218- QStringList declarations;
240+ QList<ParameterDeclaration> declarations;
219241
220242 // Rule used to detect parameter declarations.
221- QRegularExpression declarationRule (" \\ bparameter \\ s+.*(?:"
243+ QRegularExpression declarationRule (" \\ b(localparam|parameter) \\ s+.*(?:"
222244 " (;[ \\ t]*" + VerilogSyntax::COMMENT + " )|"
223245 " (;|$)|"
224- " (,([ \\ t]*" + VerilogSyntax::COMMENT +" )?(?=\\ s*\\ bparameter \\ b)))" ,
246+ " (,([ \\ t]*" + VerilogSyntax::COMMENT +" )?(?=\\ s*\\ b(?:localparam|parameter) \\ b)))" ,
225247 QRegularExpression::CaseInsensitiveOption | QRegularExpression::InvertedGreedinessOption |
226248 QRegularExpression::DotMatchesEverythingOption);
227-
249+
228250 QRegularExpression commentBegin (QStringLiteral (" //" ));
229251 QRegularExpression lineBegin (QStringLiteral (" ^|\\ r?\\ n" ));
230252
231253 QRegularExpressionMatchIterator iter = declarationRule.globalMatch (parameterArea);
232254 while (iter.hasNext ())
233255 {
234256 QRegularExpressionMatch match = iter.next ();
235- QString declaration = match.captured ();
257+ auto fullDeclaration = match.captured ();
258+ auto paramType = match.captured (1 ); // get the param type: localparam or parameter
236259 int declarationBegin = match.capturedStart ();
237260
238261 // Check keyword parameter is not inside a comment.
239262 if (parameterArea.lastIndexOf (lineBegin, declarationBegin) > parameterArea.lastIndexOf (commentBegin, declarationBegin))
240263 {
241264 if (highlighter_)
242265 {
243- highlighter_->applyHighlight (declaration , KactusColors::Importer::MODELPARAMETER , componentDeclaration);
266+ highlighter_->applyHighlight (fullDeclaration , KactusColors::Importer::MODELPARAMETER , componentDeclaration);
244267 }
245268
246- declarations.append (declaration );
269+ declarations.append (ParameterDeclaration{paramType, fullDeclaration} );
247270 }
248271 }
249272
@@ -270,7 +293,7 @@ QString VerilogParameterParser::parseType(QString const& input)
270293// -----------------------------------------------------------------------------
271294QString VerilogParameterParser::parseBitWidthLeft (QString const & declaration)
272295{
273- QString bitRange = TYPE_RULE .match (declaration).captured (3 );
296+ QString bitRange = TYPE_RULE .match (declaration).captured (4 );
274297
275298 QRegularExpressionMatch rangeMatch = VerilogSyntax::CAPTURING_RANGE .match (bitRange);
276299 QString left = rangeMatch.captured (1 );
@@ -283,7 +306,7 @@ QString VerilogParameterParser::parseBitWidthLeft(QString const& declaration)
283306// -----------------------------------------------------------------------------
284307QString VerilogParameterParser::parseBitWidthRight (QString const & declaration)
285308{
286- QString bitRange = TYPE_RULE .match (declaration).captured (3 );
309+ QString bitRange = TYPE_RULE .match (declaration).captured (4 );
287310
288311 QRegularExpressionMatch rangeMatch = VerilogSyntax::CAPTURING_RANGE .match (bitRange);
289312 QString right = rangeMatch.captured (2 );
@@ -296,7 +319,7 @@ QString VerilogParameterParser::parseBitWidthRight(QString const& declaration)
296319// -----------------------------------------------------------------------------
297320QString VerilogParameterParser::parseArrayLeft (QString const & declaration)
298321{
299- QString bitRange = TYPE_RULE .match (declaration).captured (2 );
322+ QString bitRange = TYPE_RULE .match (declaration).captured (3 );
300323
301324 return VerilogSyntax::CAPTURING_RANGE .match (bitRange).captured (1 );
302325}
@@ -306,7 +329,7 @@ QString VerilogParameterParser::parseArrayLeft(QString const& declaration)
306329// -----------------------------------------------------------------------------
307330QString VerilogParameterParser::parseArrayRight (QString const & declaration)
308331{
309- QString bitRange = TYPE_RULE .match (declaration).captured (2 );
332+ QString bitRange = TYPE_RULE .match (declaration).captured (3 );
310333
311334 return VerilogSyntax::CAPTURING_RANGE .match (bitRange).captured (2 );
312335}
@@ -345,10 +368,10 @@ QString VerilogParameterParser::parseDescription(QString const& input)
345368// -----------------------------------------------------------------------------
346369// Function: VerilogParameterParser::copyIdsFromOldModelParameters()
347370// -----------------------------------------------------------------------------
348- void VerilogParameterParser::copyIdsFromOldModelParameters (QList<QSharedPointer<ModuleParameter> > parsedParameters,
371+ void VerilogParameterParser::copyIdsFromOldModelParameters (QList<QPair< QSharedPointer<ModuleParameter>, QString > > parsedParameters,
349372 QSharedPointer<ComponentInstantiation> targetComponentInstantiation)
350373{
351- for (QSharedPointer<ModuleParameter> parameter : parsedParameters)
374+ for (auto const & [ parameter, type] : parsedParameters)
352375 {
353376 for (QSharedPointer<ModuleParameter> existingParameter :
354377 *targetComponentInstantiation->getModuleParameters ())
@@ -366,14 +389,14 @@ void VerilogParameterParser::copyIdsFromOldModelParameters(QList<QSharedPointer<
366389// Function: VerilogParameterParser::replaceReferenceNamesWithIds()
367390// -----------------------------------------------------------------------------
368391void VerilogParameterParser::replaceNamesReferencesWithIds (
369- QList<QSharedPointer<ModuleParameter> > parsedParameters,
392+ QList<QPair< QSharedPointer<ModuleParameter>, QString> > const & parsedParameters,
370393 QSharedPointer<Component> targetComponent,
371394 QSharedPointer<ComponentInstantiation> targetComponentInstantiation)
372395{
373- for (QSharedPointer<ModuleParameter> moduleParameter : parsedParameters)
396+ for (auto const & [ moduleParameter, type] : parsedParameters)
374397 {
375398 QSharedPointer<Parameter> targetParameter =
376- Search::findByName (moduleParameter->name (), *targetComponent->getParameters ());
399+ Search::findByName (moduleParameter->name (), *targetComponent->getParameters ());
377400
378401 if (targetParameter.isNull ())
379402 {
@@ -391,6 +414,12 @@ void VerilogParameterParser::replaceNamesReferencesWithIds(
391414 targetParameter->setDescription (moduleParameter->description ());
392415 targetParameter->increaseUsageCount ();
393416
417+ if (type.compare (QStringLiteral (" localparam" )) == 0 )
418+ {
419+ targetParameter->setValueResolve (QStringLiteral (" immediate" ));
420+ moduleParameter->setValueResolve (QStringLiteral (" immediate" ));
421+ }
422+
394423 moduleParameter->setValue (targetParameter->getValueId ());
395424 }
396425
@@ -416,7 +445,7 @@ void VerilogParameterParser::replaceNameReferencesWithParameterIds(QSharedPointe
416445{
417446 foreach (QSharedPointer<Parameter> define, *targetComponent->getParameters ())
418447 {
419- QRegularExpression macroUsage (" `?" + define->name () + " \\ b " );
448+ QRegularExpression macroUsage (" `?" + define->name () + " (?![ \\ ._]) " ); // don't match systemverilog struct field accesses, e.g MyStruct.field, and other things that can cause partial match
420449
421450 QString parameterValue = replaceNameWithId (parameter->getValue (), macroUsage, define);
422451 parameter->setValue (parameterValue);
0 commit comments