psub.pas 74 KB

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