📄 cp_type.c
字号:
/*Copyright (c) 2000, Red Hat, Inc.This file is part of Source-Navigator.Source-Navigator is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public License as publishedby the Free Software Foundation; either version 2, or (at your option)any later version.Source-Navigator is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith Source-Navigator; see the file COPYING. If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330, Boston,MA 02111-1307, USA.*/#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <tcl.h>#include "crossrefP.h"#define TokenSave() int iTokenA_Save = iTokenA#define TokenRestore() iTokenA = iTokenA_Save#undef TEST_TYPEstatic int iTokenA;static int iTokenT;static Declaration_t f_TypeCastToDeclaration( Type_t Type );static Type_t TypeName( void );static Declarator_t AbstractDeclarator( void );static Oper_t PtrOperator( void );static List_t FunctionOrArrayList( void );static void CvQualifierList( Oper_t Oper );static Name_t CompleteClassName( void );static List_t ArgumentDeclarationList( void );static void f_TypeStrcat( char *pc, Type_t Type, int exact );static void TokenInit( char *pc );static int Token( int i );static void TokenStep( int i );static char *TokenIdent( int i );static void TokenRead( int i );static int TokenLex( void );static void TemplateArgList( char *pname );struct _sToken{ int iToken; char *pcToken; int iLength;};static struct _sToken asToken[10000]; /* old: 1000 */static char *pcLex;static char *pcLexBeg;static char *pcToken;static int iLength;extern Type_t f_TypeCreate( void ){ Type_t Type = (Type_t) Malloc( sizeof( Type[0] )); memset((void*) Type, 0, sizeof( Type[0] )); Type->iCheck = TYPE_CHECK; return Type;}extern Type_t f_TypeDuplicate( Type_t Type ){ Type_t TypeDuplicate; if( Type == 0 ) { return 0; } if( Type->iCheck != TYPE_CHECK ) Abort(); TypeDuplicate = f_TypeCreate(); TypeDuplicate->s_unknown = Type->s_unknown ; TypeDuplicate->fct_specifier = Type->fct_specifier; TypeDuplicate->s_ellipsis = Type->s_ellipsis ; TypeDuplicate->s_const = Type->s_const ; TypeDuplicate->s_volatile = Type->s_volatile ; TypeDuplicate->s_char = Type->s_char ; TypeDuplicate->s_short = Type->s_short ; TypeDuplicate->s_int = Type->s_int ; TypeDuplicate->s_long = Type->s_long ; TypeDuplicate->s_signed = Type->s_signed ; TypeDuplicate->s_unsigned = Type->s_unsigned ; TypeDuplicate->s_float = Type->s_float ; TypeDuplicate->s_double = Type->s_double ; TypeDuplicate->s_bool = Type->s_bool ; TypeDuplicate->s_void = Type->s_void ; TypeDuplicate->Name = f_NameDuplicate ( Type->Name ); TypeDuplicate->Class = 0; TypeDuplicate->Enum = 0; TypeDuplicate->Declarator = f_DeclaratorDuplicate( Type->Declarator ); return TypeDuplicate;}extern void f_TypeDestroy( Type_t Type ){ if( Type ) { if( Type->iCheck != TYPE_CHECK ) Abort(); if( Type->Name ) f_NameDestroy ( Type->Name ); if( Type->Class ) f_ClassDestroy ( Type->Class ); if( Type->Enum ) f_EnumDestroy ( Type->Enum ); if( Type->Declarator ) f_DeclaratorDestroy ( Type->Declarator ); ckfree( (char*)Type ); }}extern void f_TypeToString( Type_t Type, char *pc, int exact ){ *pc = 0; f_TypeStrcat( pc, Type, exact );}static void f_TypeStrcat( char *pc, Type_t Type, int exact ){ Boolean_t bType = False; /* is explicit type defined ? */ if( Type ) { if( Type->fct_specifier == SN_INLINE ) f_Strcat( pc, "inline" ); if( Type->fct_specifier == SN_VIRTUAL ) f_Strcat( pc, "virtual" ); if( Type->s_unknown ) f_Strcat( pc, "unknown" ), bType = True; if( Type->s_ellipsis ) f_Strcat( pc, "..." ), bType = True; if( Type->s_const ) f_Strcat( pc, "const" ); if( Type->s_volatile ) f_Strcat( pc, "volatile" );/* if( Type->s_signed ) f_Strcat( pc, "signed" ); */ if( Type->s_unsigned ) f_Strcat( pc, "unsigned" ); if( Type->s_short ) if( ! Type->s_int && ! Type->s_float && ! Type->s_double ) f_Strcat( pc, "short" ), bType = True; if( Type->s_long ) if( ! Type->s_int && ! Type->s_float && ! Type->s_double ) f_Strcat( pc, "long" ), bType = True; if( Type->s_char ) f_Strcat( pc, "char" ), bType = True; if( Type->s_int ) /**/ if( Type->s_long ) f_Strcat( pc, "long" ), bType = True; else if( Type->s_short ) f_Strcat( pc, "short" ), bType = True; else f_Strcat( pc, "int" ), bType = True; if( Type->s_float ) if( Type->s_long ) f_Strcat( pc, "double" ), bType = True; else f_Strcat( pc, "float" ), bType = True; if( Type->s_double ) if( Type->s_short ) f_Strcat( pc, "float" ), bType = True; else f_Strcat( pc, "double" ), bType = True; if( Type->s_bool ) f_Strcat( pc, "bool" ), bType = True; if( Type->s_void ) f_Strcat( pc, "void" ), bType = True; if( Type->Name ) { f_Strcat( pc, Type->Name->pcName ); bType = True; } if( Type->Class ) { if( Type->Class->Name ) f_Strcat( pc, Type->Class->Name->pcName ); bType = True; } if( Type->Enum ) { if( Type->Enum->Name ) f_Strcat( pc, Type->Enum->Name->pcName ); bType = True; } if( ! bType ) { f_Strcat( pc, "int" ), bType = True; } if( Type->Declarator ) f_DeclaratorStrcat( pc, Type->Declarator, exact ); }}extern void f_TypeDelPointer( Type_t Type ){ Oper_t Oper; if( Type == 0 ) { f_InternalError( 33 ); return; } if( Type->s_unknown ) return; if( Type->Declarator ) { if(( Oper = (Oper_t) d_ElemFirst( Type->Declarator->ListOper ))) { if( Oper->type == POINTER_STAR || Oper->type == POINTER_AMPERSAND ) { f_ListRemoveFirst( Type->Declarator->ListOper ); f_OperDestroy( Oper ); } } }}extern void f_TypeDelFunction( Type_t Type ){ Oper_t Oper; if( Type->s_unknown ) return; if( Type->Declarator ) { if(( Oper = (Oper_t) d_ElemFirst( Type->Declarator->ListOper ))) { if( Oper->type == FUNCTION ) { f_ListRemoveFirst( Type->Declarator->ListOper ); f_OperDestroy( Oper ); } } }}extern void f_TypeAddPointer( Type_t Type ){ if( Type->s_unknown ) return; if( Type && Type->Declarator ) { Oper_t Oper = f_OperCreate(); Oper->type = POINTER_STAR; f_ListAddFirst( &Type->Declarator->ListOper, (Elem_t) Oper ); }}extern Type_t f_TypeCreateUnknown( void ){ Type_t Type = f_TypeCreate(); Type->s_unknown = True; return Type;}extern Type_t f_TypeCreateInt( void ){ Type_t Type = f_TypeCreate(); Type->s_int = True; return Type;}extern Type_t f_TypeCreateLong( void ){ Type_t Type = f_TypeCreate(); Type->s_long = True; return Type;}extern Type_t f_TypeCreateChar( void ){ Type_t Type = f_TypeCreate(); Type->s_char = True; return Type;}extern Type_t f_TypeCreateShort( void ){ Type_t Type = f_TypeCreate(); Type->s_short = True; return Type;}extern Type_t f_TypeCreateFloat( void ){ Type_t Type = f_TypeCreate(); Type->s_float = True; return Type;}extern Type_t f_TypeCreateDouble( void ){ Type_t Type = f_TypeCreate(); Type->s_double = True; return Type;}extern Type_t f_TypeCreateBool( void ){ Type_t Type = f_TypeCreate(); Type->s_bool = True; return Type;}extern Type_t f_TypeCreateUnsigned( void ){ Type_t Type = f_TypeCreate(); Type->s_unsigned = True; return Type;}extern Type_t f_TypeCreateSigned( void ){ Type_t Type = f_TypeCreate(); Type->s_signed = True; return Type;}extern Type_t f_TypeCreateVoid( void ){ Type_t Type = f_TypeCreate(); Type->s_void = True; return Type;}extern Type_t f_TypeCreateString( void ){ Type_t Type = f_TypeCreate(); Declarator_t Declarator = f_DeclaratorCreate(); Oper_t Oper = f_OperCreate();/* printf( "TypeCreateString\n" ); */ Type->s_char = True; Type->s_const = True; Type->Declarator = Declarator; Oper->type = POINTER_STAR; f_ListAddLast( &Type->Declarator->ListOper, (Elem_t) Oper ); return Type;}extern Type_t f_TypeCreateName( Name_t Name ){ Type_t Type = f_TypeCreate(); Type->Name = Name; return Type;}extern Type_t f_TypeFromString( char *type ){ Type_t Type;/* printf( "f_TypeFromString: <%s>\n", type ); */ TokenInit( type ); Type = TypeName(); return Type;}extern Type_t f_TypeFromDeclarationAndDeclarator( Declaration_t Declaration, Declarator_t Declarator ){ Type_t Type = f_TypeCreate(); char *name; if( Declaration->Name ) { name = Declaration->Name->pcName; } else if( Declaration->Class ) { if( Declaration->Class->Name ) name = Declaration->Class->Name->pcName; else name = 0; } else if( Declaration->Enum ) { if( Declaration->Enum->Name ) name = Declaration->Enum->Name->pcName; else name = 0; } else { name = 0; } Type->fct_specifier = Declaration->fct_specifier; Type->s_ellipsis = Declaration->s_ellipsis; Type->s_const = Declaration->s_const; Type->s_volatile = Declaration->s_volatile; Type->s_char = Declaration->s_char; Type->s_short = Declaration->s_short; Type->s_int = Declaration->s_int; Type->s_long = Declaration->s_long; Type->s_signed = Declaration->s_signed; Type->s_unsigned = Declaration->s_unsigned; Type->s_float = Declaration->s_float; Type->s_double = Declaration->s_double; Type->s_bool = Declaration->s_bool; Type->s_void = Declaration->s_void; Type->Name = name ? f_NameCreate( name ) : 0; Type->Class = 0; Type->Enum = 0; Type->Declarator = Declarator; return Type;}extern Type_t f_TypeFromDeclaration( Declaration_t Declaration ){ Type_t Type = f_TypeCreate(); char *name; if( Declaration->Name ) { name = Declaration->Name->pcName; } else if( Declaration->Class ) { if( Declaration->Class->Name ) name = Declaration->Class->Name->pcName; else name = 0; } else if( Declaration->Enum ) { if( Declaration->Enum->Name ) name = Declaration->Enum->Name->pcName; else name = 0; } else { name = 0; } Type->fct_specifier = Declaration->fct_specifier; Type->s_ellipsis = Declaration->s_ellipsis; Type->s_const = Declaration->s_const; Type->s_volatile = Declaration->s_volatile; Type->s_char = Declaration->s_char; Type->s_short = Declaration->s_short; Type->s_int = Declaration->s_int; Type->s_long = Declaration->s_long; Type->s_signed = Declaration->s_signed; Type->s_unsigned = Declaration->s_unsigned; Type->s_float = Declaration->s_float; Type->s_double = Declaration->s_double; Type->s_bool = Declaration->s_bool; Type->s_void = Declaration->s_void; Type->Name = name ? f_NameCreate( name ) : 0; Type->Class = 0; Type->Enum = 0; Type->Declarator = 0; return Type;}static Declaration_t f_TypeCastToDeclaration( Type_t Type ){ Declaration_t Declaration; Declaration = f_DeclarationCreate( DECLARATION_ARGUMENT ); Declaration->fct_specifier = Type->fct_specifier; Declaration->s_ellipsis = Type->s_ellipsis; Declaration->s_const = Type->s_const; Declaration->s_volatile = Type->s_volatile; Declaration->s_char = Type->s_char; Declaration->s_short = Type->s_short; Declaration->s_int = Type->s_int; Declaration->s_long = Type->s_long; Declaration->s_signed = Type->s_signed; Declaration->s_unsigned = Type->s_unsigned; Declaration->s_float = Type->s_float; Declaration->s_double = Type->s_double; Declaration->s_bool = Type->s_bool; Declaration->s_void = Type->s_void; Declaration->Name = Type->Name; Declaration->Class = Type->Class; Declaration->Enum = Type->Enum; if( Type->Declarator ) { f_ListAddLast( &Declaration->ListDeclarator, (Elem_t) Type->Declarator ); } Type->Name = 0; Type->Class = 0; Type->Enum = 0; Type->Declarator = 0; f_TypeDestroy( Type ); return Declaration;}static Type_t TypeName( void ){ Type_t Type; Declarator_t Declarator; int bFirst = True;/* printf( "TypeName: {%s}\n", pcLex ); */ if( Token( 0 ) == '(' ) { TokenStep( 1 ); Type = TypeName(); TokenStep( 1 ); return Type; } Type = f_TypeCreate(); while( True ) { int t = Token( 0 );/* printf( "token: %d %s {%s}\n", t, TokenIdent( 0 ), pcLex ); */ switch( t ) { case SN_CONST : Type->s_const = True ; goto label_1; case SN_VOLATILE : Type->s_volatile = True ; goto label_1; case SN_INLINE : Type->fct_specifier = SN_INLINE ; goto label_1; case SN_VIRTUAL : Type->fct_specifier = SN_VIRTUAL; goto label_1;label_1: TokenStep( 1 ); break; case SN_STRUCT : TokenStep( 1 ); break; case SN_UNION : TokenStep( 1 ); break; case SN_CLASS : TokenStep( 1 ); break; case SN_ENUM : TokenStep( 1 ); break; case SN_CHAR : Type->s_char = True ; goto label; case SN_SHORT : Type->s_short = True ; goto label; case SN_INT : Type->s_int = True ; goto label; case SN_LONG : Type->s_long = True ; goto label; case SN_FLOAT : Type->s_float = True ; goto label; case SN_DOUBLE : Type->s_double = True ; goto label; case SN_BOOL : Type->s_bool = True ; goto label; case SN_VOID : Type->s_void = True ; goto label; case SN_SIGNED : Type->s_signed = True ; goto label; case SN_UNSIGNED : Type->s_unsigned = True ; goto label;label: bFirst = False; TokenStep( 1 ); break; case SN_IDENTIFIER: if( bFirst ) { Type->Name = CompleteClassName();/* printf( "After complete class name: %d\n", Token( 0 ));*/ bFirst = False; } else { if(( Declarator = AbstractDeclarator()) == 0 ) { Name_t Name = CompleteClassName(); if( Type->Name == 0 ) { Type->Name = Name; } else { f_NameDestroy( Name ); } bFirst = False; } else { if( Type->Declarator == 0 ) { Type->Declarator = Declarator; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -