Skip to content

Commit cbc6289

Browse files
committed
[TypeID] Use .def files to introduce TypeID specializations.
Simplify the static registration of types for use with TypeID by introducing a more declarative approach. Each zone provides a .def file listing the types and templates defined by that zone. The .def file is processed by include/swift/Basic/DefineTypeIDZone.h with its zone number, which assigns values to each of the types/templates and introduces the TypeID specializations.
1 parent 4b9e3ea commit cbc6289

File tree

5 files changed

+141
-49
lines changed

5 files changed

+141
-49
lines changed

Diff for: include/swift/Basic/CTypeIDZone.def

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===--- CTypeIDZone.def - Define the C++ TypeID Zone -----------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This definition file describes the types in the "C++" TypeID zone,
14+
// for use with the TypeID template.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
// C types.
19+
SWIFT_TYPEID_NAMED(unsigned char, UnsignedChar)
20+
SWIFT_TYPEID_NAMED(signed char, SignedChar)
21+
SWIFT_TYPEID_NAMED(char, Char)
22+
SWIFT_TYPEID_NAMED(short, Short)
23+
SWIFT_TYPEID_NAMED(unsigned short, UnsignedShort)
24+
SWIFT_TYPEID_NAMED(int, Int)
25+
SWIFT_TYPEID_NAMED(unsigned int, UnsignedInt)
26+
SWIFT_TYPEID_NAMED(long, Long)
27+
SWIFT_TYPEID_NAMED(unsigned long, UnsignedLong)
28+
SWIFT_TYPEID_NAMED(long long, LongLong)
29+
SWIFT_TYPEID_NAMED(unsigned long long, UnsignedLongLong)
30+
SWIFT_TYPEID_NAMED(float, Float)
31+
SWIFT_TYPEID_NAMED(double, Double)
32+
SWIFT_TYPEID_NAMED(bool, Bool)
33+
SWIFT_TYPEID_NAMED(decltype(nullptr), NullPtr)
34+
SWIFT_TYPEID_NAMED(void, Void)
35+
36+
// C++ standard library types.
37+
SWIFT_TYPEID_TEMPLATE1_NAMED(std::vector, Vector, typename T, T)

Diff for: include/swift/Basic/DefineTypeIDZone.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===--- DefineTypeIDZone.h - Define a TypeID Zone --------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file should be #included to define the TypeIDs for a given zone.
14+
// Two macros should be #define'd before inclusion, and will be #undef'd at
15+
// the end of this file:
16+
//
17+
// SWIFT_TYPEID_ZONE: The name of the Zone being defined, e.g.,
18+
// SwiftAST for the Swift AST library's zone. All zones need to be defined
19+
// a priori as
20+
//
21+
// SWIFT_TYPEID_HEADER: A (quoted) name of the header to be
22+
// included to define the types in the zone.
23+
//
24+
//===----------------------------------------------------------------------===//
25+
26+
#ifndef SWIFT_TYPEID_ZONE
27+
# error Must define the value of the TypeID zone with the given name.
28+
#endif
29+
30+
#ifndef SWIFT_TYPEID_HEADER
31+
# error Must define the TypeID header name with SWIFT_TYPEID_HEADER
32+
#endif
33+
34+
// Define a TypeID where the type name and internal name are the same.
35+
#define SWIFT_TYPEID(Type) SWIFT_TYPEID_NAMED(Type, Type)
36+
37+
// First pass: put all of the names into an enum so we get values for them.
38+
template<> struct TypeIDZoneTypes<SWIFT_TYPEID_ZONE> {
39+
enum Types : uint8_t {
40+
#define SWIFT_TYPEID_NAMED(Type, Name) Name,
41+
#define SWIFT_TYPEID_TEMPLATE1_NAMED(Template, Name, Param1, Arg1) Name,
42+
#include SWIFT_TYPEID_HEADER
43+
#undef SWIFT_TYPEID_NAMED
44+
#undef SWIFT_TYPEID_TEMPLATE1_NAMED
45+
};
46+
};
47+
48+
// Second pass: create specializations of TypeID for these types.
49+
#define SWIFT_TYPEID_NAMED(Type, Name) \
50+
template<> struct TypeID<Type> { \
51+
static const uint64_t value = \
52+
formTypeID(SWIFT_TYPEID_ZONE, \
53+
TypeIDZoneTypes<SWIFT_TYPEID_ZONE>::Name); \
54+
};
55+
56+
#define SWIFT_TYPEID_TEMPLATE1_NAMED(Template, Name, Param1, Arg1) \
57+
template<Param1> struct TypeID<Template<Arg1>> { \
58+
private: \
59+
static const uint64_t templateID = \
60+
formTypeID(SWIFT_TYPEID_ZONE, \
61+
TypeIDZoneTypes<SWIFT_TYPEID_ZONE>::Name); \
62+
\
63+
public: \
64+
static const uint64_t value = \
65+
(TypeID<Arg1>::value << 16) | templateID; \
66+
};
67+
68+
#include SWIFT_TYPEID_HEADER
69+
#undef SWIFT_TYPEID_NAMED
70+
#undef SWIFT_TYPEID_TEMPLATE1_NAMED
71+
72+
#undef SWIFT_TYPEID
73+
#undef SWIFT_TYPEID_ZONE
74+
#undef SWIFT_TYPEID_HEADER

