psub.pas 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. {
  2. $Id$
  3. Copyright (c) 1998 by Florian Klaempfl, Daniel Mantione
  4. Does the parsing of the procedures/functions
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit psub;
  19. interface
  20. uses
  21. cobjects,
  22. symconst,tokens,symtable;
  23. const
  24. pd_global = $1; { directive must be global }
  25. pd_body = $2; { directive needs a body }
  26. pd_implemen = $4; { directive can be used implementation section }
  27. pd_interface = $8; { directive can be used interface section }
  28. pd_object = $10; { directive can be used object declaration }
  29. pd_procvar = $20; { directive can be used procvar declaration }
  30. pd_notobject = $40;{ directive can not be used object declaration }
  31. procedure compile_proc_body(const proc_names:Tstringcontainer;
  32. make_global,parent_has_class:boolean);
  33. procedure parse_proc_head(options:tproctypeoption);
  34. procedure parse_proc_dec;
  35. function is_proc_directive(tok:ttoken):boolean;
  36. procedure parse_var_proc_directives(var sym : psym);
  37. procedure parse_object_proc_directives(var sym : pprocsym);
  38. procedure read_proc;
  39. implementation
  40. uses
  41. globtype,systems,
  42. strings,globals,verbose,files,
  43. scanner,aasm,tree,types,
  44. import,gendef,
  45. {$ifdef newcg}
  46. cgbase,
  47. {$else newcg}
  48. hcodegen,
  49. {$endif newcg}
  50. temp_gen,pass_1,cpubase,cpuasm
  51. {$ifndef NOPASS2}
  52. ,pass_2
  53. {$endif}
  54. {$ifdef GDB}
  55. ,gdb
  56. {$endif GDB}
  57. {$ifdef newcg}
  58. {$ifndef NOOPT}
  59. ,aopt
  60. {$endif}
  61. {$else}
  62. {$ifdef i386}
  63. ,tgeni386
  64. ,cgai386
  65. {$ifndef NOOPT}
  66. ,aopt386
  67. {$endif}
  68. {$endif}
  69. {$ifdef m68k}
  70. ,tgen68k,cga68k
  71. {$endif}
  72. {$endif newcg}
  73. { parser specific stuff }
  74. ,pbase,ptype,pdecl,pexpr,pstatmnt
  75. {$ifdef newcg}
  76. ,tgcpu,convtree,cgobj,tgeni386 { for the new code generator tgeni386 is only a dummy }
  77. {$endif newcg}
  78. ;
  79. var
  80. realname:string; { contains the real name of a procedure as it's typed }
  81. procedure parse_proc_head(options:tproctypeoption);
  82. var sp:stringid;
  83. pd:Pprocdef;
  84. paramoffset:longint;
  85. sym:Psym;
  86. hs:string;
  87. st : psymtable;
  88. overloaded_level:word;
  89. storepos,procstartfilepos : tfileposinfo;
  90. begin
  91. { Save the position where this procedure really starts and set col to 1 which
  92. looks nicer }
  93. procstartfilepos:=tokenpos;
  94. { procstartfilepos.column:=1; I do not agree here !!
  95. lets keep excat position PM }
  96. if (options=potype_operator) then
  97. begin
  98. sp:=overloaded_names[optoken];
  99. realname:=sp;
  100. end
  101. else
  102. begin
  103. sp:=pattern;
  104. realname:=orgpattern;
  105. consume(_ID);
  106. end;
  107. { method ? }
  108. if not(parse_only) and try_to_consume(_POINT) then
  109. begin
  110. storepos:=tokenpos;
  111. tokenpos:=procstartfilepos;
  112. getsym(sp,true);
  113. sym:=srsym;
  114. tokenpos:=storepos;
  115. { load proc name }
  116. sp:=pattern;
  117. realname:=orgpattern;
  118. procstartfilepos:=tokenpos;
  119. { qualifier is class name ? }
  120. if (sym^.typ<>typesym) or
  121. (ptypesym(sym)^.definition^.deftype<>objectdef) then
  122. begin
  123. Message(parser_e_class_id_expected);
  124. aktprocsym:=nil;
  125. consume(_ID);
  126. end
  127. else
  128. begin
  129. { used to allow private syms to be seen }
  130. aktobjectdef:=pobjectdef(ptypesym(sym)^.definition);
  131. procinfo^._class:=pobjectdef(ptypesym(sym)^.definition);
  132. aktprocsym:=pprocsym(procinfo^._class^.symtable^.search(sp));
  133. consume(_ID);
  134. {The procedure has been found. So it is
  135. a global one. Set the flags to mark this.}
  136. procinfo^.flags:=procinfo^.flags or pi_is_global;
  137. aktobjectdef:=nil;
  138. { we solve this below }
  139. if not(assigned(aktprocsym)) then
  140. Message(parser_e_methode_id_expected);
  141. end;
  142. end
  143. else
  144. begin
  145. { check for constructor/destructor which is not allowed here }
  146. if (not parse_only) and
  147. (options in [potype_constructor,potype_destructor]) then
  148. Message(parser_e_constructors_always_objects);
  149. tokenpos:=procstartfilepos;
  150. aktprocsym:=pprocsym(symtablestack^.search(sp));
  151. if not(parse_only) then
  152. begin
  153. {The procedure we prepare for is in the implementation
  154. part of the unit we compile. It is also possible that we
  155. are compiling a program, which is also some kind of
  156. implementaion part.
  157. We need to find out if the procedure is global. If it is
  158. global, it is in the global symtable.}
  159. if not assigned(aktprocsym) and
  160. (symtablestack^.symtabletype=staticsymtable) then
  161. begin
  162. {Search the procedure in the global symtable.}
  163. aktprocsym:=Pprocsym(search_a_symtable(sp,globalsymtable));
  164. if assigned(aktprocsym) then
  165. begin
  166. {Check if it is a procedure.}
  167. if aktprocsym^.typ<>procsym then
  168. DuplicateSym(aktprocsym);
  169. {The procedure has been found. So it is
  170. a global one. Set the flags to mark this.}
  171. procinfo^.flags:=procinfo^.flags or pi_is_global;
  172. end;
  173. end;
  174. end;
  175. end;
  176. { Create the mangledname }
  177. {$ifndef UseNiceNames}
  178. if assigned(procinfo^._class) then
  179. begin
  180. if (pos('_$$_',procprefix)=0) then
  181. hs:=procprefix+'_$$_'+procinfo^._class^.objname^+'_$$_'+sp
  182. else
  183. hs:=procprefix+'_$'+sp;
  184. end
  185. else
  186. begin
  187. if lexlevel=normal_function_level then
  188. hs:=procprefix+'_'+sp
  189. else
  190. hs:=procprefix+'_$'+sp;
  191. end;
  192. {$else UseNiceNames}
  193. if assigned(procinfo^._class) then
  194. begin
  195. if (pos('_5Class_',procprefix)=0) then
  196. hs:=procprefix+'_5Class_'+procinfo^._class^.name^+'_'+tostr(length(sp))+sp
  197. else
  198. hs:=procprefix+'_'+tostr(length(sp))+sp;
  199. end
  200. else
  201. begin
  202. if lexlevel=normal_function_level then
  203. hs:=procprefix+'_'+tostr(length(sp))+sp
  204. else
  205. hs:=lowercase(procprefix)+'_'+tostr(length(sp))+sp;
  206. end;
  207. {$endif UseNiceNames}
  208. if assigned(aktprocsym) then
  209. begin
  210. { Check if overloading is enabled }
  211. if not(m_fpc in aktmodeswitches) then
  212. begin
  213. if aktprocsym^.typ<>procsym then
  214. begin
  215. DuplicateSym(aktprocsym);
  216. { try to recover by creating a new aktprocsym }
  217. tokenpos:=procstartfilepos;
  218. aktprocsym:=new(pprocsym,init(sp));
  219. end
  220. else
  221. begin
  222. if not(aktprocsym^.definition^.forwarddef) then
  223. Message(parser_e_procedure_overloading_is_off);
  224. end;
  225. end
  226. else
  227. begin
  228. { Check if the overloaded sym is realy a procsym }
  229. if aktprocsym^.typ<>procsym then
  230. begin
  231. Message1(parser_e_overloaded_no_procedure,aktprocsym^.name);
  232. { try to recover by creating a new aktprocsym }
  233. tokenpos:=procstartfilepos;
  234. aktprocsym:=new(pprocsym,init(sp));
  235. end;
  236. end;
  237. end
  238. else
  239. begin
  240. { create a new procsym and set the real filepos }
  241. tokenpos:=procstartfilepos;
  242. aktprocsym:=new(pprocsym,init(sp));
  243. { for operator we have only one definition for each overloaded
  244. operation }
  245. if (options=potype_operator) then
  246. begin
  247. { the only problem is that nextoverloaded might not be in a unit
  248. known for the unit itself }
  249. if assigned(overloaded_operators[optoken]) then
  250. aktprocsym^.definition:=overloaded_operators[optoken]^.definition;
  251. end;
  252. symtablestack^.insert(aktprocsym);
  253. end;
  254. st:=symtablestack;
  255. pd:=new(pprocdef,init);
  256. pd^.symtablelevel:=symtablestack^.symtablelevel;
  257. if assigned(procinfo^._class) then
  258. pd^._class := procinfo^._class;
  259. { set the options from the caller (podestructor or poconstructor) }
  260. pd^.proctypeoption:=options;
  261. { calculate the offset of the parameters }
  262. paramoffset:=8;
  263. { calculate frame pointer offset }
  264. if lexlevel>normal_function_level then
  265. begin
  266. procinfo^.framepointer_offset:=paramoffset;
  267. inc(paramoffset,target_os.size_of_pointer);
  268. { this is needed to get correct framepointer push for local
  269. forward functions !! }
  270. pd^.parast^.symtablelevel:=lexlevel;
  271. end;
  272. if assigned (procinfo^._Class) and
  273. not(procinfo^._Class^.is_class) and
  274. (pd^.proctypeoption in [potype_constructor,potype_destructor]) then
  275. inc(paramoffset,target_os.size_of_pointer);
  276. { self pointer offset }
  277. { self isn't pushed in nested procedure of methods }
  278. if assigned(procinfo^._class) and (lexlevel=normal_function_level) then
  279. begin
  280. procinfo^.selfpointer_offset:=paramoffset;
  281. if assigned(aktprocsym^.definition) and
  282. not(po_containsself in aktprocsym^.definition^.procoptions) then
  283. inc(paramoffset,target_os.size_of_pointer);
  284. end;
  285. { destructor flag ? }
  286. if assigned (procinfo^._Class) and
  287. procinfo^._class^.is_class and
  288. (pd^.proctypeoption=potype_destructor) then
  289. inc(paramoffset,target_os.size_of_pointer);
  290. procinfo^.call_offset:=paramoffset;
  291. pd^.parast^.datasize:=0;
  292. pd^.nextoverloaded:=aktprocsym^.definition;
  293. aktprocsym^.definition:=pd;
  294. { this is probably obsolete now PM }
  295. aktprocsym^.definition^.fileinfo:=procstartfilepos;
  296. aktprocsym^.definition^.setmangledname(hs);
  297. aktprocsym^.definition^.procsym:=aktprocsym;
  298. if not parse_only then
  299. begin
  300. overloaded_level:=0;
  301. { we need another procprefix !!! }
  302. { count, but only those in the same unit !!}
  303. while assigned(pd) and
  304. (pd^.owner^.symtabletype in [globalsymtable,staticsymtable]) do
  305. begin
  306. { only count already implemented functions }
  307. if not(pd^.forwarddef) then
  308. inc(overloaded_level);
  309. pd:=pd^.nextoverloaded;
  310. end;
  311. if overloaded_level>0 then
  312. procprefix:=hs+'$'+tostr(overloaded_level)+'$'
  313. else
  314. procprefix:=hs+'$';
  315. end;
  316. { this must also be inserted in the right symtable !! PM }
  317. { otherwise we get subbtle problems with
  318. definitions of args defs in staticsymtable for
  319. implementation of a global method }
  320. if token=_LKLAMMER then
  321. parameter_dec(aktprocsym^.definition);
  322. { so we only restore the symtable now }
  323. symtablestack:=st;
  324. if (options=potype_operator) then
  325. overloaded_operators[optoken]:=aktprocsym;
  326. end;
  327. procedure parse_proc_dec;
  328. var
  329. hs : string;
  330. isclassmethod : boolean;
  331. begin
  332. inc(lexlevel);
  333. { read class method }
  334. if token=_CLASS then
  335. begin
  336. consume(_CLASS);
  337. isclassmethod:=true;
  338. end
  339. else
  340. isclassmethod:=false;
  341. case token of
  342. _FUNCTION : begin
  343. consume(_FUNCTION);
  344. parse_proc_head(potype_none);
  345. if token<>_COLON then
  346. begin
  347. if not(aktprocsym^.definition^.forwarddef) or
  348. (m_repeat_forward in aktmodeswitches) then
  349. begin
  350. consume(_COLON);
  351. consume_all_until(_SEMICOLON);
  352. end;
  353. end
  354. else
  355. begin
  356. consume(_COLON);
  357. inc(testcurobject);
  358. aktprocsym^.definition^.retdef:=single_type(hs,false);
  359. aktprocsym^.definition^.test_if_fpu_result;
  360. dec(testcurobject);
  361. end;
  362. end;
  363. _PROCEDURE : begin
  364. consume(_PROCEDURE);
  365. parse_proc_head(potype_none);
  366. aktprocsym^.definition^.retdef:=voiddef;
  367. end;
  368. _CONSTRUCTOR : begin
  369. consume(_CONSTRUCTOR);
  370. parse_proc_head(potype_constructor);
  371. if assigned(procinfo^._class) and
  372. procinfo^._class^.is_class then
  373. begin
  374. { CLASS constructors return the created instance }
  375. aktprocsym^.definition^.retdef:=procinfo^._class;
  376. end
  377. else
  378. begin
  379. { OBJECT constructors return a boolean }
  380. {$IfDef GDB}
  381. { GDB doesn't like unnamed types !}
  382. aktprocsym^.definition^.retdef:=globaldef('boolean');
  383. {$else GDB}
  384. aktprocsym^.definition^.retdef:=new(porddef,init(bool8bit,0,1));
  385. {$Endif GDB}
  386. end;
  387. end;
  388. _DESTRUCTOR : begin
  389. consume(_DESTRUCTOR);
  390. parse_proc_head(potype_destructor);
  391. aktprocsym^.definition^.retdef:=voiddef;
  392. end;
  393. _OPERATOR : begin
  394. if lexlevel>normal_function_level then
  395. Message(parser_e_no_local_operator);
  396. consume(_OPERATOR);
  397. if not(token in [_PLUS..last_overloaded]) then
  398. Message(parser_e_overload_operator_failed);
  399. optoken:=token;
  400. consume(Token);
  401. procinfo^.flags:=procinfo^.flags or pi_operator;
  402. parse_proc_head(potype_operator);
  403. if token<>_ID then
  404. begin
  405. opsym:=nil;
  406. if not(m_result in aktmodeswitches) then
  407. consume(_ID);
  408. end
  409. else
  410. begin
  411. opsym:=new(pvarsym,init(pattern,voiddef));
  412. consume(_ID);
  413. end;
  414. if not try_to_consume(_COLON) then
  415. begin
  416. consume(_COLON);
  417. aktprocsym^.definition^.retdef:=generrordef;
  418. consume_all_until(_SEMICOLON);
  419. end
  420. else
  421. begin
  422. aktprocsym^.definition^.retdef:=
  423. single_type(hs,false);
  424. aktprocsym^.definition^.test_if_fpu_result;
  425. if (optoken in [_EQUAL,_GT,_LT,_GTE,_LTE]) and
  426. ((aktprocsym^.definition^.retdef^.deftype<>
  427. orddef) or (porddef(aktprocsym^.definition^.
  428. retdef)^.typ<>bool8bit)) then
  429. Message(parser_e_comparative_operator_return_boolean);
  430. if assigned(opsym) then
  431. opsym^.definition:=aktprocsym^.definition^.retdef;
  432. { We need to add the retrun type in the mangledname
  433. to allow overloading with just different results !! (PM) }
  434. aktprocsym^.definition^.setmangledname(
  435. aktprocsym^.definition^.mangledname+'$$'+hs);
  436. end;
  437. end;
  438. end;
  439. if isclassmethod and
  440. assigned(aktprocsym) then
  441. {$ifdef INCLUDEOK}
  442. include(aktprocsym^.definition^.procoptions,po_classmethod);
  443. {$else}
  444. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_classmethod];
  445. {$endif}
  446. consume(_SEMICOLON);
  447. dec(lexlevel);
  448. end;
  449. {****************************************************************************
  450. Procedure directive handlers
  451. ****************************************************************************}
  452. {$ifdef tp}
  453. {$F+}
  454. {$endif}
  455. procedure pd_far(const procnames:Tstringcontainer);
  456. begin
  457. Message(parser_w_proc_far_ignored);
  458. end;
  459. procedure pd_near(const procnames:Tstringcontainer);
  460. begin
  461. Message(parser_w_proc_near_ignored);
  462. end;
  463. procedure pd_export(const procnames:Tstringcontainer);
  464. begin
  465. if assigned(procinfo^._class) then
  466. Message(parser_e_methods_dont_be_export);
  467. if lexlevel<>normal_function_level then
  468. Message(parser_e_dont_nest_export);
  469. { only os/2 needs this }
  470. if target_info.target=target_i386_os2 then
  471. begin
  472. procnames.insert(realname);
  473. procinfo^.exported:=true;
  474. if cs_link_deffile in aktglobalswitches then
  475. deffile.AddExport(aktprocsym^.definition^.mangledname);
  476. end;
  477. end;
  478. procedure pd_inline(const procnames:Tstringcontainer);
  479. begin
  480. if not(cs_support_inline in aktmoduleswitches) then
  481. Message(parser_e_proc_inline_not_supported);
  482. end;
  483. procedure pd_forward(const procnames:Tstringcontainer);
  484. begin
  485. aktprocsym^.definition^.forwarddef:=true;
  486. end;
  487. procedure pd_stdcall(const procnames:Tstringcontainer);
  488. begin
  489. end;
  490. procedure pd_safecall(const procnames:Tstringcontainer);
  491. begin
  492. end;
  493. procedure pd_alias(const procnames:Tstringcontainer);
  494. begin
  495. consume(_COLON);
  496. procnames.insert(get_stringconst);
  497. end;
  498. procedure pd_asmname(const procnames:Tstringcontainer);
  499. begin
  500. aktprocsym^.definition^.setmangledname(target_os.Cprefix+pattern);
  501. if token=_CCHAR then
  502. consume(_CCHAR)
  503. else
  504. consume(_CSTRING);
  505. { we don't need anything else }
  506. aktprocsym^.definition^.forwarddef:=false;
  507. end;
  508. procedure pd_intern(const procnames:Tstringcontainer);
  509. begin
  510. consume(_COLON);
  511. aktprocsym^.definition^.extnumber:=get_intconst;
  512. end;
  513. procedure pd_system(const procnames:Tstringcontainer);
  514. begin
  515. aktprocsym^.definition^.setmangledname(realname);
  516. end;
  517. procedure pd_abstract(const procnames:Tstringcontainer);
  518. begin
  519. if (po_virtualmethod in aktprocsym^.definition^.procoptions) then
  520. {$ifdef INCLUDEOK}
  521. include(aktprocsym^.definition^.procoptions,po_abstractmethod)
  522. {$else}
  523. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_abstractmethod]
  524. {$endif}
  525. else
  526. Message(parser_e_only_virtual_methods_abstract);
  527. { the method is defined }
  528. aktprocsym^.definition^.forwarddef:=false;
  529. end;
  530. procedure pd_virtual(const procnames:Tstringcontainer);
  531. begin
  532. if (aktprocsym^.definition^.proctypeoption=potype_constructor) and
  533. not(aktprocsym^.definition^._class^.is_class) then
  534. Message(parser_e_constructor_cannot_be_not_virtual);
  535. end;
  536. procedure pd_static(const procnames:Tstringcontainer);
  537. begin
  538. if (cs_static_keyword in aktmoduleswitches) then
  539. begin
  540. {$ifdef INCLUDEOK}
  541. include(aktprocsym^.symoptions,sp_static);
  542. include(aktprocsym^.definition^.procoptions,po_staticmethod);
  543. {$else}
  544. aktprocsym^.symoptions:=aktprocsym^.symoptions+[sp_static];
  545. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_staticmethod];
  546. {$endif}
  547. end;
  548. end;
  549. procedure pd_override(const procnames:Tstringcontainer);
  550. begin
  551. if not(aktprocsym^.definition^._class^.is_class) then
  552. Message(parser_e_no_object_override);
  553. end;
  554. procedure pd_message(const procnames:Tstringcontainer);
  555. var
  556. pt : ptree;
  557. begin
  558. { check parameter type }
  559. if not(po_containsself in aktprocsym^.definition^.procoptions) and
  560. ((aktprocsym^.definition^.para^.count<>1) or
  561. (pparaitem(aktprocsym^.definition^.para^.first)^.paratyp<>vs_var)) then
  562. Message(parser_e_ill_msg_param);
  563. pt:=comp_expr(true);
  564. do_firstpass(pt);
  565. if pt^.treetype=stringconstn then
  566. begin
  567. {$ifdef INCLUDEOK}
  568. include(aktprocsym^.definition^.procoptions,po_msgstr);
  569. {$else}
  570. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgstr];
  571. {$endif}
  572. aktprocsym^.definition^.messageinf.str:=strnew(pt^.value_str);
  573. end
  574. else
  575. if is_constintnode(pt) then
  576. begin
  577. {$ifdef INCLUDEOK}
  578. include(aktprocsym^.definition^.procoptions,po_msgint);
  579. {$else}
  580. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+[po_msgint];
  581. {$endif}
  582. aktprocsym^.definition^.messageinf.i:=pt^.value;
  583. end
  584. else
  585. Message(parser_e_ill_msg_expr);
  586. disposetree(pt);
  587. end;
  588. procedure pd_cdecl(const procnames:Tstringcontainer);
  589. begin
  590. if aktprocsym^.definition^.deftype<>procvardef then
  591. aktprocsym^.definition^.setmangledname(target_os.Cprefix+realname);
  592. end;
  593. procedure pd_pascal(const procnames:Tstringcontainer);
  594. var st,parast : psymtable;
  595. lastps,ps : psym;
  596. begin
  597. new(st,init(parasymtable));
  598. parast:=aktprocsym^.definition^.parast;
  599. lastps:=nil;
  600. while assigned(parast^.symindex^.first) and (lastps<>psym(parast^.symindex^.first)) do
  601. begin
  602. ps:=psym(parast^.symindex^.first);
  603. while assigned(ps^.next) and (psym(ps^.next)<>lastps) do
  604. ps:=psym(ps^.next);
  605. ps^.owner:=st;
  606. { recalculate the corrected offset }
  607. { the really_insert_in_data procedure
  608. for parasymtable should only calculateoffset PM }
  609. ps^.insert_in_data;
  610. { reset the owner correctly }
  611. ps^.owner:=parast;
  612. lastps:=ps;
  613. end;
  614. end;
  615. procedure pd_register(const procnames:Tstringcontainer);
  616. begin
  617. Message(parser_w_proc_register_ignored);
  618. end;
  619. procedure pd_syscall(const procnames:Tstringcontainer);
  620. begin
  621. aktprocsym^.definition^.forwarddef:=false;
  622. aktprocsym^.definition^.extnumber:=get_intconst;
  623. end;
  624. procedure pd_external(const procnames:Tstringcontainer);
  625. {
  626. If import_dll=nil the procedure is assumed to be in another
  627. object file. In that object file it should have the name to
  628. which import_name is pointing to. Otherwise, the procedure is
  629. assumed to be in the DLL to which import_dll is pointing to. In
  630. that case either import_nr<>0 or import_name<>nil is true, so
  631. the procedure is either imported by number or by name. (DM)
  632. }
  633. var
  634. import_dll,
  635. import_name : string;
  636. import_nr : word;
  637. begin
  638. aktprocsym^.definition^.forwarddef:=false;
  639. { If the procedure should be imported from a DLL, a constant string follows.
  640. This isn't really correct, an contant string expression follows
  641. so we check if an semicolon follows, else a string constant have to
  642. follow (FK) }
  643. import_nr:=0;
  644. import_name:='';
  645. if not(token=_SEMICOLON) and not(idtoken=_NAME) then
  646. begin
  647. import_dll:=get_stringconst;
  648. if (idtoken=_NAME) then
  649. begin
  650. consume(_NAME);
  651. import_name:=get_stringconst;
  652. end;
  653. if (idtoken=_INDEX) then
  654. begin
  655. {After the word index follows the index number in the DLL.}
  656. consume(_INDEX);
  657. import_nr:=get_intconst;
  658. end;
  659. if (import_nr=0) and (import_name='') then
  660. {if (aktprocsym^.definition^.options and pocdecl)<>0 then
  661. import_name:=aktprocsym^.definition^.mangledname
  662. else
  663. Message(parser_w_empty_import_name);}
  664. { this should work both for win32 and Linux !! PM }
  665. import_name:=realname;
  666. if not(current_module^.uses_imports) then
  667. begin
  668. current_module^.uses_imports:=true;
  669. importlib^.preparelib(current_module^.modulename^);
  670. end;
  671. if not(m_repeat_forward in aktmodeswitches) then
  672. begin
  673. { we can only have one overloaded here ! }
  674. if assigned(aktprocsym^.definition^.nextoverloaded) then
  675. importlib^.importprocedure(aktprocsym^.definition^.nextoverloaded^.mangledname,
  676. import_dll,import_nr,import_name)
  677. else
  678. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  679. end
  680. else
  681. importlib^.importprocedure(aktprocsym^.mangledname,import_dll,import_nr,import_name);
  682. end
  683. else
  684. begin
  685. if (idtoken=_NAME) then
  686. begin
  687. consume(_NAME);
  688. import_name:=get_stringconst;
  689. aktprocsym^.definition^.setmangledname(import_name);
  690. end
  691. else
  692. begin
  693. { external shouldn't override the cdecl/system name }
  694. if not (pocall_clearstack in aktprocsym^.definition^.proccalloptions) then
  695. aktprocsym^.definition^.setmangledname(aktprocsym^.name);
  696. end;
  697. end;
  698. end;
  699. {$ifdef TP}
  700. {$F-}
  701. {$endif}
  702. {const
  703. namelength=15;}
  704. type
  705. pd_handler=procedure(const procnames:Tstringcontainer);
  706. proc_dir_rec=record
  707. idtok : ttoken;
  708. pd_flags : longint;
  709. handler : pd_handler;
  710. pocall : tproccalloptions;
  711. pooption : tprocoptions;
  712. mutexclpocall : tproccalloptions;
  713. mutexclpotype : tproctypeoptions;
  714. mutexclpo : tprocoptions;
  715. end;
  716. const
  717. {Should contain the number of procedure directives we support.}
  718. num_proc_directives=29;
  719. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  720. (
  721. (
  722. idtok:_ABSTRACT;
  723. pd_flags : pd_interface+pd_object;
  724. handler : {$ifndef TP}@{$endif}pd_abstract;
  725. pocall : [];
  726. pooption : [po_abstractmethod];
  727. mutexclpocall : [pocall_internproc,pocall_inline];
  728. mutexclpotype : [potype_constructor,potype_destructor];
  729. mutexclpo : [po_exports,po_interrupt,po_external]
  730. ),(
  731. idtok:_ALIAS;
  732. pd_flags : pd_implemen+pd_body;
  733. handler : {$ifndef TP}@{$endif}pd_alias;
  734. pocall : [];
  735. pooption : [];
  736. mutexclpocall : [pocall_inline];
  737. mutexclpotype : [];
  738. mutexclpo : [po_external]
  739. ),(
  740. idtok:_ASMNAME;
  741. pd_flags : pd_interface+pd_implemen;
  742. handler : {$ifndef TP}@{$endif}pd_asmname;
  743. pocall : [pocall_cdecl,pocall_clearstack];
  744. pooption : [po_external];
  745. mutexclpocall : [pocall_internproc];
  746. mutexclpotype : [];
  747. mutexclpo : [po_external]
  748. ),(
  749. idtok:_ASSEMBLER;
  750. pd_flags : pd_implemen+pd_body;
  751. handler : nil;
  752. pocall : [];
  753. pooption : [po_assembler];
  754. mutexclpocall : [];
  755. mutexclpotype : [];
  756. mutexclpo : [po_external]
  757. ),(
  758. idtok:_CDECL;
  759. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  760. handler : {$ifndef TP}@{$endif}pd_cdecl;
  761. pocall : [pocall_cdecl,pocall_clearstack];
  762. pooption : [po_savestdregs];
  763. mutexclpocall : [pocall_internproc,pocall_leftright,pocall_inline];
  764. mutexclpotype : [];
  765. mutexclpo : [po_assembler,po_external]
  766. ),(
  767. idtok:_DYNAMIC;
  768. pd_flags : pd_interface+pd_object;
  769. handler : {$ifndef TP}@{$endif}pd_virtual;
  770. pocall : [];
  771. pooption : [po_virtualmethod];
  772. mutexclpocall : [pocall_internproc,pocall_inline];
  773. mutexclpotype : [];
  774. mutexclpo : [po_exports,po_interrupt,po_external]
  775. ),(
  776. idtok:_EXPORT;
  777. pd_flags : pd_body+pd_global+pd_interface+pd_implemen{??};
  778. handler : {$ifndef TP}@{$endif}pd_export;
  779. pocall : [];
  780. pooption : [po_exports];
  781. mutexclpocall : [pocall_internproc,pocall_inline];
  782. mutexclpotype : [];
  783. mutexclpo : [po_external,po_interrupt]
  784. ),(
  785. idtok:_EXTERNAL;
  786. pd_flags : pd_implemen+pd_interface;
  787. handler : {$ifndef TP}@{$endif}pd_external;
  788. pocall : [];
  789. pooption : [po_external];
  790. mutexclpocall : [pocall_internproc,pocall_inline,pocall_palmossyscall];
  791. mutexclpotype : [];
  792. mutexclpo : [po_exports,po_interrupt,po_assembler]
  793. ),(
  794. idtok:_FAR;
  795. pd_flags : pd_implemen+pd_body+pd_interface+pd_procvar;
  796. handler : {$ifndef TP}@{$endif}pd_far;
  797. pocall : [];
  798. pooption : [];
  799. mutexclpocall : [pocall_internproc,pocall_inline];
  800. mutexclpotype : [];
  801. mutexclpo : []
  802. ),(
  803. idtok:_FORWARD;
  804. pd_flags : pd_implemen;
  805. handler : {$ifndef TP}@{$endif}pd_forward;
  806. pocall : [];
  807. pooption : [];
  808. mutexclpocall : [pocall_internproc,pocall_inline];
  809. mutexclpotype : [];
  810. mutexclpo : [po_external]
  811. ),(
  812. idtok:_INLINE;
  813. pd_flags : pd_implemen+pd_body;
  814. handler : {$ifndef TP}@{$endif}pd_inline;
  815. pocall : [pocall_inline];
  816. pooption : [];
  817. mutexclpocall : [pocall_internproc];
  818. mutexclpotype : [potype_constructor,potype_destructor];
  819. mutexclpo : [po_exports,po_external,po_interrupt]
  820. ),(
  821. idtok:_INTERNCONST;
  822. pd_flags : pd_implemen+pd_body;
  823. handler : {$ifndef TP}@{$endif}pd_intern;
  824. pocall : [pocall_internconst];
  825. pooption : [];
  826. mutexclpocall : [];
  827. mutexclpotype : [potype_operator];
  828. mutexclpo : []
  829. ),(
  830. idtok:_INTERNPROC;
  831. pd_flags : pd_implemen;
  832. handler : {$ifndef TP}@{$endif}pd_intern;
  833. pocall : [pocall_internproc];
  834. pooption : [];
  835. mutexclpocall : [pocall_inline,pocall_clearstack,pocall_leftright,pocall_cdecl];
  836. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  837. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck]
  838. ),(
  839. idtok:_INTERRUPT;
  840. pd_flags : pd_implemen+pd_body;
  841. handler : nil;
  842. pocall : [];
  843. pooption : [po_interrupt];
  844. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_clearstack,pocall_leftright,pocall_inline];
  845. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  846. mutexclpo : [po_external]
  847. ),(
  848. idtok:_IOCHECK;
  849. pd_flags : pd_implemen+pd_body;
  850. handler : nil;
  851. pocall : [];
  852. pooption : [po_iocheck];
  853. mutexclpocall : [pocall_internproc];
  854. mutexclpotype : [];
  855. mutexclpo : [po_external]
  856. ),(
  857. idtok:_MESSAGE;
  858. pd_flags : pd_interface+pd_object;
  859. handler : {$ifndef TP}@{$endif}pd_message;
  860. pocall : [];
  861. pooption : []; { can be po_msgstr or po_msgint }
  862. mutexclpocall : [pocall_inline,pocall_internproc];
  863. mutexclpotype : [potype_constructor,potype_destructor,potype_operator];
  864. mutexclpo : [po_interrupt,po_external]
  865. ),(
  866. idtok:_NEAR;
  867. pd_flags : pd_implemen+pd_body+pd_procvar;
  868. handler : {$ifndef TP}@{$endif}pd_near;
  869. pocall : [];
  870. pooption : [];
  871. mutexclpocall : [pocall_internproc];
  872. mutexclpotype : [];
  873. mutexclpo : []
  874. ),(
  875. idtok:_OVERRIDE;
  876. pd_flags : pd_interface+pd_object;
  877. handler : {$ifndef TP}@{$endif}pd_override;
  878. pocall : [];
  879. pooption : [po_overridingmethod,po_virtualmethod];
  880. mutexclpocall : [pocall_inline,pocall_internproc];
  881. mutexclpotype : [];
  882. mutexclpo : [po_exports,po_external,po_interrupt]
  883. ),(
  884. idtok:_PASCAL;
  885. pd_flags : pd_implemen+pd_body+pd_procvar;
  886. handler : {$ifndef TP}@{$endif}pd_pascal;
  887. pocall : [pocall_leftright];
  888. pooption : [];
  889. mutexclpocall : [pocall_internproc];
  890. mutexclpotype : [];
  891. mutexclpo : [po_external]
  892. ),(
  893. idtok:_POPSTACK;
  894. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  895. handler : nil;
  896. pocall : [pocall_clearstack];
  897. pooption : [];
  898. mutexclpocall : [pocall_inline,pocall_internproc];
  899. mutexclpotype : [];
  900. mutexclpo : [po_assembler,po_external]
  901. ),(
  902. idtok:_PUBLIC;
  903. pd_flags : pd_implemen+pd_body+pd_global+pd_notobject;
  904. handler : nil;
  905. pocall : [];
  906. pooption : [];
  907. mutexclpocall : [pocall_internproc,pocall_inline];
  908. mutexclpotype : [];
  909. mutexclpo : [po_external]
  910. ),(
  911. idtok:_REGISTER;
  912. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  913. handler : {$ifndef TP}@{$endif}pd_register;
  914. pocall : [pocall_register];
  915. pooption : [];
  916. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc];
  917. mutexclpotype : [];
  918. mutexclpo : [po_external]
  919. ),(
  920. idtok:_SAFECALL;
  921. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  922. handler : {$ifndef TP}@{$endif}pd_safecall;
  923. pocall : [pocall_safecall];
  924. pooption : [po_savestdregs];
  925. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_internproc,pocall_inline];
  926. mutexclpotype : [];
  927. mutexclpo : [po_external]
  928. ),(
  929. idtok:_SAVEREGISTERS;
  930. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  931. handler : nil;
  932. pocall : [];
  933. pooption : [po_saveregisters];
  934. mutexclpocall : [pocall_internproc];
  935. mutexclpotype : [];
  936. mutexclpo : [po_external]
  937. ),(
  938. idtok:_STATIC;
  939. pd_flags : pd_interface+pd_object;
  940. handler : {$ifndef TP}@{$endif}pd_static;
  941. pocall : [];
  942. pooption : [po_staticmethod];
  943. mutexclpocall : [pocall_inline,pocall_internproc];
  944. mutexclpotype : [potype_constructor,potype_destructor];
  945. mutexclpo : [po_external,po_interrupt,po_exports]
  946. ),(
  947. idtok:_STDCALL;
  948. pd_flags : pd_interface+pd_implemen+pd_body+pd_procvar;
  949. handler : {$ifndef TP}@{$endif}pd_stdcall;
  950. pocall : [pocall_stdcall];
  951. pooption : [po_savestdregs];
  952. mutexclpocall : [pocall_leftright,pocall_cdecl,pocall_inline,pocall_internproc];
  953. mutexclpotype : [];
  954. mutexclpo : [po_external]
  955. ),(
  956. idtok:_SYSCALL;
  957. pd_flags : pd_interface;
  958. handler : {$ifndef TP}@{$endif}pd_syscall;
  959. pocall : [pocall_palmossyscall];
  960. pooption : [];
  961. mutexclpocall : [pocall_cdecl,pocall_inline,pocall_internproc];
  962. mutexclpotype : [];
  963. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  964. ),(
  965. idtok:_SYSTEM;
  966. pd_flags : pd_implemen;
  967. handler : {$ifndef TP}@{$endif}pd_system;
  968. pocall : [pocall_clearstack];
  969. pooption : [];
  970. mutexclpocall : [pocall_leftright,pocall_inline,pocall_internproc];
  971. mutexclpotype : [];
  972. mutexclpo : [po_external,po_assembler,po_interrupt]
  973. ),(
  974. idtok:_VIRTUAL;
  975. pd_flags : pd_interface+pd_object;
  976. handler : {$ifndef TP}@{$endif}pd_virtual;
  977. pocall : [];
  978. pooption : [po_virtualmethod];
  979. mutexclpocall : [pocall_inline,pocall_internproc];
  980. mutexclpotype : [];
  981. mutexclpo : [po_external,po_interrupt,po_exports]
  982. )
  983. );
  984. function is_proc_directive(tok:ttoken):boolean;
  985. var
  986. i : longint;
  987. begin
  988. is_proc_directive:=false;
  989. for i:=1 to num_proc_directives do
  990. if proc_direcdata[i].idtok=idtoken then
  991. begin
  992. is_proc_directive:=true;
  993. exit;
  994. end;
  995. end;
  996. function parse_proc_direc(const proc_names:Tstringcontainer;var pdflags:word):boolean;
  997. {
  998. Parse the procedure directive, returns true if a correct directive is found
  999. }
  1000. var
  1001. p : longint;
  1002. found : boolean;
  1003. name : string;
  1004. begin
  1005. parse_proc_direc:=false;
  1006. name:=pattern;
  1007. found:=false;
  1008. for p:=1 to num_proc_directives do
  1009. if proc_direcdata[p].idtok=idtoken then
  1010. begin
  1011. found:=true;
  1012. break;
  1013. end;
  1014. { Check if the procedure directive is known }
  1015. if not found then
  1016. begin
  1017. { parsing a procvar type the name can be any
  1018. next variable !! }
  1019. if (pdflags and (pd_procvar or pd_object))=0 then
  1020. Message1(parser_w_unknown_proc_directive_ignored,name);
  1021. exit;
  1022. end;
  1023. { static needs a special treatment }
  1024. if (idtoken=_STATIC) and not (cs_static_keyword in aktmoduleswitches) then
  1025. exit;
  1026. { Conflicts between directives ? }
  1027. if (aktprocsym^.definition^.proctypeoption in proc_direcdata[p].mutexclpotype) or
  1028. ((aktprocsym^.definition^.proccalloptions*proc_direcdata[p].mutexclpocall)<>[]) or
  1029. ((aktprocsym^.definition^.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  1030. begin
  1031. Message1(parser_e_proc_dir_conflict,name);
  1032. exit;
  1033. end;
  1034. { Check if the directive is only for objects }
  1035. if ((proc_direcdata[p].pd_flags and pd_object)<>0) and
  1036. not assigned(aktprocsym^.definition^._class) then
  1037. begin
  1038. exit;
  1039. end;
  1040. { check if method and directive not for object public }
  1041. if ((proc_direcdata[p].pd_flags and pd_notobject)<>0) and
  1042. assigned(aktprocsym^.definition^._class) then
  1043. begin
  1044. exit;
  1045. end;
  1046. { consume directive, and turn flag on }
  1047. consume(token);
  1048. parse_proc_direc:=true;
  1049. { Check the pd_flags if the directive should be allowed }
  1050. if ((pdflags and pd_interface)<>0) and
  1051. ((proc_direcdata[p].pd_flags and pd_interface)=0) then
  1052. begin
  1053. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  1054. exit;
  1055. end;
  1056. if ((pdflags and pd_implemen)<>0) and
  1057. ((proc_direcdata[p].pd_flags and pd_implemen)=0) then
  1058. begin
  1059. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  1060. exit;
  1061. end;
  1062. if ((pdflags and pd_procvar)<>0) and
  1063. ((proc_direcdata[p].pd_flags and pd_procvar)=0) then
  1064. begin
  1065. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  1066. exit;
  1067. end;
  1068. { Return the new pd_flags }
  1069. if (proc_direcdata[p].pd_flags and pd_body)=0 then
  1070. pdflags:=pdflags and (not pd_body);
  1071. if (proc_direcdata[p].pd_flags and pd_global)<>0 then
  1072. pdflags:=pdflags or pd_global;
  1073. { Add the correct flag }
  1074. aktprocsym^.definition^.proccalloptions:=aktprocsym^.definition^.proccalloptions+proc_direcdata[p].pocall;
  1075. aktprocsym^.definition^.procoptions:=aktprocsym^.definition^.procoptions+proc_direcdata[p].pooption;
  1076. { Adjust positions of args for cdecl or stdcall }
  1077. if (aktprocsym^.definition^.deftype=procdef) and
  1078. (([pocall_cdecl,pocall_stdcall]*aktprocsym^.definition^.proccalloptions)<>[]) then
  1079. aktprocsym^.definition^.parast^.set_alignment(target_os.size_of_longint);
  1080. { Call the handler }
  1081. if pointer({$ifndef FPC}@{$endif}proc_direcdata[p].handler)<>nil then
  1082. proc_direcdata[p].handler(proc_names);
  1083. end;
  1084. {***************************************************************************}
  1085. function check_identical(var p : pprocdef) : boolean;
  1086. {
  1087. Search for idendical definitions,
  1088. if there is a forward, then kill this.
  1089. Returns the result of the forward check.
  1090. Removed from unter_dec to keep the source readable
  1091. }
  1092. var
  1093. hd,pd : Pprocdef;
  1094. storeparast : psymtable;
  1095. ad,fd : psym;
  1096. s : string;
  1097. begin
  1098. check_identical:=false;
  1099. p:=nil;
  1100. pd:=aktprocsym^.definition;
  1101. if assigned(pd) then
  1102. begin
  1103. { Is there an overload/forward ? }
  1104. if assigned(pd^.nextoverloaded) then
  1105. begin
  1106. { walk the procdef list }
  1107. while (assigned(pd)) and (assigned(pd^.nextoverloaded)) do
  1108. begin
  1109. if not(m_repeat_forward in aktmodeswitches) or
  1110. (equal_paras(aktprocsym^.definition^.para,pd^.nextoverloaded^.para,false) and
  1111. { for operators equal_paras is not enough !! }
  1112. ((aktprocsym^.definition^.proctypeoption<>potype_operator) or (optoken<>_ASSIGNMENT) or
  1113. is_equal(pd^.nextoverloaded^.retdef,aktprocsym^.definition^.retdef))) then
  1114. begin
  1115. if pd^.nextoverloaded^.forwarddef then
  1116. { remove the forward definition but don't delete it, }
  1117. { the symtable is the owner !! }
  1118. begin
  1119. hd:=pd^.nextoverloaded;
  1120. { Check if the procedure type and return type are correct }
  1121. if (hd^.proctypeoption<>aktprocsym^.definition^.proctypeoption) or
  1122. (not(is_equal(hd^.retdef,aktprocsym^.definition^.retdef)) and
  1123. (m_repeat_forward in aktmodeswitches)) then
  1124. begin
  1125. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1126. exit;
  1127. end;
  1128. { Check calling convention, no check for internconst,internproc which
  1129. are only defined in interface or implementation }
  1130. if (hd^.proccalloptions-[pocall_internconst,pocall_internproc]<>
  1131. aktprocsym^.definition^.proccalloptions-[pocall_internconst,pocall_internproc]) then
  1132. begin
  1133. { only trigger an error, becuase it doesn't hurt }
  1134. Message(parser_e_call_convention_dont_match_forward);
  1135. end;
  1136. { manglednames are equal? }
  1137. hd^.count:=false;
  1138. if (m_repeat_forward in aktmodeswitches) or
  1139. aktprocsym^.definition^.haspara then
  1140. begin
  1141. if (hd^.mangledname<>aktprocsym^.definition^.mangledname) then
  1142. begin
  1143. { When overloading is not possible then we issue an error }
  1144. { This is not true, tp7/delphi don't give an error when a renamed
  1145. type is used in the other declaration (PFV)
  1146. if not(m_repeat_forward in aktmodeswitches) then
  1147. begin
  1148. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName);
  1149. exit;
  1150. end; }
  1151. if not(po_external in aktprocsym^.definition^.procoptions) then
  1152. Message2(parser_n_interface_name_diff_implementation_name,hd^.mangledname,
  1153. aktprocsym^.definition^.mangledname);
  1154. { reset the mangledname of the interface part to be sure }
  1155. { this is wrong because the mangled name might have been used already !! }
  1156. if hd^.is_used then
  1157. renameasmsymbol(hd^.mangledname,aktprocsym^.definition^.mangledname);
  1158. hd^.setmangledname(aktprocsym^.definition^.mangledname);
  1159. { so we need to keep the name of interface !!
  1160. No!!!! The procedure directives can change the mangledname.
  1161. I fixed this by first calling check_identical and then doing
  1162. the proc directives, but this is not a good solution.(DM)}
  1163. { this is also wrong (PM)
  1164. aktprocsym^.definition^.setmangledname(hd^.mangledname);}
  1165. end
  1166. else
  1167. begin
  1168. { If mangled names are equal, therefore }
  1169. { they have the same number of parameters }
  1170. { Therefore we can check the name of these }
  1171. { parameters... }
  1172. if hd^.forwarddef and aktprocsym^.definition^.forwarddef then
  1173. begin
  1174. Message1(parser_e_function_already_declared_public_forward,aktprocsym^.demangledName);
  1175. Check_identical:=true;
  1176. { Remove other forward from the list to reduce errors }
  1177. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1178. exit;
  1179. end;
  1180. ad:=psym(hd^.parast^.symindex^.first);
  1181. fd:=psym(aktprocsym^.definition^.parast^.symindex^.first);
  1182. if assigned(ad) and assigned(fd) then
  1183. begin
  1184. while assigned(ad) and assigned(fd) do
  1185. begin
  1186. s:=ad^.name;
  1187. if s<>fd^.name then
  1188. begin
  1189. Message3(parser_e_header_different_var_names,
  1190. aktprocsym^.name,s,fd^.name);
  1191. break;
  1192. end;
  1193. { it is impossible to have a nil pointer }
  1194. { for only one parameter - since they }
  1195. { have the same number of parameters. }
  1196. { Left = next parameter. }
  1197. ad:=psym(ad^.left);
  1198. fd:=psym(fd^.left);
  1199. end;
  1200. end;
  1201. end;
  1202. end;
  1203. { also the call_offset }
  1204. hd^.parast^.address_fixup:=aktprocsym^.definition^.parast^.address_fixup;
  1205. hd^.count:=true;
  1206. { remove pd^.nextoverloaded from the list }
  1207. { and add aktprocsym^.definition }
  1208. pd^.nextoverloaded:=pd^.nextoverloaded^.nextoverloaded;
  1209. hd^.nextoverloaded:=aktprocsym^.definition^.nextoverloaded;
  1210. { Alert! All fields of aktprocsym^.definition that are modified
  1211. by the procdir handlers must be copied here!.}
  1212. hd^.forwarddef:=false;
  1213. hd^.proccalloptions:=hd^.proccalloptions + aktprocsym^.definition^.proccalloptions;
  1214. hd^.procoptions:=hd^.procoptions + aktprocsym^.definition^.procoptions;
  1215. if aktprocsym^.definition^.extnumber=-1 then
  1216. aktprocsym^.definition^.extnumber:=hd^.extnumber
  1217. else
  1218. if hd^.extnumber=-1 then
  1219. hd^.extnumber:=aktprocsym^.definition^.extnumber;
  1220. { switch parast for warning in implementation PM }
  1221. if (m_repeat_forward in aktmodeswitches) or
  1222. aktprocsym^.definition^.haspara then
  1223. begin
  1224. storeparast:=hd^.parast;
  1225. hd^.parast:=aktprocsym^.definition^.parast;
  1226. aktprocsym^.definition^.parast:=storeparast;
  1227. end;
  1228. if pd=aktprocsym^.definition then
  1229. p:=nil
  1230. else
  1231. p:=pd;
  1232. aktprocsym^.definition:=hd;
  1233. check_identical:=true;
  1234. end
  1235. else
  1236. { abstract methods aren't forward defined, but this }
  1237. { needs another error message }
  1238. if not(po_abstractmethod in pd^.nextoverloaded^.procoptions) then
  1239. Message(parser_e_overloaded_have_same_parameters)
  1240. else
  1241. Message(parser_e_abstract_no_definition);
  1242. break;
  1243. end;
  1244. pd:=pd^.nextoverloaded;
  1245. end;
  1246. end
  1247. else
  1248. begin
  1249. { there is no overloaded, so its always identical with itself }
  1250. check_identical:=true;
  1251. end;
  1252. end;
  1253. { insert opsym only in the right symtable }
  1254. if ((procinfo^.flags and pi_operator)<>0) and assigned(opsym)
  1255. and not parse_only then
  1256. begin
  1257. if ret_in_param(aktprocsym^.definition^.retdef) then
  1258. begin
  1259. pprocdef(aktprocsym^.definition)^.parast^.insert(opsym);
  1260. { this increases the data size }
  1261. { correct this to get the right ret $value }
  1262. dec(pprocdef(aktprocsym^.definition)^.parast^.datasize,opsym^.getpushsize);
  1263. { this allows to read the funcretoffset }
  1264. opsym^.address:=-4;
  1265. opsym^.varspez:=vs_var;
  1266. end
  1267. else
  1268. pprocdef(aktprocsym^.definition)^.localst^.insert(opsym);
  1269. end;
  1270. end;
  1271. procedure compile_proc_body(const proc_names:Tstringcontainer;
  1272. make_global,parent_has_class:boolean);
  1273. {
  1274. Compile the body of a procedure
  1275. }
  1276. var
  1277. oldexitlabel,oldexit2label : pasmlabel;
  1278. oldfaillabel,oldquickexitlabel:Pasmlabel;
  1279. _class,hp:Pobjectdef;
  1280. { switches can change inside the procedure }
  1281. entryswitches, exitswitches : tlocalswitches;
  1282. { code for the subroutine as tree }
  1283. {$ifdef newcg}
  1284. code:pnode;
  1285. {$else newcg}
  1286. code:ptree;
  1287. {$endif newcg}
  1288. { size of the local strackframe }
  1289. stackframe:longint;
  1290. { true when no stackframe is required }
  1291. nostackframe:boolean;
  1292. { number of bytes which have to be cleared by RET }
  1293. parasize:longint;
  1294. { filepositions }
  1295. entrypos,
  1296. savepos,
  1297. exitpos : tfileposinfo;
  1298. begin
  1299. { calculate the lexical level }
  1300. inc(lexlevel);
  1301. if lexlevel>32 then
  1302. Message(parser_e_too_much_lexlevel);
  1303. { static is also important for local procedures !! }
  1304. if (po_staticmethod in aktprocsym^.definition^.procoptions) then
  1305. allow_only_static:=true
  1306. else if (lexlevel=normal_function_level) then
  1307. allow_only_static:=false;
  1308. { save old labels }
  1309. oldexitlabel:=aktexitlabel;
  1310. oldexit2label:=aktexit2label;
  1311. oldquickexitlabel:=quickexitlabel;
  1312. oldfaillabel:=faillabel;
  1313. { get new labels }
  1314. getlabel(aktexitlabel);
  1315. getlabel(aktexit2label);
  1316. { exit for fail in constructors }
  1317. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1318. begin
  1319. getlabel(faillabel);
  1320. getlabel(quickexitlabel);
  1321. end;
  1322. { reset break and continue labels }
  1323. block_type:=bt_general;
  1324. aktbreaklabel:=nil;
  1325. aktcontinuelabel:=nil;
  1326. { insert symtables for the class, by only if it is no nested function }
  1327. if assigned(procinfo^._class) and not(parent_has_class) then
  1328. begin
  1329. { insert them in the reverse order ! }
  1330. hp:=nil;
  1331. repeat
  1332. _class:=procinfo^._class;
  1333. while _class^.childof<>hp do
  1334. _class:=_class^.childof;
  1335. hp:=_class;
  1336. _class^.symtable^.next:=symtablestack;
  1337. symtablestack:=_class^.symtable;
  1338. until hp=procinfo^._class;
  1339. end;
  1340. { insert parasymtable in symtablestack}
  1341. { only if lexlevel > 1 !!! global symtable should be right after staticsymtazble
  1342. for checking of same names used in interface and implementation !! }
  1343. if lexlevel>=normal_function_level then
  1344. begin
  1345. aktprocsym^.definition^.parast^.next:=symtablestack;
  1346. symtablestack:=aktprocsym^.definition^.parast;
  1347. symtablestack^.symtablelevel:=lexlevel;
  1348. end;
  1349. { insert localsymtable in symtablestack}
  1350. aktprocsym^.definition^.localst^.next:=symtablestack;
  1351. symtablestack:=aktprocsym^.definition^.localst;
  1352. symtablestack^.symtablelevel:=lexlevel;
  1353. { constant symbols are inserted in this symboltable }
  1354. constsymtable:=symtablestack;
  1355. { reset the temporary memory }
  1356. cleartempgen;
  1357. {$ifdef newcg}
  1358. tg.usedinproc:=[];
  1359. {$else newcg}
  1360. { no registers are used }
  1361. usedinproc:=0;
  1362. {$endif newcg}
  1363. { save entry info }
  1364. entrypos:=aktfilepos;
  1365. entryswitches:=aktlocalswitches;
  1366. {$ifdef newcg}
  1367. { parse the code ... }
  1368. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1369. code:=convtree2node(assembler_block)
  1370. else
  1371. code:=convtree2node(block(current_module^.islibrary));
  1372. {$else newcg}
  1373. { parse the code ... }
  1374. if (po_assembler in aktprocsym^.definition^.procoptions) then
  1375. code:=assembler_block
  1376. else
  1377. code:=block(current_module^.islibrary);
  1378. {$endif newcg}
  1379. { get a better entry point }
  1380. if assigned(code) then
  1381. entrypos:=code^.fileinfo;
  1382. { save exit info }
  1383. exitswitches:=aktlocalswitches;
  1384. exitpos:=last_endtoken_filepos;
  1385. { save current filepos }
  1386. savepos:=aktfilepos;
  1387. {When we are called to compile the body of a unit, aktprocsym should
  1388. point to the unit initialization. If the unit has no initialization,
  1389. aktprocsym=nil. But in that case code=nil. hus we should check for
  1390. code=nil, when we use aktprocsym.}
  1391. { set the framepointer to esp for assembler functions }
  1392. { but only if the are no local variables }
  1393. { already done in assembler_block }
  1394. {$ifdef newcg}
  1395. tg.setfirsttemp(procinfo^.firsttemp);
  1396. {$else newcg}
  1397. setfirsttemp(procinfo^.firsttemp);
  1398. {$endif newcg}
  1399. { ... and generate assembler }
  1400. { but set the right switches for entry !! }
  1401. aktlocalswitches:=entryswitches;
  1402. {$ifndef NOPASS2}
  1403. {$ifdef newcg}
  1404. tg.setfirsttemp(procinfo^.firsttemp);
  1405. {$else newcg}
  1406. if assigned(code) then
  1407. generatecode(code);
  1408. {$endif newcg}
  1409. { set switches to status at end of procedure }
  1410. aktlocalswitches:=exitswitches;
  1411. if assigned(code) then
  1412. begin
  1413. aktprocsym^.definition^.code:=code;
  1414. { the procedure is now defined }
  1415. aktprocsym^.definition^.forwarddef:=false;
  1416. {$ifdef newcg}
  1417. aktprocsym^.definition^.usedregisters:=tg.usedinproc;
  1418. {$else newcg}
  1419. aktprocsym^.definition^.usedregisters:=usedinproc;
  1420. {$endif newcg}
  1421. end;
  1422. {$ifdef newcg}
  1423. stackframe:=tg.gettempsize;
  1424. {$else newcg}
  1425. stackframe:=gettempsize;
  1426. {$endif newcg}
  1427. { first generate entry code with the correct position and switches }
  1428. aktfilepos:=entrypos;
  1429. aktlocalswitches:=entryswitches;
  1430. {$ifdef newcg}
  1431. if assigned(code) then
  1432. cg^.g_entrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1433. {$else newcg}
  1434. if assigned(code) then
  1435. genentrycode(procinfo^.aktentrycode,proc_names,make_global,stackframe,parasize,nostackframe,false);
  1436. {$endif newcg}
  1437. { now generate exit code with the correct position and switches }
  1438. aktfilepos:=exitpos;
  1439. aktlocalswitches:=exitswitches;
  1440. if assigned(code) then
  1441. begin
  1442. {$ifdef newcg}
  1443. cg^.g_exitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1444. {$else newcg}
  1445. genexitcode(procinfo^.aktexitcode,parasize,nostackframe,false);
  1446. {$endif newcg}
  1447. procinfo^.aktproccode^.insertlist(procinfo^.aktentrycode);
  1448. procinfo^.aktproccode^.concatlist(procinfo^.aktexitcode);
  1449. {$ifdef i386}
  1450. {$ifndef NoOpt}
  1451. if (cs_optimize in aktglobalswitches) and
  1452. { do not optimize pure assembler procedures }
  1453. ((procinfo^.flags and pi_is_assembler)=0) then
  1454. Optimize(procinfo^.aktproccode);
  1455. {$endif NoOpt}
  1456. {$endif}
  1457. { save local data (casetable) also in the same file }
  1458. if assigned(procinfo^.aktlocaldata) and
  1459. (not procinfo^.aktlocaldata^.empty) then
  1460. begin
  1461. procinfo^.aktproccode^.concat(new(pai_section,init(sec_data)));
  1462. procinfo^.aktproccode^.concatlist(procinfo^.aktlocaldata);
  1463. end;
  1464. { now we can insert a cut }
  1465. if (cs_create_smart in aktmoduleswitches) then
  1466. codesegment^.concat(new(pai_cut,init));
  1467. { add the procedure to the codesegment }
  1468. codesegment^.concatlist(procinfo^.aktproccode);
  1469. end;
  1470. {$else}
  1471. if assigned(code) then
  1472. firstpass(code);
  1473. {$endif NOPASS2}
  1474. { ... remove symbol tables, for the browser leave the static table }
  1475. { if (cs_browser in aktmoduleswitches) and (symtablestack^.symtabletype=staticsymtable) then
  1476. symtablestack^.next:=symtablestack^.next^.next
  1477. else }
  1478. if lexlevel>=normal_function_level then
  1479. symtablestack:=symtablestack^.next^.next
  1480. else
  1481. symtablestack:=symtablestack^.next;
  1482. { ... check for unused symbols }
  1483. { but only if there is no asm block }
  1484. if assigned(code) then
  1485. begin
  1486. if (Errorcount=0) then
  1487. begin
  1488. aktprocsym^.definition^.localst^.check_forwards;
  1489. aktprocsym^.definition^.localst^.checklabels;
  1490. end;
  1491. if (procinfo^.flags and pi_uses_asm)=0 then
  1492. begin
  1493. { not for unit init, becuase the var can be used in finalize,
  1494. it will be done in proc_unit }
  1495. if not(aktprocsym^.definition^.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1496. aktprocsym^.definition^.localst^.allsymbolsused;
  1497. aktprocsym^.definition^.parast^.allsymbolsused;
  1498. end;
  1499. end;
  1500. { the local symtables can be deleted, but the parast }
  1501. { doesn't, (checking definitons when calling a }
  1502. { function }
  1503. { not for a inline procedure !! (PM) }
  1504. { at lexlevel = 1 localst is the staticsymtable itself }
  1505. { so no dispose here !! }
  1506. if assigned(code) and
  1507. not(cs_browser in aktmoduleswitches) and
  1508. not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1509. begin
  1510. if lexlevel>=normal_function_level then
  1511. dispose(aktprocsym^.definition^.localst,done);
  1512. aktprocsym^.definition^.localst:=nil;
  1513. end;
  1514. { only now we can remove the temps }
  1515. resettempgen;
  1516. { remove code tree, if not inline procedure }
  1517. if assigned(code) and not(pocall_inline in aktprocsym^.definition^.proccalloptions) then
  1518. {$ifdef newcg}
  1519. dispose(code,done);
  1520. {$else newcg}
  1521. disposetree(code);
  1522. {$endif newcg}
  1523. { remove class member symbol tables }
  1524. while symtablestack^.symtabletype=objectsymtable do
  1525. symtablestack:=symtablestack^.next;
  1526. { restore filepos, the switches are already set }
  1527. aktfilepos:=savepos;
  1528. { free labels }
  1529. freelabel(aktexitlabel);
  1530. freelabel(aktexit2label);
  1531. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1532. begin
  1533. freelabel(faillabel);
  1534. freelabel(quickexitlabel);
  1535. end;
  1536. { restore labels }
  1537. aktexitlabel:=oldexitlabel;
  1538. aktexit2label:=oldexit2label;
  1539. quickexitlabel:=oldquickexitlabel;
  1540. faillabel:=oldfaillabel;
  1541. { reset to normal non static function }
  1542. if (lexlevel=normal_function_level) then
  1543. allow_only_static:=false;
  1544. { previous lexlevel }
  1545. dec(lexlevel);
  1546. end;
  1547. procedure parse_proc_directives(Anames:Pstringcontainer;var pdflags:word);
  1548. {
  1549. Parse the procedure directives. It does not matter if procedure directives
  1550. are written using ;procdir; or ['procdir'] syntax.
  1551. }
  1552. var
  1553. res : boolean;
  1554. begin
  1555. while token in [_ID,_LECKKLAMMER] do
  1556. begin
  1557. if try_to_consume(_LECKKLAMMER) then
  1558. begin
  1559. repeat
  1560. parse_proc_direc(Anames^,pdflags);
  1561. until not try_to_consume(_COMMA);
  1562. consume(_RECKKLAMMER);
  1563. { we always expect at least '[];' }
  1564. res:=true;
  1565. end
  1566. else
  1567. res:=parse_proc_direc(Anames^,pdflags);
  1568. { A procedure directive normally followed by a semicolon, but in
  1569. a const section we should stop when _EQUAL is found }
  1570. if res then
  1571. begin
  1572. if (block_type=bt_const) and
  1573. (token=_EQUAL) then
  1574. break;
  1575. consume(_SEMICOLON);
  1576. end
  1577. else
  1578. break;
  1579. end;
  1580. end;
  1581. procedure parse_var_proc_directives(var sym : psym);
  1582. var
  1583. anames : pstringcontainer;
  1584. pdflags : word;
  1585. oldsym : pprocsym;
  1586. pd : pabstractprocdef;
  1587. begin
  1588. oldsym:=aktprocsym;
  1589. anames:=new(pstringcontainer,init);
  1590. pdflags:=pd_procvar;
  1591. { we create a temporary aktprocsym to read the directives }
  1592. aktprocsym:=new(pprocsym,init(sym^.name));
  1593. case sym^.typ of
  1594. varsym :
  1595. pd:=pabstractprocdef(pvarsym(sym)^.definition);
  1596. typedconstsym :
  1597. pd:=pabstractprocdef(ptypedconstsym(sym)^.definition);
  1598. typesym :
  1599. pd:=pabstractprocdef(ptypesym(sym)^.definition);
  1600. else
  1601. internalerror(994932432);
  1602. end;
  1603. if pd^.deftype<>procvardef then
  1604. internalerror(994932433);
  1605. pabstractprocdef(aktprocsym^.definition):=pd;
  1606. { names should never be used anyway }
  1607. inc(lexlevel);
  1608. parse_proc_directives(anames,pdflags);
  1609. dec(lexlevel);
  1610. aktprocsym^.definition:=nil;
  1611. dispose(aktprocsym,done);
  1612. dispose(anames,done);
  1613. aktprocsym:=oldsym;
  1614. end;
  1615. procedure parse_object_proc_directives(var sym : pprocsym);
  1616. var
  1617. anames : pstringcontainer;
  1618. pdflags : word;
  1619. begin
  1620. pdflags:=pd_object;
  1621. anames:=new(pstringcontainer,init);
  1622. inc(lexlevel);
  1623. parse_proc_directives(anames,pdflags);
  1624. dec(lexlevel);
  1625. dispose(anames,done);
  1626. if (po_containsself in aktprocsym^.definition^.procoptions) and
  1627. (([po_msgstr,po_msgint]*aktprocsym^.definition^.procoptions)=[]) then
  1628. Message(parser_e_self_in_non_message_handler);
  1629. end;
  1630. procedure checkvaluepara(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  1631. var
  1632. vs : pvarsym;
  1633. s : string;
  1634. begin
  1635. with pvarsym(p)^ do
  1636. begin
  1637. if copy(name,1,3)='val' then
  1638. begin
  1639. s:=Copy(name,4,255);
  1640. if not(po_assembler in aktprocsym^.definition^.procoptions) then
  1641. begin
  1642. vs:=new(Pvarsym,init(s,definition));
  1643. vs^.fileinfo:=fileinfo;
  1644. vs^.varspez:=varspez;
  1645. aktprocsym^.definition^.localst^.insert(vs);
  1646. vs^.islocalcopy:=true;
  1647. vs^.varstate:=vs_assigned;
  1648. localvarsym:=vs;
  1649. inc(refs); { the para was used to set the local copy ! }
  1650. { warnings only on local copy ! }
  1651. varstate:=vs_used;
  1652. end
  1653. else
  1654. begin
  1655. aktprocsym^.definition^.parast^.rename(name,s);
  1656. end;
  1657. end;
  1658. end;
  1659. end;
  1660. procedure read_proc;
  1661. {
  1662. Parses the procedure directives, then parses the procedure body, then
  1663. generates the code for it
  1664. }
  1665. var
  1666. oldprefix : string;
  1667. oldprocsym : Pprocsym;
  1668. oldprocinfo : pprocinfo;
  1669. oldconstsymtable : Psymtable;
  1670. oldfilepos : tfileposinfo;
  1671. names : Pstringcontainer;
  1672. pdflags : word;
  1673. prevdef,stdef : pprocdef;
  1674. begin
  1675. { save old state }
  1676. oldprocsym:=aktprocsym;
  1677. oldprefix:=procprefix;
  1678. oldconstsymtable:=constsymtable;
  1679. oldprocinfo:=procinfo;
  1680. { create a new procedure }
  1681. new(names,init);
  1682. codegen_newprocedure;
  1683. with procinfo^ do
  1684. begin
  1685. parent:=oldprocinfo;
  1686. { clear flags }
  1687. flags:=0;
  1688. { standard frame pointer }
  1689. framepointer:=frame_pointer;
  1690. { funcret_is_valid:=false; }
  1691. funcret_state:=vs_declared;
  1692. { is this a nested function of a method ? }
  1693. if assigned(oldprocinfo) then
  1694. _class:=oldprocinfo^._class;
  1695. end;
  1696. parse_proc_dec;
  1697. { set the default function options }
  1698. if parse_only then
  1699. begin
  1700. aktprocsym^.definition^.forwarddef:=true;
  1701. { set also the interface flag, for better error message when the
  1702. implementation doesn't much this header }
  1703. aktprocsym^.definition^.interfacedef:=true;
  1704. pdflags:=pd_interface;
  1705. end
  1706. else
  1707. begin
  1708. pdflags:=pd_body;
  1709. if current_module^.in_implementation then
  1710. pdflags:=pdflags or pd_implemen;
  1711. if (not current_module^.is_unit) or (cs_create_smart in aktmoduleswitches) then
  1712. pdflags:=pdflags or pd_global;
  1713. procinfo^.exported:=false;
  1714. aktprocsym^.definition^.forwarddef:=false;
  1715. end;
  1716. { parse the directives that may follow }
  1717. inc(lexlevel);
  1718. parse_proc_directives(names,pdflags);
  1719. dec(lexlevel);
  1720. { set aktfilepos to the beginning of the function declaration }
  1721. oldfilepos:=aktfilepos;
  1722. aktfilepos:=aktprocsym^.definition^.fileinfo;
  1723. { search for forward declarations }
  1724. if not check_identical(prevdef) then
  1725. begin
  1726. { A method must be forward defined (in the object declaration) }
  1727. if assigned(procinfo^._class) and (not assigned(oldprocinfo^._class)) then
  1728. Message(parser_e_header_dont_match_any_member);
  1729. { Give a better error if there is a forward def in the interface and only
  1730. a single implementation }
  1731. if (not aktprocsym^.definition^.forwarddef) and
  1732. assigned(aktprocsym^.definition^.nextoverloaded) and
  1733. aktprocsym^.definition^.nextoverloaded^.forwarddef and
  1734. aktprocsym^.definition^.nextoverloaded^.interfacedef and
  1735. not(assigned(aktprocsym^.definition^.nextoverloaded^.nextoverloaded)) then
  1736. Message1(parser_e_header_dont_match_forward,aktprocsym^.demangledName)
  1737. else
  1738. begin
  1739. { check the global flag }
  1740. if (procinfo^.flags and pi_is_global)<>0 then
  1741. Message(parser_e_overloaded_must_be_all_global);
  1742. end
  1743. end;
  1744. { set return type here, becuase the aktprocsym^.definition can be
  1745. changed by check_identical (PFV) }
  1746. procinfo^.retdef:=aktprocsym^.definition^.retdef;
  1747. { pointer to the return value ? }
  1748. if ret_in_param(procinfo^.retdef) then
  1749. begin
  1750. procinfo^.retoffset:=procinfo^.call_offset;
  1751. inc(procinfo^.call_offset,target_os.size_of_pointer);
  1752. end;
  1753. { allows to access the parameters of main functions in nested functions }
  1754. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.call_offset;
  1755. { when it is a value para and it needs a local copy then rename
  1756. the parameter and insert a copy in the localst. This is not done
  1757. for assembler procedures }
  1758. if (not parse_only) and (not aktprocsym^.definition^.forwarddef) then
  1759. aktprocsym^.definition^.parast^.foreach({$ifndef TP}@{$endif}checkvaluepara);
  1760. { restore file pos }
  1761. aktfilepos:=oldfilepos;
  1762. { compile procedure when a body is needed }
  1763. if (pdflags and pd_body)<>0 then
  1764. begin
  1765. Message1(parser_p_procedure_start,aktprocsym^.demangledname);
  1766. names^.insert(aktprocsym^.definition^.mangledname);
  1767. { set _FAIL as keyword if constructor }
  1768. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1769. tokeninfo^[_FAIL].keyword:=m_all;
  1770. if assigned(aktprocsym^.definition^._class) then
  1771. tokeninfo^[_SELF].keyword:=m_all;
  1772. compile_proc_body(names^,((pdflags and pd_global)<>0),assigned(oldprocinfo^._class));
  1773. { reset _FAIL as normal }
  1774. if (aktprocsym^.definition^.proctypeoption=potype_constructor) then
  1775. tokeninfo^[_FAIL].keyword:=m_none;
  1776. if assigned(aktprocsym^.definition^._class) and (lexlevel=main_program_level) then
  1777. tokeninfo^[_SELF].keyword:=m_none;
  1778. consume(_SEMICOLON);
  1779. end;
  1780. { close }
  1781. dispose(names,done);
  1782. codegen_doneprocedure;
  1783. { Restore old state }
  1784. constsymtable:=oldconstsymtable;
  1785. { from now on all refernece to mangledname means
  1786. that the function is already used }
  1787. aktprocsym^.definition^.count:=true;
  1788. { restore the interface order to maintain CRC values PM }
  1789. if assigned(prevdef) and assigned(aktprocsym^.definition^.nextoverloaded) then
  1790. begin
  1791. stdef:=aktprocsym^.definition;
  1792. aktprocsym^.definition:=stdef^.nextoverloaded;
  1793. stdef^.nextoverloaded:=prevdef^.nextoverloaded;
  1794. prevdef^.nextoverloaded:=stdef;
  1795. end;
  1796. aktprocsym:=oldprocsym;
  1797. procprefix:=oldprefix;
  1798. procinfo:=oldprocinfo;
  1799. opsym:=nil;
  1800. end;
  1801. end.
  1802. {
  1803. $Log$
  1804. Revision 1.35 1999-11-17 17:05:02 pierre
  1805. * Notes/hints changes
  1806. Revision 1.34 1999/11/10 00:24:02 pierre
  1807. * more browser details
  1808. Revision 1.33 1999/11/09 23:43:08 pierre
  1809. * better browser info
  1810. Revision 1.32 1999/11/09 23:06:45 peter
  1811. * esi_offset -> selfpointer_offset to be newcg compatible
  1812. * hcogegen -> cgbase fixes for newcg
  1813. Revision 1.31 1999/11/06 14:34:23 peter
  1814. * truncated log to 20 revs
  1815. Revision 1.30 1999/10/26 12:30:44 peter
  1816. * const parameter is now checked
  1817. * better and generic check if a node can be used for assigning
  1818. * export fixes
  1819. * procvar equal works now (it never had worked at least from 0.99.8)
  1820. * defcoll changed to linkedlist with pparaitem so it can easily be
  1821. walked both directions
  1822. Revision 1.29 1999/10/22 10:39:35 peter
  1823. * split type reading from pdecl to ptype unit
  1824. * parameter_dec routine is now used for procedure and procvars
  1825. Revision 1.28 1999/10/13 10:37:36 peter
  1826. * moved mangledname creation of normal proc so it also handles a wrong
  1827. method proc
  1828. Revision 1.27 1999/10/12 21:20:46 florian
  1829. * new codegenerator compiles again
  1830. Revision 1.26 1999/10/03 19:38:39 peter
  1831. * fixed forward decl check for tp7/delphi
  1832. Revision 1.25 1999/10/01 10:05:44 peter
  1833. + procedure directive support in const declarations, fixes bug 232
  1834. Revision 1.24 1999/10/01 08:02:47 peter
  1835. * forward type declaration rewritten
  1836. Revision 1.23 1999/09/27 23:44:56 peter
  1837. * procinfo is now a pointer
  1838. * support for result setting in sub procedure
  1839. Revision 1.22 1999/09/20 16:39:00 peter
  1840. * cs_create_smart instead of cs_smartlink
  1841. * -CX is create smartlink
  1842. * -CD is create dynamic, but does nothing atm.
  1843. Revision 1.21 1999/09/15 20:35:42 florian
  1844. * small fix to operator overloading when in MMX mode
  1845. + the compiler uses now fldz and fld1 if possible
  1846. + some fixes to floating point registers
  1847. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1848. * .... ???
  1849. Revision 1.20 1999/09/10 18:48:09 florian
  1850. * some bug fixes (e.g. must_be_valid and procinfo^.funcret_is_valid)
  1851. * most things for stored properties fixed
  1852. Revision 1.19 1999/09/07 14:59:40 pierre
  1853. * bugfix for 237 and 244
  1854. Revision 1.18 1999/09/02 18:47:45 daniel
  1855. * Could not compile with TP, some arrays moved to heap
  1856. * NOAG386BIN default for TP
  1857. * AG386* files were not compatible with TP, fixed.
  1858. Revision 1.17 1999/08/30 10:17:57 peter
  1859. * fixed crash in psub
  1860. * ansistringcompare fixed
  1861. * support for #$0b8
  1862. Revision 1.16 1999/08/27 10:41:46 pierre
  1863. * pi_is_global was missing for global methods
  1864. + code to restore overloaded list order
  1865. (necessary to get same CRC values after interface and after
  1866. implementation)
  1867. Revision 1.15 1999/08/19 13:02:11 pierre
  1868. + label faillabel added for _FAIL support
  1869. Revision 1.14 1999/08/10 16:24:44 pierre
  1870. * linking to C code with cdecl;external; was broken
  1871. Revision 1.13 1999/08/10 12:37:44 pierre
  1872. + procsym field of tprocdef set
  1873. Revision 1.12 1999/08/05 16:53:06 peter
  1874. * V_Fatal=1, all other V_ are also increased
  1875. * Check for local procedure when assigning procvar
  1876. * fixed comment parsing because directives
  1877. * oldtp mode directives better supported
  1878. * added some messages to errore.msg
  1879. Revision 1.11 1999/08/04 13:03:01 jonas
  1880. * all tokens now start with an underscore
  1881. * PowerPC compiles!!
  1882. }