1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267 |
- {
- $Id$
- Copyright (c) 1998-2002 by Florian Klaempfl
- Parses variable declarations. Used for var statement and record
- definitions
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit pdecvar;
- {$i fpcdefs.inc}
- interface
- uses
- symsym,symdef;
- function read_property_dec(aclass:tobjectdef):tpropertysym;
- procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
- implementation
- uses
- { common }
- cutils,cclasses,
- { global }
- globtype,globals,tokens,verbose,
- systems,
- { symtable }
- symconst,symbase,symtype,symtable,defutil,defcmp,
- fmodule,
- { pass 1 }
- node,pass_1,
- nmat,nadd,ncal,nset,ncnv,ninl,ncon,nld,nflw,nmem,
- { codegen }
- ncgutil,
- { parser }
- scanner,
- pbase,pexpr,ptype,ptconst,pdecsub,
- { link }
- import
- {$ifdef Delphi}
- ,dmisc
- ,sysutils
- {$endif}
- ;
- function read_property_dec(aclass:tobjectdef):tpropertysym;
- { convert a node tree to symlist and return the last
- symbol }
- function parse_symlist(pl:tsymlist;var def:tdef):boolean;
- var
- idx : longint;
- sym : tsym;
- srsymtable : tsymtable;
- st : tsymtable;
- p : tnode;
- begin
- result:=true;
- def:=nil;
- if token=_ID then
- begin
- if assigned(aclass) then
- sym:=search_class_member(aclass,pattern)
- else
- searchsym(pattern,sym,srsymtable);
- if assigned(sym) then
- begin
- case sym.typ of
- varsym :
- begin
- pl.addsym(sl_load,sym);
- def:=tvarsym(sym).vartype.def;
- end;
- procsym :
- begin
- pl.addsym(sl_call,sym);
- end;
- end;
- end
- else
- begin
- Message1(parser_e_illegal_field_or_method,pattern);
- result:=false;
- end;
- consume(_ID);
- repeat
- case token of
- _ID,
- _SEMICOLON :
- begin
- break;
- end;
- _POINT :
- begin
- consume(_POINT);
- if assigned(def) then
- begin
- st:=def.getsymtable(gs_record);
- if assigned(st) then
- begin
- sym:=searchsymonlyin(st,pattern);
- if assigned(sym) then
- begin
- pl.addsym(sl_subscript,sym);
- case sym.typ of
- varsym :
- def:=tvarsym(sym).vartype.def;
- else
- begin
- Message1(sym_e_illegal_field,pattern);
- result:=false;
- end;
- end;
- end
- else
- begin
- Message1(sym_e_illegal_field,pattern);
- result:=false;
- end;
- end
- else
- begin
- Message(parser_e_invalid_qualifier);
- result:=false;
- end;
- end
- else
- begin
- Message(parser_e_invalid_qualifier);
- result:=false;
- end;
- consume(_ID);
- end;
- _LECKKLAMMER :
- begin
- consume(_LECKKLAMMER);
- repeat
- if def.deftype=arraydef then
- begin
- idx:=0;
- p:=comp_expr(true);
- if (not codegenerror) then
- begin
- if (p.nodetype=ordconstn) then
- begin
- if compare_defs(p.resulttype.def,tarraydef(def).rangetype.def,nothingn)>=te_equal then
- idx:=tordconstnode(p).value
- else
- IncompatibleTypes(p.resulttype.def,tarraydef(def).rangetype.def);
- end
- else
- Message(parser_e_illegal_expression)
- end;
- p.free;
- pl.addconst(sl_vec,idx);
- def:=tarraydef(def).elementtype.def;
- end
- else
- begin
- Message(parser_e_invalid_qualifier);
- result:=false;
- end;
- until not try_to_consume(_COMMA);
- consume(_RECKKLAMMER);
- end;
- else
- begin
- Message(parser_e_ill_property_access_sym);
- result:=false;
- break;
- end;
- end;
- until false;
- end
- else
- begin
- Message(parser_e_ill_property_access_sym);
- result:=false;
- end;
- end;
- var
- sym : tsym;
- p : tpropertysym;
- overriden : tsym;
- hs : string;
- varspez : tvarspez;
- s : string;
- tt : ttype;
- arraytype : ttype;
- def : tdef;
- pt : tnode;
- propname : stringid;
- sc : tsinglelist;
- oldregisterdef : boolean;
- readvs,
- hvs : tvarsym;
- readprocdef,
- writeprocdef : tprocvardef;
- oldsymtablestack : tsymtable;
- begin
- { Generate temp procvardefs to search for matching read/write
- procedures. the readprocdef will store all definitions }
- oldregisterdef:=registerdef;
- registerdef:=false;
- readprocdef:=tprocvardef.create(normal_function_level);
- writeprocdef:=tprocvardef.create(normal_function_level);
- registerdef:=oldregisterdef;
- { make it method pointers }
- if assigned(aclass) then
- begin
- include(readprocdef.procoptions,po_methodpointer);
- include(writeprocdef.procoptions,po_methodpointer);
- end;
- if token<>_ID then
- begin
- consume(_ID);
- consume(_SEMICOLON);
- exit;
- end;
- { Generate propertysym and insert in symtablestack }
- p:=tpropertysym.create(orgpattern);
- symtablestack.insert(p);
- propname:=pattern;
- consume(_ID);
- { Set the symtablestack to the parast of readprop so
- temp defs will be destroyed after declaration }
- readprocdef.parast.next:=symtablestack;
- symtablestack:=readprocdef.parast;
- { property parameters ? }
- if token=_LECKKLAMMER then
- begin
- if (sp_published in current_object_option) then
- Message(parser_e_cant_publish_that_property);
- { create a list of the parameters }
- sc:=tsinglelist.create;
- consume(_LECKKLAMMER);
- inc(testcurobject);
- repeat
- if token=_VAR then
- begin
- consume(_VAR);
- varspez:=vs_var;
- end
- else if token=_CONST then
- begin
- consume(_CONST);
- varspez:=vs_const;
- end
- else if (idtoken=_OUT) and (m_out in aktmodeswitches) then
- begin
- consume(_OUT);
- varspez:=vs_out;
- end
- else
- varspez:=vs_value;
- sc.reset;
- repeat
- readvs:=tvarsym.create(orgpattern,varspez,generrortype);
- readprocdef.parast.insert(readvs);
- sc.insert(readvs);
- consume(_ID);
- until not try_to_consume(_COMMA);
- if try_to_consume(_COLON) then
- begin
- { for records, don't search the recordsymtable for
- the symbols of the types }
- oldsymtablestack:=symtablestack;
- symtablestack:=symtablestack.next;
- if try_to_consume(_ARRAY) then
- begin
- consume(_OF);
- { define range and type of range }
- tt.setdef(tarraydef.create(0,-1,s32inttype));
- { define field type }
- single_type(arraytype,s,false);
- tarraydef(tt.def).setelementtype(arraytype);
- end
- else
- single_type(tt,s,false);
- symtablestack:=oldsymtablestack;
- end
- else
- tt:=cformaltype;
- readvs:=tvarsym(sc.first);
- while assigned(readvs) do
- begin
- readprocdef.concatpara(nil,tt,readvs,nil,false);
- { also update the writeprocdef }
- hvs:=tvarsym.create(readvs.realname,vs_value,generrortype);
- writeprocdef.parast.insert(hvs);
- writeprocdef.concatpara(nil,tt,hvs,nil,false);
- readvs:=tvarsym(readvs.listnext);
- end;
- until not try_to_consume(_SEMICOLON);
- sc.free;
- dec(testcurobject);
- consume(_RECKKLAMMER);
- { the parser need to know if a property has parameters, the
- index parameter doesn't count (PFV) }
- if readprocdef.minparacount>0 then
- include(p.propoptions,ppo_hasparameters);
- end;
- { overriden property ? }
- { force property interface
- there is a property parameter
- a global property }
- if (token=_COLON) or (readprocdef.minparacount>0) or (aclass=nil) then
- begin
- consume(_COLON);
- { insert types in global symtable }
- oldsymtablestack:=symtablestack;
- while not(symtablestack.symtabletype in [globalsymtable,staticsymtable]) do
- symtablestack:=symtablestack.next;
- single_type(p.proptype,hs,false);
- symtablestack:=oldsymtablestack;
- if (idtoken=_INDEX) then
- begin
- consume(_INDEX);
- pt:=comp_expr(true);
- if is_constnode(pt) and
- is_ordinal(pt.resulttype.def)
- {$ifndef cpu64bit}
- and (not is_64bitint(pt.resulttype.def))
- {$endif}
- then
- p.index:=tordconstnode(pt).value
- else
- begin
- Message(parser_e_invalid_property_index_value);
- p.index:=0;
- end;
- inserttypeconv(pt,sinttype);
- p.indextype.setdef(pt.resulttype.def);
- include(p.propoptions,ppo_indexed);
- { concat a longint to the para templates }
- hvs:=tvarsym.create('$index',vs_value,p.indextype);
- readprocdef.parast.insert(hvs);
- readprocdef.concatpara(nil,p.indextype,hvs,nil,false);
- hvs:=tvarsym.create('$index',vs_value,p.indextype);
- writeprocdef.parast.insert(hvs);
- writeprocdef.concatpara(nil,p.indextype,hvs,nil,false);
- pt.free;
- end;
- end
- else
- begin
- { do an property override }
- overriden:=search_class_member(aclass.childof,propname);
- if assigned(overriden) and (overriden.typ=propertysym) then
- begin
- p.dooverride(tpropertysym(overriden));
- end
- else
- begin
- p.proptype:=generrortype;
- message(parser_e_no_property_found_to_override);
- end;
- end;
- if (sp_published in current_object_option) and
- not(p.proptype.def.is_publishable) then
- Message(parser_e_cant_publish_that_property);
- if try_to_consume(_READ) then
- begin
- p.readaccess.clear;
- if parse_symlist(p.readaccess,def) then
- begin
- sym:=p.readaccess.firstsym^.sym;
- case sym.typ of
- procsym :
- begin
- { read is function returning the type of the property }
- readprocdef.rettype:=p.proptype;
- { Insert hidden parameters }
- handle_calling_convention(readprocdef);
- calc_parast(readprocdef);
- { search procdefs matching readprocdef }
- { we ignore hidden stuff here because the property access symbol might have
- non default calling conventions which might change the hidden stuff;
- see tw3216.pp (FK) }
- p.readaccess.procdef:=Tprocsym(sym).search_procdef_bypara(readprocdef.para,p.proptype.def,[cpo_allowdefaults,cpo_ignorehidden,cpo_allowconvert]);
- if not assigned(p.readaccess.procdef) then
- Message(parser_e_ill_property_access_sym);
- end;
- varsym :
- begin
- if not assigned(def) then
- internalerror(200310071);
- if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
- begin
- { property parameters are allowed if this is
- an indexed property, because the index is then
- the parameter.
- Note: In the help of Kylix it is written
- that it isn't allowed, but the compiler accepts it (PFV) }
- if (ppo_hasparameters in p.propoptions) then
- Message(parser_e_ill_property_access_sym);
- end
- else
- IncompatibleTypes(def,p.proptype.def);
- end;
- else
- Message(parser_e_ill_property_access_sym);
- end;
- end;
- end;
- if try_to_consume(_WRITE) then
- begin
- p.writeaccess.clear;
- if parse_symlist(p.writeaccess,def) then
- begin
- sym:=p.writeaccess.firstsym^.sym;
- case sym.typ of
- procsym :
- begin
- { write is a procedure with an extra value parameter
- of the of the property }
- writeprocdef.rettype:=voidtype;
- hvs:=tvarsym.create('$value',vs_value,p.proptype);
- writeprocdef.parast.insert(hvs);
- writeprocdef.concatpara(nil,p.proptype,hvs,nil,false);
- { Insert hidden parameters }
- handle_calling_convention(writeprocdef);
- calc_parast(writeprocdef);
- { search procdefs matching writeprocdef }
- p.writeaccess.procdef:=Tprocsym(sym).search_procdef_bypara(writeprocdef.para,writeprocdef.rettype.def,[cpo_allowdefaults,cpo_allowconvert]);
- if not assigned(p.writeaccess.procdef) then
- Message(parser_e_ill_property_access_sym);
- end;
- varsym :
- begin
- if not assigned(def) then
- internalerror(200310072);
- if compare_defs(def,p.proptype.def,nothingn)>=te_equal then
- begin
- { property parameters are allowed if this is
- an indexed property, because the index is then
- the parameter.
- Note: In the help of Kylix it is written
- that it isn't allowed, but the compiler accepts it (PFV) }
- if (ppo_hasparameters in p.propoptions) then
- Message(parser_e_ill_property_access_sym);
- end
- else
- IncompatibleTypes(def,p.proptype.def);
- end;
- else
- Message(parser_e_ill_property_access_sym);
- end;
- end;
- end;
- if assigned(aclass) then
- begin
- include(p.propoptions,ppo_stored);
- if try_to_consume(_STORED) then
- begin
- p.storedaccess.clear;
- case token of
- _ID:
- begin
- { in the case that idtoken=_DEFAULT }
- { we have to do nothing except }
- { setting ppo_stored, it's the same }
- { as stored true }
- if idtoken<>_DEFAULT then
- begin
- if parse_symlist(p.storedaccess,def) then
- begin
- sym:=p.storedaccess.firstsym^.sym;
- case sym.typ of
- procsym :
- begin
- p.storedaccess.procdef:=Tprocsym(sym).search_procdef_nopara_boolret;
- if not assigned(p.storedaccess.procdef) then
- message(parser_e_ill_property_storage_sym);
- end;
- varsym :
- begin
- if not assigned(def) then
- internalerror(200310073);
- if (ppo_hasparameters in p.propoptions) or
- not(is_boolean(def)) then
- Message(parser_e_stored_property_must_be_boolean);
- end;
- else
- Message(parser_e_ill_property_access_sym);
- end;
- end;
- end;
- end;
- _FALSE:
- begin
- consume(_FALSE);
- exclude(p.propoptions,ppo_stored);
- end;
- _TRUE:
- consume(_TRUE);
- end;
- end;
- end;
- if try_to_consume(_DEFAULT) then
- begin
- if not(is_ordinal(p.proptype.def) or
- {$ifndef cpu64bit}
- is_64bitint(p.proptype.def) or
- {$endif cpu64bit}
- is_class(p.proptype.def) or
- is_single(p.proptype.def) or
- (p.proptype.def.deftype in [classrefdef,pointerdef]) or
- ((p.proptype.def.deftype=setdef) and
- (tsetdef(p.proptype.def).settype=smallset))) or
- ((p.proptype.def.deftype=arraydef) and
- (ppo_indexed in p.propoptions)) or
- (ppo_hasparameters in p.propoptions) then
- begin
- Message(parser_e_property_cant_have_a_default_value);
- { Error recovery }
- pt:=comp_expr(true);
- pt.free;
- end
- else
- begin
- { Get the result of the default, the firstpass is
- needed to support values like -1 }
- pt:=comp_expr(true);
- if (p.proptype.def.deftype=setdef) and
- (pt.nodetype=arrayconstructorn) then
- begin
- arrayconstructor_to_set(pt);
- do_resulttypepass(pt);
- end;
- inserttypeconv(pt,p.proptype);
- if not(is_constnode(pt)) then
- Message(parser_e_property_default_value_must_const);
- { Set default value }
- case pt.nodetype of
- setconstn :
- p.default:=plongint(tsetconstnode(pt).value_set)^;
- ordconstn :
- p.default:=tordconstnode(pt).value;
- niln :
- p.default:=0;
- realconstn:
- p.default:=longint(single(trealconstnode(pt).value_real));
- end;
- pt.free;
- end;
- end
- else if try_to_consume(_NODEFAULT) then
- begin
- p.default:=0;
- end;
- { remove temporary procvardefs }
- symtablestack:=symtablestack.next;
- readprocdef.free;
- writeprocdef.free;
- result:=p;
- end;
- const
- variantrecordlevel : longint = 0;
- procedure read_var_decs(is_record,is_object,is_threadvar:boolean);
- { reads the filed of a record into a }
- { symtablestack, if record=false }
- { variants are forbidden, so this procedure }
- { can be used to read object fields }
- { if absolute is true, ABSOLUTE and file }
- { types are allowed }
- { => the procedure is also used to read }
- { a sequence of variable declaration }
- procedure insert_syms(sc : tsinglelist;tt : ttype;is_threadvar : boolean; addsymopts : tsymoptions);
- { inserts the symbols of sc in st with def as definition or sym as ttypesym, sc is disposed }
- var
- vs,vs2 : tvarsym;
- begin
- vs:=tvarsym(sc.first);
- while assigned(vs) do
- begin
- vs.vartype:=tt;
- { insert any additional hint directives }
- vs.symoptions := vs.symoptions + addsymopts;
- if (sp_static in current_object_option) then
- include(vs.symoptions,sp_static);
- if is_threadvar then
- include(vs.varoptions,vo_is_thread_var);
- { static data fields are inserted in the globalsymtable }
- if (symtablestack.symtabletype=objectsymtable) and
- (sp_static in current_object_option) then
- begin
- vs2:=tvarsym.create('$'+lower(symtablestack.name^)+'_'+vs.name,vs_value,tt);
- symtablestack.defowner.owner.insert(vs2);
- insertbssdata(vs2);
- end
- else
- begin
- { external data is not possible here }
- case symtablestack.symtabletype of
- globalsymtable,
- staticsymtable :
- insertbssdata(vs);
- recordsymtable,
- objectsymtable :
- tabstractrecordsymtable(symtablestack).insertfield(vs,false);
- end;
- end;
- vs:=tvarsym(vs.listnext);
- end;
- end;
- procedure read_default_value(sc : tsinglelist;tt : ttype;is_threadvar : boolean);
- var
- vs : tvarsym;
- tcsym : ttypedconstsym;
- begin
- vs:=tvarsym(sc.first);
- if assigned(vs.listnext) then
- Message(parser_e_initialized_only_one_var);
- if is_threadvar then
- Message(parser_e_initialized_not_for_threadvar);
- if symtablestack.symtabletype=localsymtable then
- begin
- consume(_EQUAL);
- tcsym:=ttypedconstsym.createtype('default'+vs.realname,tt,false);
- vs.defaultconstsym:=tcsym;
- symtablestack.insert(tcsym);
- insertconstdata(tcsym);
- readtypedconst(tt,tcsym,false);
- end
- else
- begin
- tcsym:=ttypedconstsym.createtype(vs.realname,tt,true);
- tcsym.fileinfo:=vs.fileinfo;
- symtablestack.replace(vs,tcsym);
- vs.free;
- insertconstdata(tcsym);
- consume(_EQUAL);
- readtypedconst(tt,tcsym,true);
- end;
- end;
- var
- sc : tsinglelist;
- old_block_type : tblock_type;
- symdone : boolean;
- { to handle absolute }
- abssym : tabsolutesym;
- { c var }
- newtype : ttypesym;
- is_dll,
- hasdefaultvalue,
- is_gpc_name,is_cdecl,
- extern_var,export_var : boolean;
- old_current_object_option : tsymoptions;
- hs,sorg,C_name,dll_name : string;
- tt,casetype : ttype;
- { maxsize contains the max. size of a variant }
- { startvarrec contains the start of the variant part of a record }
- maxsize, startvarrecsize : longint;
- usedalign,
- maxalignment,startvarrecalign : byte;
- hp,pt : tnode;
- vs,vs2 : tvarsym;
- srsym : tsym;
- oldsymtablestack,
- srsymtable : tsymtable;
- unionsymtable : trecordsymtable;
- offset : longint;
- uniondef : trecorddef;
- unionsym : tvarsym;
- uniontype : ttype;
- dummysymoptions : tsymoptions;
- semicolonatend: boolean;
- begin
- old_current_object_option:=current_object_option;
- { all variables are public if not in a object declaration }
- if not is_object then
- current_object_option:=[sp_public];
- old_block_type:=block_type;
- block_type:=bt_type;
- is_gpc_name:=false;
- { Force an expected ID error message }
- if not (token in [_ID,_CASE,_END]) then
- consume(_ID);
- { read vars }
- sc:=tsinglelist.create;
- while (token=_ID) and
- not(is_object and (idtoken in [_PUBLIC,_PRIVATE,_PUBLISHED,_PROTECTED])) do
- begin
- sorg:=orgpattern;
- sc.reset;
- repeat
- vs:=tvarsym.create(orgpattern,vs_value,generrortype);
- symtablestack.insert(vs);
- if assigned(vs.owner) then
- sc.insert(vs)
- else
- vs.free;
- consume(_ID);
- until not try_to_consume(_COMMA);
- consume(_COLON);
- if (m_gpc in aktmodeswitches) and
- not(is_record or is_object or is_threadvar) and
- (token=_ID) and (orgpattern='__asmname__') then
- begin
- consume(_ID);
- C_name:=get_stringconst;
- Is_gpc_name:=true;
- end;
- { this is needed for Delphi mode at least
- but should be OK for all modes !! (PM) }
- ignore_equal:=true;
- if is_record or is_object then
- begin
- { for records, don't search the recordsymtable for
- the symbols of the types }
- oldsymtablestack:=symtablestack;
- symtablestack:=symtablestack.next;
- read_type(tt,'',false);
- symtablestack:=oldsymtablestack;
- end
- else
- read_type(tt,'',false);
- { Process procvar directives }
- if (tt.def.deftype=procvardef) and
- (tt.def.typesym=nil) and
- is_proc_directive(token,true) then
- begin
- newtype:=ttypesym.create('unnamed',tt);
- parse_var_proc_directives(tsym(newtype));
- newtype.restype.def:=nil;
- tt.def.typesym:=nil;
- newtype.free;
- end;
- { types that use init/final are not allowed in variant parts, but
- classes are allowed }
- if (variantrecordlevel>0) and
- (tt.def.needs_inittable and not is_class(tt.def)) then
- Message(parser_e_cant_use_inittable_here);
- ignore_equal:=false;
- hasdefaultvalue:=false;
- symdone:=false;
- if is_gpc_name then
- begin
- vs:=tvarsym(sc.first);
- if assigned(vs.listnext) then
- Message(parser_e_absolute_only_one_var);
- vs.vartype:=tt;
- include(vs.varoptions,vo_is_C_var);
- vs.set_mangledname(target_info.Cprefix+sorg);
- include(vs.varoptions,vo_is_external);
- symdone:=true;
- end;
- { check for absolute }
- if not symdone and
- (idtoken=_ABSOLUTE) and not(is_record or is_object or is_threadvar) then
- begin
- consume(_ABSOLUTE);
- abssym:=nil;
- { only allowed for one var }
- vs:=tvarsym(sc.first);
- if assigned(vs.listnext) then
- Message(parser_e_absolute_only_one_var);
- { parse the rest }
- pt:=expr;
- { check allowed absolute types }
- if (pt.nodetype=stringconstn) or
- (is_constcharnode(pt)) then
- begin
- abssym:=tabsolutesym.create(vs.realname,tt);
- abssym.fileinfo:=vs.fileinfo;
- if pt.nodetype=stringconstn then
- hs:=strpas(tstringconstnode(pt).value_str)
- else
- hs:=chr(tordconstnode(pt).value);
- consume(token);
- abssym.abstyp:=toasm;
- abssym.asmname:=stringdup(hs);
- { replace the varsym }
- symtablestack.replace(vs,abssym);
- vs.free;
- end
- { address }
- else if is_constintnode(pt) and
- ((target_info.system in [system_i386_go32v2,system_i386_watcom,
- system_i386_wdosx,system_i386_win32]) or
- (m_objfpc in aktmodeswitches) or
- (m_delphi in aktmodeswitches)) then
- begin
- abssym:=tabsolutesym.create(vs.realname,tt);
- abssym.fileinfo:=vs.fileinfo;
- abssym.abstyp:=toaddr;
- abssym.fieldoffset:=tordconstnode(pt).value;
- {$ifdef i386}
- abssym.absseg:=false;
- if (target_info.system in [system_i386_go32v2,system_i386_watcom]) and
- try_to_consume(_COLON) then
- begin
- pt.free;
- pt:=expr;
- if is_constintnode(pt) then
- begin
- abssym.fieldoffset:=abssym.fieldoffset shl 4+tordconstnode(pt).value;
- abssym.absseg:=true;
- end
- else
- Message(type_e_ordinal_expr_expected);
- end;
- {$endif i386}
- symtablestack.replace(vs,abssym);
- vs.free;
- end
- { variable }
- else
- begin
- { remove subscriptn before checking for loadn }
- hp:=pt;
- while (hp.nodetype in [subscriptn,typeconvn,vecn]) do
- hp:=tunarynode(hp).left;
- if (hp.nodetype=loadn) then
- begin
- { we should check the result type of loadn }
- if not (tloadnode(hp).symtableentry.typ in [varsym,typedconstsym]) then
- Message(parser_e_absolute_only_to_var_or_const);
- abssym:=tabsolutesym.create(vs.realname,tt);
- abssym.fileinfo:=vs.fileinfo;
- abssym.abstyp:=tovar;
- abssym.ref:=node_to_symlist(pt);
- symtablestack.replace(vs,abssym);
- vs.free;
- end
- else
- Message(parser_e_absolute_only_to_var_or_const);
- end;
- if assigned(abssym) then
- begin
- { try to consume the hint directives with absolute symbols }
- dummysymoptions:=[];
- try_consume_hintdirective(dummysymoptions);
- abssym.symoptions := abssym.symoptions + dummysymoptions;
- end;
- pt.free;
- symdone:=true;
- end;
- { Process procvar directives before = and ; }
- if (tt.def.deftype=procvardef) and
- (tt.def.typesym=nil) and
- is_proc_directive(token,true) then
- begin
- newtype:=ttypesym.create('unnamed',tt);
- parse_var_proc_directives(tsym(newtype));
- newtype.restype.def:=nil;
- tt.def.typesym:=nil;
- newtype.free;
- end;
- { try to parse the hint directives }
- dummysymoptions:=[];
- try_consume_hintdirective(dummysymoptions);
- { Records and objects can't have default values }
- if is_record or is_object then
- begin
- { for a record there doesn't need to be a ; before the END or ) }
- if not(token in [_END,_RKLAMMER]) then
- consume(_SEMICOLON);
- end
- else
- begin
- { Handling of Delphi typed const = initialized vars }
- if (token=_EQUAL) and
- not(m_tp7 in aktmodeswitches) and
- (symtablestack.symtabletype<>parasymtable) then
- begin
- if (tt.def.deftype=procvardef) and
- (tt.def.typesym=nil) then
- begin
- { Add calling convention for procvar }
- handle_calling_convention(tprocvardef(tt.def));
- calc_parast(tprocvardef(tt.def));
- end;
- read_default_value(sc,tt,is_threadvar);
- { for locals we've created typedconstsym with a different name }
- if symtablestack.symtabletype<>localsymtable then
- symdone:=true;
- hasdefaultvalue:=true;
- end;
- consume(_SEMICOLON);
- end;
- { Support calling convention for procvars after semicolon }
- if not(hasdefaultvalue) and
- (tt.def.deftype=procvardef) and
- (tt.def.typesym=nil) then
- begin
- { Parse procvar directives after ; }
- if is_proc_directive(token,true) then
- begin
- newtype:=ttypesym.create('unnamed',tt);
- parse_var_proc_directives(tsym(newtype));
- newtype.restype.def:=nil;
- tt.def.typesym:=nil;
- newtype.free;
- end;
- { Add calling convention for procvar }
- handle_calling_convention(tprocvardef(tt.def));
- calc_parast(tprocvardef(tt.def));
- { Handling of Delphi typed const = initialized vars }
- if (token=_EQUAL) and
- not(is_record or is_object) and
- not(m_tp7 in aktmodeswitches) and
- (symtablestack.symtabletype<>parasymtable) then
- begin
- read_default_value(sc,tt,is_threadvar);
- consume(_SEMICOLON);
- symdone:=true;
- hasdefaultvalue:=true;
- end;
- end;
- { Check for EXTERNAL etc directives or, in macpas, if cs_external_var is set}
- if not symdone and not(is_record or is_object or is_threadvar) then
- begin
- if (
- (token=_ID) and
- (m_cvar_support in aktmodeswitches) and
- (idtoken in [_EXPORT,_EXTERNAL,_PUBLIC,_CVAR])
- ) or
- (
- (m_mac in aktmodeswitches) and
- ((cs_external_var in aktlocalswitches) or (cs_externally_visible in aktlocalswitches))
- ) then
- begin
- { only allowed for one var }
- vs:=tvarsym(sc.first);
- if assigned(vs.listnext) then
- Message(parser_e_absolute_only_one_var);
- { set type of the var }
- vs.vartype:=tt;
- vs.symoptions := vs.symoptions + dummysymoptions;
- { defaults }
- is_dll:=false;
- is_cdecl:=false;
- extern_var:=false;
- export_var:=false;
- C_name:=sorg;
- semicolonatend:= false;
- { cdecl }
- if idtoken=_CVAR then
- begin
- consume(_CVAR);
- consume(_SEMICOLON);
- is_cdecl:=true;
- C_name:=target_info.Cprefix+sorg;
- end;
- { external }
- if idtoken=_EXTERNAL then
- begin
- consume(_EXTERNAL);
- extern_var:=true;
- semicolonatend:= true;
- end;
- { macpas specific handling due to some switches}
- if (m_mac in aktmodeswitches) then
- begin
- if (cs_external_var in aktlocalswitches) then
- begin {The effect of this is the same as if cvar; external; has been given as directives.}
- is_cdecl:=true;
- C_name:=target_info.Cprefix+sorg;
- extern_var:=true;
- end
- else if (cs_externally_visible in aktlocalswitches) then
- begin {The effect of this is the same as if cvar has been given as directives.}
- is_cdecl:=true;
- C_name:=target_info.Cprefix+sorg;
- end;
- end;
- { export }
- if idtoken in [_EXPORT,_PUBLIC] then
- begin
- consume(_ID);
- if extern_var or
- (symtablestack.symtabletype in [parasymtable,localsymtable]) then
- Message(parser_e_not_external_and_export)
- else
- begin
- export_var:=true;
- semicolonatend:= true;
- end;
- end;
- { external and export need a name after when no cdecl is used }
- if not is_cdecl then
- begin
- { dll name ? }
- if (extern_var) and (idtoken<>_NAME) then
- begin
- is_dll:=true;
- dll_name:=get_stringconst;
- end;
- consume(_NAME);
- C_name:=get_stringconst;
- end;
- { consume the ; when export or external is used }
- if semicolonatend then
- consume(_SEMICOLON);
- { set some vars options }
- if is_dll then
- include(vs.varoptions,vo_is_dll_var)
- else
- include(vs.varoptions,vo_is_C_var);
- if (is_dll) and
- (target_info.system = system_powerpc_darwin) then
- C_Name := target_info.Cprefix+C_Name;
- vs.set_mangledname(C_Name);
- if export_var then
- begin
- inc(vs.refs);
- include(vs.varoptions,vo_is_exported);
- end;
- if extern_var then
- include(vs.varoptions,vo_is_external);
- { insert in the datasegment when it is not external }
- if (not extern_var) then
- insertbssdata(vs);
- { now we can insert it in the import lib if its a dll, or
- add it to the externals }
- if extern_var then
- begin
- if is_dll then
- begin
- if not(current_module.uses_imports) then
- begin
- current_module.uses_imports:=true;
- importlib.preparelib(current_module.modulename^);
- end;
- importlib.importvariable(vs,C_name,dll_name);
- end
- else
- if target_info.DllScanSupported then
- current_module.Externals.insert(tExternalsItem.create(vs.mangledname));
- end;
- symdone:=true;
- end;
- end;
- { Check for STATIC directive }
- if not symdone and (is_object) and
- (cs_static_keyword in aktmoduleswitches) and (idtoken=_STATIC) then
- begin
- include(current_object_option,sp_static);
- insert_syms(sc,tt,false,dummysymoptions);
- exclude(current_object_option,sp_static);
- consume(_STATIC);
- consume(_SEMICOLON);
- symdone:=true;
- end;
- { insert it in the symtable, if not done yet }
- if not symdone then
- begin
- { save object option, because we can turn of the sp_published }
- if (sp_published in current_object_option) and
- not(is_class(tt.def)) then
- begin
- Message(parser_e_cant_publish_that);
- exclude(current_object_option,sp_published);
- { recover by changing access type to public }
- vs2:=tvarsym(sc.first);
- while assigned (vs2) do
- begin
- exclude(vs2.symoptions,sp_published);
- include(vs2.symoptions,sp_public);
- vs2:=tvarsym(vs2.listnext);
- end;
- end
- else
- if (sp_published in current_object_option) and
- not(oo_can_have_published in tobjectdef(tt.def).objectoptions) then
- begin
- Message(parser_e_only_publishable_classes_can__be_published);
- exclude(current_object_option,sp_published);
- end;
- insert_syms(sc,tt,is_threadvar,dummysymoptions);
- current_object_option:=old_current_object_option;
- end;
- end;
- { Check for Case }
- if is_record and (token=_CASE) then
- begin
- maxsize:=0;
- maxalignment:=0;
- consume(_CASE);
- sorg:=orgpattern;
- hs:=pattern;
- searchsym(hs,srsym,srsymtable);
- { may be only a type: }
- if assigned(srsym) and (srsym.typ in [typesym,unitsym]) then
- begin
- { for records, don't search the recordsymtable for
- the symbols of the types }
- oldsymtablestack:=symtablestack;
- symtablestack:=symtablestack.next;
- read_type(casetype,'',true);
- symtablestack:=oldsymtablestack;
- end
- else
- begin
- consume(_ID);
- consume(_COLON);
- { for records, don't search the recordsymtable for
- the symbols of the types }
- oldsymtablestack:=symtablestack;
- symtablestack:=symtablestack.next;
- read_type(casetype,'',true);
- symtablestack:=oldsymtablestack;
- vs:=tvarsym.create(sorg,vs_value,casetype);
- tabstractrecordsymtable(symtablestack).insertfield(vs,true);
- end;
- if not(is_ordinal(casetype.def))
- {$ifndef cpu64bit}
- or is_64bitint(casetype.def)
- {$endif cpu64bit}
- then
- Message(type_e_ordinal_expr_expected);
- consume(_OF);
- UnionSymtable:=trecordsymtable.create(aktpackrecords);
- Unionsymtable.next:=symtablestack;
- registerdef:=false;
- UnionDef:=trecorddef.create(unionsymtable);
- uniondef.isunion:=true;
- if assigned(symtablestack.defowner) then
- Uniondef.owner:=symtablestack.defowner.owner;
- registerdef:=true;
- startvarrecsize:=UnionSymtable.datasize;
- startvarrecalign:=UnionSymtable.fieldalignment;
- symtablestack:=UnionSymtable;
- repeat
- repeat
- pt:=comp_expr(true);
- if not(pt.nodetype=ordconstn) then
- Message(parser_e_illegal_expression);
- pt.free;
- if token=_COMMA then
- consume(_COMMA)
- else
- break;
- until false;
- consume(_COLON);
- { read the vars }
- consume(_LKLAMMER);
- inc(variantrecordlevel);
- if token<>_RKLAMMER then
- read_var_decs(true,false,false);
- dec(variantrecordlevel);
- consume(_RKLAMMER);
- { calculates maximal variant size }
- maxsize:=max(maxsize,unionsymtable.datasize);
- maxalignment:=max(maxalignment,unionsymtable.fieldalignment);
- { the items of the next variant are overlayed }
- unionsymtable.datasize:=startvarrecsize;
- unionsymtable.fieldalignment:=startvarrecalign;
- if (token<>_END) and (token<>_RKLAMMER) then
- consume(_SEMICOLON)
- else
- break;
- until (token=_END) or (token=_RKLAMMER);
- { at last set the record size to that of the biggest variant }
- unionsymtable.datasize:=maxsize;
- unionsymtable.fieldalignment:=maxalignment;
- uniontype.def:=uniondef;
- uniontype.sym:=nil;
- UnionSym:=tvarsym.create('$case',vs_value,uniontype);
- symtablestack:=symtablestack.next;
- { Align the offset where the union symtable is added }
- if (trecordsymtable(symtablestack).usefieldalignment=-1) then
- usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.maxCrecordalign)
- else
- usedalign:=used_align(unionsymtable.recordalignment,aktalignment.recordalignmin,aktalignment.recordalignmax);
- offset:=align(trecordsymtable(symtablestack).datasize,usedalign);
- trecordsymtable(symtablestack).datasize:=offset+unionsymtable.datasize;
- if unionsymtable.recordalignment>trecordsymtable(symtablestack).fieldalignment then
- trecordsymtable(symtablestack).fieldalignment:=unionsymtable.recordalignment;
- trecordsymtable(symtablestack).insertunionst(Unionsymtable,offset);
- Unionsym.owner:=nil;
- unionsym.free;
- uniondef.owner:=nil;
- uniondef.free;
- end;
- block_type:=old_block_type;
- current_object_option:=old_current_object_option;
- { free the list }
- sc.free;
- end;
- end.
- {
- $Log$
- Revision 1.78 2004-08-15 13:30:18 florian
- * fixed alignment of variant records
- * more alignment problems fixed
- Revision 1.77 2004/08/07 19:14:50 florian
- * fixed problem with explicit specified calling conventions for property symbols
- Revision 1.76 2004/07/14 23:19:22 olle
- + added external facilities for macpas
- Revision 1.75 2004/06/20 08:55:30 florian
- * logs truncated
- Revision 1.74 2004/06/16 20:07:09 florian
- * dwarf branch merged
- Revision 1.73 2004/04/11 12:38:16 peter
- * fix calling convention problem when parsing default value before
- the semicolon
- Revision 1.72.2.5 2004/05/02 20:54:04 peter
- * allow convert when choosing property function with index
- Revision 1.72.2.4 2004/05/01 20:51:20 peter
- * fix val code parameter
- Revision 1.72.2.3 2004/04/28 19:55:52 peter
- * new warning for ordinal-pointer when size is different
- * fixed some cg_e_ messages to the correct section type_e_ or parser_e_
- }
|