LLDB mainline
TypeSystemFortran.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "TypeSystemFortran.h"
10#include "FortranTypes.h"
11
13
18#include "lldb/Target/Target.h"
19
20using namespace lldb;
21using namespace lldb_private;
22using namespace llvm;
23using namespace lldb_private::plugin::dwarf;
24using namespace lldb_private::plugin::fortran;
25
27
29
32
38
42
45 m_dwarf_ast_parser_up = std::make_unique<DWARFASTParserFortran>(*this);
46 return m_dwarf_ast_parser_up.get();
47}
48
50 Module *module, Target *target) {
51 if (Language::LanguageIsFortran(language))
52 return std::make_shared<TypeSystemFortran>();
53
54 return TypeSystemSP();
55}
56
67
71
72#ifndef NDEBUG
74 return !type || llvm::isa<FortranType>(static_cast<FortranType *>(type));
75}
76#endif
77
79 if (!type)
80 return false;
81
82 int kind = static_cast<FortranType *>(type)->GetKind();
84 return true;
85
86 return false;
87}
88
90 bool &is_signed) {
91 if (!type)
92 return false;
93
94 int kind = static_cast<FortranType *>(type)->GetKind();
96 is_signed = (kind == FortranType::KIND_INTEGER);
97 return true;
98 }
99
100 return false;
101}
102
106
107/// Returns the type name upper-cased to follow Fortran's general style.
109 bool base_only) {
110 if (!type)
111 return ConstString();
112
113 FortranType *fortran_type = static_cast<FortranType *>(type);
114 switch (fortran_type->GetKind()) {
120 return fortran_type->GetName();
122 return ConstString("Unsupported");
123 }
124 return ConstString("Unsupported");
125}
126
128TypeSystemFortran::CreateBaseType(llvm::dwarf::TypeKind dwarf_encoding,
129 uint64_t bitsize, ConstString name) {
130 int underlying_kind = FortranType::KIND_UNKNOWN;
131 const char *default_name = nullptr;
132
133 switch (dwarf_encoding) {
134 case dwarf::DW_ATE_boolean:
135 underlying_kind = FortranType::KIND_LOGICAL;
136 default_name = "LOGICAL";
137 break;
138 case dwarf::DW_ATE_float:
139 underlying_kind = FortranType::KIND_REAL;
140 default_name = "REAL";
141 break;
142 case dwarf::DW_ATE_signed:
143 case dwarf::DW_ATE_signed_char:
144 underlying_kind = FortranType::KIND_INTEGER;
145 default_name = "INTEGER";
146 break;
147 // Flang allows for unsigned types if the program is compiled with -funsigned.
148 case dwarf::DW_ATE_unsigned:
149 underlying_kind = FortranType::KIND_UNSIGNED;
150 default_name = "UNSIGNED";
151 break;
152 case dwarf::DW_ATE_complex_float:
153 underlying_kind = FortranType::KIND_COMPLEX;
154 default_name = "COMPLEX";
155 break;
156 default:
157 return CompilerType();
158 }
159 /// DWARFASTParserFortran provides the names it gets from DWARF, but in case
160 /// it doesn't we fallback to the default name.
161 if (name.IsEmpty() && default_name)
162 name.SetCString(default_name);
163
164 return GetOrCreateFortranBaseType(underlying_kind, bitsize, name);
165}
166
167/// Creates the type if it has not been created before, otherwise it gets it
168/// from the type map.
170 uint64_t bitsize,
171 ConstString name) {
172 FoldingSetNodeID id;
173 FortranType::Profile(id, kind, bitsize);
174 FoldingSetInsertToken token;
175 FortranType *candidate_type = m_basic_types.lookup(id, token);
176 if (candidate_type)
177 return CompilerType(weak_from_this(), (void *)candidate_type);
178
179 auto new_type_up = std::make_unique<FortranType>(kind, bitsize, name);
180 candidate_type = new_type_up.get();
181 m_basic_types.insert(candidate_type, token);
182 m_types.push_back(std::move(new_type_up));
183 return CompilerType(weak_from_this(), (void *)candidate_type);
184}
185
186lldb::TypeClass
188 return type ? lldb::eTypeClassBuiltin : lldb::eTypeClassInvalid;
189}
190
193 if (!type)
194 return CompilerType();
195 return CompilerType(weak_from_this(), type);
196}
197
198Expected<uint64_t>
200 ExecutionContextScope *exe_scope) {
201 if (!type)
202 return 0;
203 return static_cast<FortranType *>(type)->GetBitSize();
204}
205
208 if (!type)
209 return eBasicTypeInvalid;
210
211 FortranType *fortran_type = static_cast<FortranType *>(type);
212 switch (fortran_type->GetKind()) {
214 switch (fortran_type->GetBitSize()) {
215 case 8:
217 case 16:
218 return eBasicTypeShort;
219 case 32:
220 return eBasicTypeInt;
221 case 64:
222 return eBasicTypeLongLong;
223 case 128:
224 return eBasicTypeInt128;
225 default:
226 return eBasicTypeInvalid;
227 }
229 switch (fortran_type->GetBitSize()) {
230 case 8:
232 case 16:
234 case 32:
236 case 64:
238 case 128:
240 default:
241 return eBasicTypeInvalid;
242 }
244 return eBasicTypeBool;
246 switch (fortran_type->GetBitSize()) {
247 case 64:
249 case 128:
251 default:
252 return eBasicTypeInvalid;
253 }
255 switch (fortran_type->GetBitSize()) {
256 case 16:
257 return eBasicTypeHalf;
258 case 32:
259 return eBasicTypeFloat;
260 case 64:
261 return eBasicTypeDouble;
262 case 128:
263 return eBasicTypeFloat128;
264 default:
265 return eBasicTypeInvalid;
266 }
267 default:
268 return eBasicTypeInvalid;
269 }
270}
271
272// TODO: When a program is compiled with flags like -fdefault-integer-8 or
273// -fdefault-real-8, default KIND-less types have an 8-byte width instead of 4.
274// Consider whether the producer flags of the program should be queried to
275// determine the bitsize for eBasicTypeInt and eBasicTypeFloat.
277 switch (basic_type) {
278 case eBasicTypeInt:
280 ConstString("INTEGER"));
283 ConstString("UNSIGNED"));
284 case eBasicTypeFloat:
286 ConstString("REAL"));
287 case eBasicTypeDouble:
289 ConstString("REAL(KIND=8)"));
290 case eBasicTypeBool:
292 ConstString("LOGICAL"));
295 ConstString("COMPLEX"));
298 ConstString("COMPLEX(KIND=8)"));
299 default:
300 return CompilerType();
301 }
302}
303
306 size_t bit_size) {
307 switch (encoding) {
308 case eEncodingSint:
310 ConstString("INTEGER"));
311 case eEncodingUint:
313 ConstString("UNSIGNED"));
314 case eEncodingIEEE754:
316 ConstString("REAL"));
317 default:
318 return CompilerType();
319 }
320}
This file defines the DWARF AST parser for the Fortran language.
This file defines the classes that describe the Fortran types.
#define LLDB_PLUGIN_DEFINE(PluginName)
uint32_t GetKind(uint32_t data)
Return the type kind encoded in the given data.
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
void SetCString(const char *cstr)
Set the C string value.
bool IsEmpty() const
Test for empty string.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
static bool LanguageIsFortran(lldb::LanguageType language)
Definition Language.cpp:428
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:91
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, Module *module, Target *target)
bool IsIntegerType(lldb::opaque_compiler_type_t type, bool &is_signed) override
plugin::dwarf::DWARFASTParser * GetDWARFParser() override
bool IsFloatingPointType(lldb::opaque_compiler_type_t type) override
llvm::SmallVector< std::unique_ptr< plugin::fortran::FortranType > > m_types
static LanguageSet GetSupportedLanguagesForExpressions()
ConstString GetTypeName(lldb::opaque_compiler_type_t type, bool BaseOnly) override
Returns the type name upper-cased to follow Fortran's general style.
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override
llvm::FoldingSet< plugin::fortran::FortranType > m_basic_types
CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override
static LanguageSet GetSupportedLanguagesForTypes()
lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override
CompilerType GetOrCreateFortranBaseType(int kind, uint64_t bitsize, ConstString name)
Creates the type if it has not been created before, otherwise it gets it from the type map.
CompilerType CreateBaseType(llvm::dwarf::TypeKind dwarf_encoding, uint64_t bitsize, ConstString name)
CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, size_t bit_size) override
bool SupportsLanguage(lldb::LanguageType language) override
static llvm::StringRef GetPluginNameStatic()
std::unique_ptr< plugin::dwarf::DWARFASTParser > m_dwarf_ast_parser_up
lldb::BasicType GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override
bool Verify(lldb::opaque_compiler_type_t type) override
Verify the integrity of the type to catch CompilerTypes that mix and match invalid TypeSystem/Opaque ...
llvm::Expected< uint64_t > GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) override
A simplified internal representation of a Fortran type.
void Profile(llvm::FoldingSetNodeID &ID) const
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::TypeSystem > TypeSystemSP
void * opaque_compiler_type_t
Definition lldb-types.h:91
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
@ eBasicTypeUnsignedShort
@ eBasicTypeSignedChar
@ eBasicTypeUnsignedInt128
@ eBasicTypeFloatComplex
@ eBasicTypeUnsignedChar
@ eBasicTypeUnsignedLongLong
@ eBasicTypeDoubleComplex
@ eBasicTypeUnsignedInt
LanguageType
Programming language type.
@ eLanguageTypeFortran08
ISO Fortran 2008.
@ eLanguageTypeFortran18
@ eLanguageTypeFortran95
ISO Fortran 95.
@ eLanguageTypeFortran77
ISO Fortran 77.
@ eLanguageTypeFortran90
ISO Fortran 90.
@ eLanguageTypeFortran03
ISO Fortran 2003.
Encoding
Register encoding definitions.
@ eEncodingIEEE754
float
@ eEncodingUint
unsigned integer
@ eEncodingSint
signed integer
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
void Insert(lldb::LanguageType language)