local void CDefine( void ) { struct macro_parm *mp, *prev_mp, *formal_parms; int parm_cnt, parm_end = 0; int ppscan_mode; char *token_buf; source_loc macro_loc; PPNextToken(); if( CurToken != T_ID ) { ExpectIdentifier(); return; } if( strcmp( Buffer, "defined" ) == 0 ) { CErr1( ERR_CANT_DEFINE_DEFINED ); return; } token_buf = CStrSave( Buffer ); formal_parms = NULL; macro_loc = SrcFileLoc; parm_cnt = -1; /* -1 ==> no () following */ if( CurrChar == '(' ) { /* parms present */ PPNextToken(); /* grab the '(' */ PPNextToken(); parm_cnt = 0; /* 0 ==> () following */ parm_end = 0; prev_mp = NULL; for( ;; ) { if( CurToken == T_RIGHT_PAREN ) break; if( parm_end ) { ExpectingAfter( T_RIGHT_PAREN, T_DOT_DOT_DOT ); return; } if( CurToken != T_DOT_DOT_DOT && !ExpectingToken( T_ID ) ) { return; } ++parm_cnt; if( CurToken == T_DOT_DOT_DOT ) { parm_end = 1; /* can have no more parms after this. */ } mp = (struct macro_parm *)CMemAlloc( sizeof( struct macro_parm ) ); if( formal_parms == NULL ) { formal_parms = mp; } else { if( FormalParm( formal_parms ) ) { CErr2p( ERR_DUPLICATE_MACRO_PARM, Buffer ); } prev_mp->next_macro_parm = mp; } if( CurToken == T_DOT_DOT_DOT ) mp->parm = CStrSave( "__VA_ARGS__" ); else mp->parm = CStrSave( Buffer ); prev_mp = mp; PPNextToken(); if( CurToken == T_RIGHT_PAREN ) break; if( CurToken == T_NULL ) { CErr1( ERR_INVALID_MACRO_DEFN ); break; } if( parm_end ) { ExpectingAfter( T_RIGHT_PAREN, T_DOT_DOT_DOT ); return; } MustRecog( T_COMMA ); if( CurToken != T_DOT_DOT_DOT && !ExpectingToken( T_ID ) ) { return; } } } /* grab replacement tokens */ ppscan_mode = InitPPScan(); // enable T_PPNUMBER tokens GrabTokens( parm_end ? -(parm_cnt + 1) : (parm_cnt + 1), formal_parms, token_buf, ¯o_loc ); FiniPPScan( ppscan_mode ); // disable T_PPNUMBER tokens for( ; (mp = formal_parms) != NULL; ) { formal_parms = mp->next_macro_parm; CMemFree( mp->parm ); CMemFree( mp ); } CMemFree( token_buf ); }
static void CDefine( void ) { MPPTR mp; MPPTR prev_mp; MPPTR formal_parms; mac_parm_count parm_count; macro_flags mflags; bool ppscan_mode; char *token_buf; source_loc macro_loc; PPNextToken(); if( CurToken != T_ID ) { ExpectIdentifier(); return; } if( CMPLIT( Buffer, "defined" ) == 0 ) { CErr1( ERR_CANT_DEFINE_DEFINED ); return; } token_buf = CStrSave( Buffer ); formal_parms = NULL; macro_loc = SrcFileLoc; parm_count = 0; /* 0 ==> no () following */ mflags = MFLAG_USER_DEFINED; if( CurrChar == '(' ) { /* parms present */ PPNextToken(); /* grab the '(' */ PPNextToken(); parm_count = 1; /* 1 ==> () following */ prev_mp = NULL; for( ; CurToken != T_RIGHT_PAREN; ) { if( mflags & MFLAG_VAR_ARGS ) { ExpectingAfter( T_RIGHT_PAREN, T_DOT_DOT_DOT ); return; } if( CurToken != T_DOT_DOT_DOT && !ExpectingToken( T_ID ) ) { return; } ++parm_count; if( CurToken == T_DOT_DOT_DOT ) { mflags |= MFLAG_VAR_ARGS; /* can have no more parms after this. */ } mp = (MPPTR)CMemAlloc( sizeof( MPDEFN ) ); if( formal_parms == NULL ) { formal_parms = mp; } else { if( FormalParm( formal_parms ) ) { CErr2p( ERR_DUPLICATE_MACRO_PARM, Buffer ); } prev_mp->next = mp; } if( CurToken == T_DOT_DOT_DOT ) { mp->parm = CStrSave( "__VA_ARGS__" ); } else { mp->parm = CStrSave( Buffer ); } prev_mp = mp; PPNextToken(); if( CurToken == T_RIGHT_PAREN ) break; if( CurToken == T_NULL ) { CErr1( ERR_INVALID_MACRO_DEFN ); break; } if( mflags & MFLAG_VAR_ARGS ) { ExpectingAfter( T_RIGHT_PAREN, T_DOT_DOT_DOT ); return; } MustRecog( T_COMMA ); if( CurToken != T_DOT_DOT_DOT && !ExpectingToken( T_ID ) ) { return; } } } /* grab replacement tokens */ ppscan_mode = InitPPScan(); // enable T_PPNUMBER tokens GrabTokens( parm_count, mflags, formal_parms, token_buf, ¯o_loc ); FiniPPScan( ppscan_mode ); // disable T_PPNUMBER tokens for( ; (mp = formal_parms) != NULL; ) { formal_parms = mp->next; CMemFree( mp->parm ); CMemFree( mp ); } CMemFree( token_buf ); }