您的位置:首页 > 其它

Studying note of GCC-3.4.6 source (95)

2010-09-01 11:46 435 查看
5.12.3.2.1.1.1.4. Push TYPE_DECL for the class
Now d is the TYPE_DECL refered by results slot of TEMPLATE_DECL, which is returned by push_template_decl_real. And b is the scope of namespace “Loki” found at the beginning of the function.

pushtag (continue)

4659 if (b->kind == sk_class)
4660 {
4661 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4662 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4663 class. But if it's a member template class, we
4664 want the TEMPLATE_DECL, not the TYPE_DECL, so this
4665 is done later. */
4666 finish_member_declaration (d);
4667 else
4668 pushdecl_class_level (d);
4669 }
4670 else
4671 d = pushdecl_with_scope (d, b);

Again, we will re-enter pushdecl routine, but this time with following code. Argument x is d forwarded by pushdecl_with_scope.

566 tree
567 pushdecl (tree x) in name-lookup.c
568 {
569 tree t;
570 tree name;
571 int need_new_binding;
572
573 timevar_push (TV_NAME_LOOKUP);
574
575 need_new_binding = 1;

604 name = DECL_NAME (x);
605 if (name)
606 {

772 /* If declaring a type as a typedef, copy the type (unless we're
773 at line 0), and install this TYPE_DECL as the new type's typedef
774 name. See the extensive comment in ../c-decl.c (pushdecl). */
775 if (TREE_CODE (x) == TYPE_DECL)
776 {
777 tree type = TREE_TYPE (x);

797 if (type != error_mark_node
798 && TYPE_NAME (type)
799 && TYPE_IDENTIFIER (type))
800 set_identifier_type_value (DECL_NAME (x), x);
801 }

1007 }
1008
1009 if (need_new_binding)
1010 add_decl_to_level (x,
1011 DECL_NAMESPACE_SCOPE_P (x)
1012 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1013 : current_binding_level);
1014
1015 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1016 }

The TYPE_DECL and the TEMPLATE_DECL share the same name, and this name has been pushed into the namespace scope at line 3006 in push_template_decl_real; so above t points to the TEMPLATE_DECL. Then above set_identifier_type_value after finding out the cxx_binding of the TEMPLATE_DECL, invokes supplement_binding to update the cxx_binding in following way.

431 static bool
432 supplement_binding (cxx_binding *binding, tree decl) in name-lookup.c
433 {
434 tree bval = binding->value;
435 bool ok = true;
436
437 timevar_push (TV_NAME_LOOKUP);
438 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
439 /* The new name is the type name. */
440 binding->type = decl;
441 else if (/* BVAL is null when push_class_level_binding moves an
442 inherited type-binding out of the way to make room for a
443 new value binding. */
444 !bval
445 /* BVAL is error_mark_node when DECL's name has been used
446 in a non-class scope prior declaration. In that case,
447 we should have already issued a diagnostic; for graceful
448 error recovery purpose, pretend this was the intended
449 declaration for that name. */
450 || bval == error_mark_node
451 /* If BVAL is a built-in that has not yet been declared,
452 pretend it is not there at all. */
453 || (TREE_CODE (bval) == FUNCTION_DECL
454 && DECL_ANTICIPATED (bval)))
455 binding->value = decl;
456 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
457 {
458 /* The old binding was a type name. It was placed in
459 VALUE field because it was thought, at the point it was
460 declared, to be the only entity with such a name. Move the
461 type name into the type slot; it is now hidden by the new
462 binding. */
463 binding->type = bval;
464 binding->value = decl;
465 binding->value_is_inherited = false;
466 }

518 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
519 }

See condition at line 438 is satisfied. Next, at line 1010 add_decl_to_level records the TYPE_DECL into the names slot of the scope. Then when returns from pushdecl_with_scope the intermediate tree now like:

(Click here to open)
Figure 57: Push tag of class SingleThreaded – step 4
Notice that at this step, TYPE_DECL and TEMPLATE_DECL are chained by names slot of cxx_scope of “Loki” which indicates visible entities in this scope.

pushtag (continue)

4673 /* FIXME what if it gets a name from typedef? */
4674 if (ANON_AGGRNAME_P (name))
4675 DECL_IGNORED_P (d) = 1;
4676
4677 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
4678
4679 /* If this is a local class, keep track of it. We need this
4680 information for name-mangling, and so that it is possible to find
4681 all function definitions in a translation unit in a convenient
4682 way. (It's otherwise tricky to find a member function definition
4683 it's only pointed to from within a local class.) */
4684 if (TYPE_CONTEXT (type)
4685 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
4686 && !processing_template_decl)
4687 VARRAY_PUSH_TREE (local_classes, type);
4688 }
4689 if (b->kind == sk_class
4690 && !COMPLETE_TYPE_P (current_class_type))
4691 {
4692 maybe_add_class_template_decl_list (current_class_type,
4693 type, /*friend_p=*/0);
4694 CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
4695 }
4696 }
4697
4698 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
4699 /* Use the canonical TYPE_DECL for this node. */
4700 TYPE_STUB_DECL (type) = TYPE_NAME (type);
4701 else
4702 {
4703 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
4704 will be the tagged type we just added to the current
4705 binding level. This fake NULL-named TYPE_DECL node helps
4706 dwarfout.c to know when it needs to output a
4707 representation of a tagged type, and it also gives us a
4708 convenient place to record the "scope start" address for
4709 the tagged type. */
4710
4711 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
4712 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
4713 }
4714 timevar_pop (TV_NAME_LOOKUP);
4715 }

Then the important operation in the rest code is to update the context of the RECORD_TYPE.

(Click here to open)
Figure 58: Push tag of class SingleThreaded – step 5

cp_parser_class_head (continue)

12317 /* Indicate whether this class was declared as a `class' or as a
12318 `struct'. */
12319 if (TREE_CODE (type) == RECORD_TYPE)
12320 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12321 cp_parser_check_class_key (class_key, type);
12322
12323 /* Enter the scope containing the class; the names of base classes
12324 should be looked up in that context. For example, given:
12325
12326 struct A { struct B {}; struct C; };
12327 struct A::C : B {};
12328
12329 is valid. */
12330 if (nested_name_specifier)
12331 pop_p = push_scope (nested_name_specifier);
12332 /* Now, look for the base-clause. */
12333 token = cp_lexer_peek_token (parser->lexer);
12334 if (token->type == CPP_COLON)
12335 {
12336 tree bases;
12337
12338 /* Get the list of base-classes. */
12339 bases = cp_parser_base_clause (parser);
12340 /* Process them. */
12341 xref_basetypes (type, bases);
12342 }
12343 /* Leave the scope given by the nested-name-specifier. We will
12344 enter the class scope itself while processing the members. */
12345 if (pop_p)
12346 pop_scope (nested_name_specifier);
12347
12348 done:
12349 if (invalid_explicit_specialization_p)
12350 {
12351 end_specialization ();
12352 --parser->num_template_parameter_lists;
12353 }
12354 *attributes_p = attributes;
12355 return type;
12356 }

For the example, no token is between the class name and “{”, and nested_name_specifier is NULL as no nested-name-specifier part present; code above just rechecks the validation of the declaration at line 12319 and 12320, and no more. See that variable type is the tree node of RECORD_TYPE returned by xref_tag. And it is returned by the function to its caller too.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: