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 if (!type)
91 return false;
92
93 return static_cast<FortranType *>(type)->GetKind() ==
95}
96
100 if (!function_type)
101 return 0;
102
103 return function_type->GetNumParams();
104}
105
108 const size_t index) {
110 if (!function_type)
111 return CompilerType();
112
113 llvm::ArrayRef<lldb_private::CompilerType> param_types =
114 function_type->GetParamTypes();
115 if (index >= param_types.size())
116 return CompilerType();
117
118 return param_types[index];
119}
120
122 bool &is_signed) {
123 if (!type)
124 return false;
125
126 int kind = static_cast<FortranType *>(type)->GetKind();
128 is_signed = (kind == FortranType::KIND_INTEGER);
129 return true;
130 }
131
132 return false;
133}
134
138
139/// Returns the type name upper-cased to follow Fortran's general style.
141 bool base_only) {
142 if (!type)
143 return ConstString();
144
145 FortranType *fortran_type = static_cast<FortranType *>(type);
146 switch (fortran_type->GetKind()) {
152 return fortran_type->GetName();
154 return static_cast<FortranFunction *>(fortran_type)->GetSignature();
156 return ConstString("Unsupported");
157 }
158 return ConstString("Unsupported");
159}
160
162TypeSystemFortran::CreateBaseType(llvm::dwarf::TypeKind dwarf_encoding,
163 uint64_t bitsize, ConstString name) {
164 int underlying_kind = FortranType::KIND_UNKNOWN;
165 const char *default_name = nullptr;
166
167 switch (dwarf_encoding) {
168 case dwarf::DW_ATE_boolean:
169 underlying_kind = FortranType::KIND_LOGICAL;
170 default_name = "LOGICAL";
171 break;
172 case dwarf::DW_ATE_float:
173 underlying_kind = FortranType::KIND_REAL;
174 default_name = "REAL";
175 break;
176 case dwarf::DW_ATE_signed:
177 case dwarf::DW_ATE_signed_char:
178 underlying_kind = FortranType::KIND_INTEGER;
179 default_name = "INTEGER";
180 break;
181 // Flang allows for unsigned types if the program is compiled with -funsigned.
182 case dwarf::DW_ATE_unsigned:
183 underlying_kind = FortranType::KIND_UNSIGNED;
184 default_name = "UNSIGNED";
185 break;
186 case dwarf::DW_ATE_complex_float:
187 underlying_kind = FortranType::KIND_COMPLEX;
188 default_name = "COMPLEX";
189 break;
190 default:
191 return CompilerType();
192 }
193 /// DWARFASTParserFortran provides the names it gets from DWARF, but in case
194 /// it doesn't we fallback to the default name.
195 if (name.IsEmpty() && default_name)
196 name.SetCString(default_name);
197
198 return GetOrCreateFortranBaseType(underlying_kind, bitsize, name);
199}
200
201/// Creates the type if it has not been created before, otherwise it gets it
202/// from the type map.
204 uint64_t bitsize,
205 ConstString name) {
206 FoldingSetNodeID id;
207 FortranType::Profile(id, kind, bitsize);
208 FoldingSetInsertToken token;
209 FortranType *candidate_type = m_basic_types.lookup(id, token);
210 if (candidate_type)
211 return CompilerType(weak_from_this(), (void *)candidate_type);
212
213 auto new_type_up = std::make_unique<FortranType>(kind, bitsize, name);
214 candidate_type = new_type_up.get();
215 m_basic_types.insert(candidate_type, token);
216 m_types.push_back(std::move(new_type_up));
217 return CompilerType(weak_from_this(), (void *)candidate_type);
218}
219
221 ConstString name, const SmallVectorImpl<CompilerType> &parameters,
222 const SmallVectorImpl<StringRef> &parameter_names,
223 CompilerType return_type) {
224 llvm::FoldingSetNodeID id;
225 FortranFunction::Profile(id, name, parameters, return_type);
226 FoldingSetInsertToken token;
227
228 FortranFunction *function_type = m_functions.lookup(id, token);
229 if (function_type)
230 return CompilerType(weak_from_this(), (void *)function_type);
231
232 auto new_type_up = std::make_unique<FortranFunction>(
233 name, parameters, parameter_names, return_type);
234 function_type = new_type_up.get();
235
236 m_functions.insert(function_type, token);
237 m_types.push_back(std::move(new_type_up));
238
239 return CompilerType(weak_from_this(), (void *)function_type);
240}
241
242lldb::TypeClass
244 return type ? lldb::eTypeClassBuiltin : lldb::eTypeClassInvalid;
245}
246
249 if (!type)
250 return CompilerType();
251 return CompilerType(weak_from_this(), type);
252}
253
257 if (!function_type)
258 return -1;
259
260 return function_type->GetNumParams();
261}
262
266 if (!function_type)
267 return CompilerType();
268
269 CompilerType return_type = function_type->GetReturnType();
270
271 return return_type.IsValid() ? return_type : CompilerType();
272}
273
274Expected<uint64_t>
276 ExecutionContextScope *exe_scope) {
277 if (!type)
278 return 0;
279 return static_cast<FortranType *>(type)->GetBitSize();
280}
281
284 if (!type)
285 return eBasicTypeInvalid;
286
287 FortranType *fortran_type = static_cast<FortranType *>(type);
288 switch (fortran_type->GetKind()) {
290 switch (fortran_type->GetBitSize()) {
291 case 8:
293 case 16:
294 return eBasicTypeShort;
295 case 32:
296 return eBasicTypeInt;
297 case 64:
298 return eBasicTypeLongLong;
299 case 128:
300 return eBasicTypeInt128;
301 default:
302 return eBasicTypeInvalid;
303 }
305 switch (fortran_type->GetBitSize()) {
306 case 8:
308 case 16:
310 case 32:
312 case 64:
314 case 128:
316 default:
317 return eBasicTypeInvalid;
318 }
320 return eBasicTypeBool;
322 switch (fortran_type->GetBitSize()) {
323 case 64:
325 case 128:
327 default:
328 return eBasicTypeInvalid;
329 }
331 switch (fortran_type->GetBitSize()) {
332 case 16:
333 return eBasicTypeHalf;
334 case 32:
335 return eBasicTypeFloat;
336 case 64:
337 return eBasicTypeDouble;
338 case 128:
339 return eBasicTypeFloat128;
340 default:
341 return eBasicTypeInvalid;
342 }
343 default:
344 return eBasicTypeInvalid;
345 }
346}
347
348// TODO: When a program is compiled with flags like -fdefault-integer-8 or
349// -fdefault-real-8, default KIND-less types have an 8-byte width instead of 4.
350// Consider whether the producer flags of the program should be queried to
351// determine the bitsize for eBasicTypeInt and eBasicTypeFloat.
353 switch (basic_type) {
354 case eBasicTypeInt:
356 ConstString("INTEGER"));
359 ConstString("UNSIGNED"));
360 case eBasicTypeFloat:
362 ConstString("REAL"));
363 case eBasicTypeDouble:
365 ConstString("REAL(KIND=8)"));
366 case eBasicTypeBool:
368 ConstString("LOGICAL"));
371 ConstString("COMPLEX"));
374 ConstString("COMPLEX(KIND=8)"));
375 default:
376 return CompilerType();
377 }
378}
379
382 size_t bit_size) {
383 switch (encoding) {
384 case eEncodingSint:
386 ConstString("INTEGER"));
387 case eEncodingUint:
389 ConstString("UNSIGNED"));
390 case eEncodingIEEE754:
392 ConstString("REAL"));
393 default:
394 return CompilerType();
395 }
396}
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:442
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:92
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 IsFunctionType(lldb::opaque_compiler_type_t type) override
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()
llvm::FoldingSet< plugin::fortran::FortranFunction > m_functions
ConstString GetTypeName(lldb::opaque_compiler_type_t type, bool BaseOnly) override
Returns the type name upper-cased to follow Fortran's general style.
int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override
size_t GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override
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
CompilerType GetFunctionReturnType(lldb::opaque_compiler_type_t type) override
bool SupportsLanguage(lldb::LanguageType language) override
CompilerType CreateFortranFunction(ConstString name, const llvm::SmallVectorImpl< CompilerType > &parameters, const llvm::SmallVectorImpl< llvm::StringRef > &parameter_names, CompilerType return_type)
static llvm::StringRef GetPluginNameStatic()
CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, const size_t index) override
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
static FortranFunction * GetFunctionType(void *type)
void Profile(llvm::FoldingSetNodeID &id) const
llvm::ArrayRef< CompilerType > GetParamTypes() const
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)