as_declarator_op2ss_type.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 /*! \file
00029   \brief Declarator operator visitor.
00030 
00031   Definition of classes performing declarator operator analysis.
00032   \author pt
00033 */
00034 #include <lestes/common.hh>
00035 #include <lestes/std/source_location.hh>
00036 #include <lestes/lang/cplus/lex/cpp_token.hh>
00037 #include <lestes/lang/cplus/sem/as_declarator_op2ss_type.g.hh>
00038 #include <lestes/lang/cplus/sem/sa_param_declaration_type.g.hh>
00039 #include <lestes/lang/cplus/sem/sa_param_declaration_empty.g.hh>
00040 #include <lestes/lang/cplus/sem/as_cv_qualifier2ss_type.g.hh>
00041 #include <lestes/lang/cplus/sem/ss_type.g.hh>
00042 #include <lestes/lang/cplus/sem/ss_type2cv_unqualified.g.hh>
00043 #include <lestes/lang/cplus/sem/as_decl.g.hh>
00044 #include <lestes/lang/cplus/sem/ss_type2info.g.hh>
00045 #include <lestes/lang/cplus/sem/ss_type_builtin.g.hh>
00046 #if 0
00047 #include <lestes/lang/cplus/sem/ss_misc.g.hh>
00048 #include <lestes/lang/cplus/sem/sa_declarator.g.hh>
00049 #include <lestes/lang/cplus/sem/sa_decl_spec.g.hh>
00050 #include <lestes/lang/cplus/sem/as_other.g.hh>
00051 #include <lestes/lang/cplus/sem/ss_decl_name.g.hh>
00052 #include <lestes/lang/cplus/sem/ss_expression.g.hh>
00053 #endif
00054 
00055 package(lestes);
00056 package(lang);
00057 package(cplus);
00058 package(sem);
00059 
00060 /*!
00061   Processes the declarator operator, updating the fields.
00062   \pre dop != NULL
00063   \param dop  The declarator operator to process.
00064 */
00065 void as_declarator_op2ss_type::process(ptr<as_declarator_op> dop)
00066 {
00067         dop->accept_as_declarator_op_visitor(this);
00068 }
00069 
00070 /*!
00071   Visits the star declarator operator.
00072   \pre dop != NULL
00073   \dop  The declarator operator to visit.
00074 */
00075 void as_declarator_op2ss_type::visit_as_ptr_op_star(ptr<as_ptr_op_star> dop)
00076 {
00077         lassert(dop);
00078 
00079         typedef list< srp<as_cv_qualifier> > as_cv_qualifier_list;
00080         
00081         ptr<as_cv_qualifier2ss_type> acqst =
00082                 as_cv_qualifier2ss_type::create(ss_pointer::instance(type));
00083 
00084         ptr<as_cv_qualifier_list> lst = dop->cv_qualifiers_get();
00085         
00086         for (as_cv_qualifier_list::iterator it = lst->begin(), end = lst->end();
00087                         it != end; ++it) {
00088                 acqst->process(*it);
00089         }
00090 
00091         type = acqst->type_get();
00092 }
00093 
00094 /*!
00095   Visits the ampersand declarator operator.
00096   \pre dop != NULL
00097   \dop  The declarator operator to visit.
00098 */
00099 void as_declarator_op2ss_type::visit_as_ptr_op_amp(ptr<as_ptr_op_amp> dop)
00100 {
00101         lassert(dop);
00102         
00103         // TODO pt make own visitor for is_void???
00104         if (ss_type2cv_unqualified::create()->process(type)->is_void()) {
00105                 // TODO pt report error: reference to void
00106                 return;
00107         }
00108 
00109         if (ss_type2info::create()->process(type) == ss_type2info::SS_REFERENCE) {
00110                 // TODO pt report error: reference to reference
00111                 return;
00112         }
00113         
00114         type = ss_reference::instance(type);
00115 }
00116 
00117 /*!
00118   Visits the function declarator operator.
00119   \pre dop != NULL
00120   \dop  The declarator operator to visit.
00121 */
00122 void as_declarator_op2ss_type::visit_as_declarator_op_func(ptr<as_declarator_op_func> dop)
00123 {
00124         lassert(dop);
00125   
00126         typedef ::lestes::std::list< srp<as_param_declaration> > as_param_declaration_list_type;
00127         typedef ::lestes::std::list< srp<ss_type> > ss_type_list_type;
00128   
00129         if (ss_type2info::create()->process(type) == ss_type2info::SS_FUNCTION) {
00130                 // TODO pt report error: function returning function
00131         }
00132 
00133         ptr<as_param_decl_clause> apdc = dop->parameters_get();
00134         bool ellipsis = apdc->ellipsis_get();
00135 
00136         ptr<ss_type_list_type> sstl = ss_type_list_type::create();
00137 
00138         ptr<as_param_declaration_list_type> apdl = apdc->parameters_get();
00139         ptr<sa_param_declaration_type> sapdt = sa_param_declaration_type::create();
00140 
00141         //  special (void) parameter 
00142         if (apdl->size() != 1 || !sa_param_declaration_empty::create()->process(apdl->front())) {
00143                 for (as_param_declaration_list_type::iterator it = apdl->begin(), end = apdl->end();
00144                                         it != end; ++it) {
00145                         
00146                         // fixes types for function type use
00147                         sapdt->process(*it);
00148                         sstl->push_back(sapdt->type_get());
00149                 }
00150         }
00151 
00152         // create the type from the op
00153         type = ss_function::instance(type,sstl,ellipsis);
00154 }
00155 
00156 #if 0
00157 /*!
00158   Visits the function declarator operator.
00159   \pre dop != NULL
00160   \dop  The declarator operator to visit.
00161 */
00162 void as_declarator_op2ss_type::visit_as_declarator_op_func(ptr<as_declarator_op_func> dop)
00163 {
00164         lassert(dop);
00165   
00166         typedef ::lestes::std::list< srp<as_param_declaration> > as_param_declaration_list_type;
00167         typedef ::lestes::std::list< srp<ss_type> > ss_type_list_type;
00168   // TODO pt remove typedef ::lestes::std::list< srp<ss_parameter_declaration> > ss_parameter_declaration_list_type;
00169   
00170         ptr<ss_type2info> ssti = ss_type2info::create();
00171         ssti->process(type);
00172 
00173         if (ssti->info_get() == ss_type2info::SS_FUNCTION) {
00174                 // TODO pt report error: function returning function
00175         }
00176 
00177         ptr<as_param_decl_clause> apdc = dop->parameters_get();
00178         bool ellipsis = apdc->ellipsis_get();
00179 
00180         ptr<ss_type_list_type> sstl = ss_type_list_type::create();
00181         parameters = ss_type_list_type::create();
00182 
00183         ptr<as_param_declaration_list_type> apdl = apdc->parameters_get();
00184         ptr<sa_param_declaration_type> sapdt = sa_param_declaration_type::create(apdl->size() == 1/*TODO context*/);
00185         
00186         {
00187                 as_param_declaration_list_type::iterator it = apdl->begin(), end = apdl->end();
00188                 
00189                 if (apdl->size() == 1)
00190                 if (it != end) {
00191                         sapdt->process(*it);
00192                         ++it;
00193 
00194                         if (it
00195                 }
00196 
00197                         
00198         for (as_param_declaration_list_type::iterator it = apdl->begin(), end = apdl->end();
00199                                 it != end; ++it) {
00200                 
00201                 sapdt->process(*it);
00202                 // fixes types for function type use
00203                 sstl->push_back(sapdt->norm_type_get());
00204                 parameters->push_back(sapdt->param_type_get());
00205         }
00206         
00207         if (sstl->size() == 1 && sslt->begin()->is_void()) {
00208                 // one void parameter means no parameters at all
00209                 sstl->clear();
00210                 parameters->clear();
00211         } else {
00212                 // check for void
00213                 for (ss_type_list_type::iterator it =  sstl->begin(), end = sstl->end();
00214                                 it != end; ++it) {
00215                         if ((*it)->is_void()) {
00216                                 // TODO pt report error: parameter declared void
00217                                 *it = ss_sint::instance();
00218                         }
00219                 }
00220         }
00221 
00222         // create the type from the op
00223 
00224         // TODO pt how to distinguish ss_function from ss_member_function ???
00225         // by looking at the name and context
00226         // not in this place, here only ss_function will be possible, as this is not used at toplevel
00227         type = ss_function::instance(type,sstl,ellipsis);
00228         type = ss_member_function::instance(type,sstl,ellipsis);
00229 }
00230 #endif
00231 
00232 /*!
00233   Visits the array declarator operator.
00234   \pre dop != NULL
00235   \dop  The declarator operator to visit.
00236 */
00237 void as_declarator_op2ss_type::visit_as_declarator_op_array(ptr<as_declarator_op_array> dop)
00238 {
00239         lassert(dop);
00240         
00241         if (type->is_void()) {
00242                 // TODO pt report error: array of void
00243                 // make it int defaultly
00244                 type = ss_type_sint::instance();
00245         } else {
00246                 ptr<ss_type2info> sst2i = ss_type2info::create();
00247                 sst2i->process(type);
00248                 switch (ss_type2info::create()->process(type)) {
00249                         case ss_type2info::SS_REFERENCE:
00250                                 // TODO pt report error: array of references
00251                                 type = ss_type_sint::instance();
00252                                 break;
00253                         case ss_type2info::SS_FUNCTION:
00254                                 // TODO pt report error: array of functions
00255                                 type = ss_type_sint::instance();
00256                                 break;
00257                         default:
00258                                 break;
00259                 }
00260         }
00261         // TODO pt check type is not abstract class
00262 
00263         // TODO pt get the actulal size, check 0 ??
00264         // t_size size = evaluate(dop->constant_expression_get());
00265 
00266         t_size size = 1;
00267         type = ss_array::instance(size,type);
00268 }
00269 
00270 /*!
00271   Visits the array declarator operator.
00272   \pre dop != NULL
00273   \dop  The declarator operator to visit.
00274 */
00275 void as_declarator_op2ss_type::visit_as_declarator_op_non_constant_array(ptr<as_declarator_op_non_constant_array> dop)
00276 {
00277         lassert(dop);
00278 
00279         // TODO pt
00280         lassert2(false,"Non constant array is not supported.");
00281 }
00282 
00283 /*!
00284   Visits the member pointer declarator operator.
00285   \pre dop != NULL
00286   \dop  The declarator operator to visit.
00287   \todo pt Not supported yet.
00288 */
00289 void as_declarator_op2ss_type::visit_as_ptr_op_member_ptr(ptr<as_ptr_op_member_ptr> dop)
00290 {
00291         lassert(dop);
00292 
00293         // TODO pt Not supported
00294         lassert2(false,"Member pointer not supported.");
00295 }
00296 
00297 #if 0
00298         FUCK THIS ALL
00299         // TODO pt containing scope? where from?
00300         // no scope needed for processing of types
00301         // otherwise create the scope for parameters
00302         // with parent
00303 
00304         ptr<sa_param_decl_context> sapdc = sa_param_decl_context::create(declaration_context::CTX_PARAMETER,
00305                         
00306                         //ss_decl_seq::root_instance()
00307                         // FIXME
00308                         NULL
00309                         
00310                         );
00311         ptr<sa_param_decl> sapd = sa_param_decl::create(sapdc);
00312         
00313         // only process parameters thoroughly when actually creating function declaration
00314         // TODO pt what the fuck? only processing types, 
00315         if (last_flag) {
00316                 parameters = ss_parameter_declaration_list_type::create();
00317                 ulint position = 0;
00318 
00319                 for (as_param_declaration_list_type::iterator it = apdl->begin(), end = apdl->end();
00320                                 it != end; ++it) {
00321                         
00322                         sapd->process(*it,++position);
00323                         parameters->push_back(sapd->declaration_get());
00324                         sstl->push_back(sapd->type_get());
00325                 }
00326         
00327         } else {
00328         for (as_param_declaration_list_type::iterator it = apdl->begin(), end = apdl->end();
00329                                 it != end; ++it) {
00330                 
00331                 sapd->process(*it);
00332                 sstl->push_back(sapd->type_get());
00333         }
00334         
00335         }
00336 #endif
00337         
00338 #if 0
00339 /*!
00340   Visits the function declarator operator in parameter.
00341   \pre dop != NULL
00342   \dop  The declarator operator to visit.
00343 */
00344 void as_declarator_op2ss_type_param::visit_as_declarator_op_func(ptr<as_declarator_op_func> dop)
00345 {
00346         lassert(dop);
00347   
00348         typedef ::lestes::std::list< srp<as_param_declaration> > as_param_declaration_list_type;
00349         typedef ::lestes::std::list< srp<ss_type> > ss_type_list_type;
00350         typedef ::lestes::std::list< srp<ss_parameter_declaration> > ss_parameter_declaration_list_type;
00351   
00352         // TODO pt check type for function and return error: function returning function
00353         // but how? we lost the function flag, shall it be restored??
00354         
00355         ptr<as_param_decl_clause> apdc = dop->parameters_get();
00356         bool ellipsis = apdc->ellipsis_get();
00357         ptr<as_param_declaration_list_type> apdl = apdc->parameters_get();
00358         ptr<ss_type_list_type> sstl = ss_type_list_type::create();
00359         
00360         // TODO pt containing scope
00361         ptr<sa_param_decl_context> sapdc = sa_param_decl_context::create(declaration_context::CTX_PARAMETER,
00362                         
00363                         //ss_decl_seq::root_instance()
00364                         // FIXME
00365                         NULL
00366                         );
00367 
00368         ptr<sa_param_decl> sapd = sa_param_decl::create(sapdc);
00369         
00370         for (as_param_declaration_list_type::iterator it = apdl->begin(), end = apdl->end();
00371                         it != end; ++it) {
00372                 
00373                 sapd->process(*it);
00374                 sstl->push_back(sapd->type_get());
00375         }
00376         
00377         ptr<ss_type> sst = type_get();
00378         // create the type from the op
00379         sst = ss_function::instance(sst,sstl,ellipsis);
00380         // in parameters function is equivalent to pointer to function
00381         if (last_flag_get()) sst = ss_pointer::instance(sst);
00382         type_set(sst);
00383 }
00384 
00385 /*!
00386   Visits the function declarator operator without creating parameter declarations.
00387   \pre dop != NULL
00388   \dop  The declarator operator to visit.
00389 */
00390 void as_declarator_op2ss_type_plain::visit_as_declarator_op_func(ptr<as_declarator_op_func> dop)
00391 {
00392         lassert(dop);
00393   
00394         typedef ::lestes::std::list< srp<as_param_declaration> > as_param_declaration_list_type;
00395         typedef ::lestes::std::list< srp<ss_type> > ss_type_list_type;
00396   
00397         // TODO pt check type for function and return error: function returning function
00398         // but how? we lost the function flag, shall it be restored??
00399         
00400         ptr<as_param_decl_clause> apdc = dop->parameters_get();
00401         bool ellipsis = apdc->ellipsis_get();
00402         ptr<as_param_declaration_list_type> apdl = apdc->parameters_get();
00403         ptr<ss_type_list_type> sstl = ss_type_list_type::create();
00404         ptr<sa_param_decl_context> sapdc = sa_param_decl_context::create(
00405                         declaration_context::CTX_PARAMETER,
00406                         //TODO
00407                         NULL
00408                         );
00409         ptr<sa_param_decl> sapd = sa_param_decl::create(sapdc);
00410         
00411         // only process types of parameters
00412         for (as_param_declaration_list_type::iterator it = apdl->begin(), end = apdl->end();
00413                         it != end; ++it) {
00414                 
00415                 sapd->process(*it);
00416                 sstl->push_back(sapd->type_get());
00417         }
00418         
00419         // create the type from the op
00420         type_set(ss_function::instance(type_get(),sstl,ellipsis));
00421 }
00422 
00423 #endif
00424 
00425 end_package(sem);
00426 end_package(cplus);
00427 end_package(lang);
00428 end_package(lestes);
00429 
00430 /* vim: set ft=lestes : */

Generated on Mon Feb 12 18:22:21 2007 for lestes by doxygen 1.5.1-20070107