[lldb] Move Objective-C constants into ObjCConstants.h

Move Objective-C constants into ObjCConstants.h and share them between
Cocoa and AppleObjCTypeEncodingParser.

Differential revision: https://reviews.llvm.org/D107679
This commit is contained in:
Jonas Devlieghere 2021-08-06 18:41:55 -07:00
parent 88003cea1c
commit 47a889c668
4 changed files with 83 additions and 69 deletions

View File

@ -8,6 +8,7 @@
#include "Cocoa.h"
#include "NSString.h"
#include "ObjCConstants.h"
#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
@ -30,37 +31,6 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/bit.h"
// Objective-C Type Encoding
#define _C_ID '@'
#define _C_CLASS '#'
#define _C_SEL ':'
#define _C_CHR 'c'
#define _C_UCHR 'C'
#define _C_SHT 's'
#define _C_USHT 'S'
#define _C_INT 'i'
#define _C_UINT 'I'
#define _C_LNG 'l'
#define _C_ULNG 'L'
#define _C_LNG_LNG 'q'
#define _C_ULNG_LNG 'Q'
#define _C_FLT 'f'
#define _C_DBL 'd'
#define _C_BFLD 'b'
#define _C_BOOL 'B'
#define _C_VOID 'v'
#define _C_UNDEF '?'
#define _C_PTR '^'
#define _C_CHARPTR '*'
#define _C_ATOM '%'
#define _C_ARY_B '['
#define _C_ARY_E ']'
#define _C_UNION_B '('
#define _C_UNION_E ')'
#define _C_STRUCT_B '{'
#define _C_STRUCT_E '}'
#define _C_VECTOR '!'
#define _C_CONST 'r'
using namespace lldb;
using namespace lldb_private;

View File

@ -0,0 +1,44 @@
//===-- ObjCConstants.h------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_SOURCE_PLUGINS_LANGUAGE_OBJC_OBJCCONSTANTS_H
#define LLDB_SOURCE_PLUGINS_LANGUAGE_OBJC_OBJCCONSTANTS_H
// Objective-C Type Encoding
#define _C_ID '@'
#define _C_CLASS '#'
#define _C_SEL ':'
#define _C_CHR 'c'
#define _C_UCHR 'C'
#define _C_SHT 's'
#define _C_USHT 'S'
#define _C_INT 'i'
#define _C_UINT 'I'
#define _C_LNG 'l'
#define _C_ULNG 'L'
#define _C_LNG_LNG 'q'
#define _C_ULNG_LNG 'Q'
#define _C_FLT 'f'
#define _C_DBL 'd'
#define _C_BFLD 'b'
#define _C_BOOL 'B'
#define _C_VOID 'v'
#define _C_UNDEF '?'
#define _C_PTR '^'
#define _C_CHARPTR '*'
#define _C_ATOM '%'
#define _C_ARY_B '['
#define _C_ARY_E ']'
#define _C_UNION_B '('
#define _C_UNION_E ')'
#define _C_STRUCT_B '{'
#define _C_STRUCT_E '}'
#define _C_VECTOR '!'
#define _C_CONST 'r'
#endif // LLDB_SOURCE_PLUGINS_LANGUAGE_OBJC_OBJCCONSTANTS_H

View File

