Skip to content

Commit 259b154

Browse files
committed
Parse C typeof specifiers
1 parent d6fcb8c commit 259b154

File tree

8 files changed

+198
-45
lines changed

8 files changed

+198
-45
lines changed

packages/cxx-frontend/src/TokenKind.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ export enum TokenKind {
170170
REGISTER,
171171
REINTERPRET_CAST,
172172
REQUIRES,
173-
RESTRICT,
174173
RETURN,
175174
SHORT,
176175
SIGNED,

packages/cxx-gen-ast/src/tokens.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ export const CXX_TOKEN_ALIASES = {
269269
__inline__: "INLINE",
270270
__inline: "INLINE",
271271
__restrict: "__RESTRICT__",
272-
__typeof__: "DECLTYPE",
273-
__typeof: "DECLTYPE",
272+
__typeof__: "TYPEOF",
273+
__typeof: "TYPEOF",
274274
__volatile__: "VOLATILE",
275275
__volatile: "VOLATILE",
276276
_Alignof: "ALIGNOF",
@@ -305,7 +305,7 @@ export const C_KEYWORDS: string[] = [
305305
"long",
306306
"nullptr",
307307
"register",
308-
"restrict",
308+
"__restrict__",
309309
"return",
310310
"short",
311311
"signed",
@@ -347,26 +347,34 @@ export const C_KEYWORDS: string[] = [
347347
"__int128",
348348
"__int64",
349349
"__real__",
350-
"__restrict__",
351350
"__thread",
352351
"__underlying_type",
353352
"_Atomic",
354353
"_Complex",
355354
];
356355

357356
export const C_TOKEN_ALIASES = {
357+
restrict: "__RESTRICT__",
358+
__alignof__: "ALIGNOF",
359+
__alignof: "ALIGNOF",
358360
__asm__: "ASM",
359361
__asm: "ASM",
362+
__attribute: "__ATTRIBUTE__",
363+
__decltype__: "DECLTYPE",
364+
__decltype: "DECLTYPE",
360365
__inline__: "INLINE",
361366
__inline: "INLINE",
367+
__restrict: "__RESTRICT__",
368+
__typeof__: "TYPEOF",
369+
__typeof: "TYPEOF",
370+
__volatile__: "VOLATILE",
371+
__volatile: "VOLATILE",
362372
_Alignas: "ALIGNAS",
363373
_Alignof: "ALIGNOF",
364374
_asm: "ASM",
365375
_Bool: "BOOL",
366376
_Static_assert: "STATIC_ASSERT",
367377
_Thread_local: "THREAD_LOCAL",
368-
__typeof__: "typeof",
369-
__typeof: "typeof",
370378
};
371379

372380
export const C_AND_CXX_KEYWORDS = Array.from(

src/parser/cxx/parser.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5165,7 +5165,10 @@ auto Parser::parse_decl_specifier_seq_no_typespecs(List<SpecifierAST*>*& yyast)
51655165

51665166
auto Parser::parse_decltype_specifier(DecltypeSpecifierAST*& yyast) -> bool {
51675167
SourceLocation decltypeLoc;
5168-
if (!match(TokenKind::T_DECLTYPE, decltypeLoc)) return false;
5168+
if (!match(TokenKind::T_DECLTYPE, decltypeLoc) &&
5169+
!match(TokenKind::T_TYPEOF, decltypeLoc) &&
5170+
!match(TokenKind::T_TYPEOF_UNQUAL, decltypeLoc))
5171+
return false;
51695172

51705173
SourceLocation lparenLoc;
51715174
if (!match(TokenKind::T_LPAREN, lparenLoc)) return false;

src/parser/cxx/parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,8 @@ class Parser final {
739739
[[nodiscard]] auto parse_identifier_list(List<NameIdAST*>*& yyast) -> bool;
740740

741741
private:
742-
[[nodiscard]] auto is_c() const -> bool;
743-
[[nodiscard]] auto is_cxx() const -> bool;
742+
[[nodiscard]] auto is_c() const -> bool;
743+
[[nodiscard]] auto is_cxx() const -> bool;
744744

745745
[[nodiscard]] auto lookat(auto... tokens) {
746746
return lookatHelper(0, tokens...);

src/parser/cxx/private/c_keywords-priv.h

Lines changed: 158 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,20 @@ static inline auto classifyC9(const char* s) -> cxx::TokenKind {
706706
}
707707
}
708708
}
709+
} else if (s[2] == 'a') {
710+
if (s[3] == 'l') {
711+
if (s[4] == 'i') {
712+
if (s[5] == 'g') {
713+
if (s[6] == 'n') {
714+
if (s[7] == 'o') {
715+
if (s[8] == 'f') {
716+
return cxx::TokenKind::T___ALIGNOF;
717+
}
718+
}
719+
}
720+
}
721+
}
722+
}
709723
}
710724
}
711725
}
@@ -771,6 +785,22 @@ static inline auto classifyC10(const char* s) -> cxx::TokenKind {
771785
}
772786
}
773787
}
788+
} else if (s[2] == 'd') {
789+
if (s[3] == 'e') {
790+
if (s[4] == 'c') {
791+
if (s[5] == 'l') {
792+
if (s[6] == 't') {
793+
if (s[7] == 'y') {
794+
if (s[8] == 'p') {
795+
if (s[9] == 'e') {
796+
return cxx::TokenKind::T___DECLTYPE;
797+
}
798+
}
799+
}
800+
}
801+
}
802+
}
803+
}
774804
} else if (s[2] == 'i') {
775805
if (s[3] == 'n') {
776806
if (s[4] == 'l') {
@@ -787,6 +817,22 @@ static inline auto classifyC10(const char* s) -> cxx::TokenKind {
787817
}
788818
}
789819
}
820+
} else if (s[2] == 'r') {
821+
if (s[3] == 'e') {
822+
if (s[4] == 's') {
823+
if (s[5] == 't') {
824+
if (s[6] == 'r') {
825+
if (s[7] == 'i') {
826+
if (s[8] == 'c') {
827+
if (s[9] == 't') {
828+
return cxx::TokenKind::T___RESTRICT;
829+
}
830+
}
831+
}
832+
}
833+
}
834+
}
835+
}
790836
} else if (s[2] == 't') {
791837
if (s[3] == 'y') {
792838
if (s[4] == 'p') {
@@ -803,6 +849,22 @@ static inline auto classifyC10(const char* s) -> cxx::TokenKind {
803849
}
804850
}
805851
}
852+
} else if (s[2] == 'v') {
853+
if (s[3] == 'o') {
854+
if (s[4] == 'l') {
855+
if (s[5] == 'a') {
856+
if (s[6] == 't') {
857+
if (s[7] == 'i') {
858+
if (s[8] == 'l') {
859+
if (s[9] == 'e') {
860+
return cxx::TokenKind::T___VOLATILE;
861+
}
862+
}
863+
}
864+
}
865+
}
866+
}
867+
}
806868
}
807869
}
808870
}
@@ -850,27 +912,32 @@ static inline auto classifyC11(const char* s) -> cxx::TokenKind {
850912
}
851913
}
852914
}
853-
}
854-
}
855-
}
856-
return cxx::TokenKind::T_IDENTIFIER;
857-
}
858-
859-
static inline auto classifyC12(const char* s) -> cxx::TokenKind {
860-
if (s[0] == 't') {
861-
if (s[1] == 'h') {
862-
if (s[2] == 'r') {
863-
if (s[3] == 'e') {
864-
if (s[4] == 'a') {
865-
if (s[5] == 'd') {
866-
if (s[6] == '_') {
867-
if (s[7] == 'l') {
868-
if (s[8] == 'o') {
869-
if (s[9] == 'c') {
870-
if (s[10] == 'a') {
871-
if (s[11] == 'l') {
872-
return cxx::TokenKind::T_THREAD_LOCAL;
873-
}
915+
} else if (s[2] == 'a') {
916+
if (s[3] == 'l') {
917+
if (s[4] == 'i') {
918+
if (s[5] == 'g') {
919+
if (s[6] == 'n') {
920+
if (s[7] == 'o') {
921+
if (s[8] == 'f') {
922+
if (s[9] == '_') {
923+
if (s[10] == '_') {
924+
return cxx::TokenKind::T___ALIGNOF__;
925+
}
926+
}
927+
}
928+
}
929+
}
930+
}
931+
}
932+
} else if (s[3] == 't') {
933+
if (s[4] == 't') {
934+
if (s[5] == 'r') {
935+
if (s[6] == 'i') {
936+
if (s[7] == 'b') {
937+
if (s[8] == 'u') {
938+
if (s[9] == 't') {
939+
if (s[10] == 'e') {
940+
return cxx::TokenKind::T___ATTRIBUTE;
874941
}
875942
}
876943
}
@@ -881,7 +948,12 @@ static inline auto classifyC12(const char* s) -> cxx::TokenKind {
881948
}
882949
}
883950
}
884-
} else if (s[0] == '_') {
951+
}
952+
return cxx::TokenKind::T_IDENTIFIER;
953+
}
954+
955+
static inline auto classifyC12(const char* s) -> cxx::TokenKind {
956+
if (s[0] == '_') {
885957
if (s[1] == '_') {
886958
if (s[2] == 'r') {
887959
if (s[3] == 'e') {
@@ -903,6 +975,70 @@ static inline auto classifyC12(const char* s) -> cxx::TokenKind {
903975
}
904976
}
905977
}
978+
} else if (s[2] == 'd') {
979+
if (s[3] == 'e') {
980+
if (s[4] == 'c') {
981+
if (s[5] == 'l') {
982+
if (s[6] == 't') {
983+
if (s[7] == 'y') {
984+
if (s[8] == 'p') {
985+
if (s[9] == 'e') {
986+
if (s[10] == '_') {
987+
if (s[11] == '_') {
988+
return cxx::TokenKind::T___DECLTYPE__;
989+
}
990+
}
991+
}
992+
}
993+
}
994+
}
995+
}
996+
}
997+
}
998+
} else if (s[2] == 'v') {
999+
if (s[3] == 'o') {
1000+
if (s[4] == 'l') {
1001+
if (s[5] == 'a') {
1002+
if (s[6] == 't') {
1003+
if (s[7] == 'i') {
1004+
if (s[8] == 'l') {
1005+
if (s[9] == 'e') {
1006+
if (s[10] == '_') {
1007+
if (s[11] == '_') {
1008+
return cxx::TokenKind::T___VOLATILE__;
1009+
}
1010+
}
1011+
}
1012+
}
1013+
}
1014+
}
1015+
}
1016+
}
1017+
}
1018+
}
1019+
}
1020+
} else if (s[0] == 't') {
1021+
if (s[1] == 'h') {
1022+
if (s[2] == 'r') {
1023+
if (s[3] == 'e') {
1024+
if (s[4] == 'a') {
1025+
if (s[5] == 'd') {
1026+
if (s[6] == '_') {
1027+
if (s[7] == 'l') {
1028+
if (s[8] == 'o') {
1029+
if (s[9] == 'c') {
1030+
if (s[10] == 'a') {
1031+
if (s[11] == 'l') {
1032+
return cxx::TokenKind::T_THREAD_LOCAL;
1033+
}
1034+
}
1035+
}
1036+
}
1037+
}
1038+
}
1039+
}
1040+
}
1041+
}
9061042
}
9071043
}
9081044
}

src/parser/cxx/token_fwd.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ class Token;
185185
V(REGISTER, "register") \
186186
V(REINTERPRET_CAST, "reinterpret_cast") \
187187
V(REQUIRES, "requires") \
188-
V(RESTRICT, "restrict") \
189188
V(RETURN, "return") \
190189
V(SHORT, "short") \
191190
V(SIGNED, "signed") \
@@ -266,18 +265,27 @@ class Token;
266265
V(__IS_VOLATILE, "__is_volatile")
267266

268267
#define FOR_EACH_TOKEN_ALIAS(V) \
268+
V(RESTRICT, __RESTRICT__) \
269+
V(__ALIGNOF__, ALIGNOF) \
270+
V(__ALIGNOF, ALIGNOF) \
269271
V(__ASM__, ASM) \
270272
V(__ASM, ASM) \
273+
V(__ATTRIBUTE, __ATTRIBUTE__) \
274+
V(__DECLTYPE__, DECLTYPE) \
275+
V(__DECLTYPE, DECLTYPE) \
271276
V(__INLINE__, INLINE) \
272277
V(__INLINE, INLINE) \
278+
V(__RESTRICT, __RESTRICT__) \
279+
V(__TYPEOF__, TYPEOF) \
280+
V(__TYPEOF, TYPEOF) \
281+
V(__VOLATILE__, VOLATILE) \
282+
V(__VOLATILE, VOLATILE) \
273283
V(_ALIGNAS, ALIGNAS) \
274284
V(_ALIGNOF, ALIGNOF) \
275285
V(_ASM, ASM) \
276286
V(_BOOL, BOOL) \
277287
V(_STATIC_ASSERT, STATIC_ASSERT) \
278288
V(_THREAD_LOCAL, THREAD_LOCAL) \
279-
V(__TYPEOF__, DECLTYPE) \
280-
V(__TYPEOF, DECLTYPE) \
281289
V(AND_EQ, AMP_EQUAL) \
282290
V(AND, AMP_AMP) \
283291
V(BITAND, AMP) \
@@ -288,15 +296,7 @@ class Token;
288296
V(OR_EQ, BAR_EQUAL) \
289297
V(OR, BAR_BAR) \
290298
V(XOR_EQ, CARET_EQUAL) \
291-
V(XOR, CARET) \
292-
V(__ALIGNOF__, ALIGNOF) \
293-
V(__ALIGNOF, ALIGNOF) \
294-
V(__ATTRIBUTE, __ATTRIBUTE__) \
295-
V(__DECLTYPE__, DECLTYPE) \
296-
V(__DECLTYPE, DECLTYPE) \
297-
V(__RESTRICT, __RESTRICT__) \
298-
V(__VOLATILE__, VOLATILE) \
299-
V(__VOLATILE, VOLATILE)
299+
V(XOR, CARET)
300300

301301
#define FOR_EACH_TOKEN(V) \
302302
FOR_EACH_BASE_TOKEN(V) \

tests/unit_tests/lit.cfg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
config.test_format = lit.formats.ShTest()
2828

29-
config.suffixes = [".cc", ".yml"]
29+
config.suffixes = [".c", ".cc", ".yml"]
3030

3131
config.test_source_root = os.path.dirname(__file__)
3232

tests/unit_tests/sema/typeof_01.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %cxx -xc -verify -fcheck -dump-symbols %s | %filecheck %s
2+
3+
typeof('x') c;
4+
typeof(0) i;
5+
6+
// CHECK: variable char c
7+
// CHECK-NEXT: variable int i

0 commit comments

Comments
 (0)