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 Expansion list itemm. 00030 00031 Definition of macro_item class representing part of macro expansion list. 00032 \author pt 00033 */ 00034 #include <lestes/common.hh> 00035 #include <lestes/equality.hh> 00036 #include <lestes/lang/cplus/lex/macro_item.hh> 00037 #include <lestes/lang/cplus/lex/token_sequence.hh> 00038 #include <lestes/lang/cplus/lex/pp_token.hh> 00039 00040 #include <iostream> 00041 00042 package(lestes); 00043 package(lang); 00044 package(cplus); 00045 package(lex); 00046 00047 /*! 00048 Returns literal macro item, initializes with a_value. 00049 \pre a_value != NULL 00050 \param a_value The initialization value. 00051 \return Macro item representing literal. 00052 */ 00053 ptr<macro_item> macro_item::create_literal(const ptr<token_sequence> &a_value) 00054 { 00055 lassert(a_value); 00056 // the index and blank is not used 00057 return new macro_item(LITERAL,0,a_value->clone()); 00058 } 00059 00060 /*! 00061 Returns expansion macro item, initializes with a_index. 00062 \param a_index The index of referenced parameter. 00063 \return Macro item representing parameter expansion. 00064 */ 00065 ptr<macro_item> macro_item::create_expansion(index_type a_index) 00066 { 00067 // the value parameter is not used 00068 return new macro_item(EXPANSION,a_index,NULL); 00069 } 00070 00071 /*! 00072 Returns copy macro item, initializes with a_index. 00073 \param a_index The index of referenced parameter. 00074 \return Macro item representing parameter copy without expansion. 00075 */ 00076 ptr<macro_item> macro_item::create_copy(index_type a_index) 00077 { 00078 // the value and blank is not used 00079 return new macro_item(COPY,a_index,NULL); 00080 } 00081 00082 /*! 00083 Returns stringification macro item, initializes with index and blank flag. 00084 \pre a_value != NULL 00085 \param a_index The index of referenced parameter. 00086 \param a_value The stringification operator with optional space. 00087 \return Macro item representing parameter stringification. 00088 */ 00089 ptr<macro_item> macro_item::create_str(index_type a_index, const ptr<token_sequence> &a_value) 00090 { 00091 lassert(a_value); 00092 // the value is not used 00093 return new macro_item(STR,a_index,a_value->clone()); 00094 } 00095 00096 /*! 00097 Returns concatenation macro item, initializes with sequence containing a_token. 00098 \pre a_value != NULL 00099 \param a_value The concatenation operator. 00100 \return Macro item representing token concatenation 00101 */ 00102 ptr<macro_item> macro_item::create_concat(const ptr<token_sequence> &a_value) 00103 { 00104 lassert(a_value); 00105 00106 // the index and blank is not used 00107 return new macro_item(CONCAT,0,a_value->clone()); 00108 } 00109 00110 /*! 00111 Creates item object, not all parameters are valid for every action type. 00112 No checks of consistency are done on this level. 00113 \param a_action The action represented by the item. 00114 \param a_index The index of parameter for expand, nonexpand and stringify actions. 00115 \param a_value The value of item for literal and operators. 00116 */ 00117 macro_item::macro_item(action_type a_action, index_type a_index, const ptr<token_sequence> &a_value): 00118 action(a_action), 00119 index(a_index), 00120 value(a_value) 00121 { 00122 } 00123 00124 /*! 00125 Returns action of the macro item. 00126 \return The action represented by the macro item. 00127 */ 00128 macro_item::action_type macro_item::action_get(void) const 00129 { 00130 return action; 00131 } 00132 00133 /*! 00134 Returns value of stored token sequence literal. 00135 \pre action_get() != EXPANSION && action_get() != COPY 00136 \return The represented value. 00137 */ 00138 ptr<token_sequence> macro_item::value_get(void) const 00139 { 00140 lassert(action_get() != EXPANSION && action_get() != COPY); 00141 return value->clone(); 00142 } 00143 00144 /*! 00145 Returns index of the macro parameter. 00146 \pre action_get() == EXPAND || action_get() == COPY || action_get() == STR 00147 \return The index of macro parameter referenced in action. 00148 */ 00149 macro_item::index_type macro_item::index_get(void) const 00150 { 00151 lassert(action_get() == EXPANSION || action_get() == COPY || action_get() == STR); 00152 return index; 00153 } 00154 00155 /*! 00156 Tests equality to other macro item. Only fields relevant for selected actions are compared. 00157 \param other The macro item to compare with. 00158 \return true If both actions and their respective parameters are equal. 00159 */ 00160 bool macro_item::equals(const ptr<macro_item> &other) const 00161 { 00162 if (!other || action_get() != other->action_get()) return false; 00163 00164 // TODO pt make resembles back equals 00165 switch (action_get()) { 00166 case LITERAL: 00167 return value->congruent(other->value); 00168 case EXPANSION: 00169 case COPY: 00170 return is_equal(index_get(),other->index_get()); 00171 case STR: 00172 return is_equal(index_get(),other->index_get()) && value->congruent(other->value); 00173 case CONCAT: 00174 return value->congruent(other->value); 00175 default: 00176 lassert2(false,"You should never get here"); 00177 } 00178 return false; 00179 } 00180 00181 /*! 00182 Marks the object. 00183 */ 00184 void macro_item::gc_mark(void) 00185 { 00186 value.gc_mark(); 00187 object::gc_mark(); 00188 } 00189 00190 end_package(lex); 00191 end_package(cplus); 00192 end_package(lang); 00193 end_package(lestes); 00194 /* vim: set ft=lestes : */
1.5.1-20070107