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 "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
|
|
|
|
|
|
|
|
|
#include "V3CUse.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2020-02-01 22:45:11 +01:00
|
|
|
#include "V3Ast.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
#include "V3Global.h"
|
2020-02-01 22:45:11 +01:00
|
|
|
|
2021-07-20 20:52:15 +02:00
|
|
|
#include <set>
|
2020-02-01 22:45:11 +01:00
|
|
|
|
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
|
|
|
|
|
bool m_impOnly = false; // In details needed only for implementation
|
|
|
|
|
AstNodeModule* const m_modp; // Current module
|
|
|
|
|
std::set<std::pair<VUseType, std::string>> 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) {
|
|
|
|
|
if (m_didUse.emplace(useType, name).second) {
|
2021-07-12 00:42:01 +02:00
|
|
|
AstCUse* const newp = new AstCUse{nodep->fileline(), useType, name};
|
2022-09-15 20:43:56 +02:00
|
|
|
m_modp->addStmtsp(newp);
|
2020-02-01 22:45:11 +01:00
|
|
|
UINFO(8, "Insert " << newp << endl);
|
|
|
|
|
}
|
|
|
|
|
}
|
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 {
|
2021-07-20 20:52:15 +02:00
|
|
|
if (nodep->user1SetOnce()) return; // Process once
|
|
|
|
|
if (!m_impOnly) addNewUse(nodep, VUseType::INT_FWD_CLASS, nodep->classp()->name());
|
2020-04-05 15:30:23 +02:00
|
|
|
// Need to include extends() when we implement, but no need for pointers to know
|
2020-08-25 03:10:43 +02:00
|
|
|
VL_RESTORER(m_impOnly);
|
2020-04-05 15:30:23 +02:00
|
|
|
{
|
|
|
|
|
m_impOnly = true;
|
|
|
|
|
iterateChildren(nodep->classp()); // This also gets all extend classes
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeDType* nodep) override {
|
2021-07-20 20:52:15 +02:00
|
|
|
if (nodep->user1SetOnce()) return; // Process once
|
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);
|
|
|
|
|
}
|
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
|
|
|
|
|
if (nodep->dtypep() && !nodep->dtypep()->user1()) 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);
|
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
|
|
|
}
|