2020-02-01 22:45:11 +01:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Handle SV classes
|
|
|
|
|
//
|
|
|
|
|
// Code available from: https://verilator.org
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2023-01-01 16:18:39 +01:00
|
|
|
// Copyright 2003-2023 by Wilson Snyder. This program is free software; you
|
2020-03-21 16:24:24 +01:00
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
2020-02-01 22:45:11 +01:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2020-03-21 16:24:24 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2020-02-01 22:45:11 +01:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2021-07-20 20:52:15 +02:00
|
|
|
// V3CUse's Transformations:
|
2020-02-01 22:45:11 +01:00
|
|
|
//
|
2020-02-02 01:11:19 +01:00
|
|
|
// Each module:
|
|
|
|
|
// Each cell:
|
|
|
|
|
// Create CUse for cell forward declaration
|
2021-07-20 20:52:15 +02:00
|
|
|
// Search for dtypes referencing class, and create CUse for forward declaration
|
2020-02-01 22:45:11 +01:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
|
|
#include "V3CUse.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2023-10-18 04:50:27 +02:00
|
|
|
#include "V3PchAstNoMT.h" // VL_MT_DISABLED_CODE_UNIT
|
|
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2020-02-01 22:45:11 +01:00
|
|
|
//######################################################################
|
|
|
|
|
|
2021-07-20 20:52:15 +02:00
|
|
|
// Visit within a module all nodes and data types they reference, finding
|
|
|
|
|
// any classes so we can make sure they are defined when Verilated code
|
|
|
|
|
// compiles
|
2022-01-02 19:56:40 +01:00
|
|
|
class CUseVisitor final : public VNVisitor {
|
2020-02-01 22:45:11 +01:00
|
|
|
// NODE STATE
|
2021-07-20 20:52:15 +02:00
|
|
|
// AstNode::user1() -> bool. True if already visited
|
2022-01-02 19:56:40 +01:00
|
|
|
const VNUser1InUse m_inuser1;
|
2020-02-01 22:45:11 +01:00
|
|
|
|
2021-07-20 20:52:15 +02:00
|
|
|
// MEMBERS
|
|
|
|
|
AstNodeModule* const m_modp; // Current module
|
2023-09-12 17:59:57 +02:00
|
|
|
std::map<std::string, std::pair<FileLine*, VUseType>> m_didUse; // What we already used
|
2020-02-01 22:45:11 +01:00
|
|
|
|
2021-07-20 20:52:15 +02:00
|
|
|
// METHODS
|
|
|
|
|
void addNewUse(AstNode* nodep, VUseType useType, const string& name) {
|
2023-09-12 17:59:57 +02:00
|
|
|
auto e = m_didUse.emplace(name, std::make_pair(nodep->fileline(), useType));
|
|
|
|
|
if (e.second || ((e.first->second.second & useType) != useType)) {
|
|
|
|
|
e.first->second.second = e.first->second.second | useType;
|
2020-02-01 22:45:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-02 01:11:19 +01:00
|
|
|
|
2021-07-20 20:52:15 +02:00
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstClassRefDType* nodep) override {
|
2023-05-22 14:29:01 +02:00
|
|
|
addNewUse(nodep, VUseType::INT_FWD_CLASS, nodep->classp()->name());
|
|
|
|
|
}
|
|
|
|
|
void visit(AstCFunc* nodep) override {
|
2023-09-12 17:59:57 +02:00
|
|
|
if (nodep->user1SetOnce()) return;
|
2023-05-22 14:29:01 +02:00
|
|
|
iterateAndNextNull(nodep->argsp());
|
2023-09-12 17:59:57 +02:00
|
|
|
iterateAndNextNull(nodep->stmtsp());
|
2023-05-22 14:29:01 +02:00
|
|
|
}
|
2023-09-12 17:59:57 +02:00
|
|
|
void visit(AstCCall* nodep) override { return; }
|
2023-05-22 14:29:01 +02:00
|
|
|
void visit(AstCReturn* nodep) override {
|
2023-09-12 17:59:57 +02:00
|
|
|
UASSERT(!nodep->user1SetOnce(), "Visited same return twice.");
|
|
|
|
|
iterate(nodep->lhsp()->dtypep());
|
2020-04-05 15:30:23 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeDType* nodep) override {
|
2020-04-05 15:30:23 +02:00
|
|
|
if (nodep->virtRefDTypep()) iterate(nodep->virtRefDTypep());
|
|
|
|
|
if (nodep->virtRefDType2p()) iterate(nodep->virtRefDType2p());
|
2022-12-21 01:22:42 +01:00
|
|
|
|
|
|
|
|
// Add a CUse for every struct that requires a declaration
|
2023-01-28 04:41:12 +01:00
|
|
|
AstNodeUOrStructDType* const stypep = VN_CAST(nodep->skipRefp(), NodeUOrStructDType);
|
2022-12-21 01:22:42 +01:00
|
|
|
if (stypep && stypep->classOrPackagep()) {
|
|
|
|
|
addNewUse(nodep, VUseType::INT_INCLUDE, stypep->classOrPackagep()->name());
|
|
|
|
|
iterateChildren(stypep);
|
2023-05-22 14:29:01 +02:00
|
|
|
} else if (AstClassRefDType* const classp = VN_CAST(nodep->skipRefp(), ClassRefDType)) {
|
|
|
|
|
addNewUse(nodep, VUseType::INT_FWD_CLASS, classp->name());
|
2022-12-21 01:22:42 +01:00
|
|
|
}
|
2020-04-05 15:30:23 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNode* nodep) override {
|
2021-07-20 20:52:15 +02:00
|
|
|
if (nodep->user1SetOnce()) return; // Process once
|
2023-09-12 17:59:57 +02:00
|
|
|
if (nodep->dtypep()) iterate(nodep->dtypep());
|
2020-04-05 15:30:23 +02:00
|
|
|
iterateChildren(nodep);
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCell* nodep) override {
|
2021-07-20 20:52:15 +02:00
|
|
|
if (nodep->user1SetOnce()) return; // Process once
|
|
|
|
|
// Currently no IMP_INCLUDE because we include __Syms which has them all
|
|
|
|
|
addNewUse(nodep, VUseType::INT_FWD_CLASS, nodep->modp()->name());
|
|
|
|
|
iterateChildren(nodep);
|
2020-02-01 22:45:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CONSTRUCTORS
|
2021-07-20 20:52:15 +02:00
|
|
|
explicit CUseVisitor(AstNodeModule* modp)
|
|
|
|
|
: m_modp(modp) {
|
|
|
|
|
iterate(modp);
|
2023-09-12 17:59:57 +02:00
|
|
|
|
|
|
|
|
for (auto& used : m_didUse) {
|
|
|
|
|
AstCUse* const newp = new AstCUse{used.second.first, used.second.second, used.first};
|
|
|
|
|
m_modp->addStmtsp(newp);
|
|
|
|
|
UINFO(8, "Insert " << newp << endl);
|
|
|
|
|
}
|
2020-02-01 22:45:11 +01:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
~CUseVisitor() override = default;
|
2020-02-01 22:45:11 +01:00
|
|
|
VL_UNCOPYABLE(CUseVisitor);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Class class functions
|
|
|
|
|
|
2020-06-01 18:49:42 +02:00
|
|
|
void V3CUse::cUseAll() {
|
2020-02-01 22:45:11 +01:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2020-02-02 01:11:19 +01:00
|
|
|
// Call visitor separately for each module, so visitor state is cleared
|
2020-02-04 05:21:56 +01:00
|
|
|
for (AstNodeModule* modp = v3Global.rootp()->modulesp(); modp;
|
2021-10-22 14:56:48 +02:00
|
|
|
modp = VN_AS(modp->nextp(), NodeModule)) {
|
2020-02-02 01:11:19 +01:00
|
|
|
// Insert under this module; someday we should e.g. make Ast
|
|
|
|
|
// for each output file and put under that
|
2021-07-20 20:52:15 +02:00
|
|
|
CUseVisitor{modp};
|
2020-02-02 01:11:19 +01:00
|
|
|
}
|
2023-05-04 00:04:10 +02:00
|
|
|
V3Global::dumpCheckGlobalTree("cuse", 0, dumpTreeLevel() >= 3);
|
2020-02-01 22:45:11 +01:00
|
|
|
}
|