00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "cppEnumType.h"
00021 #include "cppTypedef.h"
00022 #include "cppExpression.h"
00023 #include "cppSimpleType.h"
00024 #include "cppScope.h"
00025 #include "cppParser.h"
00026 #include "indent.h"
00027
00028
00029
00030
00031
00032
00033 CPPEnumType::
00034 CPPEnumType(CPPIdentifier *ident, CPPScope *current_scope,
00035 const CPPFile &file) :
00036 CPPExtensionType(T_enum, ident, current_scope, file)
00037 {
00038 }
00039
00040
00041
00042
00043
00044
00045 void CPPEnumType::
00046 add_element(const string &name, CPPScope *scope, CPPExpression *value) {
00047 CPPType *type =
00048 CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
00049 CPPSimpleType::F_unsigned));
00050 CPPIdentifier *ident = new CPPIdentifier(name);
00051 CPPInstance *inst = new CPPInstance(type, ident);
00052 inst->_initializer = value;
00053 _elements.push_back(inst);
00054 scope->add_enum_value(inst);
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 bool CPPEnumType::
00064 is_incomplete() const {
00065 return false;
00066 }
00067
00068
00069
00070
00071
00072
00073 CPPDeclaration *CPPEnumType::
00074 substitute_decl(CPPDeclaration::SubstDecl &subst,
00075 CPPScope *current_scope, CPPScope *global_scope) {
00076 SubstDecl::const_iterator si = subst.find(this);
00077 if (si != subst.end()) {
00078 return (*si).second;
00079 }
00080
00081 CPPEnumType *rep = new CPPEnumType(*this);
00082 if (_ident != NULL) {
00083 rep->_ident =
00084 _ident->substitute_decl(subst, current_scope, global_scope);
00085 }
00086
00087 if (rep->_ident == _ident) {
00088 delete rep;
00089 rep = this;
00090 }
00091 rep = CPPType::new_type(rep)->as_enum_type();
00092 subst.insert(SubstDecl::value_type(this, rep));
00093
00094 return rep;
00095 }
00096
00097
00098
00099
00100
00101
00102 void CPPEnumType::
00103 output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
00104 if (!complete && _ident != NULL) {
00105
00106 if (cppparser_output_class_keyword) {
00107 out << _type << " ";
00108 }
00109 out << _ident->get_local_name(scope);
00110
00111 } else if (!complete && !_typedefs.empty()) {
00112
00113 out << _typedefs.front()->get_local_name(scope);
00114
00115 } else {
00116 out << _type;
00117 if (_ident != NULL) {
00118 out << " " << _ident->get_local_name(scope);
00119 }
00120
00121 out << " {\n";
00122 Elements::const_iterator ei;
00123 for (ei = _elements.begin(); ei != _elements.end(); ++ei) {
00124 indent(out, indent_level + 2) << (*ei)->get_local_name();
00125 if ((*ei)->_initializer != NULL) {
00126 out << " = " << *(*ei)->_initializer;
00127 }
00128 out << ",\n";
00129 }
00130 indent(out, indent_level) << "}";
00131 }
00132 }
00133
00134
00135
00136
00137
00138
00139 CPPDeclaration::SubType CPPEnumType::
00140 get_subtype() const {
00141 return ST_enum;
00142 }
00143
00144
00145
00146
00147
00148
00149 CPPEnumType *CPPEnumType::
00150 as_enum_type() {
00151 return this;
00152 }