1
1
from __future__ import division
2
2
3
+ import itertools
4
+
3
5
import pycparser .c_parser
4
6
import pycparser .c_ast as c_ast
5
7
try :
@@ -290,21 +292,40 @@ class UnionExt(_StructUnionEnumMixin, c_ast.Union):
290
292
# {{{ attributes
291
293
292
294
class _AttributesMixin (object ):
295
+
296
+ def _merge_attributes (self , p , attrs ):
297
+ print (999 ,attrs )
298
+ attrs = [
299
+ attr
300
+ for attr in attrs
301
+ if attr is not None
302
+ ]
303
+ if attrs :
304
+ fst , * others = attrs
305
+ fst .exprs .extend (itertools .chain .from_iterable (
306
+ attr .exprs
307
+ for attr in others
308
+ ))
309
+ return fst
310
+ else :
311
+ return c_ast .ExprList ([], self ._coord (p .lineno (1 )))
312
+
293
313
def p_attributes_opt_1 (self , p ):
294
314
""" attributes_opt : attribute_decl attributes_opt
295
315
"""
296
- p [ 1 ]. exprs . extend ( p [ 2 ]. exprs )
297
- p [0 ] = p [1 ]
316
+ print ( 991 )
317
+ p [0 ] = self . _merge_attributes ( p , ( p [1 ], p [ 2 ]))
298
318
299
319
def p_attributes_opt_2 (self , p ):
300
320
""" attributes_opt : empty
301
321
"""
302
- p [0 ] = c_ast . ExprList ([], self ._coord ( p . lineno ( 1 )) )
322
+ p [0 ] = self ._merge_attributes ( p , [] )
303
323
304
324
def p_attribute_decl (self , p ):
305
325
""" attribute_decl : __ATTRIBUTE__ LPAREN LPAREN attribute_list RPAREN RPAREN
306
326
| __ATTRIBUTE LPAREN LPAREN attribute_list RPAREN RPAREN
307
327
"""
328
+ print (992 )
308
329
p [0 ] = p [4 ]
309
330
310
331
def p_attribute_list_1 (self , p ):
@@ -315,8 +336,7 @@ def p_attribute_list_1(self, p):
315
336
def p_attribute_list_2 (self , p ):
316
337
""" attribute_list : attribute_list COMMA attribute
317
338
"""
318
- p [1 ].exprs .append (p [3 ])
319
- p [0 ] = p [1 ]
339
+ p [0 ] = self ._merge_attributes (p , (p [1 ], p [3 ]))
320
340
321
341
def p_attribute_1 (self , p ):
322
342
""" attribute : CONST
@@ -331,6 +351,7 @@ def p_attribute_3(self, p):
331
351
def p_function_specifier_attr (self , p ):
332
352
""" function_specifier : attribute_decl
333
353
"""
354
+ print (993 )
334
355
p [0 ] = AttributeSpecifier (p [1 ])
335
356
336
357
# }}}
@@ -421,6 +442,7 @@ class _AsmAndAttributesMixin(_AsmMixin, _AttributesMixin):
421
442
def p_xxx_declarator_1 (self , p ):
422
443
""" xxx_declarator : direct_xxx_declarator asm_label_opt attributes_opt
423
444
"""
445
+ print (555 , p [3 ])
424
446
if p [2 ] or p [3 ].exprs :
425
447
if isinstance (p [1 ], (c_ast .ArrayDecl , c_ast .FuncDecl )):
426
448
decl_ext = to_decl_ext (p [1 ].type )
@@ -450,6 +472,7 @@ def p_xxx_declarator_2(self, p):
450
472
| pointer attributes_opt direct_xxx_declarator \
451
473
asm_label_opt
452
474
"""
475
+ print (666 )
453
476
if hasattr (p [4 ], "exprs" ):
454
477
attr_decl = p [4 ]
455
478
asm_label = p [3 ]
@@ -490,6 +513,7 @@ def p_direct_xxx_declarator_6(self, p):
490
513
LPAREN identifier_list_opt RPAREN \
491
514
asm_label_opt attributes_opt
492
515
"""
516
+ print (777 )
493
517
func = FuncDeclExt (
494
518
args = p [3 ],
495
519
type = None ,
@@ -503,6 +527,7 @@ def p_direct_abstract_declarator_6(self, p):
503
527
""" direct_abstract_declarator : direct_abstract_declarator \
504
528
LPAREN parameter_type_list_opt RPAREN asm_label_opt attributes_opt
505
529
"""
530
+ print (888 )
506
531
func = FuncDeclExt (
507
532
args = p [3 ],
508
533
type = None ,
@@ -520,149 +545,92 @@ def _select_struct_union_class(self, token):
520
545
}[klass ]
521
546
522
547
def p_struct_or_union_specifier_with_attr_1 (self , p ):
523
- """ struct_or_union_specifier : struct_or_union ID brace_open brace_close attributes_opt
524
- | struct_or_union TYPEID brace_open brace_close attributes_opt
525
- """
526
- klass = self ._select_struct_union_class (p [1 ])
527
- p [0 ] = klass (
528
- name = p [2 ],
529
- decls = [],
530
- attributes = p [5 ],
531
- coord = self ._token_coord (p , 2 ))
532
-
533
- def p_struct_or_union_specifier_with_attr_2 (self , p ):
534
- """ struct_or_union_specifier : struct_or_union attributes_opt ID brace_open brace_close
535
- | struct_or_union attributes_opt TYPEID brace_open brace_close
548
+ """ struct_or_union_specifier : struct_or_union attributes_opt ID brace_open brace_close attributes_opt
549
+ | struct_or_union attributes_opt TYPEID brace_open brace_close attributes_opt
536
550
"""
551
+ print (111 )
537
552
klass = self ._select_struct_union_class (p [1 ])
538
- p [0 ] = klass (
539
- name = p [3 ],
540
- decls = [],
541
- attributes = p [2 ],
542
- coord = self ._token_coord (p , 3 ))
553
+ attrs = self ._merge_attributes (p , (p [2 ], p [6 ]))
543
554
544
- def p_struct_or_union_specifier_with_attr_3 (self , p ):
545
- """ struct_or_union_specifier : attributes_opt struct_or_union ID brace_open brace_close
546
- | attributes_opt struct_or_union TYPEID brace_open brace_close
547
- """
548
- klass = self ._select_struct_union_class (p [2 ])
549
555
p [0 ] = klass (
550
556
name = p [3 ],
551
557
decls = [],
552
- attributes = p [ 1 ] ,
558
+ attributes = attrs ,
553
559
coord = self ._token_coord (p , 3 ))
554
560
555
- def p_struct_or_union_specifier_with_attr_4 (self , p ):
556
- """ struct_or_union_specifier : struct_or_union ID brace_open struct_declaration_list brace_close attributes_opt
557
- | struct_or_union TYPEID brace_open struct_declaration_list brace_close attributes_opt
558
- """
559
- klass = self ._select_struct_union_class (p [1 ])
560
- p [0 ] = klass (
561
- name = p [2 ],
562
- decls = p [4 ],
563
- attributes = p [6 ],
564
- coord = self ._token_coord (p , 2 ))
565
-
566
561
def p_struct_or_union_specifier_with_attr_5 (self , p ):
567
- """ struct_or_union_specifier : struct_or_union attributes_opt ID brace_open struct_declaration_list brace_close
568
- | struct_or_union attributes_opt TYPEID brace_open struct_declaration_list brace_close
562
+ """ struct_or_union_specifier : struct_or_union attributes_opt ID brace_open struct_declaration_list brace_close attributes_opt
563
+ | struct_or_union attributes_opt TYPEID brace_open struct_declaration_list brace_close attributes_opt
569
564
"""
565
+ print (222 )
570
566
klass = self ._select_struct_union_class (p [1 ])
571
- p [0 ] = klass (
572
- name = p [3 ],
573
- decls = p [5 ],
574
- attributes = p [2 ],
575
- coord = self ._token_coord (p , 3 ))
567
+ attrs = self ._merge_attributes (p , (p [2 ], p [7 ]))
576
568
577
- def p_struct_or_union_specifier_with_attr_6 (self , p ):
578
- """ struct_or_union_specifier : attributes_opt struct_or_union ID brace_open struct_declaration_list brace_close
579
- | attributes_opt struct_or_union TYPEID brace_open struct_declaration_list brace_close
580
- """
581
- klass = self ._select_struct_union_class (p [2 ])
582
569
p [0 ] = klass (
583
570
name = p [3 ],
584
571
decls = p [5 ],
585
- attributes = p [ 1 ] ,
572
+ attributes = attrs ,
586
573
coord = self ._token_coord (p , 3 ))
587
574
588
575
def p_struct_or_union_specifier_with_attr_7 (self , p ):
589
- """ struct_or_union_specifier : struct_or_union attributes_opt ID
590
- | struct_or_union attributes_opt TYPEID
576
+ """ struct_or_union_specifier : struct_or_union attributes_opt ID attributes_opt
577
+ | struct_or_union attributes_opt TYPEID attributes_opt
591
578
"""
579
+ print (333 )
592
580
klass = self ._select_struct_union_class (p [1 ])
581
+ attrs = self ._merge_attributes (p , (p [2 ], p [4 ]))
582
+
593
583
# None means no list of members
594
584
p [0 ] = klass (
595
585
name = p [3 ],
596
586
decls = None ,
597
- attributes = p [ 2 ] ,
587
+ attributes = attrs ,
598
588
coord = self ._token_coord (p , 3 ))
599
589
600
590
def p_struct_or_union_specifier_with_attr_8 (self , p ):
601
- """ struct_or_union_specifier : struct_or_union brace_open brace_close attributes_opt
591
+ """ struct_or_union_specifier : struct_or_union attributes_opt brace_open brace_close attributes_opt
602
592
"""
593
+ print (444 )
603
594
klass = self ._select_struct_union_class (p [1 ])
604
- p [0 ] = klass (
605
- name = None ,
606
- decls = [],
607
- attributes = p [4 ],
608
- coord = self ._token_coord (p , 2 ))
595
+ attrs = self ._merge_attributes (p , (p [2 ], p [5 ]))
609
596
610
- def p_struct_or_union_specifier_with_attr_9 (self , p ):
611
- """ struct_or_union_specifier : struct_or_union attributes_opt brace_open brace_close
612
- """
613
- klass = self ._select_struct_union_class (p [1 ])
614
597
p [0 ] = klass (
615
598
name = None ,
616
599
decls = [],
617
- attributes = p [2 ],
618
- coord = self ._token_coord (p , 3 ))
619
-
620
- def p_struct_or_union_specifier_with_attr_10 (self , p ):
621
- """ struct_or_union_specifier : struct_or_union brace_open struct_declaration_list brace_close attributes_opt
622
- """
623
- klass = self ._select_struct_union_class (p [1 ])
624
- p [0 ] = klass (
625
- name = None ,
626
- decls = p [3 ],
627
- attributes = p [5 ],
628
- coord = self ._token_coord (p , 2 ))
629
-
630
- def p_struct_or_union_specifier_with_attr_11 (self , p ):
631
- """ struct_or_union_specifier : struct_or_union attributes_opt brace_open struct_declaration_list brace_close
632
- """
633
- klass = self ._select_struct_union_class (p [1 ])
634
- p [0 ] = klass (
635
- name = None ,
636
- decls = p [4 ],
637
- attributes = p [2 ],
600
+ attributes = attrs ,
638
601
coord = self ._token_coord (p , 3 ))
639
602
640
603
def p_enum_specifier_with_attr_1 (self , p ):
641
604
""" enum_specifier : ENUM attributes_opt ID
642
605
| ENUM attributes_opt TYPEID
643
606
"""
607
+ print (991 )
644
608
p [0 ] = EnumExt (p [3 ], None , self ._token_coord (p , 1 ), attributes = p [2 ])
645
609
646
610
def p_enum_specifier_with_attr_2 (self , p ):
647
611
""" enum_specifier : ENUM attributes_opt brace_open enumerator_list brace_close
648
612
"""
613
+ print (992 )
649
614
p [0 ] = EnumExt (None , p [4 ], self ._token_coord (p , 1 ), attributes = p [2 ])
650
615
651
616
def p_enum_specifier_with_attr_3 (self , p ):
652
617
""" enum_specifier : ENUM brace_open enumerator_list brace_close attributes_opt
653
618
"""
619
+ print (993 )
654
620
p [0 ] = EnumExt (None , p [3 ], self ._token_coord (p , 1 ), attributes = p [5 ])
655
621
656
622
def p_enum_specifier_with_attr_4 (self , p ):
657
623
""" enum_specifier : ENUM attributes_opt ID brace_open enumerator_list brace_close
658
624
| ENUM attributes_opt TYPEID brace_open enumerator_list brace_close
659
625
"""
626
+ print (994 )
660
627
p [0 ] = EnumExt (p [3 ], p [5 ], self ._token_coord (p , 1 ), attributes = p [2 ])
661
628
662
629
def p_enum_specifier_with_attr_5 (self , p ):
663
630
""" enum_specifier : ENUM ID brace_open enumerator_list brace_close attributes_opt
664
631
| ENUM TYPEID brace_open enumerator_list brace_close attributes_opt
665
632
"""
633
+ print (995 )
666
634
p [0 ] = EnumExt (p [2 ], p [4 ], self ._token_coord (p , 1 ), attributes = p [6 ])
667
635
668
636
def p_enumerator (self , p ):
@@ -671,6 +639,7 @@ def p_enumerator(self, p):
671
639
| ID attributes_opt
672
640
| ID attributes_opt EQUALS constant_expression
673
641
"""
642
+ print (996 )
674
643
if len (p ) in (2 , 4 ):
675
644
super ().p_enumerator (p )
676
645
else :
0 commit comments