On 4/13/12, Chiheng Xu <chiheng...@gmail.com> wrote: > On Apr 9, 2012, Richard Guenther <richard.guent...@gmail.com> wrote: >>> Certainly there are cases where the type must be made more specific, >>> and getting the wrong type here would necessarily be a dynamic check. >>> However, the number of dynamic checks can be substantially reduced. >>> To provide a specific example, suppose I have a common_decl *p and >>> need to do extra work if it is a var_decl. >>> >>> do_general_work (p); >>> if (var_decl *q = p->to_var ()) >>> { >>> do_var_work_1 (q); >>> do_var_work_2 (q); >>> do_var_work_3 (q); >>> do_var_work_4 (q); >>> } >>> >>> The only dynamic work is in the pointer conversion. All other >>> function calls can be statically typed. >> >> Ok. But the above represents a completely different programming >> style than what we use currently. We do >> >> if (is_var_decl (p)) >> { >> do_var_work_1 (p); >> ... >> } >> >> so what I was refering to was static errors we get when we are >> able to promote function argument / return types to more specific >> sub-classes. > > What about this: > if(is_var_decl(p)) { > var_decl * p_var_decl = (var_decl *) p; > do_var_work_1 (p_var_decl); > }else if(is_type_decl(p)){ > type_decl * p_type_decl = (type_decl *) p; > do_type_work_2 (p_type_decl); > }else if(is_field_decl(p)){ > field_decl * p_field_decl = (field_decl *) p; > do_field_work_3 (p_field_decl); > }
That approach will certainly work, but makes the correctness of the assignment contingent on the condition being right. If the condition changes in appropriately, you can get a bug. Even so, I think what you suggest is a good intermediate step. -- Lawrence Crowl