Diff for: include/swift/Basic/TypeID.h

+7-46
Original file line numberDiff line numberDiff line change
@@ -32,58 +32,19 @@ namespace swift {
3232
template<typename T>
3333
struct TypeID;
3434

35-
/// Each type "zone" can contain 256 separate types for use in typeid,
36-
/// which should be enumerated.
37-
struct TypeIdZones {
38-
enum ZoneValues : uint8_t {
39-
/// C/C++ language and standard library types.
40-
C = 0,
41-
42-
/// A zone used only for unit testing.
43-
UnitTests = 1,
44-
};
45-
};
35+
/// Template whose specializations provide the set of type IDs within a
36+
/// given zone.
37+
template<uint8_t Zone> struct TypeIDZoneTypes;
4638

4739
/// Form a type ID given a zone and type value.
4840
constexpr uint64_t formTypeID(uint8_t zone, uint8_t type) {
4941
return (uint64_t(zone) << 8) | uint64_t(type);
5042
}
5143

52-
/// Assign the given type a particular value in the zone.
53-
#define SWIFT_TYPEID(Zone, Type, Value) \
54-
template<> struct TypeID<Type> { \
55-
static const uint64_t value = formTypeID(TypeIdZones::Zone, Value); \
56-
}
57-
58-
/// Assign the given single-param template a particular value in the zone.
59-
#define SWIFT_TYPEID_TEMPLATE1(Zone, Template, Value, Param1, Arg1) \
60-
template<Param1> struct TypeID<Template<Arg1>> { \
61-
static const uint64_t templateID = formTypeID(TypeIdZones::Zone, Value); \
62-
\
63-
public: \
64-
static const uint64_t value = (TypeID<Arg1>::value << 16) | templateID; \
65-
}
66-
67-
// C types.
68-
SWIFT_TYPEID(C, unsigned char, 1);
69-
SWIFT_TYPEID(C, signed char, 2);
70-
SWIFT_TYPEID(C, char, 3);
71-
SWIFT_TYPEID(C, short, 4);
72-
SWIFT_TYPEID(C, unsigned short, 5);
73-
SWIFT_TYPEID(C, int, 6);
74-
SWIFT_TYPEID(C, unsigned int, 7);
75-
SWIFT_TYPEID(C, long, 8);
76-
SWIFT_TYPEID(C, unsigned long, 9);
77-
SWIFT_TYPEID(C, long long, 10);
78-
SWIFT_TYPEID(C, unsigned long long, 11);
79-
SWIFT_TYPEID(C, float, 12);
80-
SWIFT_TYPEID(C, double, 13);
81-
SWIFT_TYPEID(C, bool, 14);
82-
SWIFT_TYPEID(C, decltype(nullptr), 15);
83-
SWIFT_TYPEID(C, void, 16);
84-
85-
// C++ standard library types.
86-
SWIFT_TYPEID_TEMPLATE1(C, std::vector, 100, typename T, T);
44+
// Define the C type zone (zone 0).
45+
#define SWIFT_TYPEID_ZONE 0
46+
#define SWIFT_TYPEID_HEADER "swift/Basic/CTypeIDZone.def"
47+
#include "swift/Basic/DefineTypeIDZone.h"
8748

8849
} // end namespace swift
8950

Diff for: unittests/AST/ArithmeticEvaluator.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,11 @@ struct ExternallyCachedEvaluationRule :
169169
: EvaluationRule{expr} { }
170170
};
171171

172+
// Define the arithmetic evaluator's zone.
172173
namespace swift {
173-
SWIFT_TYPEID(UnitTests, UncachedEvaluationRule, 1);
174-
SWIFT_TYPEID(UnitTests, InternallyCachedEvaluationRule, 2);
175-
SWIFT_TYPEID(UnitTests, ExternallyCachedEvaluationRule, 3);
174+
#define SWIFT_TYPEID_ZONE 255
175+
#define SWIFT_TYPEID_HEADER "ArithmeticEvaluatorTypeIDZone.def"
176+
#include "swift/Basic/DefineTypeIDZone.h"
176177
}
177178

178179
TEST(ArithmeticEvaluator, Simple) {

Diff for: unittests/AST/ArithmeticEvaluatorTypeIDZone.def

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===--- ArithmeticEvaluatorTypeIDZone.def - --------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This definition file describes the types in the "arithmetic evaluatior"
14+
// TypeID zone, for use with the TypeID template.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
SWIFT_TYPEID(UncachedEvaluationRule)
18+
SWIFT_TYPEID(InternallyCachedEvaluationRule)
19+
SWIFT_TYPEID(ExternallyCachedEvaluationRule)

0 commit comments

Comments
 (0)