aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/intel/common/ff_map.v
diff options
context:
space:
mode:
authorDag Lem <dag@nimrod.no>2022-11-12 08:48:25 +0100
committerDag Lem <dag@nimrod.no>2022-11-12 08:48:25 +0100
commitbab88630c28b43851162772c2ce35666357d8933 (patch)
tree96e2dc4f9c1e552281b4f2b8260ced1783f856d0 /techlibs/intel/common/ff_map.v
parenta217450524e21222d8d32bd448f1ea2291685258 (diff)
downloadyosys-bab88630c28b43851162772c2ce35666357d8933.tar.gz
yosys-bab88630c28b43851162772c2ce35666357d8933.tar.bz2
yosys-bab88630c28b43851162772c2ce35666357d8933.zip
Support for arrays with swapped ranges within structs
This also corrects the implementation of C type arrays within structs. Fixes #3550
Diffstat (limited to 'techlibs/intel/common/ff_map.v')
0 files changed, 0 insertions, 0 deletions
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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
import logging
import os
import json
from ctypes import byref
import libghdl
import libghdl.thin.errorout_memory as errorout_memory
import libghdl.thin.flags
import libghdl.thin.errorout as errorout
import libghdl.thin.files_map as files_map
import libghdl.thin.libraries as libraries
import libghdl.thin.name_table as name_table
import libghdl.thin.vhdl.nodes as nodes
import libghdl.thin.vhdl.lists as lists
import libghdl.thin.vhdl.std_package as std_package
import libghdl.thin.vhdl.parse
import libghdl.thin.vhdl.pyutils as pyutils
import libghdl.thin.vhdl.sem_lib as sem_lib

from . import lsp
from . import document, symbols

log = logging.getLogger(__name__)

class ProjectError(Exception):
    "Exception raised in case of unrecoverable error in the project file."
    def __init__(self, msg):
        super().__init__()
        self.msg = msg

class Workspace(object):
    def __init__(self, root_uri, server):
        self._root_uri = root_uri
        self._server = server
        self._root_path = lsp.path_from_uri(self._root_uri)
        self._docs = {}     # uri -> doc
        self._fe_map = {}   # fe -> doc
        self._prj = {}
        self._last_linted_doc = None
        errorout_memory.Install_Handler()
        libghdl.thin.flags.Flag_Elocations.value = True
        #thin.Flags.Verbose.value = True
        # We do analysis even in case of errors.
        libghdl.thin.vhdl.parse.Flag_Parse_Parenthesis.value = True
        # Force analysis to get more feedback + navigation even in case
        # of errors.
        libghdl.thin.flags.Flag_Force_Analysis.value = True
        # Do not consider analysis order issues.
        libghdl.thin.flags.Flag_Elaborate_With_Outdated.value = True
        libghdl.thin.errorout.Enable_Warning(errorout.Msgid.Warnid_Unused, True)
        self.read_project()
        self.set_options_from_project()
        libghdl.analyze_init()
        self._diags_set = set() # Documents with at least one diagnostic.
        self.read_files_from_project()
        self.gather_diagnostics(None)

    @property
    def documents(self):
        return self._docs

    @property
    def root_path(self):
        return self._root_path

    @property
    def root_uri(self):
        return self._root_uri

    def _create_document(self, doc_uri, sfe, version=None):
        """Create a document and put it in this workspace."""
        doc = document.Document(doc_uri, sfe, version)
        self._docs[doc_uri] = doc
        self._fe_map[sfe] = doc
        return doc

    def create_document_from_sfe(self, sfe, abspath):
        # A filename has been given without a corresponding document.
        # Create the document.
        # Common case: an error message was reported in a non-open document.
        #  Create a document so that it could be reported to the client.
        doc_uri = lsp.path_to_uri(os.path.normpath(abspath))
        return self._create_document(doc_uri, sfe)

    def create_document_from_uri(self, doc_uri, source=None, version=None):
        # A document is referenced by an uri but not known.  Load it.
        # We assume the path is correct.
        path = lsp.path_from_uri(doc_uri)
        if source is None:
            source = open(path).read()
        sfe = document.Document.load(source, os.path.dirname(path), os.path.basename(path))
        return self._create_document(doc_uri, sfe)

    def get_or_create_document(self, doc_uri):
        res = self.get_document(doc_uri)
        if res is not None:
            return res
        res = self.create_document_from_uri(doc_uri)
        res.parse_document()
        return res

    def get_document(self, doc_uri):
        """Get a document from :param doc_uri:  Note that the document may not exist,
        and this function may return None."""
        return self._docs.get(doc_uri)

    def put_document(self, doc_uri, source, version=None):
        doc = self.get_document(doc_uri)
        if doc is None:
            doc = self.create_document_from_uri(doc_uri, source=source, version=version)
        else:
            # The document may already be present (loaded from a project)
            # In that case, overwrite it as the client may have a more
            # recent version.
            doc.reload(source)
        return doc

    def sfe_to_document(self, sfe):
        """Get the document correspond to :param sfe: source file.
        Can create the document if needed."""
        assert sfe != 0
        doc = self._fe_map.get(sfe, None)
        if doc is None:
            # Could be a document from outside...
            filename = pyutils.name_image(files_map.Get_File_Name(sfe))
            if not os.path.isabs(filename):
                dirname = pyutils.name_image(files_map.Get_Directory_Name(sfe))
                filename = os.path.join(dirname, filename)
            doc = self.create_document_from_sfe(sfe, filename)
        return doc

    def add_vhdl_file(self, name):
        log.info("loading %s", name)
        if os.path.isabs(name):
            absname = name
        else:
            absname = os.path.join(self._root_path, name)
        # Create a document for this file.
        try:
            fd = open(absname)
            sfe = document.Document.load(fd.read(), self._root_path, name)
            fd.close()
        except OSError as err:
            self._server.show_message(
                lsp.MessageType.Error,
                "cannot load {}: {}".format(name, err.strerror))
            return
        doc = self.create_document_from_sfe(sfe, absname)
        doc.parse_document()

    def read_project(self):
        prj_file = os.path.join(self.root_path, 'hdl-prj.json')
        if not os.path.exists(prj_file):
            log.info("project file %s does not exist", prj_file)
            return
        try:
            f = open(prj_file)
        except OSError as err:
            self._server.show_message(
                lsp.MessageType.Error,
                "cannot open project file {}: {}".format(prj_file, err.strerror))
            return
        log.info("reading project file %s", prj_file)
        try:
            self._prj = json.load(f)
        except json.decoder.JSONDecodeError as e:
            log.info("error in project file")
            self._server.show_message(
                lsp.MessageType.Error,
                "json error in project file {}:{}:{}".format(
                    prj_file, e.lineno, e.colno))