COMMON BLOCK Does Not Support Real(kind=x)
COMMON BLOCK does not support real(kind=x)
In Fortran programming, common blocks are used to share variables between different routines. However, when it comes to real numbers with a specific kind, the common block interface does not support it. This article will explore this limitation and provide insights into how to handle it.
Common blocks are a way to share variables between different routines in Fortran programming. They are defined using the COMMON
statement, which specifies the variables to be shared and the name of the common block. The variables in a common block are stored in a contiguous block of memory, and all routines that use the common block must access the variables through the common block interface.
When it comes to real numbers with a specific kind, the common block interface does not support it. This is because the common block interface is designed to work with variables of a specific type, and real numbers with a specific kind are not considered to be of that type.
To test this limitation, we can use a unit test to verify that the common block interface does not support real numbers with a specific kind. We can create a dummy test routine and use the Fparser2Reader
to process the declarations. We can then use the symtab
to lookup the variables and verify that they have the correct interface.
@pytest.mark.usefixtures("f2008_parser")
def test_named_common_block():
''' Test that named common blocks are correctly captured and the symbols
they reference have a CommonBlockInterface. '''
# Create a dummy test routine
routine = Routine.create("test_routine")
symtab = routine.symbol_table
processor = Fparser2Reader()
# Test with a single common block
reader = FortranStringReader('''
integer :: a, b, c
common /name1/ a, b, c''')
fparser2spec = Specification_Part(reader)
processor.process_declarations(routine, fparser2spec.content, [])
# There is a name1 commonblock symbol
commonblock = symtab.lookup("_PSYCLONE_INTERNAL_COMMONBLOCK")
assert isinstance(commonblock.datatype, UnsupportedFortranType)
assert commonblock.datatype.declaration == "COMMON /name1/ a, b, c"
# The variables have been updated to a common block interface
assert isinstance(symtab.lookup("a").interface, CommonBlockInterface)
assert isinstance(symtab.lookup("b").interface, CommonBlockInterface)
assert isinstance(symtab.lookup("c").interface, CommonBlockInterface)
# The same common block can also bring other variables in a separate
# statement
reader = FortranStringReader("""
real :: d, e
real(kind=8) :: f
common /name1/ d, e, f""")
fparser2spec = Specification_Part(reader)
processor.process_declarations(routine, fparser2spec.content, [])
# This is stored in a separate symbol, but the declaration has the right
# text
commonblock_2 = symtab.lookup("_PSYCLONE_INTERNAL_COMMONBLOCK_1")
assert commonblock_2.datatype.declaration == "COMMON /name1/ d, e, f"
assert isinstance(symtab.lookup("d").interface, CommonBlockInterface)
esym: DataSymbol = symtab.lookup("e")
assert isinstance(esym.interface, CommonBlockInterface)
assert not isinstance(esym.datatype, UnsupportedFortranType)
fsym: DataSymbol = symtab.lookup("f")
assert isinstance(fsym.interface, CommonBlockInterface)
assert not isinstance(fsym.datatype, UnsupportedFortranType)
The test code is failing because the common block interface does not support real numbers with a specific kind. The assert
statement is failing because the datatype
of the esym
and fsym
variables is not an instance of UnsupportedFortranType
.
In conclusion, the common block interface in Fortran programming does not support real numbers with a specific kind. This is a limitation of the common block interface, and it can cause issues when working with real numbers with a specific kind. To handle this limitation, we can use a workaround such as using a different type of variable or using a different programming language.
One possible workaround is to use a different type of variable, such as a real
variable without a specific kind. This can be done by changing the declaration of the d
, e
, and f
variables to use a real
type without a specific kind.
real :: d, e
real :: f
common /name1/ d, e, f
This code will work with the common block interface, but it will not support real numbers with a specific kind.
In the future, it would be beneficial to add support for real numbers with a specific kind to the common block interface. This can be done by modifying the common block interface to support real numbers with a specific kind. This would require changes to the CommonBlockInterface
class and the symtab
lookup function.
- Fortran 2008 Standard
- Fparser2Reader documentation
- Psyclone documentation
COMMON BLOCK does not support real(kind=x) - Q&A
In our previous article, we discussed the limitation of the common block interface in Fortran programming, specifically that it does not support real numbers with a specific kind. In this article, we will provide a Q&A section to address some of the common questions and concerns related to this limitation.
A: The common block interface is a way to share variables between different routines in Fortran programming. It is defined using the COMMON
statement, which specifies the variables to be shared and the name of the common block.
A: The common block interface is designed to work with variables of a specific type, and real numbers with a specific kind are not considered to be of that type. This is a limitation of the common block interface, and it can cause issues when working with real numbers with a specific kind.
A: One possible workaround is to use a different type of variable, such as a real
variable without a specific kind. Another workaround is to use a different programming language that supports real numbers with a specific kind.
A: Yes, it is possible to modify the common block interface to support real numbers with a specific kind. This would require changes to the CommonBlockInterface
class and the symtab
lookup function.
A: Some best practices for working with common blocks include:
- Using a consistent naming convention for common blocks
- Avoiding the use of common blocks with a large number of variables
- Using a different type of variable, such as a
real
variable without a specific kind, when working with real numbers with a specific kind
A: Some common pitfalls to avoid when working with common blocks include:
- Not initializing variables before using them in a common block
- Not checking for the existence of a common block before using it
- Not using a consistent naming convention for common blocks
A: You can find more information about common blocks in the Fortran 2008 standard, as well as in the documentation for the Fparser2Reader and Psyclone tools.
In conclusion, the common block interface in Fortran programming does not support real numbers with a specific kind. However, there are workarounds available, and it is possible to modify the common block interface to support real numbers with a specific kind. By following best practices and avoiding common pitfalls, you can effectively use common blocks in your Fortran programming.