lex_literal_to_ss_literal_info.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 #include <lestes/lang/cplus/lex/lex_literal.g.hh>
00029 #include <lestes/lang/cplus/lex/cpp_token.hh>
00030 #include <lestes/lang/cplus/lex/token_value.hh>
00031 #include <lestes/lang/cplus/sem/lex_literal_to_ss_literal_info.g.hh>
00032 #include <lestes/lang/cplus/sem/lex_literal_to_ss_literal_info.m.hh>
00033 #include <lestes/lang/cplus/sem/ss_literal_info.g.hh>
00034 #include <lestes/lang/cplus/sem/ss_type.g.hh>
00035 #include <lestes/lang/cplus/sem/ss_type_builtin.g.hh>
00036 #include <lestes/md/types/type_info.g.hh>
00037 
00038 package(lestes);
00039 package(lang);
00040 package(cplus);
00041 package(sem);
00042 
00043 using namespace ::lestes::md::types;
00044 
00045 ptr< ::lestes::lang::cplus::sem::ss_literal_info > lex_literal_to_ss_literal_info::process_token(ptr < ::lestes::lang::cplus::lex::cpp_token > tok)
00046 {
00047         lassert2(tok, "Cannot convert NULL token to ss_literal_info.");
00048         return lex_literal_to_ss_literal_info::create(tok)->process(tok->literal_get());
00049 }
00050 
00051 void lex_literal_to_ss_literal_info::visit_lex_integral_literal(ptr< lex::lex_integral_literal > ili)
00052 {
00053         ptr < ss_type > type;
00054         switch (ili->suffix_get()) {
00055         case lex::lex_integral_literal::NONE:
00056         {
00057                 if (ili->base_get() == lex::lex_integral_literal::DECIMAL) {
00058                         // FIFI: signed int, signed long int
00059                         ucn_string val = token->value_get()->content_get();
00060                         if (type_info::instance()->does_value_match_type(val, ss_type_sint::instance())) {
00061                                 type = ss_type_sint::instance();
00062                         } else if (type_info::instance()->does_value_match_type(val, ss_type_slong::instance())) {
00063                                 type = ss_type_slong::instance();
00064                         } else {
00065                                 // FIXME: error: [2.13.1/3]
00066                                 report << integer_literal_cannot_be_represented << val << token->location_get();
00067                                 type = ss_type_sint::instance();
00068                         }
00069                 } else {
00070                         ucn_string val = token->value_get()->content_get();
00071                         // FIFI: int, unsigned int, long int, unsigned long int
00072                         if (type_info::instance()->does_value_match_type(val, ss_type_sint::instance())) {
00073                                 type = ss_type_sint::instance();
00074                         } else if (type_info::instance()->does_value_match_type(val, ss_type_uint::instance())) {
00075                                 type = ss_type_uint::instance();
00076                         } else if (type_info::instance()->does_value_match_type(val, ss_type_slong::instance())) {
00077                                 type = ss_type_slong::instance();
00078                         } else if (type_info::instance()->does_value_match_type(val, ss_type_ulong::instance())) {
00079                                 type = ss_type_ulong::instance();
00080                         } else {
00081                                 // FIXME: error: [2.13.1/3]
00082                                 report << integer_literal_cannot_be_represented << val << token->location_get();
00083                                 type = ss_type_sint::instance();
00084                         }
00085                 }
00086                 break;
00087         }
00088         case lex::lex_integral_literal::UNSIGNED:
00089         {
00090                 // FIFI: unsigned int, unsigned long int
00091                 ucn_string val(token->value_get()->content_get().begin(), token->value_get()->content_get().begin() + ili->end_get());
00092                 if (type_info::instance()->does_value_match_type(val, ss_type_uint::instance())) {
00093                         type = ss_type_uint::instance();
00094                 } else if (type_info::instance()->does_value_match_type(val, ss_type_ulong::instance())) {
00095                         type = ss_type_ulong::instance();
00096                 } else {
00097                         // FIXME: error: [2.13.1/3]
00098                         report << integer_literal_cannot_be_represented << token->value_get()->content_get() << token->location_get();
00099                         type = ss_type_uint::instance();
00100                 }
00101                 break;
00102         }
00103         case lex::lex_integral_literal::LONG:
00104         {
00105                 // FIFI: signed long int, unsigned long int
00106                 ucn_string val(token->value_get()->content_get().begin(), token->value_get()->content_get().begin() + ili->end_get());
00107                 if (type_info::instance()->does_value_match_type(val, ss_type_slong::instance())) {
00108                         type = ss_type_slong::instance();
00109                 } else if (type_info::instance()->does_value_match_type(val, ss_type_ulong::instance())) {
00110                         type = ss_type_ulong::instance();
00111                 } else {
00112                         // FIXME: error: [2.13.1/3]
00113                         report << integer_literal_cannot_be_represented << token->value_get()->content_get() << token->location_get();
00114                         type = ss_type_slong::instance();
00115                 }
00116                 break;
00117         }
00118         case lex::lex_integral_literal::UNSIGNED_LONG:
00119         {
00120                 type = ss_type_ulong::instance();
00121                 // FIXME: check that fits
00122                 ucn_string val(token->value_get()->content_get().begin(), token->value_get()->content_get().begin() + ili->end_get());
00123                 if (!type_info::instance()->does_value_match_type(val, ss_type_ulong::instance())) {
00124                         // FIXME: error: [2.13.1/3]
00125                         report << integer_literal_cannot_be_represented << token->value_get()->content_get() << token->location_get();
00126                 }
00127                 break;
00128         }
00129         default:
00130                 lassert2(false,"You should never get here");
00131         }
00132         lassert(type);
00133         // TODO passes also 0x for hexa and suffixes, cut them ? the base can also be a problem
00134         result_set(ss_integral_literal_info::create(type, token->value_get()->content_get()));
00135         //lassert2(false, "FIXME: implement me");
00136 }
00137 
00138 void lex_literal_to_ss_literal_info::visit_lex_floating_literal( ptr< lex::lex_floating_literal > a )
00139 {
00140         (void)a;
00141         lassert2(false, "FIXME: implement me");
00142 }
00143 
00144 void lex_literal_to_ss_literal_info::visit_lex_character_literal( ptr< lex::lex_character_literal > cl )
00145 {
00146         // TODO pt takes only the last character
00147         ptr<ss_type> type = cl->wide_flag_get() ? ss_type_wchar_t::instance() : ss_type_pchar::instance();
00148 
00149         ucn_string u = token->value_get()->content_get();
00150         ulint val = character::extract_value(u[u.length() - 1]);
00151         
00152         ptr<ss_literal_info> lit = ss_integral_literal_info::create_from_number(type,val);
00153         result_set(lit);
00154 }
00155 
00156 void lex_literal_to_ss_literal_info::visit_lex_string_literal( ptr< lex::lex_string_literal > sl )
00157 {
00158         //lassert2(false, "FIXME: implement me");
00159 
00160         ptr < ss_type > type;
00161         if (sl->wide_flag_get()) {
00162                 type = ss_type_wchar_t::instance();
00163         } else {
00164                 type = ss_type_pchar::instance();
00165         }
00166 
00167         ucn_string ucs = token->value_get()->content_get();
00168         ptr < ss_type > arr_type = ss_array::instance(ucs.size()+1, ss_const::instance(type));
00169 
00170         ptr < list < srp < ss_literal_info > > > c = list < srp < ss_literal_info > >::create();
00171         ptr < ss_compound_literal_info > cli = ss_compound_literal_info::create(arr_type, c);
00172         
00173         ucn_string::iterator it = ucs.begin(), end = ucs.end();
00174 
00175         for ( ; it != end ; ++it ) {
00176                 c->push_back(ss_integral_literal_info::create_from_number(type, character::extract_value(*it)));
00177         }
00178         c->push_back(ss_integral_literal_info::create_from_number(type, 0));
00179 
00180         result_set(cli);
00181         
00182 }
00183 
00184 /*!
00185   The routine uses the different lengths of true/false keywords to distinguish
00186   between them, without the need to consider any encoding interaction.
00187  */
00188 void lex_literal_to_ss_literal_info::visit_lex_boolean_literal( ptr< lex::lex_boolean_literal >)
00189 {
00190         //bool value = token->value_get()->content_get() == "true";
00191         bool value = token->value_get()->content_get().size() == 4;
00192         result_set(ss_integral_literal_info::create_from_number(ss_bool::instance(), value));
00193 }
00194 
00195 end_package(sem);
00196 end_package(cplus);
00197 end_package(lang);
00198 end_package(lestes);
00199 

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