@ -78,13 +78,13 @@ AppleObjCTypeEncodingParser::ReadStructElement(TypeSystemClang &ast_ctx,
clang::QualType AppleObjCTypeEncodingParser::BuildStruct(
TypeSystemClang &ast_ctx, StringLexer &type, bool for_expression) {
return BuildAggregate(ast_ctx, type, for_expression, '{', '}',
return BuildAggregate(ast_ctx, type, for_expression, _C_STRUCT_B, _C_STRUCT_E,
clang::TTK_Struct);
}
clang::QualType AppleObjCTypeEncodingParser::BuildUnion(
TypeSystemClang &ast_ctx, StringLexer &type, bool for_expression) {
return BuildAggregate(ast_ctx, type, for_expression, '(', ')',
return BuildAggregate(ast_ctx, type, for_expression, _C_UNION_B, _C_UNION_E,
clang::TTK_Union);
}
@ -148,11 +148,11 @@ clang::QualType AppleObjCTypeEncodingParser::BuildAggregate(
clang::QualType AppleObjCTypeEncodingParser::BuildArray(
TypeSystemClang &ast_ctx, StringLexer &type, bool for_expression) {
if (!type.NextIf('['))
if (!type.NextIf(_C_ARY_B))
return clang::QualType();
uint32_t size = ReadNumber(type);
clang::QualType element_type(BuildType(ast_ctx, type, for_expression));
if (!type.NextIf(']'))
if (!type.NextIf(_C_ARY_E))
return clang::QualType();
CompilerType array_type(ast_ctx.CreateArrayType(
CompilerType(&ast_ctx, element_type.getAsOpaquePtr()), size, false));
@ -166,7 +166,7 @@ clang::QualType AppleObjCTypeEncodingParser::BuildArray(
// dynamic typing will resolve things for us anyway
clang::QualType AppleObjCTypeEncodingParser::BuildObjCObjectPointerType(
TypeSystemClang &clang_ast_ctx, StringLexer &type, bool for_expression) {
if (!type.NextIf('@'))
if (!type.NextIf(_C_ID))
return clang::QualType();
clang::ASTContext &ast_ctx = clang_ast_ctx.getASTContext();
@ -203,9 +203,9 @@ clang::QualType AppleObjCTypeEncodingParser::BuildObjCObjectPointerType(
2); // undo our consumption of the string and of the quotes
name.clear();
break;
case '}':
case ')':
case ']':
case _C_STRUCT_E:
case _C_UNION_E:
case _C_ARY_E:
case '"':
// the quoted string is a class name see the rule
break;
@ -260,13 +260,13 @@ AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
switch (type.Peek()) {
default:
break;
case '{':
case _C_STRUCT_B:
return BuildStruct(clang_ast_ctx, type, for_expression);
case '[':
case _C_ARY_B:
return BuildArray(clang_ast_ctx, type, for_expression);
case '(':
case _C_UNION_B:
return BuildUnion(clang_ast_ctx, type, for_expression);
case '@':
case _C_ID:
return BuildObjCObjectPointerType(clang_ast_ctx, type, for_expression);
}
@ -274,46 +274,46 @@ AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
default:
type.PutBack(1);
return clang::QualType();
case 'c':
case _C_CHR:
return ast_ctx.CharTy;
case 'i':
case _C_INT:
return ast_ctx.IntTy;
case 's':
case _C_SHT:
return ast_ctx.ShortTy;
case 'l':
case _C_LNG:
return ast_ctx.getIntTypeForBitwidth(32, true);
// this used to be done like this:
// return clang_ast_ctx->GetIntTypeFromBitSize(32, true).GetQualType();
// which uses one of the constants if one is available, but we don't think
// all this work is necessary.
case 'q':
case _C_LNG_LNG:
return ast_ctx.LongLongTy;
case 'C':
case _C_UCHR:
return ast_ctx.UnsignedCharTy;
case 'I':
case _C_UINT:
return ast_ctx.UnsignedIntTy;
case 'S':
case _C_USHT:
return ast_ctx.UnsignedShortTy;
case 'L':
case _C_ULNG:
return ast_ctx.getIntTypeForBitwidth(32, false);
// see note for 'l'
case 'Q':
// see note for _C_LNG
case _C_ULNG_LNG:
return ast_ctx.UnsignedLongLongTy;
case 'f':
case _C_FLT:
return ast_ctx.FloatTy;
case 'd':
case _C_DBL:
return ast_ctx.DoubleTy;
case 'B':
case _C_BOOL:
return ast_ctx.BoolTy;
case 'v':
case _C_VOID:
return ast_ctx.VoidTy;
case '*':
case _C_CHARPTR:
return ast_ctx.getPointerType(ast_ctx.CharTy);
case '#':
case _C_CLASS:
return ast_ctx.getObjCClassType();
case ':':
case _C_SEL:
return ast_ctx.getObjCSelType();
case 'b': {
case _C_BFLD: {
uint32_t size = ReadNumber(type);
if (bitfield_bit_size) {
*bitfield_bit_size = size;
@ -321,7 +321,7 @@ AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
} else
return clang::QualType();
}
case 'r': {
case _C_CONST: {
clang::QualType target_type =
BuildType(clang_ast_ctx, type, for_expression);
if (target_type.isNull())
@ -331,8 +331,8 @@ AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
else
return ast_ctx.getConstType(target_type);
}
case '^': {
if (!for_expression && type.NextIf('?')) {
case _C_PTR: {
if (!for_expression && type.NextIf(_C_UNDEF)) {
// if we are not supporting the concept of unknownAny, but what is being
// created here is an unknownAny*, then we can just get away with a void*
// this is theoretically wrong (in the same sense as 'theoretically
@ -350,7 +350,7 @@ AppleObjCTypeEncodingParser::BuildType(TypeSystemClang &clang_ast_ctx,
return ast_ctx.getPointerType(target_type);
}
}
case '?':
case _C_UNDEF:
return for_expression ? ast_ctx.UnknownAnyTy : clang::QualType();
}
}

View File

@ -9,11 +9,11 @@
#ifndef LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_APPLEOBJCRUNTIME_APPLEOBJCTYPEENCODINGPARSER_H
#define LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_APPLEOBJCRUNTIME_APPLEOBJCTYPEENCODINGPARSER_H
#include "clang/AST/ASTContext.h"
#include "Plugins/Language/ObjC/ObjCConstants.h"
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
#include "lldb/lldb-private.h"
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
#include "clang/AST/ASTContext.h"
namespace lldb_private {
class StringLexer;