home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Programming / GDBbundle-1.0-MIS / src / TextEdit / GdbBundle.bproj / Variable.m < prev   
Encoding:
Text File  |  1997-04-13  |  7.7 KB  |  333 lines

  1. /* Variable.m created by ovidiu on Sun 30-Mar-1997 */
  2.  
  3. #include <stdio.h>
  4.  
  5. #import <Foundation/NSString.h>
  6. #import <Foundation/NSArray.h>
  7. #import <Foundation/NSValue.h>
  8. #import "GdbDisplayController.h"
  9. #import "Frame.h"
  10. #import "Variable.h"
  11.  
  12. #define ASSIGN(variable, value) \
  13.   [value retain]; \
  14.   [variable release]; \
  15.   variable = value;
  16.  
  17.  
  18. @implementation Variable
  19. - (void)setName:(NSString*)aString        { ASSIGN(name, aString); }
  20. - (NSString*)name                { return name; }
  21. - copyWithZone:(NSZone*)zone            { return self; }
  22.  
  23. - (void)setValue:(Value*)aValue
  24. {
  25.   ASSIGN(value, aValue);
  26.   [value setGdbDisplayController:gdbController];
  27. }
  28.  
  29. - (void)setGdbDisplayController:(GdbDisplayController*)anObject
  30. {
  31.   gdbController = anObject;
  32.   [value setGdbDisplayController:gdbController];
  33. }
  34.  
  35. - (void)dealloc
  36. {
  37.   [name release];
  38.   [value release];
  39.   [super dealloc];
  40. }
  41.  
  42. - (NSString*)description
  43. {
  44.   return [NSString stringWithFormat:@"%@ = %@", name, value];
  45. }
  46.  
  47. /* GdbDisplayValue protocol's methods */
  48.  
  49. - (NSString*)title                { return name; }
  50. - (NSString*)stringValue                   { return [value stringValue]; }
  51. - (NSString*)additionalDescription        { return nil; }
  52. - (Value*)value                    { return value; }
  53.  
  54. - (NSArray*)indirectedComponents
  55. {
  56.   return [value indirectedComponents];
  57. }
  58.  
  59. - (BOOL)indirectedComponentsAreKnown
  60. {
  61.   return [value indirectedComponentsAreKnown];
  62. }
  63.  
  64. - (void)getIndirectedComponentsFromGDB
  65. {
  66.   [value getIndirectedComponentsFromGDB];
  67. }
  68.  
  69. @end /* Variable */
  70.  
  71.  
  72. @implementation Type
  73. - (void)setTypeName:(NSMutableString*)typeName    { ASSIGN(name, typeName); }
  74. - (void)setScalarTypeKind:(tScalarTypeKind)type    { scalarKind = type; }
  75. - (NSMutableString*)typeName            { return name; }
  76. - (tTypeKind)typeKind                { return kind; }
  77. - (tScalarTypeKind)scalarTypeKind        { return scalarKind; }
  78. - (NSString*)description            { return name; }
  79.  
  80. - (void)setTypeKind:(tTypeKind)type
  81. {
  82.   if (kind == kScalar && scalarKind == kChar && type == kPointer)
  83.     kind = kCString;
  84.   else if (kind == kVoid && type == kPointer)
  85.     kind = kVoidPointer;
  86.   else
  87.     kind = type;
  88. }
  89.  
  90. - (Class)valueClass
  91. {
  92.   switch (kind) {
  93.     case kVoid:        return [VoidValue class];
  94.     case kScalar:    return [SimpleValue class];
  95.     case kEnum:        return [EnumerationValue class];
  96.     case kStructure:    return [StructureValue class];
  97.     case kClass:    return [ClassValue class];
  98.     case kUnion:    return [UnionValue class];
  99.     case kArray:    return [ArrayValue class];
  100.     case kCString:
  101.     case kVoidPointer:
  102.     case kPointer:    return [PointerValue class];
  103.     default:        return nil;
  104.   }
  105. }
  106.  
  107. - (void)dealloc
  108. {
  109.   [name release];
  110.   [super dealloc];
  111. }
  112. @end /* Type */
  113.  
  114.  
  115. @implementation Value
  116. - (void)setType:(Type*)aType            { ASSIGN(type, aType); }
  117. - (Type*)type                    { return type; }
  118. - (void)setStringValue:(NSString*)value               {}
  119.  
  120. - (void)setGdbDisplayController:(GdbDisplayController*)anObject
  121. {
  122.   gdbController = anObject;
  123. }
  124.  
  125. - (void)dealloc
  126. {
  127.   [type release];
  128.   [super dealloc];
  129. }
  130.  
  131. - copyWithZone:(NSZone*)zone
  132. {
  133.   return [self retain];
  134. }
  135.  
  136. /* GdbDisplayValue protocol's methods */
  137. - (NSString*)title                { return @""; }
  138. - (NSString*)stringValue            { return @""; }
  139. - (NSString*)additionalDescription        { return nil; }
  140. - (Value*)value                    { return self; }
  141. - (NSArray*)indirectedComponents        { return nil; }
  142. - (BOOL)indirectedComponentsAreKnown        { return YES; }
  143. - (void)getIndirectedComponentsFromGDB        {}
  144. @end /* Value */
  145.  
  146.  
  147. @implementation VoidValue
  148. - (NSString*)description            { return @"void "; }
  149. @end
  150.  
  151.  
  152. @implementation SimpleValue
  153. + (SimpleValue*)numberWithString:(NSString*)string
  154. {
  155.   SimpleValue* value = [[[self alloc] init] autorelease];
  156.   value->stringNumber = [string copy];
  157.   return value;
  158. }
  159.  
  160. - (void)dealloc
  161. {
  162.   [stringNumber release];
  163.   [description release];
  164.   [super dealloc];
  165. }
  166.  
  167. - (void)setStringValue:(NSString*)value        { ASSIGN(stringNumber, value); }
  168. - (void)setDescription:(NSString*)str        { ASSIGN(description, str); }
  169. - (NSString*)additionalDescription        { return description; }
  170. - (NSString*)stringValue            { return stringNumber; }
  171.  
  172. - (NSString*)description
  173. {
  174.   if (description)
  175.     return [NSString stringWithFormat:@"(%@) %@ %@",
  176.                 type, stringNumber, description];
  177.   else
  178.     return [NSString stringWithFormat:@"(%@) %@", type, stringNumber];
  179. }
  180. @end /* SimpleValue */
  181.  
  182.  
  183. @implementation EnumerationValue
  184. - (void)setStringValue:(NSString*)val        { ASSIGN(value, val); }
  185. - (NSString*)stringValue            { return value; }
  186.  
  187. - (void)dealloc
  188. {
  189.   [value release];
  190.   [super dealloc];
  191. }
  192.  
  193. - (NSString*)description
  194. {
  195.   return [NSString stringWithFormat:@"(%@) %@", type, value];
  196. }
  197. @end
  198.  
  199.  
  200. @implementation AggregateValue
  201. - (BOOL)isStructure                { return NO; }
  202. - (BOOL)isClass                    { return NO; }
  203. - (BOOL)isUnion                    { return NO; }
  204. - (BOOL)isArray                    { return NO; }
  205. - (NSArray*)indirectedComponents        { return componentValues; }
  206.  
  207. - (void)setArrayValue:(NSMutableArray*)value
  208. {
  209.   int i, count = [value count];
  210.  
  211.   ASSIGN(componentValues, value);
  212.   for (i = 0; i < count; i++)
  213.     [[componentValues objectAtIndex:i] setGdbDisplayController:gdbController];
  214. }
  215.  
  216. - (void)dealloc
  217. {
  218.   [componentValues release];
  219.   [super dealloc];
  220. }
  221.  
  222. - (NSString*)description
  223. {
  224.   NSMutableString* description = [NSMutableString string];
  225.   int i, count = [componentValues count];
  226.   BOOL firstSeen = NO;
  227.  
  228.   [description appendFormat:@"(%@) {\n", type];
  229.   for (i = 0; i < count; i++) {
  230.     if (firstSeen)
  231.       [description appendString:@",\n"];
  232.     else
  233.       firstSeen = YES;
  234.     [description appendString:[[componentValues objectAtIndex:i] description]];
  235.   }
  236.   [description appendString:@"}\n"];
  237.  
  238.   return description;
  239. }
  240.  
  241. @end /* AggregateValue */
  242.  
  243. @implementation StructureValue
  244. - (BOOL)isStructure        { return YES; }
  245. @end
  246.  
  247. @implementation ClassValue
  248. - (BOOL)isClass            { return YES; }
  249. @end
  250.  
  251. @implementation UnionValue
  252. - (BOOL)isUnion            { return YES; }
  253. @end
  254.  
  255. @implementation ArrayValue
  256. - (BOOL)isArray            { return YES; }
  257. @end
  258.  
  259.  
  260. @implementation PointerValue
  261. + (PointerValue*)pointerWithAddress:(NSString*)address
  262. {
  263.   PointerValue* value = [[[self alloc] init] autorelease];
  264.   value->addressString = [address copy];
  265.   return value;
  266. }
  267.  
  268. - (void)setStringValue:(NSString*)val          { ASSIGN(addressString, val); }
  269. - (void)setDescription:(NSString*)str        { ASSIGN(description, str); }
  270.  
  271. /* GdbDisplayValue protocol's methods */
  272. - (id)stringValue                      { return addressString; }
  273. - (NSString*)additionalDescription        { return description; }
  274.  
  275. - (NSArray*)indirectedComponents
  276. {
  277.   if (indirectedValue) {
  278.     if ([indirectedValue isKindOfClass:[NSArray class]])
  279.       return indirectedValue;
  280.     else
  281.       return [NSArray arrayWithObject:indirectedValue];
  282.   }
  283.   else
  284.     return nil;
  285. }
  286.  
  287. - (BOOL)indirectedComponentsAreKnown
  288. {
  289.   return indirectedValue != nil;
  290. }
  291.  
  292. - (void)getIndirectedComponentsFromGDB
  293. {
  294.   NSString* command;
  295.  
  296.   /* Avoid recursivity in the case of a parse error in the GDB output */
  297.   if (printCommandExecuted)
  298.     return;
  299.  
  300.   command = [NSString stringWithFormat:@"print-typed *(%@)%@",
  301.                                         [self type], [self stringValue]];
  302. //  NSLog (@"execute '%@'", command);
  303.   [gdbController executeGDBCommand:command
  304.                           annotate:NO
  305.                       notifyObject:self
  306.                           selector:@selector(indirectedValue:type:)];
  307. }
  308.  
  309. - (void)indirectedValue:(NSString*)gdbOutput type:(GdbOutputType)gdbOutputType
  310. {
  311.   NSArray* array;
  312.  
  313.   if (gdbOutputType != GDB_OUTPUT_ANNOTATION)
  314.     return;
  315.  
  316. //  NSLog (@"received '%@'", gdbOutput);
  317.   printCommandExecuted = YES;
  318.   [indirectedValue release];
  319.   array = [Variable variablesFromDescription:gdbOutput];
  320.   indirectedValue = [[array objectAtIndex:0] value];
  321.  
  322.   /* If the value is a structure then indirect it */
  323.   if ((array = [indirectedValue indirectedComponents]))
  324.     indirectedValue = array;
  325.   [indirectedValue retain];
  326.  
  327.   [indirectedValue setGdbDisplayController:gdbController];
  328.  
  329.   [gdbController updateCurrentFrame];
  330. }
  331.  
  332. @end /* PointerValue */
  333.